Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376import {cloneElement, Fragment, isValidElement, useMemo} from 'react'import { Pressable, type StyleProp, type TextStyle, View, type ViewStyle,} from 'react-native'import {msg} from '@lingui/core/macro'import {useLingui} from '@lingui/react'import {Trans} from '@lingui/react/macro'import flattenReactChildren from 'react-keyed-flatten-children'
import {atoms as a, useTheme} from '#/alf'import {Button, ButtonText} from '#/components/Button'import * as Dialog from '#/components/Dialog'import {useInteractionState} from '#/components/hooks/useInteractionState'import { Context, ItemContext, useMenuContext, useMenuItemContext,} from '#/components/Menu/context'import { type ContextType, type GroupProps, type ItemIconProps, type ItemProps, type ItemTextProps, type TriggerProps,} from '#/components/Menu/types'import {Text} from '#/components/Typography'import {IS_ANDROID, IS_IOS, IS_NATIVE} from '#/env'
export { type DialogControlProps as MenuControlProps, useDialogControl as useMenuControl,} from '#/components/Dialog'
export {useMenuContext}
export function Root({ children, control,}: React.PropsWithChildren<{ control?: Dialog.DialogControlProps}>) { const defaultControl = Dialog.useDialogControl() const context = useMemo<ContextType>( () => ({ control: control || defaultControl, }), [control, defaultControl], )
return <Context.Provider value={context}>{children}</Context.Provider>}
export function Trigger({ children, label, role = 'button', hint,}: TriggerProps) { const context = useMenuContext() const {state: focused, onIn: onFocus, onOut: onBlur} = useInteractionState() const { state: pressed, onIn: onPressIn, onOut: onPressOut, } = useInteractionState()
return children({ IS_NATIVE: true, control: context.control, state: { hovered: false, focused, pressed, }, props: { ref: null, onPress: context.control.open, onFocus, onBlur, onPressIn, onPressOut, accessibilityHint: hint, accessibilityLabel: label, accessibilityRole: role, }, })}
export function Outer({ children, showCancel,}: React.PropsWithChildren<{ showCancel?: boolean style?: StyleProp<ViewStyle> onCloseAutoFocus?: (event: Event) => void}>) { const context = useMenuContext() const {_} = useLingui()
return ( <Dialog.Outer control={context.control} nativeOptions={{preventExpansion: true}}> <Dialog.Handle /> {/* Re-wrap with context since Dialogs are portal-ed to root */} <Context.Provider value={context}> <Dialog.ScrollableInner label={_(msg`Menu`)}> <View style={[a.gap_lg]}> {children} {IS_NATIVE && showCancel && <Cancel />} </View> </Dialog.ScrollableInner> </Context.Provider> </Dialog.Outer> )}
export function Item({ children, label, style, onPress, destructive = false, ...rest}: ItemProps) { const t = useTheme() const context = useMenuContext() const {state: focused, onIn: onFocus, onOut: onBlur} = useInteractionState() const { state: pressed, onIn: onPressIn, onOut: onPressOut, } = useInteractionState()
return ( <Pressable {...rest} accessibilityHint="" accessibilityLabel={label} onFocus={onFocus} onBlur={onBlur} onPress={e => { if (IS_ANDROID) { /** * Below fix for iOS doesn't work for Android, this does. */ onPress?.(e) context.control.close() } else if (IS_IOS) { /** * Fixes a subtle bug on iOS * {@link https://github.com/bluesky-social/social-app/pull/5849/files#diff-de516ef5e7bd9840cd639213301df38cf03acfcad5bda85a1d63efd249ba79deL124-L127} */ context.control.close(() => { onPress?.(e) }) } }} onPressIn={e => { onPressIn() rest.onPressIn?.(e) }} onPressOut={e => { onPressOut() rest.onPressOut?.(e) }} style={[ a.flex_row, a.align_center, a.gap_sm, a.px_md, a.rounded_md, a.overflow_hidden, a.border, t.atoms.bg_contrast_25, t.atoms.border_contrast_low, {minHeight: 44, paddingVertical: 10}, style, (focused || pressed) && !rest.disabled && [t.atoms.bg_contrast_50], ]}> <ItemContext.Provider value={{disabled: Boolean(rest.disabled), destructive}}> {children} </ItemContext.Provider> </Pressable> )}
export function ItemText({children, style}: ItemTextProps) { const t = useTheme() const {disabled, destructive} = useMenuItemContext() return ( <Text numberOfLines={1} ellipsizeMode="middle" style={[ a.flex_1, a.text_md, a.font_semi_bold, t.atoms.text_contrast_high, style, destructive && {color: t.palette.negative_500}, disabled && t.atoms.text_contrast_low, ]}> {children} </Text> )}
export function ItemIcon({icon: Comp, fill}: ItemIconProps) { const t = useTheme() const {disabled, destructive} = useMenuItemContext() return ( <Comp size="lg" fill={ fill ? fill({disabled}) : disabled ? t.atoms.text_contrast_low.color : destructive ? t.palette.negative_500 : t.atoms.text_contrast_medium.color } /> )}
export function ItemRadio({selected}: {selected: boolean}) { const t = useTheme() return ( <View style={[ a.justify_center, a.align_center, a.rounded_full, t.atoms.border_contrast_high, { borderWidth: 1, height: 20, width: 20, }, ]}> {selected ? ( <View style={[ a.absolute, a.rounded_full, {height: 14, width: 14}, selected ? { backgroundColor: t.palette.primary_500, } : {}, ]} /> ) : null} </View> )}
/** * NATIVE ONLY - for adding non-pressable items to the menu * * @platform ios, android */export function ContainerItem({ children, style,}: { children: React.ReactNode style?: StyleProp<ViewStyle>}) { const t = useTheme() return ( <View style={[ a.flex_row, a.align_center, a.gap_sm, a.px_md, a.rounded_lg, a.curve_continuous, a.border, t.atoms.bg_contrast_25, t.atoms.border_contrast_low, {paddingVertical: 10}, style, ]}> {children} </View> )}
export function LabelText({ children, style,}: { children: React.ReactNode style?: StyleProp<TextStyle>}) { const t = useTheme() return ( <Text style={[ a.font_semi_bold, t.atoms.text_contrast_medium, {marginBottom: -8}, style, ]}> {children} </Text> )}
export function Group({children, style}: GroupProps) { const t = useTheme() return ( <View style={[ a.rounded_lg, a.curve_continuous, a.overflow_hidden, a.border, t.atoms.border_contrast_low, style, ]}> {flattenReactChildren(children).map((child, i) => { return isValidElement(child) && (child.type === Item || child.type === ContainerItem) ? ( <Fragment key={i}> {i > 0 ? ( <View style={[a.border_b, t.atoms.border_contrast_low]} /> ) : null} {cloneElement(child, { // @ts-expect-error cloneElement is not aware of the types style: { borderRadius: 0, borderWidth: 0, }, })} </Fragment> ) : null })} </View> )}
function Cancel() { const {_} = useLingui() const context = useMenuContext()
return ( <Button label={_(msg`Close this dialog`)} size="small" variant="ghost" color="secondary" onPress={() => context.control.close()}> <ButtonText> <Trans>Cancel</Trans> </ButtonText> </Button> )}
export function Divider() { return null}