From b52a742925cff4429885e94815d61f3f7cfb5a66 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Mon, 19 Feb 2024 18:18:13 -0600 Subject: Improve dialogs (#2933) * Improve dialogs * Remove comment, revert storybook * Hacky fix * Comments --- src/components/Dialog/types.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/components/Dialog/types.ts') diff --git a/src/components/Dialog/types.ts b/src/components/Dialog/types.ts index d36784183..00178926a 100644 --- a/src/components/Dialog/types.ts +++ b/src/components/Dialog/types.ts @@ -1,15 +1,27 @@ import React from 'react' -import type {ViewStyle, AccessibilityProps} from 'react-native' +import type {AccessibilityProps} from 'react-native' import {BottomSheetProps} from '@gorhom/bottom-sheet' +import {ViewStyleProp} from '#/alf' + type A11yProps = Required export type DialogContextProps = { close: () => void } +export type DialogControlOpenOptions = { + /** + * NATIVE ONLY + * + * Optional index of the snap point to open the bottom sheet to. Defaults to + * 0, which is the first snap point (i.e. "open"). + */ + index?: number +} + export type DialogControlProps = { - open: (index?: number) => void + open: (options?: DialogControlOpenOptions) => void close: () => void } @@ -26,10 +38,7 @@ export type DialogOuterProps = { webOptions?: {} } -type DialogInnerPropsBase = React.PropsWithChildren<{ - style?: ViewStyle -}> & - T +type DialogInnerPropsBase = React.PropsWithChildren & T export type DialogInnerProps = | DialogInnerPropsBase<{ label?: undefined -- cgit 1.4.1 From f88b16525498584f81ea7f594a63623fc5dc7ce9 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Tue, 20 Feb 2024 18:20:59 -0600 Subject: Add optional close callback to Dialog (#2947) * Add optional close callback * No emitter --- src/components/Dialog/context.ts | 10 +++++++--- src/components/Dialog/index.tsx | 8 +++++++- src/components/Dialog/types.ts | 6 ++---- src/view/screens/Storybook/Dialogs.tsx | 6 +++++- 4 files changed, 21 insertions(+), 9 deletions(-) (limited to 'src/components/Dialog/types.ts') diff --git a/src/components/Dialog/context.ts b/src/components/Dialog/context.ts index b28b9f5a2..f0c7c983a 100644 --- a/src/components/Dialog/context.ts +++ b/src/components/Dialog/context.ts @@ -1,7 +1,11 @@ import React from 'react' import {useDialogStateContext} from '#/state/dialogs' -import {DialogContextProps, DialogControlProps} from '#/components/Dialog/types' +import { + DialogContextProps, + DialogControlProps, + DialogOuterProps, +} from '#/components/Dialog/types' export const Context = React.createContext({ close: () => {}, @@ -11,7 +15,7 @@ export function useDialogContext() { return React.useContext(Context) } -export function useDialogControl() { +export function useDialogControl(): DialogOuterProps['control'] { const id = React.useId() const control = React.useRef({ open: () => {}, @@ -30,6 +34,6 @@ export function useDialogControl() { return { ref: control, open: () => control.current.open(), - close: () => control.current.close(), + close: cb => control.current.close(cb), } } diff --git a/src/components/Dialog/index.tsx b/src/components/Dialog/index.tsx index 529535051..27f43afd3 100644 --- a/src/components/Dialog/index.tsx +++ b/src/components/Dialog/index.tsx @@ -35,6 +35,7 @@ export function Outer({ const sheetOptions = nativeOptions?.sheet || {} const hasSnapPoints = !!sheetOptions.snapPoints const insets = useSafeAreaInsets() + const closeCallback = React.useRef<() => void>() /* * Used to manage open/closed, but index is otherwise handled internally by `BottomSheet` @@ -54,7 +55,10 @@ export function Outer({ [setOpenIndex], ) - const close = React.useCallback(() => { + const close = React.useCallback(cb => { + if (cb) { + closeCallback.current = cb + } sheet.current?.close() }, []) @@ -70,6 +74,8 @@ export function Outer({ const onChange = React.useCallback( (index: number) => { if (index === -1) { + closeCallback.current?.() + closeCallback.current = undefined onClose?.() setOpenIndex(-1) } diff --git a/src/components/Dialog/types.ts b/src/components/Dialog/types.ts index 00178926a..75ba825ac 100644 --- a/src/components/Dialog/types.ts +++ b/src/components/Dialog/types.ts @@ -22,15 +22,13 @@ export type DialogControlOpenOptions = { export type DialogControlProps = { open: (options?: DialogControlOpenOptions) => void - close: () => void + close: (callback?: () => void) => void } export type DialogOuterProps = { control: { ref: React.RefObject - open: (index?: number) => void - close: () => void - } + } & DialogControlProps onClose?: () => void nativeOptions?: { sheet?: Omit diff --git a/src/view/screens/Storybook/Dialogs.tsx b/src/view/screens/Storybook/Dialogs.tsx index 821ac3c88..09be124db 100644 --- a/src/view/screens/Storybook/Dialogs.tsx +++ b/src/view/screens/Storybook/Dialogs.tsx @@ -110,7 +110,11 @@ export function Dialogs() { variant="outline" color="primary" size="small" - onPress={() => scrollable.close()} + onPress={() => + scrollable.close(() => { + console.log('CLOSED') + }) + } label="Open basic dialog"> Close dialog -- cgit 1.4.1