mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
refactor: use CSS variable
This commit is contained in:
parent
2b998e3fcf
commit
02e3337037
2 changed files with 15 additions and 5 deletions
|
|
@ -1,10 +1,11 @@
|
|||
import { HorizontalResizeHandler } from 'components/ResizeHandler'
|
||||
import { useConfigs } from 'containers/ConfigsContext'
|
||||
import * as React from 'react'
|
||||
import { useWindowSize } from 'react-use'
|
||||
import { useDebounce, useWindowSize } from 'react-use'
|
||||
import { cx } from 'utils/cx'
|
||||
import { setResizingState } from 'utils/DOMHelper'
|
||||
import * as features from 'utils/features'
|
||||
import { useCSSVariable } from './useCSSVariable'
|
||||
|
||||
export type Size = number
|
||||
type Props = {
|
||||
|
|
@ -34,10 +35,8 @@ export function Resizable({ baseSize, className, children }: React.PropsWithChil
|
|||
return () => clearTimeout(timer)
|
||||
}, [width, size])
|
||||
|
||||
React.useEffect(() => {
|
||||
document.documentElement.style.setProperty('--gitako-width', size + 'px')
|
||||
configContext.set({ sideBarWidth: size })
|
||||
}, [size])
|
||||
useCSSVariable('--gitako-width', `${size}px`)
|
||||
useDebounce(() => configContext.set({ sideBarWidth: size }), 100, [size])
|
||||
|
||||
const onResize = React.useCallback((size: number) => {
|
||||
if (size < window.innerWidth - MINIMAL_CONTENT_VIEWPORT_WIDTH) setSize(size)
|
||||
|
|
|
|||
11
src/components/useCSSVariable.tsx
Normal file
11
src/components/useCSSVariable.tsx
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import { useLayoutEffect } from 'react'
|
||||
|
||||
export function useCSSVariable(
|
||||
name: string,
|
||||
value: string,
|
||||
element: HTMLElement = document.documentElement,
|
||||
) {
|
||||
useLayoutEffect(() => {
|
||||
element.style.setProperty(name, value)
|
||||
}, [name, value, element])
|
||||
}
|
||||
Loading…
Reference in a new issue