Skip to content

Commit 23608ed

Browse files
committed
docs(tabs): add docs for reusable TabList/Tab/TabPanel
1 parent 8d11289 commit 23608ed

File tree

4 files changed

+86
-60
lines changed

4 files changed

+86
-60
lines changed

apps/website/src/components/mdx-components/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,10 @@ export const components: Record<string, any> = {
159159
}),
160160
AnatomyTable,
161161
APITable,
162+
CodeSnippet,
163+
InstallSnippet,
162164
KeyboardInteractionTable,
165+
Note,
163166
StatusBanner,
164167
Showcase,
165-
CodeSnippet,
166-
InstallSnippet,
167168
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { Slot, component$ } from '@builder.io/qwik';
2+
import {
3+
Tab,
4+
TabList,
5+
TabListProps,
6+
TabPanel,
7+
TabPanelProps,
8+
TabProps,
9+
Tabs,
10+
TabsProps,
11+
} from '@qwik-ui/headless';
12+
13+
const CustomTabs = (props: TabsProps) => (
14+
<Tabs {...props} TabList={CustomTabList} Tab={CustomTab} TabPanel={CustomTabPanel} />
15+
);
16+
17+
const CustomTabList = component$<TabListProps>(() => {
18+
return (
19+
<TabList>
20+
<Slot />
21+
</TabList>
22+
);
23+
});
24+
25+
const CustomTab = component$<TabProps>(({ ...props }) => {
26+
return (
27+
<Tab {...props}>
28+
<span class="text-red-500">Custom</span> <Slot />
29+
</Tab>
30+
);
31+
});
32+
33+
const CustomTabPanel = component$<TabPanelProps>(({ ...props }) => {
34+
return (
35+
<TabPanel {...props}>
36+
<span class="text-red-500">Description:</span> <Slot />
37+
</TabPanel>
38+
);
39+
});
40+
41+
export default component$(() => {
42+
return (
43+
<div class="tabs-example mr-auto">
44+
<CustomTabs>
45+
<CustomTabList>
46+
<CustomTab>Tab 1</CustomTab>
47+
<CustomTab>Tab 2</CustomTab>
48+
<CustomTab>Tab 3</CustomTab>
49+
</CustomTabList>
50+
<CustomTabPanel>
51+
<p>Maria Theresia Ahlefeldt (16 January 1755 - 20 December 1810) ...</p>
52+
</CustomTabPanel>
53+
<CustomTabPanel>
54+
<p>Carl Joachim Andersen (29 April 1847 - 7 May 1909) ...</p>
55+
</CustomTabPanel>
56+
<CustomTabPanel>
57+
<p>Ida Henriette da Fonseca (July 27, 1802 - July 6, 1858) ...</p>
58+
</CustomTabPanel>
59+
</CustomTabs>
60+
</div>
61+
);
62+
});

apps/website/src/routes/docs/headless/tabs/index.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,27 @@ You can pass a custom `onClick$` handler.
8585

8686
<Showcase name="on-click" />
8787

88+
## Creating reusable Tabs
89+
90+
In order to remove the need for a linking `value` prop for each Tab and TabPanel pair, we have chosen to create the Tabs component as an [inline component](https://qwik.builder.io/docs/components/overview/#inline-components).
91+
92+
By consequence, the Tabs component needs to be aware of its children. If you want to create your custom reusable TabList/Tab/TabPanel components, you will have to pass them to the Tabs component through its `TabList`, `Tab`, and `TabPanel` props:
93+
94+
```tsx
95+
const CustomTabs = (props: TabsProps) => (
96+
<Tabs {...props} TabList={CustomTabList} Tab={CustomTab} TabPanel={CustomTabPanel} />
97+
);
98+
```
99+
100+
<br />
101+
102+
<Note status="warning">
103+
If you don't do the above, the Tabs component cannot know if your CustomTab component is
104+
a Tab component.
105+
</Note>
106+
107+
<Showcase name="reusable" />
108+
88109
## Accessibility
89110

90111
### Keyboard interaction

apps/website/src/routes/test/index.tsx

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)