mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {StyleProp, StyleSheet, TextStyle, View, ViewStyle} from 'react-native'
3import {Text} from '../text/Text'
4import {Button, ButtonType} from './Button'
5import {useTheme} from '../../../lib/ThemeContext'
6import {choose} from '../../../../lib/functions'
7import {colors} from '../../../lib/styles'
8
9export function ToggleButton({
10 type = 'default-light',
11 label,
12 isSelected,
13 style,
14 onPress,
15}: {
16 type?: ButtonType
17 label: string
18 isSelected: boolean
19 style?: StyleProp<ViewStyle>
20 onPress: () => void
21}) {
22 const theme = useTheme()
23 const circleStyle = choose<TextStyle, Record<ButtonType, TextStyle>>(type, {
24 primary: {
25 borderColor: theme.palette.primary.text,
26 },
27 secondary: {
28 borderColor: theme.palette.secondary.text,
29 },
30 inverted: {
31 borderColor: theme.palette.inverted.text,
32 },
33 'primary-outline': {
34 borderColor: theme.palette.primary.border,
35 },
36 'secondary-outline': {
37 borderColor: theme.palette.secondary.border,
38 },
39 'primary-light': {
40 borderColor: theme.palette.primary.border,
41 },
42 'secondary-light': {
43 borderColor: theme.palette.secondary.border,
44 },
45 'default-light': {
46 borderColor: theme.palette.default.border,
47 },
48 })
49 const circleFillStyle = choose<TextStyle, Record<ButtonType, TextStyle>>(
50 type,
51 {
52 primary: {
53 backgroundColor: theme.palette.primary.text,
54 opacity: isSelected ? 1 : 0.33,
55 },
56 secondary: {
57 backgroundColor: theme.palette.secondary.text,
58 opacity: isSelected ? 1 : 0.33,
59 },
60 inverted: {
61 backgroundColor: theme.palette.inverted.text,
62 opacity: isSelected ? 1 : 0.33,
63 },
64 'primary-outline': {
65 backgroundColor: theme.palette.primary.background,
66 opacity: isSelected ? 1 : 0.5,
67 },
68 'secondary-outline': {
69 backgroundColor: theme.palette.secondary.background,
70 opacity: isSelected ? 1 : 0.5,
71 },
72 'primary-light': {
73 backgroundColor: theme.palette.primary.background,
74 opacity: isSelected ? 1 : 0.5,
75 },
76 'secondary-light': {
77 backgroundColor: theme.palette.secondary.background,
78 opacity: isSelected ? 1 : 0.5,
79 },
80 'default-light': {
81 backgroundColor: isSelected
82 ? theme.palette.primary.background
83 : colors.gray3,
84 },
85 },
86 )
87 const labelStyle = choose<TextStyle, Record<ButtonType, TextStyle>>(type, {
88 primary: {
89 color: theme.palette.primary.text,
90 fontWeight: theme.palette.primary.isLowContrast ? '500' : undefined,
91 },
92 secondary: {
93 color: theme.palette.secondary.text,
94 fontWeight: theme.palette.secondary.isLowContrast ? '500' : undefined,
95 },
96 inverted: {
97 color: theme.palette.inverted.text,
98 fontWeight: theme.palette.inverted.isLowContrast ? '500' : undefined,
99 },
100 'primary-outline': {
101 color: theme.palette.primary.textInverted,
102 fontWeight: theme.palette.primary.isLowContrast ? '500' : undefined,
103 },
104 'secondary-outline': {
105 color: theme.palette.secondary.textInverted,
106 fontWeight: theme.palette.secondary.isLowContrast ? '500' : undefined,
107 },
108 'primary-light': {
109 color: theme.palette.primary.textInverted,
110 fontWeight: theme.palette.primary.isLowContrast ? '500' : undefined,
111 },
112 'secondary-light': {
113 color: theme.palette.secondary.textInverted,
114 fontWeight: theme.palette.secondary.isLowContrast ? '500' : undefined,
115 },
116 'default-light': {
117 color: theme.palette.default.text,
118 fontWeight: theme.palette.default.isLowContrast ? '500' : undefined,
119 },
120 })
121 return (
122 <Button type={type} onPress={onPress} style={style}>
123 <View style={styles.outer}>
124 <View style={[circleStyle, styles.circle]}>
125 <View
126 style={[
127 circleFillStyle,
128 styles.circleFill,
129 isSelected ? styles.circleFillSelected : undefined,
130 ]}
131 />
132 </View>
133 <Text type="button" style={[labelStyle, styles.label]}>
134 {label}
135 </Text>
136 </View>
137 </Button>
138 )
139}
140
141const styles = StyleSheet.create({
142 outer: {
143 flexDirection: 'row',
144 alignItems: 'center',
145 },
146 circle: {
147 width: 42,
148 height: 26,
149 borderRadius: 15,
150 padding: 4,
151 borderWidth: 1,
152 marginRight: 10,
153 },
154 circleFill: {
155 width: 16,
156 height: 16,
157 borderRadius: 10,
158 },
159 circleFillSelected: {
160 marginLeft: 16,
161 },
162 label: {
163 flex: 1,
164 },
165})