Skip to content

Commit

Permalink
chore: Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Carvalho committed Sep 20, 2023
1 parent ec8ade6 commit da8988c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/anchor-navigation/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface AnchorNavigationProps extends BaseComponentProps {
* * `level` (number) - Level of nesting of the anchor.
* * `info` (string | undefined) - Additional information to display next to the link, for example: "New" or "Updated".
*
* Note: The list of anchors should be sorted in the order they appear on the page.
*/
anchors: AnchorNavigationProps.Anchor[];

Expand Down
2 changes: 1 addition & 1 deletion src/anchor-navigation/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import styles from './styles.css.js';
import testUtilsStyles from './test-classes/styles.css.js';
import { AnchorNavigationProps } from './interfaces';
import { checkSafeUrl } from '../internal/utils/check-safe-url';
import useScrollSpy from './scroll-spy.js';
import useScrollSpy from './use-scroll-spy.js';
import { fireCancelableEvent, fireNonCancelableEvent, isPlainLeftClick } from '../internal/events/index';
import { InternalBaseComponentProps } from '../internal/hooks/use-base-component/index.js';
import { getBaseProps } from '../internal/base-component/index.js';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ import { useCallback, useEffect, useState } from 'react';

const isBrowser = typeof window !== 'undefined';

/**
* Hook to implement scroll-spy functionality.
*
* @param hrefs An array of href strings that correspond to the IDs of the target elements on the page.
* The hrefs should be sorted in the order they appear on the page for accurate scroll-spy behavior.
* @param scrollSpyOffset The number of pixels to offset from the top of the document when activating anchors.
* Useful for accommodating fixed or sticky headers.
* @param activeHref The currently active href. If provided, the hook will operate in a controlled manner,
* and the scroll-spy logic will be disabled.
*
* @returns {string | undefined} - The href of the currently active element as per scroll position, or undefined if none is active.
*/
export default function useScrollSpy({
hrefs,
scrollSpyOffset,
Expand Down Expand Up @@ -34,7 +46,7 @@ export default function useScrollSpy({
return lastAnchorExists && Math.ceil(window.scrollY) >= Math.floor(document.body.scrollHeight - window.innerHeight);
}, [lastAnchorExists]);

// Find the href for which the element is within the viewport
// Find the first href for which the element is within the viewport
const findHrefInView = useCallback(() => {
return hrefs.find(href => {
const rect = getRectByHref(href);
Expand Down

0 comments on commit da8988c

Please sign in to comment.