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