-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathAutoSizer.tsx
37 lines (33 loc) · 969 Bytes
/
AutoSizer.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
import React from 'react'
import { useCallback, useState } from 'react'
import { LayoutChangeEvent, View } from 'react-native'
import { sharedStyles } from '../shared/styles'
type WidthAndHeight = {
width: number
height: number
}
export default function AutoSizer({
children,
}: {
children: ({ width, height }: WidthAndHeight) => any
}) {
const [layout, setLayout] = useState<WidthAndHeight | null>(null)
const onLayout = useCallback(
(event: LayoutChangeEvent) => {
const nl = event.nativeEvent.layout
// https://github.com/necolas/react-native-web/issues/1704
if (!layout || layout.width !== nl.width || layout.height !== nl.height) {
setLayout({ width: nl.width, height: nl.height })
}
},
[layout, setLayout]
)
return (
<View
onLayout={onLayout}
style={[sharedStyles.overflowHidden, sharedStyles.root, layout && layout]}
>
{layout ? children(layout) : null}
</View>
)
}