@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
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { IconProvider } from "@fabricator-ui/icons-react-native"
|
|
3
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
4
|
+
import { cn } from "cn"
|
|
5
|
+
import { View } from "react-native"
|
|
6
|
+
import { useResolveClassNames } from "uniwind"
|
|
7
|
+
|
|
8
|
+
import { renderSlot, type RenderProps } from "../lib/render"
|
|
9
|
+
import { renderTextChildren, TextStyle } from "./text"
|
|
10
|
+
|
|
11
|
+
// Fabricator's badge: the status variants info, success, warning and error are
|
|
12
|
+
// tinted pills from the status surfaces. `self-start` is the website's `w-fit`:
|
|
13
|
+
// a view stretches across its column otherwise.
|
|
14
|
+
const badgeVariants = cva(
|
|
15
|
+
"h-(--density-24) gap-(--density-6) rounded-(--radius-control) border border-transparent px-(--density-10) data-[invalid=true]:border-destructive/50 corner-curve shrink-0 flex-row items-center justify-center self-start overflow-hidden",
|
|
16
|
+
{
|
|
17
|
+
variants: {
|
|
18
|
+
variant: {
|
|
19
|
+
default: "bg-active active:bg-foreground/10",
|
|
20
|
+
secondary: "bg-hover active:bg-active",
|
|
21
|
+
destructive: "bg-destructive-surface-1 dark:bg-destructive-surface-3 border-destructive-border active:bg-destructive-surface-2 dark:active:bg-destructive-surface-4",
|
|
22
|
+
outline: "border-border active:bg-hover",
|
|
23
|
+
ghost: "active:bg-hover",
|
|
24
|
+
link: "active:opacity-70",
|
|
25
|
+
info: "bg-info-surface-1 dark:bg-info-surface-3 border-info-border active:bg-info-surface-2 dark:active:bg-info-surface-4",
|
|
26
|
+
success: "bg-success-surface-1 dark:bg-success-surface-3 border-success-border active:bg-success-surface-2 dark:active:bg-success-surface-4",
|
|
27
|
+
warning: "bg-warning-surface-1 dark:bg-warning-surface-3 border-warning-border active:bg-warning-surface-2 dark:active:bg-warning-surface-4",
|
|
28
|
+
error: "bg-destructive-surface-1 dark:bg-destructive-surface-3 border-destructive-border active:bg-destructive-surface-2 dark:active:bg-destructive-surface-4",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
defaultVariants: {
|
|
32
|
+
variant: "default",
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
const badgeTextVariants = cva("text-(length:--density-text-12) font-medium", {
|
|
38
|
+
variants: {
|
|
39
|
+
variant: {
|
|
40
|
+
default: "text-foreground",
|
|
41
|
+
secondary: "text-foreground",
|
|
42
|
+
destructive: "text-destructive-text",
|
|
43
|
+
outline: "text-foreground",
|
|
44
|
+
ghost: "text-muted-foreground",
|
|
45
|
+
link: "text-primary-text underline",
|
|
46
|
+
info: "text-info-text",
|
|
47
|
+
success: "text-success-text",
|
|
48
|
+
warning: "text-warning-text",
|
|
49
|
+
error: "text-destructive-text",
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
// A status badge's icon takes the role's fill, not its text colour.
|
|
55
|
+
const badgeIconVariants = cva("", {
|
|
56
|
+
variants: {
|
|
57
|
+
variant: {
|
|
58
|
+
default: "text-foreground",
|
|
59
|
+
secondary: "text-foreground",
|
|
60
|
+
destructive: "text-destructive",
|
|
61
|
+
outline: "text-foreground",
|
|
62
|
+
ghost: "text-muted-foreground",
|
|
63
|
+
link: "text-primary-text",
|
|
64
|
+
info: "text-info",
|
|
65
|
+
success: "text-success",
|
|
66
|
+
warning: "text-warning",
|
|
67
|
+
error: "text-destructive",
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
function Badge({
|
|
73
|
+
className,
|
|
74
|
+
variant = "default",
|
|
75
|
+
render,
|
|
76
|
+
asChild,
|
|
77
|
+
children,
|
|
78
|
+
"aria-invalid": isInvalid,
|
|
79
|
+
...props
|
|
80
|
+
}: React.ComponentProps<typeof View> &
|
|
81
|
+
VariantProps<typeof badgeVariants> &
|
|
82
|
+
RenderProps & { "aria-invalid"?: boolean }) {
|
|
83
|
+
const { color } = useResolveClassNames(badgeIconVariants({ variant }))
|
|
84
|
+
const shared = {
|
|
85
|
+
"data-slot": "badge",
|
|
86
|
+
"data-variant": variant,
|
|
87
|
+
"data-invalid": Boolean(isInvalid),
|
|
88
|
+
"aria-invalid": isInvalid,
|
|
89
|
+
className: cn(badgeVariants({ variant }), className),
|
|
90
|
+
// Rendered as a link, it's a target too small to reach without help.
|
|
91
|
+
...(render || asChild ? { hitSlop: 8 } : {}),
|
|
92
|
+
...props,
|
|
93
|
+
}
|
|
94
|
+
const slotted = renderSlot(
|
|
95
|
+
{ render, asChild },
|
|
96
|
+
shared,
|
|
97
|
+
render ? renderTextChildren(children ?? render.props.children) : children
|
|
98
|
+
)
|
|
99
|
+
return (
|
|
100
|
+
<TextStyle className={badgeTextVariants({ variant })}>
|
|
101
|
+
<IconProvider size={12} {...(typeof color === "string" ? { color } : {})}>
|
|
102
|
+
{slotted ?? <View {...shared}>{renderTextChildren(children)}</View>}
|
|
103
|
+
</IconProvider>
|
|
104
|
+
</TextStyle>
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { Badge, badgeTextVariants, badgeVariants }
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { IconProvider } from "@fabricator-ui/icons-react-native"
|
|
3
|
+
import { cva, type VariantProps } from "class-variance-authority"
|
|
4
|
+
import { cn } from "cn"
|
|
5
|
+
import { Pressable, type GestureResponderEvent } from "react-native"
|
|
6
|
+
import Animated, { useAnimatedStyle } from "react-native-reanimated"
|
|
7
|
+
|
|
8
|
+
import { getTouchSlop } from "../lib/density-scale"
|
|
9
|
+
import { useFeedback, type FeedbackName } from "../lib/feedback"
|
|
10
|
+
import { useIconStroke } from "../lib/icon-stroke"
|
|
11
|
+
import { usePressScale } from "../lib/motion"
|
|
12
|
+
import { PressWash } from "../lib/press-wash"
|
|
13
|
+
import { renderSlot, type RenderProps } from "../lib/render"
|
|
14
|
+
import { DensityScope, useControlSize } from "./density"
|
|
15
|
+
import { renderTextChildren, TextStyle } from "./text"
|
|
16
|
+
|
|
17
|
+
const AnimatedPressable = Animated.createAnimatedComponent(Pressable)
|
|
18
|
+
|
|
19
|
+
const buttonVariants = cva(
|
|
20
|
+
"rounded-(--radius-control) border border-transparent corner-curve shrink-0 flex-row items-center justify-center disabled:opacity-50",
|
|
21
|
+
{
|
|
22
|
+
variants: {
|
|
23
|
+
variant: {
|
|
24
|
+
default: "bg-primary",
|
|
25
|
+
outline: "border-border bg-transparent",
|
|
26
|
+
secondary: "bg-secondary",
|
|
27
|
+
ghost: "bg-transparent",
|
|
28
|
+
destructive: "bg-destructive/12",
|
|
29
|
+
link: "bg-transparent active:opacity-70",
|
|
30
|
+
},
|
|
31
|
+
size: {
|
|
32
|
+
default: "h-(--density-36) gap-(--density-6) px-(--density-16)",
|
|
33
|
+
xs: "h-6 gap-1 px-2 md:h-7 md:gap-1.5 md:px-2.5",
|
|
34
|
+
sm: "h-(--density-36) gap-(--density-6) px-(--density-16)",
|
|
35
|
+
lg: "h-(--density-36) gap-(--density-6) px-(--density-16)",
|
|
36
|
+
icon: "size-(--density-36)",
|
|
37
|
+
"icon-xs": "size-6 md:size-7",
|
|
38
|
+
"icon-sm": "size-(--density-36)",
|
|
39
|
+
"icon-lg": "size-(--density-36)",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
defaultVariants: {
|
|
43
|
+
variant: "default",
|
|
44
|
+
size: "default",
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
const buttonTextVariants = cva("text-(length:--density-text-13) font-medium", {
|
|
50
|
+
variants: {
|
|
51
|
+
variant: {
|
|
52
|
+
default: "text-primary-foreground",
|
|
53
|
+
outline: "text-foreground",
|
|
54
|
+
secondary: "text-secondary-foreground",
|
|
55
|
+
ghost: "text-muted-foreground",
|
|
56
|
+
destructive: "text-destructive-text",
|
|
57
|
+
link: "text-primary-text underline",
|
|
58
|
+
},
|
|
59
|
+
size: {
|
|
60
|
+
default: "",
|
|
61
|
+
xs: "text-[12px] md:text-[13px]",
|
|
62
|
+
sm: "",
|
|
63
|
+
lg: "",
|
|
64
|
+
icon: "",
|
|
65
|
+
"icon-xs": "",
|
|
66
|
+
"icon-sm": "",
|
|
67
|
+
"icon-lg": "",
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
defaultVariants: {
|
|
71
|
+
variant: "default",
|
|
72
|
+
size: "default",
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
// Held, or with its popup open: a ghost button's label turns foreground.
|
|
77
|
+
const buttonLiftedTextVariants = cva("", {
|
|
78
|
+
variants: {
|
|
79
|
+
variant: {
|
|
80
|
+
default: "",
|
|
81
|
+
outline: "",
|
|
82
|
+
secondary: "",
|
|
83
|
+
ghost: "text-foreground",
|
|
84
|
+
destructive: "",
|
|
85
|
+
link: "",
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
defaultVariants: {
|
|
89
|
+
variant: "default",
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
// The pressed fill, a layer under the label that eases in and out.
|
|
94
|
+
const buttonWashVariants = cva("-inset-px rounded-(--radius-control) corner-curve", {
|
|
95
|
+
variants: {
|
|
96
|
+
variant: {
|
|
97
|
+
default: "bg-background/20",
|
|
98
|
+
outline: "bg-active",
|
|
99
|
+
secondary: "bg-background/30",
|
|
100
|
+
ghost: "bg-active",
|
|
101
|
+
destructive: "bg-destructive/8",
|
|
102
|
+
link: "bg-transparent",
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
defaultVariants: {
|
|
106
|
+
variant: "default",
|
|
107
|
+
},
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
// A joined button squares the corners it shares, and its wash follows them.
|
|
111
|
+
const WASH_CORNERS: Partial<Record<string, ReturnType<typeof cva>>> = {
|
|
112
|
+
"horizontal-first": cva("rounded-r-none"),
|
|
113
|
+
"horizontal-middle": cva("rounded-none"),
|
|
114
|
+
"horizontal-last": cva("rounded-l-none"),
|
|
115
|
+
"vertical-first": cva("rounded-b-none"),
|
|
116
|
+
"vertical-middle": cva("rounded-none"),
|
|
117
|
+
"vertical-last": cva("rounded-t-none"),
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
type ButtonSize = NonNullable<VariantProps<typeof buttonVariants>["size"]>
|
|
121
|
+
type ButtonVariant = VariantProps<typeof buttonVariants>["variant"]
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Joined controls stay flat when pressed, as the website's `motion-press` has
|
|
125
|
+
* them inside a button group or an input group. The pressed wash still shows.
|
|
126
|
+
*/
|
|
127
|
+
const ButtonJoinedContext = React.createContext(false)
|
|
128
|
+
|
|
129
|
+
// Which step of the scale each size takes: the icon sizes match their text sizes.
|
|
130
|
+
const SIZE_STEP: Partial<Record<ButtonSize, "sm" | "lg">> = {
|
|
131
|
+
sm: "sm",
|
|
132
|
+
"icon-sm": "sm",
|
|
133
|
+
lg: "lg",
|
|
134
|
+
"icon-lg": "lg",
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Heavier for the primary action, a damped thud for a destructive one, the
|
|
138
|
+
// lightest sound for a link.
|
|
139
|
+
function getButtonFeedback(variant: ButtonVariant) {
|
|
140
|
+
if (variant === "default") return "press"
|
|
141
|
+
if (variant === "destructive") return "thud"
|
|
142
|
+
if (variant === "link") return "tick"
|
|
143
|
+
return "tap"
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Smaller buttons sound a little higher, larger ones a little lower.
|
|
147
|
+
const SIZE_PITCH: Partial<Record<string, number>> = {
|
|
148
|
+
xs: 1.12,
|
|
149
|
+
"icon-xs": 1.12,
|
|
150
|
+
sm: 1.06,
|
|
151
|
+
"icon-sm": 1.06,
|
|
152
|
+
lg: 0.92,
|
|
153
|
+
"icon-lg": 0.92,
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function getHitSlop(slop: number, groupItem: string | undefined) {
|
|
157
|
+
if (!slop || !groupItem) return slop
|
|
158
|
+
// Joined buttons touch: reaching along the group would take a neighbour's press.
|
|
159
|
+
return groupItem.startsWith("horizontal")
|
|
160
|
+
? { top: slop, bottom: slop }
|
|
161
|
+
: { left: slop, right: slop }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function Button({
|
|
165
|
+
className,
|
|
166
|
+
variant = "default",
|
|
167
|
+
size = "default",
|
|
168
|
+
render,
|
|
169
|
+
asChild,
|
|
170
|
+
children,
|
|
171
|
+
onPress,
|
|
172
|
+
onPressIn,
|
|
173
|
+
onPressOut,
|
|
174
|
+
style,
|
|
175
|
+
hitSlop,
|
|
176
|
+
washClassName,
|
|
177
|
+
"data-sound": sound,
|
|
178
|
+
...props
|
|
179
|
+
}: React.ComponentProps<typeof Pressable> &
|
|
180
|
+
VariantProps<typeof buttonVariants> &
|
|
181
|
+
RenderProps & {
|
|
182
|
+
className?: string
|
|
183
|
+
children?: React.ReactNode
|
|
184
|
+
/** For a button drawn with other corners, so the pressed wash matches them. */
|
|
185
|
+
washClassName?: string
|
|
186
|
+
/** Another sound in place of the button's, or `none`, as on the website. */
|
|
187
|
+
"data-sound"?: FeedbackName | "none"
|
|
188
|
+
/** Set by ButtonGroup: where this button sits, joined to its neighbours. */
|
|
189
|
+
"data-group-item"?: string
|
|
190
|
+
/** The size a part draws itself, when it isn't `size`: it sets the pitch. */
|
|
191
|
+
"data-size"?: string | null
|
|
192
|
+
}) {
|
|
193
|
+
const isExpanded = props["aria-expanded"] === true
|
|
194
|
+
const press = usePressScale({ isHeld: isExpanded })
|
|
195
|
+
const [isPressedIn, setIsPressedIn] = React.useState(false)
|
|
196
|
+
const isLifted = isExpanded || isPressedIn
|
|
197
|
+
const feedback = useFeedback()
|
|
198
|
+
const isJoined = React.use(ButtonJoinedContext)
|
|
199
|
+
const pressStyle = useAnimatedStyle(() => ({
|
|
200
|
+
transform: [{ scale: isJoined ? 1 : press.scale.value }],
|
|
201
|
+
}))
|
|
202
|
+
const resolvedSize = size ?? "default"
|
|
203
|
+
const groupItem = props["data-group-item"]
|
|
204
|
+
const control = useControlSize(SIZE_STEP[resolvedSize])
|
|
205
|
+
const isXs = resolvedSize === "xs" || resolvedSize === "icon-xs"
|
|
206
|
+
const shared = {
|
|
207
|
+
"data-slot": "button",
|
|
208
|
+
role: "button" as const,
|
|
209
|
+
accessibilityState: { disabled: Boolean(props.disabled) },
|
|
210
|
+
hitSlop:
|
|
211
|
+
hitSlop ??
|
|
212
|
+
getHitSlop(
|
|
213
|
+
getTouchSlop(isXs ? control.xs.height : control.height),
|
|
214
|
+
groupItem
|
|
215
|
+
),
|
|
216
|
+
className: cn(buttonVariants({ variant, size, className })),
|
|
217
|
+
onPress: (event: GestureResponderEvent) => {
|
|
218
|
+
// Still focusable while disabled, as on the website: it says no instead.
|
|
219
|
+
if (props["aria-disabled"]) {
|
|
220
|
+
feedback("deny")
|
|
221
|
+
return
|
|
222
|
+
}
|
|
223
|
+
if (sound !== "none") {
|
|
224
|
+
feedback(sound ?? getButtonFeedback(variant), {
|
|
225
|
+
pitch: SIZE_PITCH[props["data-size"] ?? resolvedSize],
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
onPress?.(event)
|
|
229
|
+
},
|
|
230
|
+
...props,
|
|
231
|
+
}
|
|
232
|
+
// `render` takes the label as its children, and a bare string can't be drawn.
|
|
233
|
+
const slotted = renderSlot(
|
|
234
|
+
{ render, asChild },
|
|
235
|
+
shared,
|
|
236
|
+
render && children !== undefined ? renderTextChildren(children) : children
|
|
237
|
+
)
|
|
238
|
+
return (
|
|
239
|
+
<DensityScope density={control.density}>
|
|
240
|
+
<TextStyle
|
|
241
|
+
// Changing this wrapper on a variant switch remounts the press target
|
|
242
|
+
// before its delayed release, leaving the pressed wash held.
|
|
243
|
+
eases
|
|
244
|
+
className={cn(
|
|
245
|
+
buttonTextVariants({ variant, size }),
|
|
246
|
+
isLifted ? buttonLiftedTextVariants({ variant }) : undefined
|
|
247
|
+
)}
|
|
248
|
+
>
|
|
249
|
+
<IconProvider
|
|
250
|
+
strokeWidth={useIconStroke(isLifted)}
|
|
251
|
+
{...(isXs ? { size: control.xs.icon } : {})}
|
|
252
|
+
>
|
|
253
|
+
{slotted ?? (
|
|
254
|
+
<AnimatedPressable
|
|
255
|
+
{...shared}
|
|
256
|
+
style={[pressStyle, style]}
|
|
257
|
+
onPressIn={(event) => {
|
|
258
|
+
press.onPressIn()
|
|
259
|
+
setIsPressedIn(true)
|
|
260
|
+
onPressIn?.(event)
|
|
261
|
+
}}
|
|
262
|
+
onPressOut={(event) => {
|
|
263
|
+
press.onPressOut()
|
|
264
|
+
setIsPressedIn(false)
|
|
265
|
+
onPressOut?.(event)
|
|
266
|
+
}}
|
|
267
|
+
>
|
|
268
|
+
<PressWash
|
|
269
|
+
press={press}
|
|
270
|
+
className={cn(
|
|
271
|
+
buttonWashVariants({ variant }),
|
|
272
|
+
groupItem ? WASH_CORNERS[groupItem]?.() : undefined,
|
|
273
|
+
washClassName
|
|
274
|
+
)}
|
|
275
|
+
/>
|
|
276
|
+
{renderTextChildren(children)}
|
|
277
|
+
</AnimatedPressable>
|
|
278
|
+
)}
|
|
279
|
+
</IconProvider>
|
|
280
|
+
</TextStyle>
|
|
281
|
+
</DensityScope>
|
|
282
|
+
)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export { Button, ButtonJoinedContext, buttonTextVariants, buttonVariants }
|