Skip to content

Commit a699873

Browse files
committed
Fix handleNavigation to correctly support HashRouter page links
1 parent cf9e035 commit a699873

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/utils/navigation.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,42 @@ export const scrollToSection = (sectionId: string, offset: number = 80): void =>
1212
};
1313

1414
export const handleNavigation = (href: string, e: React.MouseEvent): void => {
15-
if (href.startsWith('#')) {
15+
// Only act on hash links that are for same-page section scrolling.
16+
// These typically look like "#section-id" or "/#section-id".
17+
// HashRouter page links look like "#/page-route".
18+
if (href.startsWith('#') && href.indexOf('/') === -1) { // No slash after #
1619
e.preventDefault();
1720
const sectionId = href.substring(1);
1821
scrollToSection(sectionId);
22+
} else if (href.startsWith('/#') && href.indexOf('/', 2) === -1) { // For links like /#section-id from other pages
23+
e.preventDefault();
24+
// First, navigate to homepage if not already there (HashRouter handles this with Link to="/")
25+
// Then, scroll to section. This part needs care with HashRouter and Link component.
26+
// The Link component should handle going to "/" (which becomes /#/) and then ScrollToTop/useEffect can handle the actual scroll.
27+
// For now, let's ensure it correctly scrolls if already on the homepage or if the Link component itself handles the root navigation.
28+
const sectionId = href.substring(href.lastIndexOf('#') + 1);
29+
const targetPath = href.substring(0, href.lastIndexOf('#'));
30+
31+
// If Link is to "/#section", and current path is already "/" (or "/#/")
32+
// Allow Link to navigate to "/#/". The ScrollToTop or a similar mechanism should handle the scroll to sectionId.
33+
// The main goal here is NOT to preventDefault for full page hash navigations like '#/careers'.
34+
if (window.location.hash === '#/' || window.location.hash === '' || window.location.pathname === '/') {
35+
scrollToSection(sectionId);
36+
}
37+
// If it's a link like "/#contact" from a page like "/team" (i.e. current hash is "#/team")
38+
// the <Link to="/#contact"> will correctly change the hash to "/#contact".
39+
// Then ScrollToTop logic in App.tsx needs to pick up the section from the hash state after the page potentially re-renders.
40+
// The `handleNavigation` itself doesn't need to do much more for these `/#[section]` links when using React Router's <Link>
1941
}
42+
// If it's a link like "#/careers", the conditions above won't match, so default Link behavior proceeds.
2043
};
2144

2245
export const scrollToContact = (inquiryType?: string): void => {
46+
// Ensure we scroll to the contact section on the current page (expected to be homepage)
47+
// If called from another page, the navigation to homepage + section scroll should be handled by a Link component.
48+
// This utility is best used when already on the page with the contact section.
2349
scrollToSection('contact');
2450

25-
// If an inquiry type is specified, set it in the form
2651
if (inquiryType) {
2752
const selectElement = document.getElementById('inquiryType') as HTMLSelectElement;
2853
if (selectElement) {

0 commit comments

Comments
 (0)