Try to use BottomSheet's own APIs

This commit is contained in:
Eric Bailey
2024-01-15 10:48:59 -06:00
parent 4f71e4ee7d
commit 94d31d4cc5
3 changed files with 28 additions and 33 deletions
+26 -31
View File
@@ -1,6 +1,9 @@
import React, {useImperativeHandle} from 'react' import React, {useImperativeHandle} from 'react'
import {View, Dimensions, LayoutChangeEvent} from 'react-native' import {View, Dimensions} from 'react-native'
import BottomSheet, {BottomSheetBackdrop} from '@gorhom/bottom-sheet' import BottomSheet, {
BottomSheetBackdrop,
BottomSheetView,
} from '@gorhom/bottom-sheet'
import {useSafeAreaInsets} from 'react-native-safe-area-context' import {useSafeAreaInsets} from 'react-native-safe-area-context'
import {useTheme, atoms as a} from '#/alf' import {useTheme, atoms as a} from '#/alf'
@@ -29,12 +32,8 @@ export function Outer({
onClose, onClose,
nativeOptions, nativeOptions,
}: React.PropsWithChildren<DialogOuterProps>) { }: React.PropsWithChildren<DialogOuterProps>) {
const insets = useSafeAreaInsets()
const t = useTheme() const t = useTheme()
const sheet = React.useRef<BottomSheet>(null) const sheet = React.useRef<BottomSheet>(null)
const [defaultSnapPoints, setDefaultSnapPoints] = React.useState<
string | number
>('25%')
const open = React.useCallback<DialogControlProps['open']>((i = 0) => { const open = React.useCallback<DialogControlProps['open']>((i = 0) => {
sheet.current?.snapToIndex(i) sheet.current?.snapToIndex(i)
@@ -45,15 +44,6 @@ export function Outer({
onClose?.() onClose?.()
}, [onClose]) }, [onClose])
const measureDefaultSnapPoint = React.useCallback(
(e: LayoutChangeEvent) => {
const min = e.nativeEvent.layout.height + insets.bottom + 40
const max = Dimensions.get('window').height - insets.top - 40
setDefaultSnapPoints(min < max ? min : max)
},
[insets, setDefaultSnapPoints],
)
useImperativeHandle( useImperativeHandle(
control.ref, control.ref,
() => ({ () => ({
@@ -68,10 +58,11 @@ export function Outer({
return ( return (
<Portal> <Portal>
<BottomSheet <BottomSheet
snapPoints={[defaultSnapPoints]} enableDynamicSizing
enablePanDownToClose enablePanDownToClose
keyboardBehavior="interactive" keyboardBehavior="interactive"
android_keyboardInputMode="adjustResize" android_keyboardInputMode="adjustResize"
keyboardBlurBehavior="restore"
{...(nativeOptions?.sheet || {})} {...(nativeOptions?.sheet || {})}
ref={sheet} ref={sheet}
index={-1} index={-1}
@@ -86,21 +77,23 @@ export function Outer({
handleIndicatorStyle={{backgroundColor: t.palette.primary_500}} handleIndicatorStyle={{backgroundColor: t.palette.primary_500}}
handleStyle={{display: 'none'}} handleStyle={{display: 'none'}}
onClose={onClose}> onClose={onClose}>
<Context.Provider value={context}> <BottomSheetView>
<View <Context.Provider value={context}>
style={[ <View
a.absolute, style={[
a.inset_0, a.absolute,
t.atoms.bg, a.inset_0,
{ t.atoms.bg,
borderTopLeftRadius: 40, {
borderTopRightRadius: 40, borderTopLeftRadius: 40,
height: Dimensions.get('window').height * 2, borderTopRightRadius: 40,
}, height: Dimensions.get('window').height * 2,
]} },
/> ]}
<View onLayout={measureDefaultSnapPoint}>{children}</View> />
</Context.Provider> {children}
</Context.Provider>
</BottomSheetView>
</BottomSheet> </BottomSheet>
</Portal> </Portal>
) )
@@ -108,6 +101,7 @@ export function Outer({
// TODO a11y props here, or is that handled by the sheet? // TODO a11y props here, or is that handled by the sheet?
export function Inner(props: DialogInnerProps) { export function Inner(props: DialogInnerProps) {
const insets = useSafeAreaInsets()
return ( return (
<View <View
style={[ style={[
@@ -116,6 +110,7 @@ export function Inner(props: DialogInnerProps) {
{ {
borderTopLeftRadius: 40, borderTopLeftRadius: 40,
borderTopRightRadius: 40, borderTopRightRadius: 40,
paddingBottom: insets.bottom + a.pb_5xl.paddingBottom,
}, },
]}> ]}>
{props.children} {props.children}
+1 -1
View File
@@ -51,7 +51,7 @@ export function Dialogs() {
accessibilityDescribedBy="dialog-description"> accessibilityDescribedBy="dialog-description">
<Dialog.Handle /> <Dialog.Handle />
<View style={[a.gap_md]}> <View style={[a.relative, a.gap_md, a.w_full]}>
<H3 nativeID="dialog-title">Dialog</H3> <H3 nativeID="dialog-title">Dialog</H3>
<P nativeID="dialog-description">Description</P> <P nativeID="dialog-description">Description</P>
<View style={[a.flex_row, a.justify_end]}> <View style={[a.flex_row, a.justify_end]}>
+1 -1
View File
@@ -53,6 +53,7 @@ export function Storybook() {
</Button> </Button>
</View> </View>
<Dialogs />
<ThemeProvider theme="light"> <ThemeProvider theme="light">
<Theming /> <Theming />
</ThemeProvider> </ThemeProvider>
@@ -68,7 +69,6 @@ export function Storybook() {
<Buttons /> <Buttons />
<Links /> <Links />
<Forms /> <Forms />
<Dialogs />
<Breakpoints /> <Breakpoints />
</View> </View>
</CenteredView> </CenteredView>