Add duration optional prop
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
export const DEFAULT_TOAST_DURATION = 3000
|
||||||
@@ -20,10 +20,10 @@ import {useSafeAreaInsets} from 'react-native-safe-area-context'
|
|||||||
|
|
||||||
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
|
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
|
||||||
import {atoms as a} from '#/alf'
|
import {atoms as a} from '#/alf'
|
||||||
|
import {DEFAULT_TOAST_DURATION} from '#/components/Toast/const'
|
||||||
import {Toast} from '#/components/Toast/Toast'
|
import {Toast} from '#/components/Toast/Toast'
|
||||||
import {type ToastApi, type ToastType} from '#/components/Toast/types'
|
import {type ToastApi, type ToastType} from '#/components/Toast/types'
|
||||||
|
|
||||||
const TIMEOUT = 2e3
|
|
||||||
const TOAST_ANIMATION_DURATION = 300
|
const TOAST_ANIMATION_DURATION = 300
|
||||||
|
|
||||||
export function ToastContainer() {
|
export function ToastContainer() {
|
||||||
@@ -44,6 +44,7 @@ export const toast: ToastApi = {
|
|||||||
type={props.type}
|
type={props.type}
|
||||||
content={props.content}
|
content={props.content}
|
||||||
a11yLabel={props.a11yLabel}
|
a11yLabel={props.a11yLabel}
|
||||||
|
duration={props.duration ?? DEFAULT_TOAST_DURATION}
|
||||||
destroy={() => item.destroy()}
|
destroy={() => item.destroy()}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
@@ -55,11 +56,13 @@ function AnimatedToast({
|
|||||||
type,
|
type,
|
||||||
content,
|
content,
|
||||||
a11yLabel,
|
a11yLabel,
|
||||||
|
duration,
|
||||||
destroy,
|
destroy,
|
||||||
}: {
|
}: {
|
||||||
type: ToastType
|
type: ToastType
|
||||||
content: React.ReactNode
|
content: React.ReactNode
|
||||||
a11yLabel: string
|
a11yLabel: string
|
||||||
|
duration: number
|
||||||
destroy: () => void
|
destroy: () => void
|
||||||
}) {
|
}) {
|
||||||
const {top} = useSafeAreaInsets()
|
const {top} = useSafeAreaInsets()
|
||||||
@@ -82,7 +85,7 @@ function AnimatedToast({
|
|||||||
const destroyTimeoutRef = useRef<ReturnType<typeof setTimeout>>()
|
const destroyTimeoutRef = useRef<ReturnType<typeof setTimeout>>()
|
||||||
const hideAndDestroyAfterTimeout = useNonReactiveCallback(() => {
|
const hideAndDestroyAfterTimeout = useNonReactiveCallback(() => {
|
||||||
clearTimeout(destroyTimeoutRef.current)
|
clearTimeout(destroyTimeoutRef.current)
|
||||||
destroyTimeoutRef.current = setTimeout(hideAndDestroyImmediately, TIMEOUT)
|
destroyTimeoutRef.current = setTimeout(hideAndDestroyImmediately, duration)
|
||||||
})
|
})
|
||||||
const pauseDestroy = useNonReactiveCallback(() => {
|
const pauseDestroy = useNonReactiveCallback(() => {
|
||||||
clearTimeout(destroyTimeoutRef.current)
|
clearTimeout(destroyTimeoutRef.current)
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ import {msg} from '@lingui/macro'
|
|||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
|
|
||||||
import {atoms as a, web} from '#/alf'
|
import {atoms as a, web} from '#/alf'
|
||||||
|
import {DEFAULT_TOAST_DURATION} from '#/components/Toast/const'
|
||||||
import {Toast} from '#/components/Toast/Toast'
|
import {Toast} from '#/components/Toast/Toast'
|
||||||
import {type ToastApi, type ToastType} from '#/components/Toast/types'
|
import {type ToastApi, type ToastType} from '#/components/Toast/types'
|
||||||
|
|
||||||
const DURATION = 3500
|
|
||||||
const TOAST_ANIMATION_STYLES = {
|
const TOAST_ANIMATION_STYLES = {
|
||||||
entering: {
|
entering: {
|
||||||
animation: 'toastFadeIn 0.3s ease-out forwards',
|
animation: 'toastFadeIn 0.3s ease-out forwards',
|
||||||
@@ -97,6 +97,6 @@ export const toast: ToastApi = {
|
|||||||
|
|
||||||
toastTimeout = setTimeout(() => {
|
toastTimeout = setTimeout(() => {
|
||||||
globalSetActiveToast?.(undefined)
|
globalSetActiveToast?.(undefined)
|
||||||
}, DURATION)
|
}, props.duration || DEFAULT_TOAST_DURATION)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,5 +5,6 @@ export type ToastApi = {
|
|||||||
type: ToastType
|
type: ToastType
|
||||||
content: React.ReactNode
|
content: React.ReactNode
|
||||||
a11yLabel: string
|
a11yLabel: string
|
||||||
|
duration?: number
|
||||||
}) => void
|
}) => void
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,18 @@ export function Toasts() {
|
|||||||
}>
|
}>
|
||||||
<Toast content="Default toast" type="default" />
|
<Toast content="Default toast" type="default" />
|
||||||
</Pressable>
|
</Pressable>
|
||||||
|
<Pressable
|
||||||
|
accessibilityRole="button"
|
||||||
|
onPress={() =>
|
||||||
|
toast.show({
|
||||||
|
type: 'default',
|
||||||
|
content: 'Default toast, 6 seconds',
|
||||||
|
a11yLabel: 'Default toast, 6 seconds',
|
||||||
|
duration: 6e3,
|
||||||
|
})
|
||||||
|
}>
|
||||||
|
<Toast content="Default toast, 6 seconds" type="default" />
|
||||||
|
</Pressable>
|
||||||
<Pressable
|
<Pressable
|
||||||
accessibilityRole="button"
|
accessibilityRole="button"
|
||||||
onPress={() =>
|
onPress={() =>
|
||||||
|
|||||||
Reference in New Issue
Block a user