Skip to content

Commit

Permalink
fix dialog back button for android (#3428)
Browse files Browse the repository at this point in the history
* fix types

* remove unused async

* add try/catch
  • Loading branch information
haileyok authored Apr 5, 2024
1 parent c80dcc5 commit cd29dba
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
51 changes: 36 additions & 15 deletions src/components/Dialog/index.web.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import React, {useImperativeHandle} from 'react'
import {View, TouchableWithoutFeedback} from 'react-native'
import {FocusScope} from '@tamagui/focus-scope'
import Animated, {FadeInDown, FadeIn} from 'react-native-reanimated'
import {TouchableWithoutFeedback, View} from 'react-native'
import Animated, {FadeIn, FadeInDown} from 'react-native-reanimated'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {FocusScope} from '@tamagui/focus-scope'

import {useTheme, atoms as a, useBreakpoints, web, flatten} from '#/alf'
import {Portal} from '#/components/Portal'

import {DialogOuterProps, DialogInnerProps} from '#/components/Dialog/types'
import {Context} from '#/components/Dialog/context'
import {logger} from '#/logger'
import {useDialogStateControlContext} from '#/state/dialogs'
import {atoms as a, flatten, useBreakpoints, useTheme, web} from '#/alf'
import {Button, ButtonIcon} from '#/components/Button'
import {Context} from '#/components/Dialog/context'
import {
DialogControlProps,
DialogInnerProps,
DialogOuterProps,
} from '#/components/Dialog/types'
import {TimesLarge_Stroke2_Corner0_Rounded as X} from '#/components/icons/Times'
import {useDialogStateControlContext} from '#/state/dialogs'
import {Portal} from '#/components/Portal'

export {useDialogControl, useDialogContext} from '#/components/Dialog/context'
export {useDialogContext, useDialogControl} from '#/components/Dialog/context'
export * from '#/components/Dialog/types'
export {Input} from '#/components/forms/TextField'

Expand All @@ -37,22 +41,39 @@ export function Outer({
setDialogIsOpen(control.id, true)
}, [setIsOpen, setDialogIsOpen, control.id])

const close = React.useCallback(async () => {
const onCloseInner = React.useCallback(async () => {
setIsVisible(false)
await new Promise(resolve => setTimeout(resolve, 150))
setIsOpen(false)
setIsVisible(true)
setDialogIsOpen(control.id, false)
onClose?.()
}, [onClose, setIsOpen, setDialogIsOpen, control.id])
}, [control.id, onClose, setDialogIsOpen])

const close = React.useCallback<DialogControlProps['close']>(
cb => {
try {
if (cb && typeof cb === 'function') {
cb()
}
} catch (e: any) {
logger.error(`Dialog closeCallback failed`, {
message: e.message,
})
} finally {
onCloseInner()
}
},
[onCloseInner],
)

useImperativeHandle(
control.ref,
() => ({
open,
close,
}),
[open, close],
[close, open],
)

React.useEffect(() => {
Expand All @@ -65,7 +86,7 @@ export function Outer({
document.addEventListener('keydown', handler)

return () => document.removeEventListener('keydown', handler)
}, [isOpen, close])
}, [close, isOpen])

const context = React.useMemo(
() => ({
Expand All @@ -82,7 +103,7 @@ export function Outer({
<TouchableWithoutFeedback
accessibilityHint={undefined}
accessibilityLabel={_(msg`Close active dialog`)}
onPress={close}>
onPress={onCloseInner}>
<View
style={[
web(a.fixed),
Expand Down
3 changes: 1 addition & 2 deletions src/components/Prompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ export function Action({
const {gtMobile} = useBreakpoints()
const {close} = Dialog.useDialogContext()
const handleOnPress = React.useCallback(() => {
close()
onPress()
close(onPress)
}, [close, onPress])

return (
Expand Down
6 changes: 1 addition & 5 deletions src/view/com/composer/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,7 @@ export const ComposePost = observer(function ComposePost({
title={_(msg`Discard draft?`)}
description={_(msg`Are you sure you'd like to discard this draft?`)}
onConfirm={() => {
if (isWeb) {
onClose()
} else {
discardPromptControl.close(onClose)
}
discardPromptControl.close(onClose)
}}
confirmButtonCta={_(msg`Discard`)}
confirmButtonColor="negative"
Expand Down

0 comments on commit cd29dba

Please sign in to comment.