Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: scroll to top on navigating to new page #1054

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/link/CustomLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const CustomLink = ({
target={target}
rel={rel}
aria-label={link.ariaLabel}
scroll={scroll}
>
<span className={styles.span}>{link.linkTitle}</span>
</Link>
Expand Down Expand Up @@ -87,6 +88,7 @@ const CustomLink = ({
target={target}
rel={rel}
aria-label={link.ariaLabel}
scroll={scroll}
>
{link.linkTitle}
</Link>
Expand All @@ -101,6 +103,7 @@ const CustomLink = ({
target={target}
rel={rel}
aria-label={link.ariaLabel}
scroll={scroll}
>
{link.linkTitle}
</Link>
Expand Down
84 changes: 49 additions & 35 deletions src/components/navigation/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import LanguageSwitcher from "src/components/languageSwitcher/LanguageSwitcher";
import CustomLink from "src/components/link/CustomLink";
import LinkButton from "src/components/linkButton/LinkButton";
import useScrollDirection from "src/utils/hooks/useScrollDirection";
import useScrollToTop from "src/utils/hooks/useScrollToTop";
import { getHref } from "src/utils/link";
import { Announcement } from "studio/lib/interfaces/announcement";
import { BrandAssets } from "studio/lib/interfaces/brandAssets";
Expand Down Expand Up @@ -45,6 +46,7 @@ export const Header = ({
const [isOpen, setIsOpen] = useState(false);
const sidebarData = navigation.sidebar || navigation.main;

useScrollToTop();
const scrollDirection = useScrollDirection();

const links = filterLinks(navigation.main, linkID);
Expand Down Expand Up @@ -99,8 +101,8 @@ export const Header = ({
className={styles.logo}
scroll={false}
/>
{renderPageLinks(links, false, pathname)}
{renderPageCTAs(ctas, false)}
<PageLinks links={links} isMobile={false} pathname={pathname} />
<PageCTAs ctas={ctas} isMobile={false} />
<div className={styles.languageSwitcher}>
{defaultLanguage && (
<LanguageSwitcher
Expand Down Expand Up @@ -136,7 +138,11 @@ export const Header = ({
aria-label="Mobile Menu"
onClick={() => setIsOpen(false)}
>
{renderPageLinks(sidebarLinks, true, pathname)}
<PageLinks
links={sidebarLinks}
isMobile={true}
pathname={pathname}
/>
<hr className={styles.divider} />
<div className={styles.mobileButtons}>
{defaultLanguage && (
Expand Down Expand Up @@ -182,39 +188,47 @@ export const Header = ({
);
};

export const renderPageLinks = (
links: ILink[],
isMobile: boolean,
pathname: string,
) => (
<ul className={isMobile ? styles.listMobile : styles.desktopLinks}>
{links?.map((link: ILink) => {
const linkUrl = getHref(link);
const isSelected = pathname === `${linkUrl}`;
return (
function PageLinks({
links,
isMobile,
pathname,
}: {
links: ILink[];
isMobile: boolean;
pathname: string;
}) {
return (
<ul className={isMobile ? styles.listMobile : styles.desktopLinks}>
{links?.map((link: ILink) => {
const linkUrl = getHref(link);
const isSelected = pathname === `${linkUrl}`;
return (
<li key={link._key}>
<CustomLink
link={link}
type="headerLink"
isSelected={isSelected}
scroll={false}
/>
</li>
);
})}
</ul>
);
}

function PageCTAs({ ctas, isMobile }: { ctas: ILink[]; isMobile: boolean }) {
return (
<ul className={isMobile ? styles.listMobile : styles.desktopCtas}>
{ctas?.map((link: ILink, index) => (
<li key={link._key}>
<CustomLink
<LinkButton
link={link}
type="headerLink"
isSelected={isSelected}
scroll={false}
size="S"
type={ctas.length < 2 || index === 1 ? "primary" : "secondary"}
/>
</li>
);
})}
</ul>
);

export const renderPageCTAs = (ctas: ILink[], isMobile: boolean) => (
<ul className={isMobile ? styles.listMobile : styles.desktopCtas}>
{ctas?.map((link: ILink, index) => (
<li key={link._key}>
<LinkButton
link={link}
size="S"
type={ctas.length < 2 || index === 1 ? "primary" : "secondary"}
/>
</li>
))}
</ul>
);
))}
</ul>
);
}
7 changes: 7 additions & 0 deletions src/utils/hooks/useScrollToTop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useEffect } from "react";

export default function useScrollToTop() {
useEffect(() => {
window.scrollTo({ top: 0, behavior: "instant" });
}, []);
}
Loading