From 3d4e9ce5de2df582436d931df6ee4706cfe05f34 Mon Sep 17 00:00:00 2001 From: Jeffrey Phillips Date: Thu, 23 Jan 2025 15:48:21 -0500 Subject: [PATCH] feat(JumpLinks): Add replaceNavHistory option --- .../react-core/src/components/JumpLinks/JumpLinks.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/react-core/src/components/JumpLinks/JumpLinks.tsx b/packages/react-core/src/components/JumpLinks/JumpLinks.tsx index 30dfd49ce02..1251782a810 100644 --- a/packages/react-core/src/components/JumpLinks/JumpLinks.tsx +++ b/packages/react-core/src/components/JumpLinks/JumpLinks.tsx @@ -46,6 +46,8 @@ export interface JumpLinksProps extends Omit, 'labe toggleAriaLabel?: string; /** Class for nav */ className?: string; + /** Whether the navigation history should be replaced rather than pushed */ + replaceNavHistory?: boolean; } // Recursively find JumpLinkItems and return an array of all their scrollNodes @@ -92,6 +94,7 @@ export const JumpLinks: React.FunctionComponent = ({ alwaysShowLabel = true, toggleAriaLabel = 'Toggle jump links', className, + replaceNavHistory = false, ...props }: JumpLinksProps) => { const hasScrollSpy = Boolean(scrollableRef || scrollableSelector); @@ -207,7 +210,11 @@ export const JumpLinks: React.FunctionComponent = ({ 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); }