mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react' 2import { 3 type AccessibilityProps, 4 type TextStyle, 5 View, 6 type ViewStyle, 7} from 'react-native' 8 9import {atoms as a, native, useTheme} from '#/alf' 10import * as Toggle from '#/components/forms/Toggle' 11import {Text} from '#/components/Typography' 12 13type ItemProps = Omit<Toggle.ItemProps, 'style' | 'role' | 'children'> & 14 AccessibilityProps & { 15 children: React.ReactElement<any> 16 testID?: string 17 } 18 19export type GroupProps = Omit<Toggle.GroupProps, 'style' | 'type'> & { 20 multiple?: boolean 21} 22 23export function Group({children, multiple, ...props}: GroupProps) { 24 const t = useTheme() 25 return ( 26 <Toggle.Group type={multiple ? 'checkbox' : 'radio'} {...props}> 27 <View 28 style={[ 29 a.w_full, 30 a.flex_row, 31 a.rounded_sm, 32 a.overflow_hidden, 33 t.atoms.border_contrast_low, 34 {borderWidth: 1}, 35 ]}> 36 {children} 37 </View> 38 </Toggle.Group> 39 ) 40} 41 42export function Button({children, ...props}: ItemProps) { 43 return ( 44 <Toggle.Item {...props} style={[a.flex_grow, a.flex_1]}> 45 <ButtonInner>{children}</ButtonInner> 46 </Toggle.Item> 47 ) 48} 49 50function ButtonInner({children}: React.PropsWithChildren<{}>) { 51 const t = useTheme() 52 const state = Toggle.useItemContext() 53 54 const {baseStyles, hoverStyles, activeStyles} = React.useMemo(() => { 55 const base: ViewStyle[] = [] 56 const hover: ViewStyle[] = [] 57 const active: ViewStyle[] = [] 58 59 hover.push( 60 t.name === 'light' ? t.atoms.bg_contrast_100 : t.atoms.bg_contrast_25, 61 ) 62 63 if (state.selected) { 64 active.push({ 65 backgroundColor: t.palette.contrast_800, 66 }) 67 hover.push({ 68 backgroundColor: t.palette.contrast_800, 69 }) 70 71 if (state.disabled) { 72 active.push({ 73 backgroundColor: t.palette.contrast_500, 74 }) 75 } 76 } 77 78 if (state.disabled) { 79 base.push({ 80 backgroundColor: t.palette.contrast_100, 81 }) 82 } 83 84 return { 85 baseStyles: base, 86 hoverStyles: hover, 87 activeStyles: active, 88 } 89 }, [t, state]) 90 91 return ( 92 <View 93 style={[ 94 { 95 borderLeftWidth: 1, 96 marginLeft: -1, 97 }, 98 a.flex_grow, 99 a.py_md, 100 native({ 101 paddingBottom: 10, 102 }), 103 a.px_md, 104 t.atoms.bg, 105 t.atoms.border_contrast_low, 106 baseStyles, 107 activeStyles, 108 (state.hovered || state.pressed) && hoverStyles, 109 ]}> 110 {children} 111 </View> 112 ) 113} 114 115export function ButtonText({children}: {children: React.ReactNode}) { 116 const t = useTheme() 117 const state = Toggle.useItemContext() 118 119 const textStyles = React.useMemo(() => { 120 const text: TextStyle[] = [] 121 if (state.selected) { 122 text.push(t.atoms.text_inverted) 123 } 124 if (state.disabled) { 125 text.push({ 126 opacity: 0.5, 127 }) 128 } 129 return text 130 }, [t, state]) 131 132 return ( 133 <Text 134 style={[ 135 a.text_center, 136 a.font_semi_bold, 137 t.atoms.text_contrast_medium, 138 textStyles, 139 ]}> 140 {children} 141 </Text> 142 ) 143}