1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
import React, {useContext, useMemo} from 'react'
import {GestureResponderEvent, StyleProp, View, ViewStyle} from 'react-native'
import {HITSLOP_10} from '#/lib/constants'
import {atoms as a, useTheme, ViewStyleProp} from '#/alf'
import * as Button from '#/components/Button'
import {ChevronRight_Stroke2_Corner0_Rounded as ChevronRightIcon} from '#/components/icons/Chevron'
import {Link, LinkProps} from '#/components/Link'
import {createPortalGroup} from '#/components/Portal'
import {Text} from '#/components/Typography'
const ItemContext = React.createContext({
destructive: false,
withinGroup: false,
})
const Portal = createPortalGroup()
export function Container({children}: {children: React.ReactNode}) {
return <View style={[a.flex_1, a.py_md]}>{children}</View>
}
/**
* This uses `Portal` magic ✨ to render the icons and title correctly. ItemIcon and ItemText components
* get teleported to the top row, leaving the rest of the children in the bottom row.
*/
export function Group({
children,
destructive = false,
iconInset = true,
style,
contentContainerStyle,
}: {
children: React.ReactNode
destructive?: boolean
iconInset?: boolean
style?: StyleProp<ViewStyle>
contentContainerStyle?: StyleProp<ViewStyle>
}) {
const context = useMemo(
() => ({destructive, withinGroup: true}),
[destructive],
)
return (
<View style={[a.w_full, style]}>
<Portal.Provider>
<ItemContext.Provider value={context}>
<Item style={[a.pb_2xs, {minHeight: 42}]}>
<Portal.Outlet />
</Item>
<Item
style={[
a.flex_col,
a.pt_2xs,
a.align_start,
a.gap_0,
contentContainerStyle,
]}
iconInset={iconInset}>
{children}
</Item>
</ItemContext.Provider>
</Portal.Provider>
</View>
)
}
export function Item({
children,
destructive,
iconInset = false,
style,
}: {
children?: React.ReactNode
destructive?: boolean
/**
* Adds left padding so that the content will be aligned with other Items that contain icons
* @default false
*/
iconInset?: boolean
style?: StyleProp<ViewStyle>
}) {
const context = useContext(ItemContext)
const childContext = useMemo(() => {
if (typeof destructive !== 'boolean') return context
return {...context, destructive}
}, [context, destructive])
return (
<View
style={[
a.px_xl,
a.py_sm,
a.align_center,
a.gap_md,
a.w_full,
a.flex_row,
{minHeight: 48},
iconInset && {
paddingLeft:
// existing padding
a.pl_xl.paddingLeft +
// icon
28 +
// gap
a.gap_md.gap,
},
style,
]}>
<ItemContext.Provider value={childContext}>
{children}
</ItemContext.Provider>
</View>
)
}
export function LinkItem({
children,
destructive = false,
contentContainerStyle,
chevronColor,
...props
}: LinkProps & {
contentContainerStyle?: StyleProp<ViewStyle>
destructive?: boolean
chevronColor?: string
}) {
const t = useTheme()
return (
<Link color="secondary" {...props}>
{args => (
<Item
destructive={destructive}
style={[
(args.hovered || args.pressed) && [t.atoms.bg_contrast_25],
contentContainerStyle,
]}>
{typeof children === 'function' ? children(args) : children}
<Chevron color={chevronColor} />
</Item>
)}
</Link>
)
}
export function PressableItem({
children,
destructive = false,
contentContainerStyle,
hoverStyle,
...props
}: Button.ButtonProps & {
contentContainerStyle?: StyleProp<ViewStyle>
destructive?: boolean
}) {
const t = useTheme()
return (
<Button.Button {...props}>
{args => (
<Item
destructive={destructive}
style={[
(args.hovered || args.pressed) && [
t.atoms.bg_contrast_25,
hoverStyle,
],
contentContainerStyle,
]}>
{typeof children === 'function' ? children(args) : children}
</Item>
)}
</Button.Button>
)
}
export function ItemIcon({
icon: Comp,
size = 'xl',
color: colorProp,
}: Omit<React.ComponentProps<typeof Button.ButtonIcon>, 'position'> & {
color?: string
}) {
const t = useTheme()
const {destructive, withinGroup} = useContext(ItemContext)
/*
* Copied here from icons/common.tsx so we can tweak if we need to, but
* also so that we can calculate transforms.
*/
const iconSize = {
xs: 12,
sm: 16,
md: 20,
lg: 24,
xl: 28,
'2xl': 32,
}[size]
const color =
colorProp ?? (destructive ? t.palette.negative_500 : t.atoms.text.color)
const content = (
<View style={[a.z_20, {width: iconSize, height: iconSize}]}>
<Comp width={iconSize} style={[{color}]} />
</View>
)
if (withinGroup) {
return <Portal.Portal>{content}</Portal.Portal>
} else {
return content
}
}
export function ItemText({
// eslint-disable-next-line react/prop-types
style,
...props
}: React.ComponentProps<typeof Button.ButtonText>) {
const t = useTheme()
const {destructive, withinGroup} = useContext(ItemContext)
const content = (
<Button.ButtonText
style={[
a.text_md,
a.font_normal,
a.text_left,
a.flex_1,
destructive ? {color: t.palette.negative_500} : t.atoms.text,
style,
]}
{...props}
/>
)
if (withinGroup) {
return <Portal.Portal>{content}</Portal.Portal>
} else {
return content
}
}
export function Divider({style}: ViewStyleProp) {
const t = useTheme()
return (
<View
style={[
a.border_t,
t.atoms.border_contrast_medium,
a.w_full,
a.my_sm,
style,
]}
/>
)
}
export function Chevron({color: colorProp}: {color?: string}) {
const {destructive} = useContext(ItemContext)
const t = useTheme()
const color =
colorProp ?? (destructive ? t.palette.negative_500 : t.palette.contrast_500)
return <ItemIcon icon={ChevronRightIcon} size="md" color={color} />
}
export function BadgeText({
children,
style,
}: {
children: React.ReactNode
style?: StyleProp<ViewStyle>
}) {
const t = useTheme()
return (
<Text
style={[
t.atoms.text_contrast_low,
a.text_md,
a.text_right,
a.leading_snug,
style,
]}
numberOfLines={1}>
{children}
</Text>
)
}
export function BadgeButton({
label,
onPress,
}: {
label: string
onPress: (evt: GestureResponderEvent) => void
}) {
const t = useTheme()
return (
<Button.Button label={label} onPress={onPress} hitSlop={HITSLOP_10}>
{({pressed}) => (
<Button.ButtonText
style={[
a.text_md,
a.font_normal,
a.text_right,
{color: pressed ? t.palette.contrast_300 : t.palette.primary_500},
]}>
{label}
</Button.ButtonText>
)}
</Button.Button>
)
}
|