Skip to content

Commit ba30c6c

Browse files
authoredSep 13, 2024··
[v3] use render props for className with selected, disabled and hover state for <Tab> (#3244)
* use render props for className with `selected`, `disabled` and `hover` state for `<Tab>` * fix border primary
1 parent 0b70b44 commit ba30c6c

File tree

5 files changed

+42
-14
lines changed

5 files changed

+42
-14
lines changed
 

‎.changeset/grumpy-pumas-attack.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'nextra-theme-blog': major
3+
'nextra-theme-docs': major
4+
'nextra': major
5+
---
6+
7+
use render props for className with `selected`, `disabled` and `hover` state for `<Tab>`

‎packages/nextra-theme-docs/tailwind.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default {
1414
content: [
1515
'./src/**/*.tsx',
1616
'../nextra/src/client/icons/*.{tsx,svg}',
17-
'../nextra/src/client/components/*.tsx'
17+
'../nextra/src/client/components/**/*.tsx'
1818
],
1919
theme: {
2020
screens: {

‎packages/nextra/src/client/components/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export { Pre } from './pre.js'
88
export { RemoteContent, evaluate } from './remote-content.js'
99
export { Steps } from './steps.js'
1010
export { Table } from './table.js'
11-
export { Tabs } from './tabs.js'
11+
export { Tabs } from './tabs/index.js'
1212
export { Td } from './td.js'
1313
export { Th } from './th.js'
1414
export { Tr } from './tr.js'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { ComponentProps } from 'react'
2+
import { Tabs as _Tabs, Tab } from './tabs.js'
3+
4+
// Workaround to fix
5+
// Error: Cannot access Tab.propTypes on the server. You cannot dot into a client module from a
6+
// server component. You can only pass the imported name through.
7+
export const Tabs = Object.assign(
8+
(props: ComponentProps<typeof _Tabs>) => <_Tabs {...props} />,
9+
{ Tab }
10+
)

‎packages/nextra/src/client/components/tabs.tsx renamed to ‎packages/nextra/src/client/components/tabs/tabs.tsx

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use client'
2+
13
import {
24
Tab as HeadlessTab,
35
TabGroup,
@@ -20,7 +22,7 @@ function isTabObjectItem(item: unknown): item is TabObjectItem {
2022
return !!item && typeof item === 'object' && 'label' in item
2123
}
2224

23-
function Tabs_({
25+
export function Tabs({
2426
items,
2527
selectedIndex: _selectedIndex,
2628
defaultIndex = 0,
@@ -97,14 +99,25 @@ function Tabs_({
9799
<HeadlessTab
98100
key={index}
99101
disabled={isTabObjectItem(item) && item.disabled}
100-
className={cn(
101-
'_ring-inset',
102-
'_rounded-t _p-2 _font-medium _leading-5 _transition-colors',
103-
'_-mb-0.5 _select-none _border-b-2',
104-
'data-[selected]:!_border-current data-[selected]:!_text-primary-600',
105-
'_border-transparent _text-gray-600 hover:_border-gray-200 hover:_text-black dark:_text-gray-200 dark:hover:_border-neutral-800 dark:hover:_text-white',
106-
'disabled:_pointer-events-none disabled:_text-gray-400 disabled:dark:_text-neutral-600'
107-
)}
102+
className={({ selected, disabled, hover }) =>
103+
cn(
104+
'_ring-inset',
105+
'_rounded-t _p-2 _font-medium _leading-5 _transition-colors',
106+
'_-mb-0.5 _select-none _border-b-2',
107+
selected
108+
? '_border-current'
109+
: hover
110+
? '_border-gray-200 dark:_border-neutral-800'
111+
: '_border-transparent',
112+
selected
113+
? '_text-primary-600'
114+
: disabled
115+
? '_text-gray-400 dark:_text-neutral-600 _pointer-events-none'
116+
: hover
117+
? '_text-black dark:_text-white'
118+
: '_text-gray-600 dark:_text-gray-200'
119+
)
120+
}
108121
>
109122
{isTabObjectItem(item) ? item.label : item}
110123
</HeadlessTab>
@@ -115,7 +128,7 @@ function Tabs_({
115128
)
116129
}
117130

118-
function Tab({
131+
export function Tab({
119132
children,
120133
// For SEO display all the Panel in the DOM and set `display: none;` for those that are not selected
121134
unmount = false,
@@ -127,5 +140,3 @@ function Tab({
127140
</TabPanel>
128141
)
129142
}
130-
131-
export const Tabs = Object.assign(Tabs_, { displayName: 'Tabs', Tab })

0 commit comments

Comments
 (0)
Please sign in to comment.