@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,566 @@
|
|
|
1
|
+
// Fabricator motion: what moves, how fast, and on which curve.
|
|
2
|
+
//
|
|
3
|
+
// Every animation belongs to a role (feedback, move, tooltip, popup, …) and
|
|
4
|
+
// reads that role's tokens, never milliseconds. Four speeds set the tokens,
|
|
5
|
+
// from smooth (marketing sites) to instant (dense dashboards). Default is the
|
|
6
|
+
// one in :root; `data-motion="<speed>"` on <html> or on any element switches
|
|
7
|
+
// everything inside it, and `data-motion="off"` stops it. `--motion-scale`
|
|
8
|
+
// still multiplies every duration on top.
|
|
9
|
+
//
|
|
10
|
+
// The default speed follows Emil Kowalski's guidance: strong ease-out for
|
|
11
|
+
// anything that enters or leaves, ease-in-out for things that move on screen,
|
|
12
|
+
// exits quicker than entrances, popups that grow from their trigger, a 0.97
|
|
13
|
+
// press, tooltips that skip their animation once one is open, and nothing
|
|
14
|
+
// under 0.9 scale.
|
|
15
|
+
|
|
16
|
+
import type { CssObject } from "./css-object"
|
|
17
|
+
import {
|
|
18
|
+
getCriticalSpring,
|
|
19
|
+
retimeSpring,
|
|
20
|
+
sampleSpring,
|
|
21
|
+
type SpringConfig,
|
|
22
|
+
} from "../spring-model"
|
|
23
|
+
|
|
24
|
+
export const MOTION_ROLES = [
|
|
25
|
+
// Hover, colour, focus, icon stroke and weight changes.
|
|
26
|
+
"feedback",
|
|
27
|
+
// The Fluid Hover highlight chasing the pointer.
|
|
28
|
+
"glide",
|
|
29
|
+
// Something travelling or resizing on screen: tab indicators, switch
|
|
30
|
+
// thumbs, selection backgrounds, checkmarks, progress, the sidebar.
|
|
31
|
+
"move",
|
|
32
|
+
// A full-viewport theme reveal, slower than a control moving.
|
|
33
|
+
"theme",
|
|
34
|
+
"tooltip",
|
|
35
|
+
// Menus, selects, comboboxes, popovers, hover cards, navigation menus.
|
|
36
|
+
"popup",
|
|
37
|
+
// Dialogs, alert dialogs and their scrims.
|
|
38
|
+
"dialog",
|
|
39
|
+
// Sheets and drawers.
|
|
40
|
+
"sheet",
|
|
41
|
+
// Accordion and collapsible panels.
|
|
42
|
+
"disclosure",
|
|
43
|
+
// Layout that springs into place: accordion items opening and parting.
|
|
44
|
+
"bounce",
|
|
45
|
+
// Layout that follows the pointer and springs into place without a visible
|
|
46
|
+
// overshoot: an avatar group parting around the avatar under it.
|
|
47
|
+
"settle",
|
|
48
|
+
"toast",
|
|
49
|
+
// A chart drawing its marks the first time it shows: lines sweeping in,
|
|
50
|
+
// bars growing from the baseline; Text Motion's pieces playing in. Slower
|
|
51
|
+
// than anything else that moves, since the eye follows the data or the
|
|
52
|
+
// words being laid out; none when instant.
|
|
53
|
+
"reveal",
|
|
54
|
+
] as const
|
|
55
|
+
|
|
56
|
+
export type MotionRole = (typeof MOTION_ROLES)[number]
|
|
57
|
+
|
|
58
|
+
export const MOTION_SPEED_NAMES = [
|
|
59
|
+
"smooth",
|
|
60
|
+
"default",
|
|
61
|
+
"snappy",
|
|
62
|
+
"instant",
|
|
63
|
+
] as const
|
|
64
|
+
|
|
65
|
+
export type MotionSpeedName = (typeof MOTION_SPEED_NAMES)[number]
|
|
66
|
+
|
|
67
|
+
export const DEFAULT_MOTION_SPEED: MotionSpeedName = "default"
|
|
68
|
+
|
|
69
|
+
// Springs for what moves under the pointer or can be interrupted: they keep
|
|
70
|
+
// their velocity when the target changes, so motion bends instead of
|
|
71
|
+
// restarting. Named by job, as "stiffness damping" with mass 1. The default
|
|
72
|
+
// speed's values come from the Motion Studies at designbookmark.com/motion.
|
|
73
|
+
export const MOTION_SPRING_NAMES = [
|
|
74
|
+
// Something held by the pointer: no overshoot, no lag.
|
|
75
|
+
"follow",
|
|
76
|
+
// Letting go, landing on a value: one small overshoot.
|
|
77
|
+
"settle",
|
|
78
|
+
// Large moves: panels, sheets, a whole component shifting.
|
|
79
|
+
"soft",
|
|
80
|
+
// A stretchy highlight's edge that faces the move.
|
|
81
|
+
"lead",
|
|
82
|
+
// The edge that lags behind it, which makes the stretch.
|
|
83
|
+
"trail",
|
|
84
|
+
// An icon, chip or number popping on select.
|
|
85
|
+
"pop",
|
|
86
|
+
// Opacity and small slides.
|
|
87
|
+
"fade",
|
|
88
|
+
] as const
|
|
89
|
+
|
|
90
|
+
export type MotionSpringName = (typeof MOTION_SPRING_NAMES)[number]
|
|
91
|
+
|
|
92
|
+
const DEFAULT_SPRINGS: Record<MotionSpringName, SpringConfig> = {
|
|
93
|
+
follow: { stiffness: 900, damping: 50 },
|
|
94
|
+
settle: { stiffness: 280, damping: 22 },
|
|
95
|
+
soft: { stiffness: 260, damping: 24 },
|
|
96
|
+
lead: { stiffness: 620, damping: 36 },
|
|
97
|
+
trail: { stiffness: 190, damping: 19 },
|
|
98
|
+
pop: { stiffness: 520, damping: 16 },
|
|
99
|
+
fade: { stiffness: 420, damping: 32 },
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// A speed plays the default springs `timeScale` times as long, with the same
|
|
103
|
+
// overshoot; `isCritical` takes the overshoot out (instant).
|
|
104
|
+
function getSpeedSprings(timeScale: number, isCritical = false) {
|
|
105
|
+
return Object.fromEntries(
|
|
106
|
+
MOTION_SPRING_NAMES.map((name) => {
|
|
107
|
+
const spring = retimeSpring(DEFAULT_SPRINGS[name], timeScale)
|
|
108
|
+
const rounded = {
|
|
109
|
+
stiffness: Math.round(spring.stiffness),
|
|
110
|
+
damping: Math.round(spring.damping * 10) / 10,
|
|
111
|
+
}
|
|
112
|
+
if (!isCritical) return [name, rounded]
|
|
113
|
+
const critical = getCriticalSpring(rounded)
|
|
114
|
+
return [
|
|
115
|
+
name,
|
|
116
|
+
{ ...rounded, damping: Math.round(critical.damping * 10) / 10 },
|
|
117
|
+
]
|
|
118
|
+
})
|
|
119
|
+
) as Record<MotionSpringName, SpringConfig>
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
type Timing = { enter: number; exit: number }
|
|
123
|
+
|
|
124
|
+
export type MotionSpeed = {
|
|
125
|
+
/** Milliseconds per role. */
|
|
126
|
+
durations: Record<MotionRole, Timing>
|
|
127
|
+
easings: {
|
|
128
|
+
/** Entering, and the Fluid Hover glide. */
|
|
129
|
+
enter: string
|
|
130
|
+
/** Moving on screen. */
|
|
131
|
+
move: string
|
|
132
|
+
exit: string
|
|
133
|
+
/** Hover and colour changes. */
|
|
134
|
+
feedback: string
|
|
135
|
+
/** Sheets and drawers. */
|
|
136
|
+
drawer: string
|
|
137
|
+
/** Full-page theme reveals. */
|
|
138
|
+
theme: string
|
|
139
|
+
}
|
|
140
|
+
/** The scale a surface grows from and shrinks to; press is while held. */
|
|
141
|
+
scales: { press: number; tooltip: number; popup: number; dialog: number }
|
|
142
|
+
/** How far tooltips and popups slide from their trigger, in px. */
|
|
143
|
+
travel: { tooltip: number; popup: number }
|
|
144
|
+
/** Blur popups fade through, in px. */
|
|
145
|
+
popupBlur: number
|
|
146
|
+
/** How far the bounce role's spring overshoots: 0 settles without it. */
|
|
147
|
+
bounce: number
|
|
148
|
+
/** The settle role's spring: low enough that it never visibly overshoots. */
|
|
149
|
+
settle: number
|
|
150
|
+
/** Springs by job, for motion that follows the pointer or is interrupted. */
|
|
151
|
+
springs: Record<MotionSpringName, SpringConfig>
|
|
152
|
+
/** Milliseconds between items entering one after another. */
|
|
153
|
+
stagger: number
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const EASE_OUT = "cubic-bezier(0.23, 1, 0.32, 1)"
|
|
157
|
+
const EASE_OUT_EXPO = "cubic-bezier(0.16, 1, 0.3, 1)"
|
|
158
|
+
const EASE_IN_OUT = "cubic-bezier(0.77, 0, 0.175, 1)"
|
|
159
|
+
const EASE_IN_OUT_SOFT = "cubic-bezier(0.65, 0, 0.35, 1)"
|
|
160
|
+
const EASE_THEME = "cubic-bezier(0.4, 0, 0.2, 1)"
|
|
161
|
+
const EASE = "cubic-bezier(0.25, 0.1, 0.25, 1)"
|
|
162
|
+
// The iOS sheet curve, as in Vaul.
|
|
163
|
+
const EASE_DRAWER = "cubic-bezier(0.32, 0.72, 0, 1)"
|
|
164
|
+
|
|
165
|
+
export const MOTION_SPEEDS: Record<MotionSpeedName, MotionSpeed> = {
|
|
166
|
+
// Marketing sites and editorial pages: longer, softer, a little travel and
|
|
167
|
+
// blur, so surfaces settle instead of snapping.
|
|
168
|
+
smooth: {
|
|
169
|
+
durations: {
|
|
170
|
+
feedback: { enter: 220, exit: 160 },
|
|
171
|
+
glide: { enter: 220, exit: 160 },
|
|
172
|
+
theme: { enter: 900, exit: 900 },
|
|
173
|
+
move: { enter: 360, exit: 260 },
|
|
174
|
+
tooltip: { enter: 200, exit: 140 },
|
|
175
|
+
popup: { enter: 280, exit: 180 },
|
|
176
|
+
dialog: { enter: 380, exit: 240 },
|
|
177
|
+
sheet: { enter: 500, exit: 320 },
|
|
178
|
+
disclosure: { enter: 320, exit: 240 },
|
|
179
|
+
bounce: { enter: 700, exit: 560 },
|
|
180
|
+
settle: { enter: 460, exit: 460 },
|
|
181
|
+
toast: { enter: 450, exit: 300 },
|
|
182
|
+
reveal: { enter: 1000, exit: 600 },
|
|
183
|
+
},
|
|
184
|
+
easings: {
|
|
185
|
+
enter: EASE_OUT_EXPO,
|
|
186
|
+
move: EASE_IN_OUT_SOFT,
|
|
187
|
+
exit: EASE_OUT,
|
|
188
|
+
feedback: EASE,
|
|
189
|
+
drawer: EASE_DRAWER,
|
|
190
|
+
theme: EASE_THEME,
|
|
191
|
+
},
|
|
192
|
+
scales: { press: 0.97, tooltip: 0.96, popup: 0.94, dialog: 0.94 },
|
|
193
|
+
travel: { tooltip: 4, popup: 6 },
|
|
194
|
+
popupBlur: 2,
|
|
195
|
+
bounce: 0.3,
|
|
196
|
+
settle: 0.16,
|
|
197
|
+
springs: getSpeedSprings(1.3),
|
|
198
|
+
stagger: 55,
|
|
199
|
+
},
|
|
200
|
+
// Emil Kowalski's timings: tooltips 125ms, popups 180ms, dialogs 250ms.
|
|
201
|
+
default: {
|
|
202
|
+
durations: {
|
|
203
|
+
feedback: { enter: 150, exit: 100 },
|
|
204
|
+
glide: { enter: 150, exit: 100 },
|
|
205
|
+
theme: { enter: 700, exit: 700 },
|
|
206
|
+
move: { enter: 250, exit: 180 },
|
|
207
|
+
tooltip: { enter: 125, exit: 90 },
|
|
208
|
+
popup: { enter: 180, exit: 120 },
|
|
209
|
+
dialog: { enter: 250, exit: 180 },
|
|
210
|
+
sheet: { enter: 400, exit: 250 },
|
|
211
|
+
disclosure: { enter: 200, exit: 150 },
|
|
212
|
+
bounce: { enter: 600, exit: 480 },
|
|
213
|
+
settle: { enter: 380, exit: 380 },
|
|
214
|
+
toast: { enter: 350, exit: 220 },
|
|
215
|
+
reveal: { enter: 700, exit: 450 },
|
|
216
|
+
},
|
|
217
|
+
easings: {
|
|
218
|
+
enter: EASE_OUT,
|
|
219
|
+
move: EASE_IN_OUT,
|
|
220
|
+
exit: EASE_OUT,
|
|
221
|
+
feedback: EASE,
|
|
222
|
+
drawer: EASE_DRAWER,
|
|
223
|
+
theme: EASE_THEME,
|
|
224
|
+
},
|
|
225
|
+
scales: { press: 0.97, tooltip: 0.97, popup: 0.95, dialog: 0.95 },
|
|
226
|
+
travel: { tooltip: 0, popup: 0 },
|
|
227
|
+
popupBlur: 0,
|
|
228
|
+
bounce: 0.35,
|
|
229
|
+
// A damping ratio of 0.84, as in beUI's avatar group.
|
|
230
|
+
settle: 0.16,
|
|
231
|
+
springs: getSpeedSprings(1),
|
|
232
|
+
stagger: 45,
|
|
233
|
+
},
|
|
234
|
+
// Apps people use all day: the same shapes, about a quarter quicker and
|
|
235
|
+
// smaller.
|
|
236
|
+
snappy: {
|
|
237
|
+
durations: {
|
|
238
|
+
feedback: { enter: 100, exit: 70 },
|
|
239
|
+
glide: { enter: 110, exit: 80 },
|
|
240
|
+
theme: { enter: 500, exit: 500 },
|
|
241
|
+
move: { enter: 180, exit: 130 },
|
|
242
|
+
tooltip: { enter: 100, exit: 70 },
|
|
243
|
+
popup: { enter: 140, exit: 90 },
|
|
244
|
+
dialog: { enter: 180, exit: 120 },
|
|
245
|
+
sheet: { enter: 300, exit: 200 },
|
|
246
|
+
disclosure: { enter: 160, exit: 120 },
|
|
247
|
+
bounce: { enter: 450, exit: 360 },
|
|
248
|
+
settle: { enter: 300, exit: 300 },
|
|
249
|
+
toast: { enter: 260, exit: 180 },
|
|
250
|
+
reveal: { enter: 480, exit: 320 },
|
|
251
|
+
},
|
|
252
|
+
easings: {
|
|
253
|
+
enter: EASE_OUT,
|
|
254
|
+
move: EASE_IN_OUT,
|
|
255
|
+
exit: EASE_OUT,
|
|
256
|
+
feedback: EASE,
|
|
257
|
+
drawer: EASE_DRAWER,
|
|
258
|
+
theme: EASE_THEME,
|
|
259
|
+
},
|
|
260
|
+
scales: { press: 0.98, tooltip: 0.98, popup: 0.97, dialog: 0.97 },
|
|
261
|
+
travel: { tooltip: 0, popup: 0 },
|
|
262
|
+
popupBlur: 0,
|
|
263
|
+
bounce: 0.25,
|
|
264
|
+
settle: 0.12,
|
|
265
|
+
springs: getSpeedSprings(0.75),
|
|
266
|
+
stagger: 35,
|
|
267
|
+
},
|
|
268
|
+
// Dense dashboards and power tools: fades only, no scale or press, tooltips
|
|
269
|
+
// appear at once, and moves end on ease-out so nothing waits to start.
|
|
270
|
+
instant: {
|
|
271
|
+
durations: {
|
|
272
|
+
feedback: { enter: 50, exit: 40 },
|
|
273
|
+
glide: { enter: 70, exit: 50 },
|
|
274
|
+
theme: { enter: 200, exit: 200 },
|
|
275
|
+
move: { enter: 120, exit: 90 },
|
|
276
|
+
tooltip: { enter: 0, exit: 0 },
|
|
277
|
+
popup: { enter: 80, exit: 60 },
|
|
278
|
+
dialog: { enter: 100, exit: 80 },
|
|
279
|
+
sheet: { enter: 200, exit: 150 },
|
|
280
|
+
disclosure: { enter: 100, exit: 80 },
|
|
281
|
+
bounce: { enter: 120, exit: 90 },
|
|
282
|
+
settle: { enter: 120, exit: 90 },
|
|
283
|
+
toast: { enter: 180, exit: 120 },
|
|
284
|
+
reveal: { enter: 0, exit: 0 },
|
|
285
|
+
},
|
|
286
|
+
easings: {
|
|
287
|
+
enter: EASE_OUT,
|
|
288
|
+
move: EASE_OUT,
|
|
289
|
+
exit: EASE_OUT,
|
|
290
|
+
feedback: EASE,
|
|
291
|
+
drawer: EASE_DRAWER,
|
|
292
|
+
theme: EASE_THEME,
|
|
293
|
+
},
|
|
294
|
+
scales: { press: 1, tooltip: 1, popup: 1, dialog: 1 },
|
|
295
|
+
travel: { tooltip: 0, popup: 0 },
|
|
296
|
+
popupBlur: 0,
|
|
297
|
+
bounce: 0,
|
|
298
|
+
settle: 0,
|
|
299
|
+
springs: getSpeedSprings(0.6, true),
|
|
300
|
+
stagger: 0,
|
|
301
|
+
},
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// The spring's envelope falls to e^-SETTLE by the end of the curve.
|
|
305
|
+
const SPRING_SETTLE = 6
|
|
306
|
+
const SPRING_SAMPLES = 60
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* A spring released from 0 towards 1, sampled evenly over its duration: an
|
|
310
|
+
* underdamped oscillator whose damping ratio is `1 - bounce`, stiff enough to
|
|
311
|
+
* settle by the end. The website draws it with CSS `linear()`, native
|
|
312
|
+
* interpolates the same samples, so both bounce alike.
|
|
313
|
+
*/
|
|
314
|
+
export function getSpringSamples(bounce: number, count = SPRING_SAMPLES) {
|
|
315
|
+
const damping = 1 - bounce
|
|
316
|
+
const frequency = SPRING_SETTLE / damping
|
|
317
|
+
const decay = damping * frequency
|
|
318
|
+
const damped = frequency * Math.sqrt(1 - damping ** 2)
|
|
319
|
+
const at = (time: number) => {
|
|
320
|
+
const envelope = Math.exp(-decay * time)
|
|
321
|
+
if (damped === 0) return 1 - envelope * (1 + frequency * time)
|
|
322
|
+
return (
|
|
323
|
+
1 -
|
|
324
|
+
envelope *
|
|
325
|
+
(Math.cos(damped * time) + (decay / damped) * Math.sin(damped * time))
|
|
326
|
+
)
|
|
327
|
+
}
|
|
328
|
+
return Array.from({ length: count + 1 }, (_, index) => {
|
|
329
|
+
if (index === count) return 1
|
|
330
|
+
return Math.round(at(index / count) * 1000) / 1000
|
|
331
|
+
})
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** The spring as a CSS easing. */
|
|
335
|
+
export function toSpringEasing(bounce: number) {
|
|
336
|
+
return `linear(${getSpringSamples(bounce).join(", ")})`
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const toMs = (value: number) => `${value}ms`
|
|
340
|
+
const toPx = (value: number) => `${value}px`
|
|
341
|
+
|
|
342
|
+
/** A speed's tokens, keyed by name without the `--`. */
|
|
343
|
+
export function toMotionVars(speed: MotionSpeed): Record<string, string> {
|
|
344
|
+
return {
|
|
345
|
+
...Object.fromEntries(
|
|
346
|
+
MOTION_ROLES.flatMap((role) => [
|
|
347
|
+
[`motion-${role}`, toMs(speed.durations[role].enter)],
|
|
348
|
+
[`motion-${role}-exit`, toMs(speed.durations[role].exit)],
|
|
349
|
+
])
|
|
350
|
+
),
|
|
351
|
+
"motion-ease-enter": speed.easings.enter,
|
|
352
|
+
"motion-ease-move": speed.easings.move,
|
|
353
|
+
"motion-ease-exit": speed.easings.exit,
|
|
354
|
+
"motion-ease-feedback": speed.easings.feedback,
|
|
355
|
+
"motion-ease-drawer": speed.easings.drawer,
|
|
356
|
+
"motion-ease-theme": speed.easings.theme,
|
|
357
|
+
"motion-ease-bounce": toSpringEasing(speed.bounce),
|
|
358
|
+
"motion-ease-settle": toSpringEasing(speed.settle),
|
|
359
|
+
"motion-press-scale": String(speed.scales.press),
|
|
360
|
+
"motion-tooltip-scale": String(speed.scales.tooltip),
|
|
361
|
+
"motion-popup-scale": String(speed.scales.popup),
|
|
362
|
+
"motion-dialog-scale": String(speed.scales.dialog),
|
|
363
|
+
"motion-tooltip-travel": toPx(speed.travel.tooltip),
|
|
364
|
+
"motion-popup-travel": toPx(speed.travel.popup),
|
|
365
|
+
"motion-popup-blur": toPx(speed.popupBlur),
|
|
366
|
+
...Object.fromEntries(
|
|
367
|
+
MOTION_SPRING_NAMES.flatMap((name) => {
|
|
368
|
+
const spring = speed.springs[name]
|
|
369
|
+
const { duration, samples } = sampleSpring(spring)
|
|
370
|
+
return [
|
|
371
|
+
[`motion-spring-${name}`, `${spring.stiffness} ${spring.damping}`],
|
|
372
|
+
// The same spring for CSS: the time it takes to settle and its
|
|
373
|
+
// curve, for transitions that only start and land.
|
|
374
|
+
[`motion-spring-${name}-duration`, toMs(duration)],
|
|
375
|
+
[`motion-ease-${name}-spring`, `linear(${samples.join(", ")})`],
|
|
376
|
+
]
|
|
377
|
+
})
|
|
378
|
+
),
|
|
379
|
+
"motion-stagger": toMs(speed.stagger),
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** A role's duration, multiplied by --motion-scale where it is used. */
|
|
384
|
+
const scaled = (token: string) =>
|
|
385
|
+
`calc(var(--motion-${token}) * var(--motion-scale, 1))`
|
|
386
|
+
|
|
387
|
+
// The old three tiers, kept so code written against them still works.
|
|
388
|
+
const TIER_ALIASES: Record<string, MotionRole> = {
|
|
389
|
+
fast: "feedback",
|
|
390
|
+
moderate: "move",
|
|
391
|
+
slow: "dialog",
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const durationKeys = (name: string, role: MotionRole) => [
|
|
395
|
+
[`transition-duration-${name}`, scaled(role)],
|
|
396
|
+
[`transition-duration-${name}-exit`, scaled(`${role}-exit`)],
|
|
397
|
+
[`transition-delay-${name}`, scaled(role)],
|
|
398
|
+
]
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Theme keys: `duration-<role>`, `duration-<role>-exit`, `delay-<role>` and
|
|
402
|
+
* the `ease-enter|move|exit|feedback|drawer|theme` curves. They resolve where they
|
|
403
|
+
* are used, so a speed set on a subtree applies inside it.
|
|
404
|
+
*/
|
|
405
|
+
export const MOTION_THEME: Record<string, string> = {
|
|
406
|
+
...Object.fromEntries(
|
|
407
|
+
MOTION_ROLES.flatMap((role) => durationKeys(role, role))
|
|
408
|
+
),
|
|
409
|
+
...Object.fromEntries(
|
|
410
|
+
Object.entries(TIER_ALIASES).flatMap(([tier, role]) =>
|
|
411
|
+
durationKeys(tier, role)
|
|
412
|
+
)
|
|
413
|
+
),
|
|
414
|
+
"ease-enter": "var(--motion-ease-enter)",
|
|
415
|
+
"ease-move": "var(--motion-ease-move)",
|
|
416
|
+
"ease-exit": "var(--motion-ease-exit)",
|
|
417
|
+
"ease-feedback": "var(--motion-ease-feedback)",
|
|
418
|
+
"ease-drawer": "var(--motion-ease-drawer)",
|
|
419
|
+
"ease-theme": "var(--motion-ease-theme)",
|
|
420
|
+
"ease-bounce": "var(--motion-ease-bounce)",
|
|
421
|
+
// Springs as CSS transitions: `duration-pop-spring ease-pop-spring`.
|
|
422
|
+
...Object.fromEntries(
|
|
423
|
+
MOTION_SPRING_NAMES.flatMap((name) => [
|
|
424
|
+
[`transition-duration-${name}-spring`, scaled(`spring-${name}-duration`)],
|
|
425
|
+
[`ease-${name}-spring`, `var(--motion-ease-${name}-spring)`],
|
|
426
|
+
])
|
|
427
|
+
),
|
|
428
|
+
"ease-settle": "var(--motion-ease-settle)",
|
|
429
|
+
// The old spring curves, now the entrance curve.
|
|
430
|
+
"ease-spring": "var(--motion-ease-enter)",
|
|
431
|
+
"ease-spring-bounce": "var(--motion-ease-enter)",
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// A surface is leaving while it has data-closed (Base UI), data-state=closed
|
|
435
|
+
// (Radix), data-exiting (React Aria) or data-ending-style (Base UI
|
|
436
|
+
// transitions).
|
|
437
|
+
const LEAVING =
|
|
438
|
+
'&:where([data-closed], [data-state="closed"], [data-exiting], [data-ending-style])'
|
|
439
|
+
|
|
440
|
+
const KEEPS_EASE_ON_EXIT = new Set([
|
|
441
|
+
"var(--motion-ease-drawer)",
|
|
442
|
+
"var(--motion-ease-bounce)",
|
|
443
|
+
])
|
|
444
|
+
|
|
445
|
+
// Tailwind's `transition*` utilities and tw-animate's `animate-in`/`-out`
|
|
446
|
+
// both read --tw-duration and --tw-ease, so one utility times either.
|
|
447
|
+
function timing(role: MotionRole, ease = "var(--motion-ease-enter)") {
|
|
448
|
+
return {
|
|
449
|
+
"--tw-duration": scaled(role),
|
|
450
|
+
"--tw-ease": ease,
|
|
451
|
+
[LEAVING]: {
|
|
452
|
+
"--tw-duration": scaled(`${role}-exit`),
|
|
453
|
+
// Sheets keep their drawer curve and springs their bounce.
|
|
454
|
+
"--tw-ease": KEEPS_EASE_ON_EXIT.has(ease)
|
|
455
|
+
? ease
|
|
456
|
+
: "var(--motion-ease-exit)",
|
|
457
|
+
},
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function scale(token: string) {
|
|
462
|
+
return {
|
|
463
|
+
"--tw-enter-scale": `var(--motion-${token}-scale)`,
|
|
464
|
+
"--tw-exit-scale": `var(--motion-${token}-scale)`,
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// A slide from the trigger's side: data-side on Base UI and Radix, and
|
|
469
|
+
// data-placement on React Aria.
|
|
470
|
+
function travel(token: string) {
|
|
471
|
+
const distance = `var(--motion-${token}-travel)`
|
|
472
|
+
return {
|
|
473
|
+
'&:where([data-side="bottom"], [data-placement="bottom"])': {
|
|
474
|
+
"--tw-enter-translate-y": `calc(${distance} * -1)`,
|
|
475
|
+
},
|
|
476
|
+
'&:where([data-side="top"], [data-placement="top"])': {
|
|
477
|
+
"--tw-enter-translate-y": distance,
|
|
478
|
+
},
|
|
479
|
+
'&:where([data-side="right"], [data-side="inline-end"], [data-placement="right"])':
|
|
480
|
+
{ "--tw-enter-translate-x": `calc(${distance} * -1)` },
|
|
481
|
+
'&:where([data-side="left"], [data-side="inline-start"], [data-placement="left"])':
|
|
482
|
+
{ "--tw-enter-translate-x": distance },
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const STILL = {
|
|
487
|
+
"--motion-scale": "0",
|
|
488
|
+
"--motion-press-scale": "1",
|
|
489
|
+
"--motion-tooltip-scale": "1",
|
|
490
|
+
"--motion-popup-scale": "1",
|
|
491
|
+
"--motion-dialog-scale": "1",
|
|
492
|
+
"--motion-tooltip-travel": "0px",
|
|
493
|
+
"--motion-popup-travel": "0px",
|
|
494
|
+
"--motion-popup-blur": "0px",
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const vars = (entries: Record<string, string>) =>
|
|
498
|
+
Object.fromEntries(
|
|
499
|
+
Object.entries(entries).map(([name, value]) => [`--${name}`, value])
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Speed switches and the motion utilities, in the shape of a registry item's
|
|
504
|
+
* `css` field. The speed selectors are one notch more specific than :root, so
|
|
505
|
+
* they win on <html>, and plain attribute selectors anywhere else.
|
|
506
|
+
*/
|
|
507
|
+
export const MOTION_CSS: CssObject = {
|
|
508
|
+
...Object.fromEntries(
|
|
509
|
+
MOTION_SPEED_NAMES.map((name) => [
|
|
510
|
+
`:root[data-motion="${name}"], [data-motion="${name}"]`,
|
|
511
|
+
vars(toMotionVars(MOTION_SPEEDS[name])),
|
|
512
|
+
])
|
|
513
|
+
),
|
|
514
|
+
':root[data-motion="off"], [data-motion="off"]': STILL,
|
|
515
|
+
// Tooltips skip their animation once one is open, or when they open on
|
|
516
|
+
// focus: Base UI marks those with data-instant, Radix with instant-open.
|
|
517
|
+
"@utility motion-tooltip": {
|
|
518
|
+
...scale("tooltip"),
|
|
519
|
+
...timing("tooltip"),
|
|
520
|
+
...travel("tooltip"),
|
|
521
|
+
'&:where([data-instant], [data-state="instant-open"])': {
|
|
522
|
+
"--tw-duration": "0s",
|
|
523
|
+
},
|
|
524
|
+
},
|
|
525
|
+
"@utility motion-popup": {
|
|
526
|
+
...scale("popup"),
|
|
527
|
+
"--tw-enter-blur": "var(--motion-popup-blur)",
|
|
528
|
+
"--tw-exit-blur": "var(--motion-popup-blur)",
|
|
529
|
+
...timing("popup"),
|
|
530
|
+
...travel("popup"),
|
|
531
|
+
},
|
|
532
|
+
// Dialogs grow from the centre; their scrims only fade, on the same time.
|
|
533
|
+
"@utility motion-dialog": { ...scale("dialog"), ...timing("dialog") },
|
|
534
|
+
"@utility motion-scrim": timing("dialog"),
|
|
535
|
+
"@utility motion-sheet": timing("sheet", "var(--motion-ease-drawer)"),
|
|
536
|
+
"@utility motion-disclosure": timing("disclosure"),
|
|
537
|
+
"@utility motion-toast": timing("toast"),
|
|
538
|
+
// Accordion items: heights, gaps and corners spring both ways.
|
|
539
|
+
"@utility motion-bounce": timing("bounce", "var(--motion-ease-bounce)"),
|
|
540
|
+
// A menu's rows entering one after another as it opens: each fades and
|
|
541
|
+
// drops in on the fade spring, --motion-stagger after the row before it, up
|
|
542
|
+
// to the tenth. sibling-index() is the row's place; a browser without it
|
|
543
|
+
// drops the delay and the rows come in together. Reduce Motion skips it.
|
|
544
|
+
"@utility motion-stagger": {
|
|
545
|
+
"@media (prefers-reduced-motion: no-preference)": {
|
|
546
|
+
animation:
|
|
547
|
+
"enter calc(var(--motion-spring-fade-duration) * var(--motion-scale, 1)) var(--motion-ease-fade-spring) both",
|
|
548
|
+
"animation-delay":
|
|
549
|
+
"calc(min(sibling-index() - 1, 10) * var(--motion-stagger) * var(--motion-scale, 1))",
|
|
550
|
+
"--tw-enter-opacity": "0",
|
|
551
|
+
"--tw-enter-translate-y": "calc(var(--spacing) * -1)",
|
|
552
|
+
},
|
|
553
|
+
},
|
|
554
|
+
// Buttons and toggles give a little while held. They already transition
|
|
555
|
+
// every property, `scale` included. Joined groups stay flat.
|
|
556
|
+
"@utility motion-press": {
|
|
557
|
+
'&:active:not(:disabled, [aria-disabled="true"], [data-disabled], :where([data-slot="button-group"], [data-slot="input-group"]) *), &[data-pressed]:not(:where([data-slot="button-group"], [data-slot="input-group"]) *)':
|
|
558
|
+
{ scale: "var(--motion-press-scale)" },
|
|
559
|
+
},
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/** The default speed's tokens, for :root. */
|
|
563
|
+
export const MOTION_ROOT_VARS: Record<string, string> = {
|
|
564
|
+
"motion-scale": "1",
|
|
565
|
+
...toMotionVars(MOTION_SPEEDS[DEFAULT_MOTION_SPEED]),
|
|
566
|
+
}
|