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

solved #20 i.e contact link is now working from other than homepage #21

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Changes from 3 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
38 changes: 33 additions & 5 deletions components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Image from 'next/image';
import Link from 'next/link';
import { usePathname, useRouter } from 'next/navigation';
import { useEffect } from 'react';

export default function Footer() {
const router = useRouter();
Expand All @@ -15,17 +16,44 @@ export default function Footer() {
{ name: 'Contact', href: '/#contact' },
];

useEffect(() => {
const handleScrollToHash = () => {
const hash = window.location.hash;
if (hash) {
const id = hash.replace('#', '');
const element = document.getElementById(id);
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
history.replaceState(null, '', window.location.pathname);
}
}
};

// Immediately invoke the function once after navigation
handleScrollToHash();

// Cleanup logic: Only observe for initial change and then disconnect
const observer = new MutationObserver(() => {
handleScrollToHash();
observer.disconnect(); // Disconnect after one scroll to prevent repeated triggers
});

observer.observe(document.body, { attributes: true, subtree: true });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit overkill for a small issue. Let's stick to just taking the user to /#contact without the scroll behaviour.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah actually it is and I have rolled my to previous one. But I am still struggling with the problem that it is not taking to the /#contact when user is on supporters page or may be care page. I tried faking the network speed during development it worked there but only for localhost. I need some more time to go to the core of the problem. I have the intuation it is just a minor one but I am not able to get it.


return () => observer.disconnect();
}, [path]);



const handleNavigation = (href: string) => {
if (href.includes('#')) {
const [page, section] = href.split('#');
const targetPage = page || '/';

if (path === page) {
if (path === targetPage) {
document.getElementById(section)?.scrollIntoView({ behavior: 'smooth' });
} else {
router.push(page);
setTimeout(() => {
document.getElementById(section)?.scrollIntoView({ behavior: 'smooth' });
}, 100); // Adding a small delay to ensure the page has navigated
router.push(targetPage + '#' + section);
}
} else {
router.push(href);
Expand Down