Skip to content

Commit 3d4e9ce

Browse files
feat(JumpLinks): Add replaceNavHistory option
1 parent edc04fa commit 3d4e9ce

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

packages/react-core/src/components/JumpLinks/JumpLinks.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export interface JumpLinksProps extends Omit<React.HTMLProps<HTMLElement>, 'labe
4646
toggleAriaLabel?: string;
4747
/** Class for nav */
4848
className?: string;
49+
/** Whether the navigation history should be replaced rather than pushed */
50+
replaceNavHistory?: boolean;
4951
}
5052

5153
// Recursively find JumpLinkItems and return an array of all their scrollNodes
@@ -92,6 +94,7 @@ export const JumpLinks: React.FunctionComponent<JumpLinksProps> = ({
9294
alwaysShowLabel = true,
9395
toggleAriaLabel = 'Toggle jump links',
9496
className,
97+
replaceNavHistory = false,
9598
...props
9699
}: JumpLinksProps) => {
97100
const hasScrollSpy = Boolean(scrollableRef || scrollableSelector);
@@ -207,7 +210,11 @@ export const JumpLinks: React.FunctionComponent<JumpLinksProps> = ({
207210
scrollableElement.scrollTo(0, newScrollItem.offsetTop - offset);
208211
}
209212
newScrollItem.focus();
210-
window.history.pushState('', '', (ev.currentTarget as HTMLAnchorElement).href);
213+
if (replaceNavHistory) {
214+
window.history.replaceState('', '', (ev.currentTarget as HTMLAnchorElement).href);
215+
} else {
216+
window.history.pushState('', '', (ev.currentTarget as HTMLAnchorElement).href);
217+
}
211218
ev.preventDefault();
212219
setActiveIndex(itemIndex);
213220
}

0 commit comments

Comments
 (0)