Skip to content

Commit

Permalink
Persist & scroll to anchor when changing frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
ericclemmons committed Nov 18, 2021
1 parent a55ff8a commit e71ca35
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions docs/src/components/Layout/FrameworkChooser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,27 @@ export const FrameworkChooser = ({ platform }) => {
const router = useRouter();

const chooseFramework = (framework) => {
router.replace({
pathname: router.pathname,
query: { platform: framework },
});
const { hash } = window.location;

router.replace(
{
hash,
pathname: router.pathname,
query: { platform: framework },
},
// `as?` prop isn't needed when URL is already provided
undefined,
{
// Scroll to top if a new page
scroll: hash ? false : true,
}
);

// Because layout may change, explicitly tell the browser to scroll to that anchor
// e.g. <a id="#variation" />
if (hash) {
document.getElementById(hash.slice(1)).scrollIntoView();
}
};

return (
Expand Down

0 comments on commit e71ca35

Please sign in to comment.