-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact.tsx
54 lines (48 loc) · 1.32 KB
/
react.tsx
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { type Configuration, grid } from 'masua'
import { type JSX, useEffect, useRef } from 'react'
interface ReactConfiguration extends Configuration {
disabled: boolean
}
const configurationProperties = [
'baseWidth',
'gutter',
'gutterX',
'gutterY',
'minify',
'surroundingGutter',
'singleColumnGutter',
'direction',
'wedge',
]
export function Grid({ disabled = false, children, ...props }: JSX.IntrinsicElements['div'] & Partial<ReactConfiguration>) {
const gridRef = useRef(null)
const instance = useRef<ReturnType<typeof grid> | null>(null)
// TODO props changes on every render, cannot be memoized, should do deep compare.
const configurationProps = Object.entries(props).reduce((result: { [key: string]: string }, [key, value]) => {
if (configurationProperties.includes(key)) {
result[key] = value
// @ts-ignore
delete props[key]
}
return result
}, {})
useEffect(() => {
if (disabled) {
return
}
if (instance.current) {
instance.current.update()
return instance.current.destroy
}
if (!gridRef.current) {
return
}
instance.current = grid(gridRef.current, configurationProps)
return instance.current?.destroy
}, [configurationProps, disabled])
return (
<div ref={gridRef} {...props}>
{children}
</div>
)
}