Skip to content

Commit 7cbde06

Browse files
add id to tab key (#557)
1 parent 0852c4b commit 7cbde06

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/components/Markdown.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ const CustomHeading = ({
3030
// Convert children to array and strip any inner anchor (native 'a' or MarkdownLink)
3131
const childrenArray = React.Children.toArray(children)
3232
const sanitizedChildren = childrenArray.map((child) => {
33-
if (React.isValidElement(child) && (child.type === 'a' || child.type === MarkdownLink)) {
33+
if (
34+
React.isValidElement(child) &&
35+
(child.type === 'a' || child.type === MarkdownLink)
36+
) {
3437
// replace anchor child with its own children so outer anchor remains the only link
3538
return child.props.children ?? null
3639
}
@@ -45,7 +48,10 @@ const CustomHeading = ({
4548

4649
if (id) {
4750
return (
48-
<a href={`#${id}`} className={`anchor-heading *:scroll-my-20 *:lg:scroll-my-4`}>
51+
<a
52+
href={`#${id}`}
53+
className={`anchor-heading *:scroll-my-20 *:lg:scroll-my-4`}
54+
>
4955
{heading}
5056
</a>
5157
)
@@ -55,8 +61,7 @@ const CustomHeading = ({
5561
}
5662

5763
const makeHeading =
58-
(type: HeadingLevel) =>
59-
(props: HTMLProps<HTMLHeadingElement>) =>
64+
(type: HeadingLevel) => (props: HTMLProps<HTMLHeadingElement>) =>
6065
(
6166
<CustomHeading
6267
Comp={type}
@@ -327,7 +332,9 @@ const options: HTMLReactParserOptions = {
327332
domToReact(panel.children as any, options)
328333
)
329334

330-
return <Tabs tabs={tabs} children={children as any} />
335+
return (
336+
<Tabs id={attributes.id} tabs={tabs} children={children as any} />
337+
)
331338
}
332339
default:
333340
return <div>{domToReact(domNode.children as any, options)}</div>

src/components/Tabs.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ export type TabDefinition = {
1111
export type TabsProps = {
1212
tabs: Array<TabDefinition>
1313
children: Array<React.ReactNode>
14+
id: string
1415
}
1516

16-
export function Tabs({ tabs, children }: TabsProps) {
17+
export function Tabs({ tabs, id, children }: TabsProps) {
1718
const Route = getRouteApi()
1819
const { framework } = Route.useParams()
1920

@@ -27,6 +28,7 @@ export function Tabs({ tabs, children }: TabsProps) {
2728
{tabs.map((tab) => {
2829
return (
2930
<Tab
31+
id={id}
3032
tab={tab}
3133
activeSlug={activeSlug}
3234
setActiveSlug={setActiveSlug}
@@ -40,7 +42,7 @@ export function Tabs({ tabs, children }: TabsProps) {
4042
if (!tab) return null
4143
return (
4244
<div
43-
key={tab.slug}
45+
key={`${id}-${tab.slug}`}
4446
data-tab={tab.slug}
4547
hidden={tab.slug !== activeSlug}
4648
className="prose dark:prose-invert max-w-none flex flex-col gap-2 text-base"
@@ -55,18 +57,20 @@ export function Tabs({ tabs, children }: TabsProps) {
5557
}
5658

5759
const Tab = ({
60+
id,
5861
tab,
5962
activeSlug,
6063
setActiveSlug,
6164
}: {
65+
id: string
6266
tab: TabDefinition
6367
activeSlug: string
6468
setActiveSlug: React.Dispatch<React.SetStateAction<string>>
6569
}) => {
6670
const options = React.useMemo(() => getFrameworkOptions(tab.slug), [tab.slug])
6771
return (
6872
<button
69-
key={tab.slug}
73+
key={`${id}-${tab.slug}`}
7074
aria-label={tab.name}
7175
title={tab.name}
7276
type="button"

0 commit comments

Comments
 (0)