Skip to content

Commit

Permalink
Add optional close callback to Dialog (#2947)
Browse files Browse the repository at this point in the history
* Add optional close callback

* No emitter
  • Loading branch information
estrattonbailey authored Feb 21, 2024
1 parent 6413b8b commit f88b165
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/components/Dialog/context.ts
Original file line number Diff line number Diff line change
@@ -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<DialogContextProps>({
close: () => {},
Expand All @@ -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<DialogControlProps>({
open: () => {},
Expand All @@ -30,6 +34,6 @@ export function useDialogControl() {
return {
ref: control,
open: () => control.current.open(),
close: () => control.current.close(),
close: cb => control.current.close(cb),
}
}
8 changes: 7 additions & 1 deletion src/components/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -54,7 +55,10 @@ export function Outer({
[setOpenIndex],
)

const close = React.useCallback(() => {
const close = React.useCallback<DialogControlProps['close']>(cb => {
if (cb) {
closeCallback.current = cb
}
sheet.current?.close()
}, [])

Expand All @@ -70,6 +74,8 @@ export function Outer({
const onChange = React.useCallback(
(index: number) => {
if (index === -1) {
closeCallback.current?.()
closeCallback.current = undefined
onClose?.()
setOpenIndex(-1)
}
Expand Down
6 changes: 2 additions & 4 deletions src/components/Dialog/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DialogControlProps>
open: (index?: number) => void
close: () => void
}
} & DialogControlProps
onClose?: () => void
nativeOptions?: {
sheet?: Omit<BottomSheetProps, 'children'>
Expand Down
6 changes: 5 additions & 1 deletion src/view/screens/Storybook/Dialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
</Button>
Expand Down

0 comments on commit f88b165

Please sign in to comment.