Skip to content

Commit 985350e

Browse files
Add conditional onClick Prop to be displayed only when isHashRouter prop is passed
1 parent 75a8258 commit 985350e

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

packages/lib/src/quick-nav/QuickNav.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const Link = styled.a`
5959
}
6060
`;
6161

62-
export default function DxcQuickNav({ links, title }: QuickNavTypes) {
62+
export default function DxcQuickNav({ links, title, isHashRouter = false }: QuickNavTypes) {
6363
const translatedLabels = useContext(HalstackLanguageContext);
6464

6565
return (
@@ -70,12 +70,15 @@ export default function DxcQuickNav({ links, title }: QuickNavTypes) {
7070
<li key={link.label}>
7171
<Link
7272
href={`#${slugify(link.label, { lower: true })}`}
73-
onClick={(e) => {
74-
e.preventDefault();
75-
const id = slugify(link.label, { lower: true });
76-
document.getElementById(id)?.scrollIntoView({ behavior: "smooth" });
77-
history.replaceState(null, "", `#${id}`);
78-
}}
73+
onClick={
74+
isHashRouter
75+
? (e) => {
76+
e.preventDefault();
77+
const id = slugify(link.label, { lower: true });
78+
document.getElementById(id)?.scrollIntoView({ behavior: "smooth" });
79+
}
80+
: undefined
81+
}
7982
>
8083
<span>{link.label}</span>
8184
</Link>
@@ -87,12 +90,15 @@ export default function DxcQuickNav({ links, title }: QuickNavTypes) {
8790
href={`#${slugify(link?.label, { lower: true })}-${slugify(sublink?.label, {
8891
lower: true,
8992
})}`}
90-
onClick={(e) => {
91-
e.preventDefault();
92-
const id = `${slugify(link.label, { lower: true })}-${slugify(sublink.label, { lower: true })}`;
93-
document.getElementById(id)?.scrollIntoView({ behavior: "smooth" });
94-
history.replaceState(null, "", `#${id}`);
95-
}}
93+
onClick={
94+
isHashRouter
95+
? (e) => {
96+
e.preventDefault();
97+
const id = `${slugify(link.label, { lower: true })}-${slugify(sublink.label, { lower: true })}`;
98+
document.getElementById(id)?.scrollIntoView({ behavior: "smooth" });
99+
}
100+
: undefined
101+
}
96102
>
97103
<span>{sublink.label}</span>
98104
</Link>

packages/lib/src/quick-nav/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ type Props = {
1818
* Links to be shown inside the quick nav component.
1919
*/
2020
links: Link[];
21+
/*
22+
* Links to be shown inside the quick nav component.
23+
*/
24+
isHashRouter?: boolean;
2125
};
2226

2327
export default Props;

0 commit comments

Comments
 (0)