Skip to content

Commit

Permalink
fix: scrollbar implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
astagi committed Dec 9, 2024
1 parent b336d7f commit 3f9ddb3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/NavScroll/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export type useNavScrollResult = {
* A function to retrieve the reference of the current active element (only the last element, not the elements hierarchy).
*/
getActiveRef: () => RefObject<Element> | null;
/**
* A list of active ids (the full hierarchy).
*/
percentage: number;
};

// @private
Expand Down
13 changes: 9 additions & 4 deletions src/NavScroll/useNavScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export function useNavScroll(args: useNavScrollArgs = {}): useNavScrollResult {
const [counter, setCounter] = useState(0);
const [forceRecompute, setForceRecompute] = useState(false);
const [activeId, updateActiveId] = useState<string | null>(null);
const [percentageValue, setPercentageValue] = useState(0);

const { targetSize, useViewport } = useSizeDetector({
root,
Expand Down Expand Up @@ -145,11 +146,13 @@ export function useNavScroll(args: useNavScrollArgs = {}): useNavScrollResult {
if (!min) {
break;
}
console.log(`Window ${document.scrollingElement?.scrollTop}`);
console.log(`RECORD ${entry?.id} TOP ${entry?.getBoundingClientRect().top}`);
if (min > 0 && k > 0) {
intersectionId = els.current[k-1].ref.current?.id;
console.log('FOUND');
const totEls =
root?.previousSibling?.firstChild?.parentNode?.querySelectorAll('.it-navscroll-wrapper .nav-link').length ||
1;
setPercentageValue((k / (totEls / 2)) * 100);
console.log(`PERCENTAGE: ${percentageValue} %`);
intersectionId = els.current[k - 1].ref.current?.id;
break;
}
}
Expand Down Expand Up @@ -217,13 +220,15 @@ export function useNavScroll(args: useNavScrollArgs = {}): useNavScrollResult {
);

const isActive = useCallback((id: string) => activeLookups.has(id), [activeLookups]);
const percentage = useMemo(() => percentageValue, [percentageValue]);

const getActiveRef = useCallback(() => {
const entry = els.current.find(({ id }) => id === activeId);
return entry ? entry.ref : null;
}, [activeId]);

return {
percentage,
register,
unregister,
activeIds,
Expand Down
5 changes: 4 additions & 1 deletion stories/Components/NavScroll/NavScroll.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ const PosizionamentoTestaAltaHooks = () => {
/* Nota che i componenti Col e Row non inoltrano le ref,
/* usare quindi tag div con classi css come nell'esempio */
const containerRef = useRef(null);
const { register, isActive, getActiveRef } = useNavScroll({
const { register, isActive, getActiveRef, percentage } = useNavScroll({
root: containerRef.current || undefined
});
const getActiveClass = (id: string) => (isActive(id) ? 'active' : undefined);
Expand Down Expand Up @@ -392,6 +392,9 @@ const PosizionamentoTestaAltaHooks = () => {
<div className='menu-wrapper'>
<div className='link-list-wrapper'>
<h3>header</h3>
<div className="progress">
<div className="progress-bar it-navscroll-progressbar" role="progressbar" style={{width : `${percentage}%`}}></div>
</div>
<LinkList noWrapper>
<NavItem>
<NavLink onClick={() => toggleNavScroll(!isOpen)} href='#1' className={getActiveClass('1')}>
Expand Down

0 comments on commit 3f9ddb3

Please sign in to comment.