-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathuseButtonBlade.ts
More file actions
36 lines (29 loc) · 870 Bytes
/
Copy pathuseButtonBlade.ts
File metadata and controls
36 lines (29 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { BaseBladeParams } from '@tweakpane/core'
import { RefObject, useLayoutEffect, useRef } from 'react'
import { ButtonApi } from 'tweakpane'
import { FolderInstance } from './usePaneFolder'
interface UseButtonBladeParams extends BaseBladeParams {
title: string
label?: string
}
export function useButtonBlade<T extends Object>(
paneRef: RefObject<FolderInstance<T>>,
params: UseButtonBladeParams,
onClick: () => void
): RefObject<ButtonApi> {
const bladeRef = useRef<ButtonApi>(null!)
useLayoutEffect(() => {
const pane = paneRef.current.instance
if (pane == null) return
const blade = pane.addButton({
title: params.title,
label: params.label,
}) as ButtonApi
blade.on('click', onClick)
bladeRef.current = blade
return () => {
if (blade.element) blade.dispose()
}
}, [])
return bladeRef
}