forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 💫
1import {useMemo} 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
23/**
24 * @deprecated - use SegmentedControl
25 */
26export function Group({children, multiple, ...props}: GroupProps) {
27 const t = useTheme()
28 return (
29 <Toggle.Group type={multiple ? 'checkbox' : 'radio'} {...props}>
30 <View
31 style={[
32 a.w_full,
33 a.flex_row,
34 a.rounded_sm,
35 a.overflow_hidden,
36 t.atoms.border_contrast_low,
37 {borderWidth: 1},
38 ]}>
39 {children}
40 </View>
41 </Toggle.Group>
42 )
43}
44
45/**
46 * @deprecated - use SegmentedControl
47 */
48export function Button({children, ...props}: ItemProps) {
49 return (
50 <Toggle.Item {...props} style={[a.flex_grow, a.flex_1]}>
51 <ButtonInner>{children}</ButtonInner>
52 </Toggle.Item>
53 )
54}
55
56function ButtonInner({children}: React.PropsWithChildren<{}>) {
57 const t = useTheme()
58 const state = Toggle.useItemContext()
59
60 const {baseStyles, hoverStyles, activeStyles} = useMemo(() => {
61 const base: ViewStyle[] = []
62 const hover: ViewStyle[] = []
63 const active: ViewStyle[] = []
64
65 hover.push(
66 t.name === 'light' ? t.atoms.bg_contrast_100 : t.atoms.bg_contrast_25,
67 )
68
69 if (state.selected) {
70 active.push({
71 backgroundColor: t.palette.contrast_800,
72 })
73 hover.push({
74 backgroundColor: t.palette.contrast_800,
75 })
76
77 if (state.disabled) {
78 active.push({
79 backgroundColor: t.palette.contrast_500,
80 })
81 }
82 }
83
84 if (state.disabled) {
85 base.push({
86 backgroundColor: t.palette.contrast_100,
87 })
88 }
89
90 return {
91 baseStyles: base,
92 hoverStyles: hover,
93 activeStyles: active,
94 }
95 }, [t, state])
96
97 return (
98 <View
99 style={[
100 {
101 borderLeftWidth: 1,
102 marginLeft: -1,
103 },
104 a.flex_grow,
105 a.py_md,
106 native({
107 paddingBottom: 10,
108 }),
109 a.px_md,
110 t.atoms.bg,
111 t.atoms.border_contrast_low,
112 baseStyles,
113 activeStyles,
114 (state.hovered || state.pressed) && hoverStyles,
115 ]}>
116 {children}
117 </View>
118 )
119}
120
121/**
122 * @deprecated - use SegmentedControl
123 */
124export function ButtonText({children}: {children: React.ReactNode}) {
125 const t = useTheme()
126 const state = Toggle.useItemContext()
127
128 const textStyles = useMemo(() => {
129 const text: TextStyle[] = []
130 if (state.selected) {
131 text.push(t.atoms.text_inverted)
132 }
133 if (state.disabled) {
134 text.push({
135 opacity: 0.5,
136 })
137 }
138 return text
139 }, [t, state])
140
141 return (
142 <Text
143 style={[
144 a.text_center,
145 a.font_semi_bold,
146 t.atoms.text_contrast_medium,
147 textStyles,
148 ]}>
149 {children}
150 </Text>
151 )
152}