Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654import { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState,} from 'react'import { Keyboard, type KeyboardEvent, type LayoutChangeEvent, type NativeScrollEvent, type NativeSyntheticEvent, Pressable, ScrollView, type StyleProp, TextInput, View, type ViewStyle,} from 'react-native'import {GestureHandlerRootView} from 'react-native-gesture-handler'import {useReanimatedKeyboardAnimation} from 'react-native-keyboard-controller'import Animated, { type ScrollEvent, useAnimatedStyle,} from 'react-native-reanimated'import { SafeAreaInsetsContext, useSafeAreaInsets,} from 'react-native-safe-area-context'import {scheduleOnRN} from 'react-native-worklets'import {BottomSheet, BottomSheetSnapPoint} from '@bsky.app/bottom-sheet'import { type BottomSheetPresentationSizeChangeEvent, type BottomSheetSnapPointChangeEvent, type BottomSheetStateChangeEvent,} from '@bsky.app/bottom-sheet/src/BottomSheet.types'import {type BottomSheetNativeComponent} from '@bsky.app/bottom-sheet/src/BottomSheetNativeComponent'import {msg} from '@lingui/core/macro'import {useLingui} from '@lingui/react'
import {ScrollProvider} from '#/lib/ScrollContext'import {logger} from '#/logger'import {useA11y} from '#/state/a11y'import {useDialogStateControlContext} from '#/state/dialogs'import {List, type ListMethods, type ListProps} from '#/view/com/util/List'import { android, atoms as a, BreakpointWidthContext, ios, platform, tokens, useTheme,} from '#/alf'import {useThemeName} from '#/alf/util/useColorModeTheme'import {Context, useDialogContext} from '#/components/Dialog/context'import { getMeasuredDialogContentHeight, shouldMeasureDialogContentHeight,} from '#/components/Dialog/sheetSizing'import {getDialogSourceViewTag} from '#/components/Dialog/sourceViewTag'import { type DialogControlProps, type DialogInnerProps, type DialogOuterProps,} from '#/components/Dialog/types'import {createInput} from '#/components/forms/TextField'import {useOnKeyboard} from '#/components/hooks/useOnKeyboard'import {IS_ANDROID, IS_IOS, IS_IPAD, IS_LIQUID_GLASS} from '#/env'
export {useDialogContext, useDialogControl} from '#/components/Dialog/context'export * from '#/components/Dialog/shared'export * from '#/components/Dialog/types'export * from '#/components/Dialog/utils'
export const Input = createInput(TextInput)
export function Outer({ children, control, onOpen, onClose, preventDismiss = false, nativeOptions, testID,}: React.PropsWithChildren<DialogOuterProps>) { const themeName = useThemeName() const t = useTheme(themeName) const ref = useRef<BottomSheetNativeComponent>(null) const closeCallbacks = useRef<(() => void)[]>([]) const {setDialogIsOpen, setFullyExpandedCount} = useDialogStateControlContext() const safeAreaInsets = useSafeAreaInsets()
const prevSnapPoint = useRef<BottomSheetSnapPoint>( BottomSheetSnapPoint.Hidden, )
const [disableDrag, setDisableDrag] = useState(false) const [presentation, setPresentation] = useState<BottomSheetPresentationSizeChangeEvent['nativeEvent']>() const [sourceViewTag, setSourceViewTag] = useState<number>() const [snapPoint, setSnapPoint] = useState<BottomSheetSnapPoint>( BottomSheetSnapPoint.Partial, ) const [desiredContentHeight, setDesiredContentHeight] = useState<number>() const shouldMeasureContentHeight = shouldMeasureDialogContentHeight({ isIOS: IS_IOS, isIPad: IS_IPAD, popover: nativeOptions?.popover, })
/* * Only user dismissal paths are blocked natively; the imperative `close` * below still dismisses the sheet. */ const shouldPreventDismiss = preventDismiss || !!nativeOptions?.preventDismiss
const callQueuedCallbacks = useCallback(() => { for (const cb of closeCallbacks.current) { try { cb() } catch (e: any) { logger.error(e || 'Error running close callback') } }
closeCallbacks.current = [] }, [])
const open = useCallback<DialogControlProps['open']>( options => { // Run any leftover callbacks that might have been queued up before calling `.open()` callQueuedCallbacks() setSourceViewTag( getDialogSourceViewTag({ popover: nativeOptions?.popover, nativeSourceViewTag: nativeOptions?.sourceViewTag, options, }), ) setDesiredContentHeight(undefined) /* * A full-height sheet only has the large detent, so it always opens at * Full. Set that before the content mounts rather than waiting for * onSnapPointChange: an autofocused input raises the keyboard mid-present, * and ScrollView ignores that keyboard notification if * automaticallyAdjustKeyboardInsets is still false, leaving the content * unscrollable until the keyboard reopens. */ setSnapPoint( nativeOptions?.fullHeight ? BottomSheetSnapPoint.Full : BottomSheetSnapPoint.Partial, ) onOpen?.() setDialogIsOpen(control.id, true) ref.current?.present() }, [ setDialogIsOpen, control.id, callQueuedCallbacks, onOpen, nativeOptions?.popover, nativeOptions?.sourceViewTag, nativeOptions?.fullHeight, setDesiredContentHeight, ], )
// This is the function that we call when we want to dismiss the dialog. const close = useCallback<DialogControlProps['close']>(cb => { if (typeof cb === 'function') { closeCallbacks.current.push(cb) } ref.current?.dismiss() }, [])
// This is the actual thing we are doing once we "confirm" the dialog. We want the dialog's close animation to // happen before we run this. It is passed to the `BottomSheet` component. const onCloseAnimationComplete = useCallback(() => { // This removes the dialog from our list of stored dialogs. Not super necessary on iOS, but on Android this // tells us that we need to toggle the accessibility overlay setting setDialogIsOpen(control.id, false) setDesiredContentHeight(undefined) callQueuedCallbacks() onClose?.() }, [ callQueuedCallbacks, control.id, onClose, setDialogIsOpen, setDesiredContentHeight, ])
const onSnapPointChange = (e: BottomSheetSnapPointChangeEvent) => { const {snapPoint} = e.nativeEvent setSnapPoint(snapPoint)
if ( snapPoint === BottomSheetSnapPoint.Full && prevSnapPoint.current !== BottomSheetSnapPoint.Full ) { setFullyExpandedCount(c => c + 1) } else if ( snapPoint !== BottomSheetSnapPoint.Full && prevSnapPoint.current === BottomSheetSnapPoint.Full ) { setFullyExpandedCount(c => c - 1) } prevSnapPoint.current = snapPoint }
const onPresentationSizeChange = useCallback( (e: BottomSheetPresentationSizeChangeEvent) => { setPresentation(e.nativeEvent) }, [setPresentation], )
const onStateChange = (e: BottomSheetStateChangeEvent) => { if (e.nativeEvent.state === 'closed') { onCloseAnimationComplete() setPresentation(undefined)
if (prevSnapPoint.current === BottomSheetSnapPoint.Full) { setFullyExpandedCount(c => c - 1) } prevSnapPoint.current = BottomSheetSnapPoint.Hidden } }
useImperativeHandle( control.ref, () => ({ open, close, }), [open, close], )
const isHeightConstrained = nativeOptions?.maxHeight != null || nativeOptions?.fullHeight === true || presentation?.isPopover === true || (shouldMeasureContentHeight && presentation != null && desiredContentHeight != null) const presentationMaxHeight = presentation?.height != null && (presentation?.isPopover === true || nativeOptions?.fullHeight === true || nativeOptions?.maxHeight != null || (shouldMeasureContentHeight && desiredContentHeight != null)) ? Math.min( nativeOptions?.maxHeight ?? presentation.height, presentation.height, ) : undefined
const context = useMemo( () => ({ close, isNativeDialog: true, availableWidth: presentation?.width, presentationBottomOffset: presentation?.bottomOffset ?? 0, isNativePopover: IS_IOS && presentation?.isPopover === true, nativeSnapPoint: snapPoint, disableDrag, preventDismiss: shouldPreventDismiss, setDisableDrag, isWithinDialog: true, isHeightConstrained, shouldMeasureContentHeight, setDesiredContentHeight, }), [ close, presentation, snapPoint, disableDrag, shouldPreventDismiss, setDisableDrag, isHeightConstrained, shouldMeasureContentHeight, setDesiredContentHeight, presentation?.isPopover, nativeOptions?.popover, ], )
return ( <BottomSheet ref={ref} // device-bezel radius when undefined cornerRadius={IS_LIQUID_GLASS ? undefined : 20} backgroundColor={t.atoms.bg.backgroundColor} colorScheme={t.scheme} {...nativeOptions} preventDismiss={shouldPreventDismiss} sourceViewTag={sourceViewTag ?? nativeOptions?.sourceViewTag} onSnapPointChange={onSnapPointChange} onStateChange={onStateChange} onPresentationSizeChange={onPresentationSizeChange} desiredContentHeight={ shouldMeasureContentHeight ? desiredContentHeight : undefined } disableDrag={disableDrag}> <SafeAreaInsetsContext.Provider value={presentation?.safeAreaInsets ?? safeAreaInsets}> <Context.Provider value={context}> <BreakpointWidthContext.Provider value={presentation?.width}> <SheetGestureRoot style={isHeightConstrained && a.flex_1}> <View testID={testID} style={[ a.relative, isHeightConstrained && a.flex_1, presentationMaxHeight != null && { maxHeight: presentationMaxHeight, }, ]}> {children} </View> </SheetGestureRoot> </BreakpointWidthContext.Provider> </Context.Provider> </SafeAreaInsetsContext.Provider> </BottomSheet> )}
/** * On Android the sheet is its own window with its own React root, outside the * app's `GestureHandlerRootView`, so gesture-handler gestures inside it (like * dragging to reorder) never activate without a root of their own. */function SheetGestureRoot({ style, children,}: { style?: StyleProp<ViewStyle> children: React.ReactNode}) { if (!IS_ANDROID) return children return ( <GestureHandlerRootView style={style}>{children}</GestureHandlerRootView> )}
/** * @deprecated use `Dialog.ScrollableInner` instead */export function Inner(props: DialogInnerProps) { return <ScrollableInner {...props} />}
export function ScrollableInner({ ref, children, contentContainerStyle, header, footer, style, ...props}: DialogInnerProps & { ref?: React.Ref<React.ComponentRef<typeof ScrollView>>}) { const { nativeSnapPoint, disableDrag, setDisableDrag, isHeightConstrained, shouldMeasureContentHeight, isNativePopover, setDesiredContentHeight, } = useDialogContext() const isAtMaxSnapPoint = nativeSnapPoint === BottomSheetSnapPoint.Full || isNativePopover const insets = useSafeAreaInsets() const [keyboardHeight, setKeyboardHeight] = useState(() => IS_ANDROID ? (Keyboard.metrics()?.height ?? 0) : 0, ) const contentHeight = useRef(0) const footerHeight = useRef(0)
const updateDesiredContentHeight = () => { setDesiredContentHeight( getMeasuredDialogContentHeight({ shouldMeasureContentHeight, contentHeight: contentHeight.current, footerHeight: footerHeight.current, }), ) }
useEffect( () => () => { if (shouldMeasureContentHeight) { setDesiredContentHeight(undefined) } }, [setDesiredContentHeight, shouldMeasureContentHeight], )
const keyboardEventHandler = useCallback((e: KeyboardEvent) => { setKeyboardHeight(e.endCoordinates.height) }, []) useOnKeyboard('keyboardDidShow', keyboardEventHandler) useOnKeyboard('keyboardDidHide', keyboardEventHandler)
const onScroll = (e: NativeSyntheticEvent<NativeScrollEvent>) => { if (!IS_ANDROID) { return } const {contentOffset} = e.nativeEvent if (contentOffset.y > 0 && !disableDrag) { setDisableDrag(true) } else if (contentOffset.y <= 1 && disableDrag) { setDisableDrag(false) } }
return ( <> <ScrollView style={[isHeightConstrained && a.flex_1, style]} contentContainerStyle={[ a.pt_2xl, IS_LIQUID_GLASS ? a.px_2xl : a.px_xl, platform({ ios: a.pb_2xl, android: { paddingBottom: keyboardHeight + insets.bottom + tokens.space.xl, }, }), contentContainerStyle, ]} ref={ref} showsVerticalScrollIndicator={IS_ANDROID ? false : undefined} contentInsetAdjustmentBehavior={ isAtMaxSnapPoint ? 'automatic' : 'never' } automaticallyAdjustKeyboardInsets={isAtMaxSnapPoint} {...props} bounces={isAtMaxSnapPoint} scrollEventThrottle={50} // set drag state based on scroll on android. // we want to detect if it's at the top or not, so watch // scrollEndDrag and momentumScrollEnd as well onScroll={android(onScroll)} onScrollEndDrag={android(onScroll)} onMomentumScrollEnd={android(onScroll)} onContentSizeChange={(_, height) => { contentHeight.current = height updateDesiredContentHeight() }} keyboardShouldPersistTaps="handled" // TODO: figure out why this positions the header absolutely (rather than stickily) // on Android. fine to disable for now, because we don't have any // dialogs that use this that actually scroll -sfn stickyHeaderIndices={ios(header ? [0] : undefined)}> {header} {children} </ScrollView> {footer != null && ( <View onLayout={e => { footerHeight.current = e.nativeEvent.layout.height updateDesiredContentHeight() }}> {footer} </View> )} </> )}
export const InnerFlatList = forwardRef< ListMethods, ListProps<any> & { webInnerStyle?: StyleProp<ViewStyle> webInnerContentContainerStyle?: StyleProp<ViewStyle> footer?: React.ReactNode }>(function InnerFlatList( {headerOffset, footer, style, contentContainerStyle, ...props}, ref,) { const insets = useSafeAreaInsets() const {nativeSnapPoint, disableDrag, setDisableDrag, isNativePopover} = useDialogContext()
const isAtMaxSnapPoint = nativeSnapPoint === BottomSheetSnapPoint.Full || isNativePopover
const onScroll = (e: ScrollEvent) => { 'worklet' if (!IS_ANDROID) { return } const {contentOffset} = e if (contentOffset.y > 0 && !disableDrag) { scheduleOnRN(setDisableDrag, true) } else if (contentOffset.y <= 1 && disableDrag) { scheduleOnRN(setDisableDrag, false) } }
return ( <ScrollProvider onScroll={onScroll} onEndDrag={onScroll} onMomentumEnd={onScroll}> <List keyboardShouldPersistTaps="handled" contentInsetAdjustmentBehavior={ isAtMaxSnapPoint ? 'automatic' : 'never' } automaticallyAdjustKeyboardInsets={isAtMaxSnapPoint} scrollIndicatorInsets={{top: headerOffset}} bounces={isAtMaxSnapPoint} ref={ref} showsVerticalScrollIndicator={IS_ANDROID ? false : undefined} {...props} style={[a.h_full, style]} contentContainerStyle={[ {paddingTop: headerOffset}, android({ paddingBottom: insets.top + insets.bottom + tokens.space.xl, }), contentContainerStyle, ]} /> {footer} </ScrollProvider> )})
export function FlatListFooter({ children, onLayout, border = true,}: { children: React.ReactNode onLayout?: (event: LayoutChangeEvent) => void border?: boolean}) { const t = useTheme() const {bottom} = useSafeAreaInsets() const {presentationBottomOffset} = useDialogContext() const {height} = useReanimatedKeyboardAnimation()
const animatedStyle = useAnimatedStyle(() => { if (!IS_IOS) return {} return { transform: [ { translateY: Math.min( 0, height.get() + bottom + presentationBottomOffset - 10, ), }, ], } })
return ( <Animated.View onLayout={onLayout} style={[ a.absolute, a.bottom_0, a.w_full, a.z_10, border && a.border_t, t.atoms.bg, t.atoms.border_contrast_low, a.px_lg, a.pt_md, {paddingBottom: bottom + tokens.space.md}, // TODO: had to admit defeat here, but we should // try and get this to work for Android as well -sfn ios(animatedStyle), ]}> {children} </Animated.View> )}
export function Handle({ difference = false, fill, testID,}: { difference?: boolean fill?: string testID?: string}) { const t = useTheme() const {_} = useLingui() const {screenReaderEnabled} = useA11y() const {close, isNativePopover} = useDialogContext()
return ( <View style={[a.absolute, a.w_full, a.align_center, a.z_10, {height: 20}]}> <Pressable testID={testID} accessible={screenReaderEnabled} onPress={() => close()} accessibilityLabel={_(msg`Dismiss`)} accessibilityHint={_(msg`Double tap to close the dialog`)}> {!isNativePopover && ( <View style={[ a.rounded_sm, { top: tokens.space._2xl / 2 - 2.5, width: 35, height: 5, alignSelf: 'center', }, difference ? { // TODO: mixBlendMode is only available on the new architecture -sfn // backgroundColor: t.palette.white, // mixBlendMode: 'difference', backgroundColor: t.palette.white, opacity: 0.75, } : { backgroundColor: fill || t.palette.contrast_975, opacity: 0.5, }, ]} /> )} </Pressable> </View> )}
export function Close() { return null}
export function Backdrop() { return null}