Skip to content

Commit d148163

Browse files
committed
feat: enhance documentation components
1 parent a6344aa commit d148163

File tree

5 files changed

+32
-20
lines changed

5 files changed

+32
-20
lines changed

apps/docs/src/content/docs/my-lib/default/en/guides/components.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor i
8080

8181
:::::content-preview
8282

83-
```
83+
```mdx
8484
::::card-group{cols=3}
8585
:::card{label="Lorem ipsum dolor sit amet" icon="lucide:bell"}
8686
Lorem ipsum dolor sit amet, consectetur adipiscing elit,

packages/mdx/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"./components/callout.astro": "./src/components/callout/callout.astro",
99
"./components/card-group.astro": "./src/components/card-group/card-group.astro",
1010
"./components/card.astro": "./src/components/card-group/card.astro",
11+
"./components/tabs": "./src/components/tabs/tabs.tsx",
1112
"./components/tabs.astro": "./src/components/tabs/tabs.astro",
1213
"./components/tab.astro": "./src/components/tabs/tab.astro",
1314
"./components/steps.astro": "./src/components/steps/steps.astro",

packages/mdx/src/components/tabs/tabs.tsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,48 @@ interface TabProps {
1515
}
1616

1717
export function Tabs({ items, children, className }: TabsProps) {
18-
const [activeTab, setActiveTab] = React.useState(items[0])
18+
const [activeTab, setActiveTab] = React.useState(0)
19+
const containerRef = React.useRef<HTMLDivElement>(null)
1920

20-
const childArray = React.Children.toArray(children)
21+
React.useEffect(() => {
22+
if (!containerRef.current) return
23+
24+
const codeBlocks = containerRef.current.querySelectorAll('.code-block')
25+
if (codeBlocks.length > 0) {
26+
codeBlocks.forEach((el, index) => {
27+
;(el as HTMLElement).style.display = index === activeTab ? 'block' : 'none'
28+
})
29+
} else {
30+
const preElements = containerRef.current.querySelectorAll('pre')
31+
preElements.forEach((el, index) => {
32+
;(el as HTMLElement).style.display = index === activeTab ? 'block' : 'none'
33+
})
34+
}
35+
}, [activeTab])
2136

2237
return (
2338
<div className={cn('my-4', className)}>
2439
<div className="flex border-b" role="tablist">
25-
{items.map((item) => (
40+
{items.map((item, index) => (
2641
<button
2742
key={item}
2843
role="tab"
29-
aria-selected={activeTab === item}
44+
type="button"
45+
aria-selected={activeTab === index}
3046
className={cn(
31-
'px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px',
32-
activeTab === item
47+
'px-4 py-2 text-sm font-medium transition-colors border-b-2 -mb-px cursor-pointer',
48+
activeTab === index
3349
? 'border-primary text-primary'
3450
: 'border-transparent text-muted-foreground hover:text-foreground',
3551
)}
36-
onClick={() => setActiveTab(item)}
52+
onClick={() => setActiveTab(index)}
3753
>
3854
{item}
3955
</button>
4056
))}
4157
</div>
42-
<div className="pt-4">
43-
{childArray.map((child, index) => (
44-
<div key={items[index]} hidden={activeTab !== items[index]}>
45-
{child}
46-
</div>
47-
))}
58+
<div ref={containerRef} className="pt-4 [&_.code-block-header]:hidden [&_.code-block]:my-0 [&_.code-block]:rounded-none [&_.code-block]:border-0 [&_pre]:my-0 [&_pre]:rounded-none [&_pre]:border-0">
59+
{children}
4860
</div>
4961
</div>
5062
)

packages/mdx/src/overrides/pre/pre.astro

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ const languageIcons: Record<string, string> = {
6060
const lang = (language || '').toLowerCase()
6161
const lbl = (label || '').toLowerCase()
6262
63+
const slotHtml = await Astro.slots.render('default')
64+
6365
let icon = languageIcons[lbl] || languageIcons[lang] || 'mdi:code-tags'
6466
6567
// For shell languages, detect package manager from content
6668
if (['bash', 'sh', 'shell'].includes(lang)) {
67-
const html = await Astro.slots.render('default')
68-
const text = html.replace(/<[^>]+>/g, '').trim()
69+
const text = slotHtml.replace(/<[^>]+>/g, '').trim()
6970
const match = text.match(/^(\w+)/)
7071
if (match) {
7172
const cmd = match[1].toLowerCase()
@@ -89,13 +90,13 @@ if (['bash', 'sh', 'shell'].includes(lang)) {
8990
data-language={language}
9091
data-label={label}
9192
{...props}
92-
><slot /></pre>
93+
><Fragment set:html={slotHtml} /></pre>
9394
</div>
9495
</div>
9596
) : (
9697
<pre
9798
class={cn('my-4 overflow-x-auto rounded-lg border bg-muted p-4 text-sm leading-relaxed', className)}
9899
data-language={language}
99100
{...props}
100-
><slot /></pre>
101+
><Fragment set:html={slotHtml} /></pre>
101102
)}

packages/mdx/src/remark-auto-import.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const importStatements = [
44
`import Callout from '@explainer/mdx/components/callout.astro'`,
55
`import CardGroup from '@explainer/mdx/components/card-group.astro'`,
66
`import Card from '@explainer/mdx/components/card.astro'`,
7-
`import Tabs from '@explainer/mdx/components/tabs.astro'`,
8-
`import Tab from '@explainer/mdx/components/tab.astro'`,
97
`import Steps from '@explainer/mdx/components/steps.astro'`,
108
`import Step from '@explainer/mdx/components/step.astro'`,
119
`import CodeGroup from '@explainer/mdx/components/code-group.astro'`,

0 commit comments

Comments
 (0)