Skip to content

Commit

Permalink
release(toolbar): v0.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Oct 30, 2024
1 parent a09e3fe commit ee89858
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion index.json
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@
},
"toolbar": {
"react": true,
"version": "0.6.1",
"version": "0.6.2",
"style": true,
"icon": false,
"test": true,
Expand Down
8 changes: 8 additions & 0 deletions src/share/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export function useForceUpdate() {
return () => setForceUpdateValue((value) => value + 1)
}

export function usePrevious<T>(value: T): T | undefined {
const ref = useRef<T>()
useEffect(() => {
ref.current = value
})
return ref.current
}

export function useNonInitialEffect(
effect: EffectCallback,
deps?: DependencyList
Expand Down
2 changes: 1 addition & 1 deletion src/toolbar/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "toolbar",
"version": "0.6.1",
"version": "0.6.2",
"description": "Application toolbar",
"luna": {
"react": true
Expand Down
20 changes: 16 additions & 4 deletions src/toolbar/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Toolbar, {
} from './index'
import $ from 'licia/$'
import isUndef from 'licia/isUndef'
import { useForceUpdate } from '../share/hooks'
import { useForceUpdate, usePrevious } from '../share/hooks'
import { IComponentOptions } from '../share/Component'
import { createPortal } from 'react-dom'

Expand All @@ -33,17 +33,29 @@ const LunaToolbar: FC<PropsWithChildren<IToolbarProps>> = (props) => {
const toolbarRef = useRef<HTMLDivElement>(null)
const toolbar = useRef<Toolbar>()
const forceUpdate = useForceUpdate()
const prevProps = usePrevious(props)

useEffect(() => {
toolbar.current = new Toolbar(toolbarRef.current!)
toolbar.current.on('change', (key, val, oldVal) => {
props.onChange && props.onChange(key, val, oldVal)
})
if (props.onChange) {
toolbar.current.on('change', props.onChange)
}
forceUpdate()

return () => toolbar.current?.destroy()
}, [])

useEffect(() => {
if (toolbar.current) {
if (prevProps?.onChange) {
toolbar.current.off('change', prevProps.onChange)
}
if (props.onChange) {
toolbar.current.on('change', props.onChange)
}
}
}, [props.onChange])

useEffect(() => {
if (toolbar.current) {
toolbar.current.setOption('theme', props.theme)
Expand Down

0 comments on commit ee89858

Please sign in to comment.