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