@fabricator-ui/react-native 0.0.0-stage → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -2
- package/dist/LICENSE.md +25 -0
- package/dist/NOTICE.md +24 -0
- package/dist/lib/data-variants.ts +23 -0
- package/dist/lib/density-scale.ts +208 -0
- package/dist/lib/fabricator/css-object.ts +2 -0
- package/dist/lib/fabricator/density-scale.ts +210 -0
- package/dist/lib/fabricator/density-variables.ts +37 -0
- package/dist/lib/fabricator/motion-speeds.ts +566 -0
- package/dist/lib/fabricator/radius.ts +240 -0
- package/dist/lib/feedback.ts +124 -0
- package/dist/lib/field-frame.ts +53 -0
- package/dist/lib/field-model.ts +112 -0
- package/dist/lib/fluid-hover-model.ts +142 -0
- package/dist/lib/fluid-hover.tsx +788 -0
- package/dist/lib/icon-stroke.ts +38 -0
- package/dist/lib/motion-tokens.ts +97 -0
- package/dist/lib/motion.ts +292 -0
- package/dist/lib/overlay-model.ts +205 -0
- package/dist/lib/press-wash.tsx +73 -0
- package/dist/lib/render.tsx +53 -0
- package/dist/lib/select-model.ts +87 -0
- package/dist/lib/sheet-model.ts +101 -0
- package/dist/lib/sheet-surface.tsx +429 -0
- package/dist/lib/sound-recipes.ts +449 -0
- package/dist/lib/spring-model.ts +214 -0
- package/dist/lib/surface-color.tsx +27 -0
- package/dist/lib/text-children.ts +43 -0
- package/dist/styles.css +610 -0
- package/dist/ui/badge.tsx +108 -0
- package/dist/ui/button.tsx +285 -0
- package/dist/ui/card.tsx +365 -0
- package/dist/ui/checkbox.tsx +230 -0
- package/dist/ui/collapsible.tsx +194 -0
- package/dist/ui/density.tsx +79 -0
- package/dist/ui/dialog.tsx +232 -0
- package/dist/ui/dropdown-menu.tsx +687 -0
- package/dist/ui/fabricator-root.tsx +68 -0
- package/dist/ui/input.tsx +63 -0
- package/dist/ui/label.tsx +22 -0
- package/dist/ui/select.tsx +616 -0
- package/dist/ui/switch.tsx +177 -0
- package/dist/ui/tabs.tsx +272 -0
- package/dist/ui/text.tsx +125 -0
- package/dist/ui/textarea.tsx +65 -0
- package/package.json +54 -4
package/dist/ui/card.tsx
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
3
|
+
import { cn } from "cn"
|
|
4
|
+
import { Pressable, View } from "react-native"
|
|
5
|
+
import { useResolveClassNames } from "uniwind"
|
|
6
|
+
|
|
7
|
+
import { useFeedback } from "../lib/feedback"
|
|
8
|
+
import { SurfaceColorProvider } from "../lib/surface-color"
|
|
9
|
+
import { renderTextChildren, Text, TextStyle } from "./text"
|
|
10
|
+
|
|
11
|
+
type CardSize = "default" | "sm"
|
|
12
|
+
type CardVariant = "default" | "muted" | "tray"
|
|
13
|
+
type CardState = { size: CardSize; variant: CardVariant }
|
|
14
|
+
|
|
15
|
+
// React Native has no group-data-* or :has(): parts read the card's size and
|
|
16
|
+
// variant from here, and a CardLink hands the card its press.
|
|
17
|
+
const CardContext = React.createContext<CardState>({
|
|
18
|
+
size: "default",
|
|
19
|
+
variant: "default",
|
|
20
|
+
})
|
|
21
|
+
const CardLinkContext = React.createContext<
|
|
22
|
+
((press: (() => void) | null) => void) | null
|
|
23
|
+
>(null)
|
|
24
|
+
|
|
25
|
+
const cardVariants = cva("gap-(--density-16) overflow-hidden rounded-(--radius-card) py-(--density-16) data-[size=sm]:gap-(--density-12) data-[size=sm]:rounded-(--radius-card-sm) data-[size=sm]:py-(--density-12) active:opacity-90 corner-curve flex-col", {
|
|
26
|
+
variants: {
|
|
27
|
+
variant: {
|
|
28
|
+
default: "bg-card shadow-surface-2",
|
|
29
|
+
muted: "bg-muted",
|
|
30
|
+
tray: "bg-muted gap-(--density-4) p-(--density-4) data-[size=sm]:gap-(--density-4) data-[size=sm]:p-(--density-4)",
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
defaultVariants: {
|
|
34
|
+
variant: "default",
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
// The card's surface, for fades painted over its content. A muted card is a
|
|
39
|
+
// wash over what's around it, so it keeps that surface.
|
|
40
|
+
const cardSurfaceVariants = cva("", {
|
|
41
|
+
variants: {
|
|
42
|
+
variant: {
|
|
43
|
+
default: "text-card",
|
|
44
|
+
muted: "",
|
|
45
|
+
tray: "",
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
defaultVariants: {
|
|
49
|
+
variant: "default",
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
function Card({
|
|
54
|
+
className,
|
|
55
|
+
size = "default",
|
|
56
|
+
variant = "default",
|
|
57
|
+
children,
|
|
58
|
+
...props
|
|
59
|
+
}: React.ComponentProps<typeof View> & {
|
|
60
|
+
size?: CardSize
|
|
61
|
+
} & VariantProps<typeof cardVariants>) {
|
|
62
|
+
const feedback = useFeedback()
|
|
63
|
+
const pressRef = React.useRef<(() => void) | null>(null)
|
|
64
|
+
const [isLinked, setIsLinked] = React.useState(false)
|
|
65
|
+
const register = React.useCallback((press: (() => void) | null) => {
|
|
66
|
+
pressRef.current = press
|
|
67
|
+
setIsLinked(press !== null)
|
|
68
|
+
}, [])
|
|
69
|
+
const state = React.useMemo(
|
|
70
|
+
() => ({ size, variant: variant ?? "default" }),
|
|
71
|
+
[size, variant]
|
|
72
|
+
)
|
|
73
|
+
const shared = {
|
|
74
|
+
"data-slot": "card",
|
|
75
|
+
"data-size": size,
|
|
76
|
+
"data-variant": variant,
|
|
77
|
+
className: cn(cardVariants({ variant, className })),
|
|
78
|
+
...props,
|
|
79
|
+
}
|
|
80
|
+
const content = renderTextChildren(children)
|
|
81
|
+
const { color: surfaceColor } = useResolveClassNames(
|
|
82
|
+
cardSurfaceVariants({ variant })
|
|
83
|
+
)
|
|
84
|
+
return (
|
|
85
|
+
<SurfaceColorProvider
|
|
86
|
+
color={typeof surfaceColor === "string" ? surfaceColor : undefined}
|
|
87
|
+
>
|
|
88
|
+
<CardContext value={state}>
|
|
89
|
+
<CardLinkContext value={register}>
|
|
90
|
+
<TextStyle className={"fab:text-card-foreground fab:text-(length:--density-text-13)"}>
|
|
91
|
+
{isLinked ? (
|
|
92
|
+
// The CardLink is what a screen reader focuses; the card only
|
|
93
|
+
// widens its target, as the link's cover does on the website.
|
|
94
|
+
<Pressable
|
|
95
|
+
{...shared}
|
|
96
|
+
accessible={false}
|
|
97
|
+
onPress={() => {
|
|
98
|
+
feedback("tick")
|
|
99
|
+
pressRef.current?.()
|
|
100
|
+
}}
|
|
101
|
+
>
|
|
102
|
+
{content}
|
|
103
|
+
</Pressable>
|
|
104
|
+
) : (
|
|
105
|
+
<View {...shared}>{content}</View>
|
|
106
|
+
)}
|
|
107
|
+
</TextStyle>
|
|
108
|
+
</CardLinkContext>
|
|
109
|
+
</CardContext>
|
|
110
|
+
</SurfaceColorProvider>
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function findPart(children: React.ReactNode, part: React.ElementType) {
|
|
115
|
+
return React.Children.toArray(children).some(
|
|
116
|
+
(child) => React.isValidElement(child) && child.type === part
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// The website's header grid: media, then the title and description, then the
|
|
121
|
+
// action, each column present only when its part is.
|
|
122
|
+
function CardHeader({
|
|
123
|
+
className,
|
|
124
|
+
children,
|
|
125
|
+
...props
|
|
126
|
+
}: React.ComponentProps<typeof View>) {
|
|
127
|
+
const { size, variant } = React.use(CardContext)
|
|
128
|
+
const parts = React.Children.toArray(children)
|
|
129
|
+
const media = parts.filter(
|
|
130
|
+
(child) => React.isValidElement(child) && child.type === CardMedia
|
|
131
|
+
)
|
|
132
|
+
const action = parts.filter(
|
|
133
|
+
(child) => React.isValidElement(child) && child.type === CardAction
|
|
134
|
+
)
|
|
135
|
+
const text = parts.filter(
|
|
136
|
+
(child) =>
|
|
137
|
+
!React.isValidElement(child) ||
|
|
138
|
+
(child.type !== CardMedia && child.type !== CardAction)
|
|
139
|
+
)
|
|
140
|
+
const hasDescription = findPart(children, CardDescription)
|
|
141
|
+
return (
|
|
142
|
+
<View
|
|
143
|
+
data-slot="card-header"
|
|
144
|
+
data-size={size}
|
|
145
|
+
data-variant={variant}
|
|
146
|
+
data-description={hasDescription}
|
|
147
|
+
className={cn("items-center gap-(--density-12) px-(--density-16) data-[description=true]:items-start data-[size=sm]:px-(--density-12) data-[variant=tray]:px-(--density-12) data-[variant=tray]:pt-(--density-12) data-[variant=tray]:pb-(--density-8) flex-row", className)}
|
|
148
|
+
{...props}
|
|
149
|
+
>
|
|
150
|
+
{media}
|
|
151
|
+
<View className={"fab:gap-(--density-4) fab:min-w-0 fab:flex-1"}>
|
|
152
|
+
{renderTextChildren(text)}
|
|
153
|
+
</View>
|
|
154
|
+
{action}
|
|
155
|
+
</View>
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const cardMediaVariants = cva(
|
|
160
|
+
"shrink-0 items-center justify-center",
|
|
161
|
+
{
|
|
162
|
+
variants: {
|
|
163
|
+
variant: {
|
|
164
|
+
default: "bg-transparent",
|
|
165
|
+
icon: "bg-hover size-(--density-32) rounded-(--radius-control) data-[size=sm]:size-(--density-28) corner-curve",
|
|
166
|
+
image: "size-(--density-32) overflow-hidden rounded-(--radius-control) data-[size=sm]:size-(--density-28) corner-curve",
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
defaultVariants: {
|
|
170
|
+
variant: "default",
|
|
171
|
+
},
|
|
172
|
+
}
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
/** The header's leading picture: an icon tile, a logo or an avatar. */
|
|
176
|
+
function CardMedia({
|
|
177
|
+
className,
|
|
178
|
+
variant = "default",
|
|
179
|
+
...props
|
|
180
|
+
}: React.ComponentProps<typeof View> & VariantProps<typeof cardMediaVariants>) {
|
|
181
|
+
const { size } = React.use(CardContext)
|
|
182
|
+
return (
|
|
183
|
+
<View
|
|
184
|
+
data-slot="card-media"
|
|
185
|
+
data-variant={variant}
|
|
186
|
+
data-size={size}
|
|
187
|
+
className={cn(cardMediaVariants({ variant, className }))}
|
|
188
|
+
{...props}
|
|
189
|
+
/>
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function CardTitle({ className, ...props }: React.ComponentProps<typeof Text>) {
|
|
194
|
+
const { size } = React.use(CardContext)
|
|
195
|
+
return (
|
|
196
|
+
<Text
|
|
197
|
+
data-slot="card-title"
|
|
198
|
+
data-size={size}
|
|
199
|
+
role="heading"
|
|
200
|
+
className={cn("text-foreground text-(length:--density-text-16) font-medium leading-snug data-[size=sm]:text-(length:--density-text-14)", className)}
|
|
201
|
+
{...props}
|
|
202
|
+
/>
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function CardDescription({
|
|
207
|
+
className,
|
|
208
|
+
...props
|
|
209
|
+
}: React.ComponentProps<typeof Text>) {
|
|
210
|
+
const { size } = React.use(CardContext)
|
|
211
|
+
return (
|
|
212
|
+
<Text
|
|
213
|
+
data-slot="card-description"
|
|
214
|
+
data-size={size}
|
|
215
|
+
className={cn("text-muted-foreground text-(length:--density-text-13) data-[size=sm]:text-(length:--density-text-12)", className)}
|
|
216
|
+
{...props}
|
|
217
|
+
/>
|
|
218
|
+
)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function CardAction({
|
|
222
|
+
className,
|
|
223
|
+
children,
|
|
224
|
+
...props
|
|
225
|
+
}: React.ComponentProps<typeof View>) {
|
|
226
|
+
return (
|
|
227
|
+
<View
|
|
228
|
+
data-slot="card-action"
|
|
229
|
+
className={cn("shrink-0", className)}
|
|
230
|
+
{...props}
|
|
231
|
+
>
|
|
232
|
+
{renderTextChildren(children)}
|
|
233
|
+
</View>
|
|
234
|
+
)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The title's link to the record. The whole card takes its press, while the
|
|
239
|
+
* buttons and switches inside the card keep their own.
|
|
240
|
+
*/
|
|
241
|
+
function CardLink({
|
|
242
|
+
className,
|
|
243
|
+
onPress,
|
|
244
|
+
...props
|
|
245
|
+
}: React.ComponentProps<typeof Text> & { onPress: () => void }) {
|
|
246
|
+
const register = React.use(CardLinkContext)
|
|
247
|
+
const pressRef = React.useRef(onPress)
|
|
248
|
+
React.useLayoutEffect(() => {
|
|
249
|
+
pressRef.current = onPress
|
|
250
|
+
})
|
|
251
|
+
React.useLayoutEffect(() => {
|
|
252
|
+
register?.(() => pressRef.current())
|
|
253
|
+
return () => register?.(null)
|
|
254
|
+
}, [register])
|
|
255
|
+
return (
|
|
256
|
+
<Text
|
|
257
|
+
data-slot="card-link"
|
|
258
|
+
role="link"
|
|
259
|
+
className={cn("text-foreground", className)}
|
|
260
|
+
onPress={onPress}
|
|
261
|
+
{...props}
|
|
262
|
+
/>
|
|
263
|
+
)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function CardContent({
|
|
267
|
+
className,
|
|
268
|
+
children,
|
|
269
|
+
...props
|
|
270
|
+
}: React.ComponentProps<typeof View>) {
|
|
271
|
+
const { size, variant } = React.use(CardContext)
|
|
272
|
+
return (
|
|
273
|
+
<View
|
|
274
|
+
data-slot="card-content"
|
|
275
|
+
data-size={size}
|
|
276
|
+
data-variant={variant}
|
|
277
|
+
className={cn("gap-(--density-8) px-(--density-16) data-[size=sm]:px-(--density-12) data-[variant=tray]:bg-card data-[variant=tray]:shadow-surface-2 data-[variant=tray]:rounded-(--radius-card-inner) data-[variant=tray]:p-(--density-12) data-[variant=tray]:corner-curve", className)}
|
|
278
|
+
{...props}
|
|
279
|
+
>
|
|
280
|
+
{renderTextChildren(children)}
|
|
281
|
+
</View>
|
|
282
|
+
)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** The headline figure. A caption or a Delta after it wraps under it. */
|
|
286
|
+
function CardValue({
|
|
287
|
+
className,
|
|
288
|
+
children,
|
|
289
|
+
...props
|
|
290
|
+
}: React.ComponentProps<typeof View>) {
|
|
291
|
+
const { size } = React.use(CardContext)
|
|
292
|
+
return (
|
|
293
|
+
<View
|
|
294
|
+
data-slot="card-value"
|
|
295
|
+
data-size={size}
|
|
296
|
+
className={cn(
|
|
297
|
+
"gap-x-(--density-8) gap-y-(--density-4) flex-row flex-wrap items-baseline",
|
|
298
|
+
className
|
|
299
|
+
)}
|
|
300
|
+
{...props}
|
|
301
|
+
>
|
|
302
|
+
{React.Children.map(children, (child) =>
|
|
303
|
+
typeof child === "string" || typeof child === "number" ? (
|
|
304
|
+
<Text data-size={size} className={"fab:text-foreground fab:text-(length:--density-text-32) fab:font-semibold fab:tracking-tight fab:tabular-nums fab:data-[size=sm]:text-(length:--density-text-24)"}>
|
|
305
|
+
{child}
|
|
306
|
+
</Text>
|
|
307
|
+
) : (
|
|
308
|
+
child
|
|
309
|
+
)
|
|
310
|
+
)}
|
|
311
|
+
</View>
|
|
312
|
+
)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Muted text after a value: a unit, a total ("of 200K"), a previous value. */
|
|
316
|
+
function CardValueCaption({
|
|
317
|
+
className,
|
|
318
|
+
...props
|
|
319
|
+
}: React.ComponentProps<typeof Text>) {
|
|
320
|
+
const { size } = React.use(CardContext)
|
|
321
|
+
return (
|
|
322
|
+
<Text
|
|
323
|
+
data-slot="card-value-caption"
|
|
324
|
+
data-size={size}
|
|
325
|
+
className={cn("text-muted-foreground text-(length:--density-text-14) data-[size=sm]:text-(length:--density-text-13)", className)}
|
|
326
|
+
{...props}
|
|
327
|
+
/>
|
|
328
|
+
)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function CardFooter({
|
|
332
|
+
className,
|
|
333
|
+
children,
|
|
334
|
+
...props
|
|
335
|
+
}: React.ComponentProps<typeof View>) {
|
|
336
|
+
const { size, variant } = React.use(CardContext)
|
|
337
|
+
return (
|
|
338
|
+
<TextStyle className={"fab:text-muted-foreground fab:text-(length:--density-text-13)"}>
|
|
339
|
+
<View
|
|
340
|
+
data-slot="card-footer"
|
|
341
|
+
data-size={size}
|
|
342
|
+
data-variant={variant}
|
|
343
|
+
className={cn("mt-auto gap-(--density-8) px-(--density-16) data-[size=sm]:px-(--density-12) data-[variant=tray]:px-(--density-12) data-[variant=tray]:pt-(--density-8) data-[variant=tray]:pb-(--density-12) flex-row items-center", className)}
|
|
344
|
+
{...props}
|
|
345
|
+
>
|
|
346
|
+
{renderTextChildren(children)}
|
|
347
|
+
</View>
|
|
348
|
+
</TextStyle>
|
|
349
|
+
)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export {
|
|
353
|
+
Card,
|
|
354
|
+
CardAction,
|
|
355
|
+
CardContent,
|
|
356
|
+
CardDescription,
|
|
357
|
+
CardFooter,
|
|
358
|
+
CardHeader,
|
|
359
|
+
CardLink,
|
|
360
|
+
CardMedia,
|
|
361
|
+
CardTitle,
|
|
362
|
+
CardValue,
|
|
363
|
+
CardValueCaption,
|
|
364
|
+
cardVariants,
|
|
365
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { useControllableState } from "@rn-primitives/hooks"
|
|
3
|
+
import { cva } from "class-variance-authority"
|
|
4
|
+
import { cn } from "cn"
|
|
5
|
+
import { Pressable } from "react-native"
|
|
6
|
+
import Animated, {
|
|
7
|
+
useAnimatedProps,
|
|
8
|
+
useAnimatedStyle,
|
|
9
|
+
useSharedValue,
|
|
10
|
+
withDelay,
|
|
11
|
+
withTiming,
|
|
12
|
+
type SharedValue,
|
|
13
|
+
} from "react-native-reanimated"
|
|
14
|
+
import Svg, { Path } from "react-native-svg"
|
|
15
|
+
import { useResolveClassNames } from "uniwind"
|
|
16
|
+
|
|
17
|
+
import { useFeedback } from "../lib/feedback"
|
|
18
|
+
import { useFieldControl } from "../lib/field-model"
|
|
19
|
+
import { useMotion, usePressScale } from "../lib/motion"
|
|
20
|
+
import { StateFill } from "../lib/press-wash"
|
|
21
|
+
|
|
22
|
+
const AnimatedPressable = Animated.createAnimatedComponent(Pressable)
|
|
23
|
+
const AnimatedPath = Animated.createAnimatedComponent(Path)
|
|
24
|
+
|
|
25
|
+
// The press gives more than a button's: the website's `active:scale-90`.
|
|
26
|
+
const PRESS_GIVE = 0.1
|
|
27
|
+
// The website's mark, in a 24-unit box: the check starts at its short leg.
|
|
28
|
+
// react-native-svg has no `pathLength`, so each path carries its own length.
|
|
29
|
+
const CHECK = { d: "M4 12l5 5L20 6", length: 16 * Math.SQRT2 }
|
|
30
|
+
const MINUS = { d: "M5 12h14", length: 14 }
|
|
31
|
+
const MARK_STROKE = 3
|
|
32
|
+
|
|
33
|
+
// Resolved for each stroke's colour: an SVG path takes no class.
|
|
34
|
+
const checkboxCheckVariants = cva("text-primary-foreground")
|
|
35
|
+
const checkboxMinusVariants = cva("text-primary-foreground")
|
|
36
|
+
|
|
37
|
+
/** A path that draws itself from its start as `progress` goes 0 → 1. */
|
|
38
|
+
function MarkPath({
|
|
39
|
+
d,
|
|
40
|
+
length,
|
|
41
|
+
progress,
|
|
42
|
+
className,
|
|
43
|
+
}: {
|
|
44
|
+
d: string
|
|
45
|
+
length: number
|
|
46
|
+
progress: SharedValue<number>
|
|
47
|
+
className: string
|
|
48
|
+
}) {
|
|
49
|
+
const { color } = useResolveClassNames(className)
|
|
50
|
+
const animatedProps = useAnimatedProps(() => ({
|
|
51
|
+
strokeDashoffset: length * (1 - progress.value),
|
|
52
|
+
}))
|
|
53
|
+
return (
|
|
54
|
+
<AnimatedPath
|
|
55
|
+
d={d}
|
|
56
|
+
fill="none"
|
|
57
|
+
stroke={typeof color === "string" ? color : "white"}
|
|
58
|
+
strokeWidth={MARK_STROKE}
|
|
59
|
+
strokeLinecap="round"
|
|
60
|
+
strokeLinejoin="round"
|
|
61
|
+
strokeDasharray={[length, length * 2]}
|
|
62
|
+
animatedProps={animatedProps}
|
|
63
|
+
/>
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function Checkbox({
|
|
68
|
+
className,
|
|
69
|
+
checked,
|
|
70
|
+
defaultChecked = false,
|
|
71
|
+
onCheckedChange,
|
|
72
|
+
indeterminate = false,
|
|
73
|
+
disabled,
|
|
74
|
+
"aria-invalid": isInvalid,
|
|
75
|
+
onPress,
|
|
76
|
+
onPressIn,
|
|
77
|
+
onPressOut,
|
|
78
|
+
style,
|
|
79
|
+
...props
|
|
80
|
+
}: Omit<React.ComponentProps<typeof Pressable>, "children"> & {
|
|
81
|
+
checked?: boolean
|
|
82
|
+
defaultChecked?: boolean
|
|
83
|
+
onCheckedChange?: (checked: boolean) => void
|
|
84
|
+
indeterminate?: boolean
|
|
85
|
+
"aria-invalid"?: boolean
|
|
86
|
+
}) {
|
|
87
|
+
const [isChecked = false, setIsChecked] = useControllableState({
|
|
88
|
+
prop: checked,
|
|
89
|
+
defaultProp: defaultChecked,
|
|
90
|
+
onChange: onCheckedChange,
|
|
91
|
+
})
|
|
92
|
+
const feedback = useFeedback()
|
|
93
|
+
const toggle = () => {
|
|
94
|
+
feedback(isChecked ? "uncheck" : "check")
|
|
95
|
+
setIsChecked(!isChecked)
|
|
96
|
+
}
|
|
97
|
+
const field = useFieldControl({
|
|
98
|
+
activate: () => {
|
|
99
|
+
if (!isDisabled) toggle()
|
|
100
|
+
},
|
|
101
|
+
isChecked: isChecked || indeterminate,
|
|
102
|
+
})
|
|
103
|
+
const isDisabled = disabled ?? field.isDisabled
|
|
104
|
+
const isFieldInvalid = isInvalid ?? field.isInvalid
|
|
105
|
+
const isOn = isChecked || indeterminate
|
|
106
|
+
const state = indeterminate
|
|
107
|
+
? "indeterminate"
|
|
108
|
+
: isChecked
|
|
109
|
+
? "checked"
|
|
110
|
+
: "unchecked"
|
|
111
|
+
|
|
112
|
+
// The mark fades in from 0.6 on the feedback timing while its stroke draws
|
|
113
|
+
// on the move timing, both on the entrance curve; it shrinks and fades on
|
|
114
|
+
// the feedback exit. Indeterminate draws a minus.
|
|
115
|
+
const feedbackMotion = useMotion("feedback")
|
|
116
|
+
const drawMotion = useMotion("move")
|
|
117
|
+
const visible = useSharedValue(isOn ? 1 : 0)
|
|
118
|
+
const checkDraw = useSharedValue(isChecked && !indeterminate ? 1 : 0)
|
|
119
|
+
const minusDraw = useSharedValue(indeterminate ? 1 : 0)
|
|
120
|
+
React.useEffect(() => {
|
|
121
|
+
const exit = {
|
|
122
|
+
duration: feedbackMotion.exitMs,
|
|
123
|
+
easing: feedbackMotion.exitEasing,
|
|
124
|
+
}
|
|
125
|
+
// A drawn stroke stays while the mark fades out, then resets; between
|
|
126
|
+
// checked and indeterminate the mark stays, so the outgoing one retracts.
|
|
127
|
+
const draw = (isDrawn: boolean) => {
|
|
128
|
+
if (isDrawn) {
|
|
129
|
+
return withTiming(1, {
|
|
130
|
+
duration: drawMotion.enterMs,
|
|
131
|
+
easing: drawMotion.enterEasing,
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
if (isOn) return withTiming(0, exit)
|
|
135
|
+
return withDelay(exit.duration, withTiming(0, { duration: 0 }))
|
|
136
|
+
}
|
|
137
|
+
visible.set(
|
|
138
|
+
withTiming(
|
|
139
|
+
isOn ? 1 : 0,
|
|
140
|
+
isOn
|
|
141
|
+
? {
|
|
142
|
+
duration: feedbackMotion.enterMs,
|
|
143
|
+
easing: feedbackMotion.enterEasing,
|
|
144
|
+
}
|
|
145
|
+
: exit
|
|
146
|
+
)
|
|
147
|
+
)
|
|
148
|
+
checkDraw.set(draw(isChecked && !indeterminate))
|
|
149
|
+
minusDraw.set(draw(indeterminate))
|
|
150
|
+
}, [
|
|
151
|
+
checkDraw,
|
|
152
|
+
drawMotion,
|
|
153
|
+
feedbackMotion,
|
|
154
|
+
indeterminate,
|
|
155
|
+
isChecked,
|
|
156
|
+
isOn,
|
|
157
|
+
minusDraw,
|
|
158
|
+
visible,
|
|
159
|
+
])
|
|
160
|
+
const indicatorStyle = useAnimatedStyle(() => ({
|
|
161
|
+
opacity: visible.value,
|
|
162
|
+
transform: [{ scale: 0.6 + 0.4 * visible.value }],
|
|
163
|
+
}))
|
|
164
|
+
// Letting go pops the box back on the pop spring, as on the website.
|
|
165
|
+
const press = usePressScale({ releaseSpring: "pop" })
|
|
166
|
+
const isFlat = feedbackMotion.isReduced || feedbackMotion.isOff
|
|
167
|
+
const pressStyle = useAnimatedStyle(() => ({
|
|
168
|
+
transform: [{ scale: isFlat ? 1 : 1 - PRESS_GIVE * press.progress.value }],
|
|
169
|
+
}))
|
|
170
|
+
|
|
171
|
+
return (
|
|
172
|
+
<AnimatedPressable
|
|
173
|
+
data-slot="checkbox"
|
|
174
|
+
data-state={state}
|
|
175
|
+
data-invalid={Boolean(isFieldInvalid)}
|
|
176
|
+
role="checkbox"
|
|
177
|
+
aria-checked={indeterminate ? "mixed" : isChecked}
|
|
178
|
+
aria-disabled={isDisabled ?? undefined}
|
|
179
|
+
aria-invalid={isFieldInvalid}
|
|
180
|
+
disabled={isDisabled}
|
|
181
|
+
hitSlop={14}
|
|
182
|
+
className={cn(
|
|
183
|
+
"size-[calc(var(--density-icon-16)+2px)] rounded-(--radius-mark) border-[1.5px] border-foreground/25 bg-transparent data-[invalid=true]:border-destructive corner-curve shrink-0 items-center justify-center disabled:opacity-50",
|
|
184
|
+
className
|
|
185
|
+
)}
|
|
186
|
+
style={[pressStyle, style]}
|
|
187
|
+
onPress={(event) => {
|
|
188
|
+
toggle()
|
|
189
|
+
onPress?.(event)
|
|
190
|
+
}}
|
|
191
|
+
onPressIn={(event) => {
|
|
192
|
+
press.onPressIn()
|
|
193
|
+
onPressIn?.(event)
|
|
194
|
+
}}
|
|
195
|
+
onPressOut={(event) => {
|
|
196
|
+
press.onPressOut()
|
|
197
|
+
onPressOut?.(event)
|
|
198
|
+
}}
|
|
199
|
+
{...props}
|
|
200
|
+
>
|
|
201
|
+
<StateFill
|
|
202
|
+
isOn={isOn}
|
|
203
|
+
data-invalid={Boolean(isFieldInvalid)}
|
|
204
|
+
className={"fab:-inset-[1.5px] fab:rounded-(--radius-mark) fab:bg-primary fab:data-[invalid=true]:bg-destructive fab:corner-curve"}
|
|
205
|
+
/>
|
|
206
|
+
<Animated.View
|
|
207
|
+
data-slot="checkbox-indicator"
|
|
208
|
+
className={"fab:size-[calc(var(--density-icon-16)-4px)] fab:items-center fab:justify-center"}
|
|
209
|
+
style={indicatorStyle}
|
|
210
|
+
>
|
|
211
|
+
<Animated.View data-slot="checkbox-mark" className={"fab:size-full"}>
|
|
212
|
+
<Svg width="100%" height="100%" viewBox="0 0 24 24">
|
|
213
|
+
<MarkPath
|
|
214
|
+
{...CHECK}
|
|
215
|
+
progress={checkDraw}
|
|
216
|
+
className={checkboxCheckVariants()}
|
|
217
|
+
/>
|
|
218
|
+
<MarkPath
|
|
219
|
+
{...MINUS}
|
|
220
|
+
progress={minusDraw}
|
|
221
|
+
className={checkboxMinusVariants()}
|
|
222
|
+
/>
|
|
223
|
+
</Svg>
|
|
224
|
+
</Animated.View>
|
|
225
|
+
</Animated.View>
|
|
226
|
+
</AnimatedPressable>
|
|
227
|
+
)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
export { Checkbox }
|