saleor-dashboard/src/hooks/useNavigator.ts

34 lines
695 B
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import useRouter from "use-react-router";
export type UseNavigatorResult = (
url: string,
opts?: {
replace?: boolean;
preserveQs?: boolean;
resetScroll?: boolean;
}
2019-06-19 14:40:52 +00:00
) => void;
function useNavigator(): UseNavigatorResult {
const {
location: { search },
history
} = useRouter();
return (
url: string,
2021-12-06 14:41:18 +00:00
{ replace = false, preserveQs = false, resetScroll = false } = {}
) => {
2019-06-19 14:40:52 +00:00
const targetUrl = preserveQs ? url + search : url;
2019-12-02 10:49:14 +00:00
if (replace) {
history.replace(targetUrl);
} else {
history.push(targetUrl);
}
if (resetScroll) {
window.scrollTo({ behavior: "smooth", top: 0 });
}
2019-06-19 14:40:52 +00:00
};
}
export default useNavigator;