Skip to content

Commit 1757b28

Browse files
Adding deeplink capabilities to getstarted tabs
1 parent e74d728 commit 1757b28

1 file changed

Lines changed: 54 additions & 3 deletions

File tree

src/components/getstarted.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,59 @@ import { Heading } from '@/components/mdx'
2323
export const GetStarted = () => {
2424
const getstarted = React.useMemo(() => data, [])
2525
const [activeIndex, setActiveIndex] = React.useState(null)
26+
const tabSlugs = React.useMemo(
27+
() =>
28+
getstarted.map((item) =>
29+
item.name
30+
.toLowerCase()
31+
.trim()
32+
.replace(/\s+/g, '-')
33+
.replace(/[^a-z0-9-]/g, ''),
34+
),
35+
[getstarted],
36+
)
37+
38+
React.useEffect(() => {
39+
const getTabIndexFromHash = () => {
40+
const hash = window.location.hash.replace(/^#/, '').toLowerCase()
41+
42+
if (!hash) {
43+
return null
44+
}
45+
46+
const normalized = hash.startsWith('getstarted-')
47+
? hash.slice('getstarted-'.length)
48+
: hash
49+
50+
const index = tabSlugs.indexOf(normalized)
51+
return index >= 0 ? index : null
52+
}
53+
54+
const syncActiveTabFromHash = () => {
55+
const indexFromHash = getTabIndexFromHash()
56+
if (indexFromHash !== null) {
57+
setActiveIndex(indexFromHash)
58+
}
59+
}
60+
61+
syncActiveTabFromHash()
62+
window.addEventListener('hashchange', syncActiveTabFromHash)
63+
64+
return () => {
65+
window.removeEventListener('hashchange', syncActiveTabFromHash)
66+
}
67+
}, [tabSlugs])
68+
69+
const handleTabClick = (index) => {
70+
const nextIndex = activeIndex === index ? null : index
71+
setActiveIndex(nextIndex)
72+
73+
if (typeof window !== 'undefined') {
74+
const nextHash =
75+
nextIndex === null ? 'getstarted' : `getstarted-${tabSlugs[nextIndex]}`
76+
window.history.pushState(null, '', `#${nextHash}`)
77+
}
78+
}
2679

2780
const renderTextWithLinks = (text) => {
2881
const linkRegex = /\[([^\]]+)\]\((https?:\/\/[^)\s]+)\)/g
@@ -82,9 +135,7 @@ export const GetStarted = () => {
82135
height='64px'
83136
fontSize='2xl'
84137
fontWeight='bold'
85-
onClick={() =>
86-
setActiveIndex(activeIndex === index ? null : index)
87-
}
138+
onClick={() => handleTabClick(index)}
88139
display='flex'
89140
alignItems='center'
90141
justifyContent='center'

0 commit comments

Comments
 (0)