From 09874504afa9c17c4d9a1fcd62545dfb971c6892 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Thu, 11 Jan 2024 14:56:10 -0600 Subject: [PATCH] Clean up dialog types --- src/view/com/Dialog/context.ts | 19 ++++++++++++++++ src/view/com/Dialog/index.tsx | 36 +++++++++++-------------------- src/view/com/Dialog/index.web.tsx | 35 +++++++----------------------- src/view/com/Dialog/types.ts | 13 ++++++++--- src/view/screens/DebugNew.tsx | 2 +- 5 files changed, 51 insertions(+), 54 deletions(-) create mode 100644 src/view/com/Dialog/context.ts diff --git a/src/view/com/Dialog/context.ts b/src/view/com/Dialog/context.ts new file mode 100644 index 0000000000..bd150f8cbc --- /dev/null +++ b/src/view/com/Dialog/context.ts @@ -0,0 +1,19 @@ +import React from 'react' +import {DialogContextProps, DialogControlProps} from '#/view/com/Dialog/types' + +export const Context = React.createContext({ + close: () => {}, +}) + +export function useDialogContext() { + return React.useContext(Context) +} + +export function useDialogControl() { + const control = React.useRef({ + open: () => {}, + close: () => {}, + }) + + return control +} diff --git a/src/view/com/Dialog/index.tsx b/src/view/com/Dialog/index.tsx index 229cd7b867..6d6946ec44 100644 --- a/src/view/com/Dialog/index.tsx +++ b/src/view/com/Dialog/index.tsx @@ -5,36 +5,24 @@ import BottomSheet, {BottomSheetBackdrop} from '@gorhom/bottom-sheet' import {useTheme, atoms as a} from '#/alf' import {Portal} from '#/view/com/Portal' -import {DialogProps, DialogControl} from '#/view/com/Dialog/types' +import { + DialogOuterProps, + DialogControlProps, + DialogInnerProps, +} from '#/view/com/Dialog/types' +import {Context} from '#/view/com/Dialog/context' -const Context = React.createContext<{ - close: () => void -}>({ - close: () => {}, -}) - -export function useDialogControl() { - const control = React.useRef({ - open: () => {}, - close: () => {}, - }) - - return control -} - -export function useDialog() { - return React.useContext(Context) -} +export {useDialogControl} from '#/view/com/Dialog/context' export function Outer({ control, onClose, children, -}: React.PropsWithChildren) { +}: React.PropsWithChildren) { const t = useTheme() const sheet = React.useRef(null) - const open = React.useCallback((i = 0) => { + const open = React.useCallback((i = 0) => { sheet.current?.snapToIndex(i) }, []) @@ -52,6 +40,8 @@ export function Outer({ [open, close], ) + const context = React.useMemo(() => ({close}), [close]) + return ( {}} onClose={onClose}> - {children} + {children} ) } -export function Inner(props: React.PropsWithChildren<{}>) { +export function Inner(props: DialogInnerProps) { const t = useTheme() return ( e.stopPropagation() -const Context = React.createContext<{ - close: () => void -}>({ - close: () => {}, -}) - -export function useDialogControl() { - const control = React.useRef({ - open: () => {}, - close: () => {}, - }) - - return control -} - -export function useDialog() { - return React.useContext(Context) -} - export function Outer({ control, onClose, children, -}: React.PropsWithChildren) { +}: React.PropsWithChildren) { const t = useTheme() const {gtMobile} = useBreakpoints() const [isOpen, setIsOpen] = React.useState(false) @@ -127,10 +111,7 @@ export function Outer({ ) } -export function Inner({ - children, - style, -}: React.PropsWithChildren<{style?: ViewStyle}>) { +export function Inner({children, style}: DialogInnerProps) { const t = useTheme() const {gtMobile} = useBreakpoints() return ( @@ -164,7 +145,7 @@ export function Handle() { export function Close() { const t = useTheme() - const {close} = useDialog() + const {close} = useDialogContext() return ( void +} + +export type DialogControlProps = { open: (index?: number) => void close: () => void } -export type DialogProps = { - control: React.RefObject +export type DialogOuterProps = { + control: React.RefObject onClose?: () => void } + +export type DialogInnerProps = React.PropsWithChildren<{style?: ViewStyle}> diff --git a/src/view/screens/DebugNew.tsx b/src/view/screens/DebugNew.tsx index b6bd1f2c89..6abcc438b7 100644 --- a/src/view/screens/DebugNew.tsx +++ b/src/view/screens/DebugNew.tsx @@ -332,7 +332,7 @@ function Dialogs() { - +