@cerberus-design/react 0.2.0-next-ae70f9a → 0.2.0-next-87c3df1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/_tsup-dts-rollup.d.ts +125 -0
- package/build/legacy/chunk-24B4KIPX.js +46 -0
- package/build/legacy/chunk-24B4KIPX.js.map +1 -0
- package/build/legacy/chunk-6DXQW4WK.js +109 -0
- package/build/legacy/chunk-6DXQW4WK.js.map +1 -0
- package/build/legacy/chunk-BVILNJ6M.js +28 -0
- package/build/legacy/chunk-BVILNJ6M.js.map +1 -0
- package/build/legacy/chunk-UI7OR6PG.js +31 -0
- package/build/legacy/chunk-UI7OR6PG.js.map +1 -0
- package/build/legacy/components/Tab.d.ts +2 -0
- package/build/legacy/components/Tab.js +9 -0
- package/build/legacy/components/Tab.js.map +1 -0
- package/build/legacy/components/TabList.d.ts +2 -0
- package/build/legacy/components/TabList.js +7 -0
- package/build/legacy/components/TabList.js.map +1 -0
- package/build/legacy/components/TabPanel.d.ts +2 -0
- package/build/legacy/components/TabPanel.js +10 -0
- package/build/legacy/components/TabPanel.js.map +1 -0
- package/build/legacy/context/tabs.d.ts +4 -0
- package/build/legacy/context/tabs.js +10 -0
- package/build/legacy/context/tabs.js.map +1 -0
- package/build/legacy/index.d.ts +10 -0
- package/build/legacy/index.js +18 -0
- package/build/modern/_tsup-dts-rollup.d.ts +125 -0
- package/build/modern/chunk-24B4KIPX.js +46 -0
- package/build/modern/chunk-24B4KIPX.js.map +1 -0
- package/build/modern/chunk-AMFL3PZV.js +108 -0
- package/build/modern/chunk-AMFL3PZV.js.map +1 -0
- package/build/modern/chunk-BVILNJ6M.js +28 -0
- package/build/modern/chunk-BVILNJ6M.js.map +1 -0
- package/build/modern/chunk-UI7OR6PG.js +31 -0
- package/build/modern/chunk-UI7OR6PG.js.map +1 -0
- package/build/modern/components/Tab.d.ts +2 -0
- package/build/modern/components/Tab.js +9 -0
- package/build/modern/components/Tab.js.map +1 -0
- package/build/modern/components/TabList.d.ts +2 -0
- package/build/modern/components/TabList.js +7 -0
- package/build/modern/components/TabList.js.map +1 -0
- package/build/modern/components/TabPanel.d.ts +2 -0
- package/build/modern/components/TabPanel.js +10 -0
- package/build/modern/components/TabPanel.js.map +1 -0
- package/build/modern/context/tabs.d.ts +4 -0
- package/build/modern/context/tabs.js +10 -0
- package/build/modern/context/tabs.js.map +1 -0
- package/build/modern/index.d.ts +10 -0
- package/build/modern/index.js +18 -0
- package/package.json +1 -1
- package/src/components/Tab.tsx +128 -0
- package/src/components/TabList.tsx +42 -0
- package/src/components/TabPanel.tsx +45 -0
- package/src/context/tabs.tsx +83 -0
- package/src/index.ts +4 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useState,
|
|
9
|
+
type PropsWithChildren,
|
|
10
|
+
} from 'react'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This module provides a Tabs component and a hook to access its context.
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export interface TabsContextValue {
|
|
18
|
+
active: string
|
|
19
|
+
onTabUpdate: (active: string) => void
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const TabsContext = createContext<TabsContextValue | null>(null)
|
|
23
|
+
|
|
24
|
+
export interface TabsProps {
|
|
25
|
+
active?: string
|
|
26
|
+
cache?: boolean
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The Tabs component provides a context to manage tab state.
|
|
31
|
+
* @param active - the default active tab id,
|
|
32
|
+
* @param cache - whether to cache the active tab state
|
|
33
|
+
* @example
|
|
34
|
+
* ```tsx
|
|
35
|
+
* <Tabs cache>
|
|
36
|
+
* <TabList description="Button details">
|
|
37
|
+
* <Tab id="overview">Overview</Tab>
|
|
38
|
+
* <Tab id="guidelines">Guidelines</Tab>
|
|
39
|
+
* </TabList>
|
|
40
|
+
* <TabPanels>
|
|
41
|
+
* <TabPanel id="overview">Overview content</TabPanel>
|
|
42
|
+
* <TabPanel id="guidelines">Guidelines content</TabPanel>
|
|
43
|
+
* </TabPanels>
|
|
44
|
+
* </Tabs>
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export function Tabs(props: PropsWithChildren<TabsProps>): JSX.Element {
|
|
48
|
+
const { cache } = props
|
|
49
|
+
const [active, setActive] = useState(() => props.active ?? '')
|
|
50
|
+
|
|
51
|
+
const value = useMemo(
|
|
52
|
+
() => ({
|
|
53
|
+
active,
|
|
54
|
+
onTabUpdate: setActive,
|
|
55
|
+
}),
|
|
56
|
+
[active, setActive],
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
if (cache) {
|
|
61
|
+
const cachedTab = window.localStorage.getItem('cerberus-tabs')
|
|
62
|
+
setActive(cachedTab ?? active)
|
|
63
|
+
}
|
|
64
|
+
}, [cache])
|
|
65
|
+
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (cache) {
|
|
68
|
+
window.localStorage.setItem('cerberus-tabs', active)
|
|
69
|
+
}
|
|
70
|
+
}, [active, cache])
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<TabsContext.Provider value={value}>{props.children}</TabsContext.Provider>
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function useTabsContext(): TabsContextValue {
|
|
78
|
+
const context = useContext(TabsContext)
|
|
79
|
+
if (!context) {
|
|
80
|
+
throw new Error('useTabsContext must be used within a Tabs Provider.')
|
|
81
|
+
}
|
|
82
|
+
return context
|
|
83
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -13,12 +13,16 @@ export * from './components/Label'
|
|
|
13
13
|
export * from './components/NavMenuTrigger'
|
|
14
14
|
export * from './components/NavMenuList'
|
|
15
15
|
export * from './components/NavMenuLink'
|
|
16
|
+
export * from './components/Tab'
|
|
17
|
+
export * from './components/TabList'
|
|
18
|
+
export * from './components/TabPanel'
|
|
16
19
|
export * from './components/Show'
|
|
17
20
|
|
|
18
21
|
// context
|
|
19
22
|
|
|
20
23
|
export * from './context/field'
|
|
21
24
|
export * from './context/navMenu'
|
|
25
|
+
export * from './context/tabs'
|
|
22
26
|
export * from './context/theme'
|
|
23
27
|
|
|
24
28
|
// hooks
|