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