Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/components/AttachmentFields/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
MultiSlotInput,
useTheme
} from '@tetherto/pearpass-lib-ui-kit'
import { Keyboard, View } from 'react-native'
import { View } from 'react-native'
import {
Add,
TrashOutlined,
Expand All @@ -16,8 +16,7 @@ import {

import { BottomSheetAttachmentActionsContent } from '../../containers/BottomSheetAttachmentActionsContent'
import { BottomSheetUploadFileContent } from '../../containers/BottomSheetUploadFileContent'

const KEYBOARD_DISMISS_DELAY_MS = 250
import { openAfterKeyboardDismiss } from '../../utils/openAfterKeyboardDismiss'

type AttachmentLike = {
id?: string | number
Expand Down Expand Up @@ -113,8 +112,7 @@ export const AttachmentFields = <T extends AttachmentLike>({
}, [])

const openUploadSheet = useCallback((item: AttachmentFieldItem<T>) => {
Keyboard.dismiss()
setTimeout(() => setActiveUploadTarget(item), KEYBOARD_DISMISS_DELAY_MS)
openAfterKeyboardDismiss(() => setActiveUploadTarget(item))
}, [])

const handleUploadSelect = useCallback(
Expand Down
8 changes: 2 additions & 6 deletions src/components/FolderSelectField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ import {
Close,
KeyboardArrowBottom
} from '@tetherto/pearpass-lib-ui-kit/icons'
import { Keyboard } from 'react-native'

import { openAfterKeyboardDismiss } from '../../utils/openAfterKeyboardDismiss'
import { BottomSheetFolderSelectorContent } from '../../containers/BottomSheetFolderSelectorContent'

const KEYBOARD_DISMISS_DELAY_MS = 250

type Props = {
value?: string
onChange: (folderName: string) => void
Expand All @@ -37,8 +34,7 @@ export const FolderSelectField = ({

const handleOpenChange = (next: boolean) => {
if (next) {
Keyboard.dismiss()
setTimeout(() => setOpen(true), KEYBOARD_DISMISS_DELAY_MS)
openAfterKeyboardDismiss(() => setOpen(true))
return
}
setOpen(false)
Expand Down
23 changes: 23 additions & 0 deletions src/utils/openAfterKeyboardDismiss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Keyboard } from 'react-native'

const FALLBACK_DELAY_MS = 350

/**
* Dismisses the keyboard and invokes callback only after the keyboard is fully
* hidden. Falls back to a timeout when the keyboard is not currently visible.
*/
export const openAfterKeyboardDismiss = (callback: () => void): void => {
let called = false

const invoke = () => {
if (called) return
called = true
subscription.remove()
clearTimeout(fallback)
callback()
}

const subscription = Keyboard.addListener('keyboardDidHide', invoke)
Keyboard.dismiss()
const fallback = setTimeout(invoke, FALLBACK_DELAY_MS)
}
Loading