Skip to content

Commit 394e788

Browse files
committed
wip tabs
1 parent 79ebec9 commit 394e788

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

demo/main.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import {
99
useSliderBlade,
1010
useButtonBlade,
1111
useTextBlade,
12+
useTabBlade,
1213
useTweakpane,
14+
asFolderInstance
1315
} from '../src'
1416
import './index.css'
1517

18+
1619
export function App() {
1720
const meshRef = useRef<Mesh>(null!)
1821
const [fruitCount, setFruitCount] = useState(0)
@@ -30,6 +33,27 @@ export function App() {
3033
}
3134
)
3235

36+
const { tabs } = useTabBlade(pane, {
37+
pages: [
38+
{ title: 'Parameters' },
39+
{ title: 'Advanced' }
40+
]
41+
})
42+
43+
// const mainTab = tabs[0]
44+
45+
// if (!mainTab?.current) return null
46+
47+
// usePaneInput(
48+
// asFolderInstance(mainTab),
49+
// 'color',
50+
// {
51+
// label: 'Color',
52+
// value: '#ffa500',
53+
// view: 'color',
54+
// },
55+
// )
56+
3357
const [time] = useSliderBlade(pane, {
3458
label: 'Sky',
3559
value: 0.6,
@@ -96,6 +120,37 @@ export function App() {
96120
title: 'Box Settings',
97121
})
98122

123+
const mytabs = useTabBlade(folder, {
124+
pages: [
125+
{ title: 'Parameters' },
126+
{ title: 'Advanced' }
127+
]
128+
})
129+
130+
131+
// Get the specific tab page instance
132+
// const tabPage = mytabs.tabs[0].current.instance;
133+
134+
// // If you're trying to add a binding to this tab page
135+
// if (tabPage) {
136+
// // The correct way to add bindings to a tab page
137+
// tabPage.addBinding(pane.current.params, 'position', {
138+
// label: 'Position',
139+
// x: {
140+
// min: -6,
141+
// max: 6,
142+
// },
143+
// y: {
144+
// min: -6,
145+
// max: 6,
146+
// },
147+
// z: {
148+
// min: -6,
149+
// max: 6,
150+
// },
151+
// });
152+
// }
153+
99154
usePaneInput(
100155
folder,
101156
'position',

src/hooks/useTabBlade.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { TabApi, TabPageApi, TabBladeParams, FolderApi } from '@tweakpane/core'
2+
import { RefObject, useLayoutEffect, useRef } from 'react'
3+
import { FolderInstance } from './usePaneFolder'
4+
5+
interface UseTabBladeParams {
6+
pages: {
7+
title: string;
8+
}[];
9+
}
10+
11+
export function asFolderInstance<T extends object>(
12+
tab: RefObject<TabPageInstance<T>>
13+
): RefObject<FolderInstance<T>> {
14+
return tab as unknown as RefObject<FolderInstance<T>>
15+
}
16+
17+
// Create a wrapper that makes TabPageApi behave like FolderApi
18+
export interface TabPageInstance<T extends {}> {
19+
instance: TabPageApi | FolderApi
20+
params: T
21+
}
22+
23+
export function useTabBlade<T extends Object>(
24+
paneRef: RefObject<FolderInstance<T>>,
25+
params: UseTabBladeParams
26+
): {
27+
tabRef: RefObject<TabApi>,
28+
tabs: RefObject<TabPageInstance<T>>[]
29+
} {
30+
const tabRef = useRef<TabApi>(null!)
31+
const tabsRef = useRef<RefObject<TabPageInstance<T>>[]>([])
32+
33+
useLayoutEffect(() => {
34+
const pane = paneRef.current?.instance
35+
if (pane == null) return
36+
37+
const tab = pane.addTab({
38+
...params,
39+
view: 'tab'
40+
} as TabBladeParams) as TabApi
41+
42+
tabRef.current = tab
43+
44+
// Since TabPageApi implements ContainerApi, we can use it directly
45+
tabsRef.current = tab.pages.map(page => ({
46+
current: {
47+
instance: page,
48+
params: paneRef.current?.params || ({} as T)
49+
}
50+
}))
51+
52+
return () => {
53+
if (tab.element) tab.dispose()
54+
}
55+
}, [])
56+
57+
return {
58+
tabRef,
59+
tabs: tabsRef.current
60+
}
61+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export * from './hooks/useSliderBlade'
44
export * from './hooks/useButtonBlade'
55
export * from './hooks/usePaneFolder'
66
export * from './hooks/usePaneInput'
7+
export * from './hooks/useTabBlade'
78
export * from './hooks/useTweakpane'

0 commit comments

Comments
 (0)