Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344import { forwardRef, useCallback, useContext, useImperativeHandle, useMemo, useState,} from 'react'import { FlatList, type FlatListProps, type GestureResponderEvent, type LayoutChangeEvent, Pressable, type StyleProp, View, type ViewStyle,} from 'react-native'import {msg} from '@lingui/core/macro'import {useLingui} from '@lingui/react'import {DismissableLayer, FocusGuards, FocusScope} from 'radix-ui/internal'import {RemoveScrollBar} from 'react-remove-scroll-bar'
import {logger} from '#/logger'import {useA11y} from '#/state/a11y'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 { type DialogControlProps, type DialogInnerProps, type DialogOuterProps,} from '#/components/Dialog/types'import {TimesLarge_Stroke2_Corner0_Rounded as X} from '#/components/icons/Times'import {Portal} from '#/components/Portal'
export {useDialogContext, useDialogControl} from '#/components/Dialog/context'export * from '#/components/Dialog/shared'export * from '#/components/Dialog/types'export * from '#/components/Dialog/utils'export {Input} from '#/components/forms/TextField'
// 100 minus 10vh of paddingVerticalexport const WEB_DIALOG_HEIGHT = '80vh'
const stopPropagation = (e: any) => e.stopPropagation()const preventDefault = (e: any) => e.preventDefault()
export function Outer({ children, control, onClose, webOptions,}: React.PropsWithChildren<DialogOuterProps>) { const {_} = useLingui() const {gtMobile} = useBreakpoints() const [isOpen, setIsOpen] = useState(false) const {setDialogIsOpen} = useDialogStateControlContext()
const open = useCallback(() => { setDialogIsOpen(control.id, true) setIsOpen(true) }, [setIsOpen, setDialogIsOpen, control.id])
const close = useCallback<DialogControlProps['close']>( cb => { setDialogIsOpen(control.id, false) setIsOpen(false)
try { if (cb && typeof cb === 'function') { // This timeout ensures that the callback runs at the same time as it would on native. I.e. // console.log('Step 1') -> close(() => console.log('Step 3')) -> console.log('Step 2') // This should always output 'Step 1', 'Step 2', 'Step 3', but without the timeout it would output // 'Step 1', 'Step 3', 'Step 2'. setTimeout(cb) } } catch (e: any) { logger.error(`Dialog closeCallback failed`, { message: e.message, }) }
onClose?.() }, [control.id, onClose, setDialogIsOpen], )
const handleBackgroundPress = useCallback( async (e: GestureResponderEvent) => { webOptions?.onBackgroundPress ? webOptions.onBackgroundPress(e) : close() }, [webOptions, close], )
useImperativeHandle( control.ref, () => ({ open, close, }), [close, open], )
const context = useMemo( () => ({ close, isNativeDialog: false, nativeSnapPoint: 0, disableDrag: false, setDisableDrag: () => {}, isWithinDialog: true, isHeightConstrained: false, }), [close], )
return ( <> {isOpen && ( <Portal> <Context.Provider value={context}> <RemoveScrollBar /> <Pressable accessibilityHint={undefined} accessibilityLabel={_(msg`Close active dialog`)} onPress={handleBackgroundPress}> <View style={[ web(a.fixed), a.inset_0, a.z_10, a.px_xl, webOptions?.alignCenter ? a.justify_center : undefined, a.align_center, { overflowY: 'auto', paddingVertical: gtMobile ? '10vh' : a.pt_xl.paddingTop, }, ]}> <Backdrop /> {/** * This is needed to prevent centered dialogs from overflowing * above the screen, and provides a "natural" centering so that * stacked dialogs appear relatively aligned. */} <View style={[ a.w_full, a.z_20, a.align_center, web({minHeight: '60vh', position: 'static'}), ]}> {children} </View> </View> </Pressable> </Context.Provider> </Portal> )} </> )}
export function Inner({ children, style, label, accessibilityLabelledBy, accessibilityDescribedBy, header, footer, contentContainerStyle,}: DialogInnerProps) { const t = useTheme() const {close} = useContext(Context) const {gtMobile} = useBreakpoints() const {reduceMotionEnabled} = useA11y() FocusGuards.useFocusGuards() return ( <FocusScope.FocusScope loop asChild trapped> <View role="dialog" aria-role="dialog" aria-label={label} aria-labelledby={accessibilityLabelledBy} aria-describedby={accessibilityDescribedBy} // @ts-expect-error web only -prf onClick={stopPropagation} onStartShouldSetResponder={_ => true} onTouchEnd={stopPropagation} // note: flatten is required for some reason -sfn style={flatten([ a.relative, a.rounded_md, a.w_full, a.border, t.atoms.bg, { cursor: 'default', // The overlay applies `cursor: 'pointer'` to all children. maxWidth: 600, borderColor: t.palette.contrast_200, shadowColor: t.palette.black, shadowOpacity: t.name === 'light' ? 0.1 : 0.4, shadowRadius: 30, }, !reduceMotionEnabled && a.zoom_fade_in, style, ])}> <DismissableLayer.DismissableLayer onInteractOutside={preventDefault} onFocusOutside={preventDefault} onDismiss={close} style={{height: '100%', display: 'flex', flexDirection: 'column'}}> {header} <View style={[gtMobile ? a.p_2xl : a.p_xl, contentContainerStyle]}> {children} </View> {footer} </DismissableLayer.DismissableLayer> </View> </FocusScope.FocusScope> )}
export const ScrollableInner = Inner
export const InnerFlatList = forwardRef< FlatList, FlatListProps<any> & {label: string} & { webInnerStyle?: StyleProp<ViewStyle> webInnerContentContainerStyle?: StyleProp<ViewStyle> footer?: React.ReactNode }>(function InnerFlatList( { label, style, webInnerStyle, webInnerContentContainerStyle, footer, ...props }, ref,) { const {gtMobile} = useBreakpoints() return ( <Inner label={label} style={[ a.overflow_hidden, a.px_0, web({maxHeight: WEB_DIALOG_HEIGHT}), webInnerStyle, ]} contentContainerStyle={[a.h_full, a.px_0, webInnerContentContainerStyle]}> <FlatList ref={ref} style={[a.h_full, gtMobile ? a.px_2xl : a.px_xl, style]} {...props} /> {footer} </Inner> )})
export function FlatListFooter({ children, onLayout, border = true,}: { children: React.ReactNode onLayout?: (event: LayoutChangeEvent) => void border?: boolean}) { const t = useTheme()
return ( <View onLayout={onLayout} style={[ a.absolute, a.bottom_0, a.w_full, a.z_10, t.atoms.bg, border && a.border_t, t.atoms.border_contrast_low, a.px_lg, a.py_md, ]}> {children} </View> )}
export function Close() { const {_} = useLingui() const {close} = useContext(Context) return ( <View style={[ a.absolute, a.z_10, { top: a.pt_md.paddingTop, right: a.pr_md.paddingRight, }, ]}> <Button size="small" variant="ghost" color="secondary" shape="round" onPress={() => close()} label={_(msg`Close active dialog`)}> <ButtonIcon icon={X} size="md" /> </Button> </View> )}
export function Handle(_props?: {testID?: string}) { return null}
export function Backdrop() { const t = useTheme() const {reduceMotionEnabled} = useA11y() return ( <View style={{opacity: 0.8}}> <View style={[ a.fixed, a.inset_0, {backgroundColor: t.palette.black}, !reduceMotionEnabled && a.fade_in, ]} /> </View> )}