From 4f71e4ee7dbf37715a89c15d53dde5929a303ed3 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Sun, 14 Jan 2024 11:14:13 -0600 Subject: [PATCH] Autosize dialog, handle max height, Dialog.ScrolLView not working --- src/components/Dialog/index.tsx | 29 +++++++++++++++++--- src/components/Dialog/index.web.tsx | 1 + src/components/Prompt.tsx | 38 ++++++-------------------- src/view/screens/Storybook/Dialogs.tsx | 1 + 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/components/Dialog/index.tsx b/src/components/Dialog/index.tsx index 4bc1338e0b..7b3e34db20 100644 --- a/src/components/Dialog/index.tsx +++ b/src/components/Dialog/index.tsx @@ -1,6 +1,7 @@ import React, {useImperativeHandle} from 'react' -import {View, Dimensions} from 'react-native' +import {View, Dimensions, LayoutChangeEvent} from 'react-native' import BottomSheet, {BottomSheetBackdrop} from '@gorhom/bottom-sheet' +import {useSafeAreaInsets} from 'react-native-safe-area-context' import {useTheme, atoms as a} from '#/alf' import {Portal} from '#/components/Portal' @@ -12,6 +13,13 @@ import { } from '#/components/Dialog/types' import {Context} from '#/components/Dialog/context' +/** + * Exports + */ +export { + BottomSheetScrollView as ScrollView, + BottomSheetTextInput as TextInput, +} from '@gorhom/bottom-sheet' export {useDialogControl, useDialogContext} from '#/components/Dialog/context' export * from '#/components/Dialog/types' @@ -21,8 +29,12 @@ export function Outer({ onClose, nativeOptions, }: React.PropsWithChildren) { + const insets = useSafeAreaInsets() const t = useTheme() const sheet = React.useRef(null) + const [defaultSnapPoints, setDefaultSnapPoints] = React.useState< + string | number + >('25%') const open = React.useCallback((i = 0) => { sheet.current?.snapToIndex(i) @@ -33,6 +45,15 @@ export function Outer({ 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( control.ref, () => ({ @@ -47,9 +68,9 @@ export function Outer({ return ( - {children} + {children} diff --git a/src/components/Dialog/index.web.tsx b/src/components/Dialog/index.web.tsx index 8efccd55d7..88b29e13a8 100644 --- a/src/components/Dialog/index.web.tsx +++ b/src/components/Dialog/index.web.tsx @@ -11,6 +11,7 @@ import {Button} from '#/components/Button' import {DialogOuterProps, DialogInnerProps} from '#/components/Dialog/types' import {Context, useDialogContext} from '#/components/Dialog/context' +export {ScrollView, TextInput} from 'react-native' export {useDialogControl, useDialogContext} from '#/components/Dialog/context' export * from '#/components/Dialog/types' diff --git a/src/components/Prompt.tsx b/src/components/Prompt.tsx index 61bf614be0..4617943829 100644 --- a/src/components/Prompt.tsx +++ b/src/components/Prompt.tsx @@ -1,6 +1,5 @@ import React from 'react' -import {View, PressableProps, LayoutChangeEvent} from 'react-native' -import {useSafeAreaInsets} from 'react-native-safe-area-context' +import {View, PressableProps} from 'react-native' import {useTheme, atoms as a} from '#/alf' import {H4, P} from '#/components/Typography' @@ -24,43 +23,24 @@ export function Outer({ }: React.PropsWithChildren<{ control: Dialog.DialogOuterProps['control'] }>) { - const insets = useSafeAreaInsets() const titleId = React.useId() const descriptionId = React.useId() - const [defaultSnapPoints, setDefaultSnapPoints] = React.useState< - string | number - >('25%') const context = React.useMemo( () => ({titleId, descriptionId}), [titleId, descriptionId], ) - const measureDefaultSnapPoint = React.useCallback( - (e: LayoutChangeEvent) => { - setDefaultSnapPoints(e.nativeEvent.layout.height + insets.bottom + 50) - }, - [insets, setDefaultSnapPoints], - ) - return ( - + - - - - {children} - - + + + {children} + ) diff --git a/src/view/screens/Storybook/Dialogs.tsx b/src/view/screens/Storybook/Dialogs.tsx index b4a779e9d0..8ecc2d101c 100644 --- a/src/view/screens/Storybook/Dialogs.tsx +++ b/src/view/screens/Storybook/Dialogs.tsx @@ -50,6 +50,7 @@ export function Dialogs() { accessibilityLabelledBy="dialog-title" accessibilityDescribedBy="dialog-description"> +

Dialog

Description