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

feat(JumpLinks): Add replaceNavHistory option #11473

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
9 changes: 8 additions & 1 deletion packages/react-core/src/components/JumpLinks/JumpLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export interface JumpLinksProps extends Omit<React.HTMLProps<HTMLElement>, 'labe
toggleAriaLabel?: string;
/** Class for nav */
className?: string;
/** Whether the navigation history should be replaced rather than pushed */
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/** Whether the navigation history should be replaced rather than pushed */
/** Whether the current entry in the navigation history should be replaced when a JumpLinksItem is clicked. By default a new entry will be pushed to the navigation history. */

Maybe something like this for the description? Just to make it clear that only the current history item would be replaced rather than the entire history being replaced.

replaceNavHistory?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
replaceNavHistory?: boolean;
shouldReplaceNavHistory?: boolean;

}

// Recursively find JumpLinkItems and return an array of all their scrollNodes
Expand Down Expand Up @@ -92,6 +94,7 @@ export const JumpLinks: React.FunctionComponent<JumpLinksProps> = ({
alwaysShowLabel = true,
toggleAriaLabel = 'Toggle jump links',
className,
replaceNavHistory = false,
...props
}: JumpLinksProps) => {
const hasScrollSpy = Boolean(scrollableRef || scrollableSelector);
Expand Down Expand Up @@ -207,7 +210,11 @@ export const JumpLinks: React.FunctionComponent<JumpLinksProps> = ({
scrollableElement.scrollTo(0, newScrollItem.offsetTop - offset);
}
newScrollItem.focus();
window.history.pushState('', '', (ev.currentTarget as HTMLAnchorElement).href);
if (replaceNavHistory) {
window.history.replaceState('', '', (ev.currentTarget as HTMLAnchorElement).href);
} else {
window.history.pushState('', '', (ev.currentTarget as HTMLAnchorElement).href);
}
ev.preventDefault();
setActiveIndex(itemIndex);
}
Expand Down
Loading