Skip to content

Commit

Permalink
feat: update code with Pull request comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Franck Gaudin committed May 15, 2024
1 parent 4663631 commit 6086fe5
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions apps/docs/app/ui/layout/aside/Aside.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,7 @@ const Aside = ({ title, links }: React.PropsWithoutRef<AsideProps>) => {
const observer = useRef<IntersectionObserver | null>(null);
const sectionTitleDomElement = "[data-section-title]";
const titleHeight = 28;

const setInitialActiveItemIndex = () => {
const lastVisibleSection = findLastFullyVisibleSection();
if (lastVisibleSection) {
const lastVisibleSectionIndex = links.findIndex(link => link.id === lastVisibleSection);
setActiveItemIndex(lastVisibleSectionIndex);
}
};


useEffect(() => {
const sectionsTitle = document.querySelectorAll(sectionTitleDomElement);
const options = {
Expand All @@ -49,6 +41,13 @@ const Aside = ({ title, links }: React.PropsWithoutRef<AsideProps>) => {
observer.current?.observe(sectionTitle);
});

const setInitialActiveItemIndex = () => {
const lastVisibleSection = findLastFullyVisibleSection();
if (lastVisibleSection) {
const lastVisibleSectionIndex = links.findIndex(link => link.id === lastVisibleSection);
setActiveItemIndex(lastVisibleSectionIndex);
}
};

setInitialActiveItemIndex();

Expand All @@ -60,7 +59,6 @@ const Aside = ({ title, links }: React.PropsWithoutRef<AsideProps>) => {
};
}, [links]);


// Custom function to determine the last fully visible section in the viewport
const findLastFullyVisibleSection = () => {
const sectionsTitle = document.querySelectorAll(sectionTitleDomElement);
Expand All @@ -82,7 +80,6 @@ const Aside = ({ title, links }: React.PropsWithoutRef<AsideProps>) => {

// Scroll handler to set the active section when scrolling up
const handleScroll = () => {
console.log("scrolling");
const lastVisibleSection = findLastFullyVisibleSection();
if (lastVisibleSection) {
setActiveSection(lastVisibleSection);
Expand Down

0 comments on commit 6086fe5

Please sign in to comment.