Skip to content

Commit

Permalink
Fix scrolling behaviour of Navigation on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Jan 2, 2025
1 parent b7d9a06 commit a66d48e
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions website/src/components/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,28 @@ const NavItem = component$<NavItemProps>(({ navElement, text, items }) => {

// Scroll active element into view if needed
if (activeElement) {
const parentClientRect = navElement.value!.getBoundingClientRect();
const childClientRect = activeElement.getBoundingClientRect();
if (
childClientRect.top < parentClientRect.top ||
childClientRect.bottom > parentClientRect.bottom
) {
navElement.value!.scrollBy({
behavior: 'smooth',
top:
childClientRect.top -
parentClientRect.top -
parentClientRect.height / 2 +
childClientRect.height,
});
}
setTimeout(
() => {
const parentClientRect = navElement.value!.getBoundingClientRect();
if (parentClientRect.height > 0) {
const childClientRect = activeElement.getBoundingClientRect();
if (
childClientRect.top < parentClientRect.top ||
childClientRect.bottom > parentClientRect.bottom
) {
navElement.value!.scrollBy({
behavior: 'smooth',
top:
childClientRect.top -
parentClientRect.top -
parentClientRect.height / 2 +
childClientRect.height,
});
}
}
},
window.innerWidth < 1024 ? 100 : 0
);
}
},
{ strategy: 'document-idle' }
Expand Down

0 comments on commit a66d48e

Please sign in to comment.