solid-drift 0.9.0 → 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 +35 -0
- package/dist/flip.d.ts +73 -0
- package/dist/flip.js +157 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -467,6 +467,41 @@ const shuffle = () =>
|
|
|
467
467
|
|
|
468
468
|
Returns `{ stop, finished }`: `stop()` halts mid-flight and restores the original transform. SSR-safe and reduced-motion safe: the mutation runs with no animation. If the element does not move, no animation runs either.
|
|
469
469
|
|
|
470
|
+
### `createSharedLayout(ref, options)`
|
|
471
|
+
|
|
472
|
+
Shared-element transition across mounts, the `layoutId` magic. Give the same `id` to elements that represent the same thing in different places: a tab pill, a card that opens into a detail view, a thumbnail that becomes a hero image. When the old element unmounts its rect is remembered; when the new one mounts it starts at the donor's rect and springs home, sliding and morphing into place. This is cross-mount FLIP: where `animateFlip` inverts the delta around a mutation on one element, `createSharedLayout` inverts the delta between two elements that share an identity. One active carrier per id.
|
|
473
|
+
|
|
474
|
+
```tsx
|
|
475
|
+
import { createSharedLayout } from "solid-drift"
|
|
476
|
+
|
|
477
|
+
function TabPill(props: { id: string }) {
|
|
478
|
+
let pill!: HTMLDivElement
|
|
479
|
+
const { x, y, scaleX, scaleY } = createSharedLayout(() => pill, {
|
|
480
|
+
id: props.id,
|
|
481
|
+
})
|
|
482
|
+
return (
|
|
483
|
+
<div
|
|
484
|
+
ref={pill}
|
|
485
|
+
style={{
|
|
486
|
+
transform: `translate(${x()}px, ${y()}px) scale(${scaleX()}, ${scaleY()})`,
|
|
487
|
+
}}
|
|
488
|
+
/>
|
|
489
|
+
)
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// Only one pill is mounted at a time; it glides between tabs.
|
|
493
|
+
<Show when={tab() === "a"}><TabPill id="pill" /></Show>
|
|
494
|
+
<Show when={tab() === "b"}><TabPill id="pill" /></Show>
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
| Option | Default | Description |
|
|
498
|
+
| -------- | -------------------------------- | --------------------------------------------------------------- |
|
|
499
|
+
| `id` | required | Shared identity; elements with the same id hand off to each other |
|
|
500
|
+
| `spring` | `{ stiffness: 260, damping: 30 }` | Spring physics for the handoff flight |
|
|
501
|
+
| `scale` | `true` | Also morph the size delta as scale |
|
|
502
|
+
|
|
503
|
+
Returns `{ x, y, scaleX, scaleY, flying }`: the corrective transform and `flying`, true while the handoff runs. SSR-safe and reduced-motion safe: the element simply appears, with no flight.
|
|
504
|
+
|
|
470
505
|
### `springPresets`
|
|
471
506
|
|
|
472
507
|
Named spring configurations for common feels. Spread into `createSpring`, `createMagnetic`, or `createTilt` options.
|
package/dist/flip.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { type Accessor } from "solid-js";
|
|
1
2
|
import { type AnimateOptions, type AnimationControls } from "./animate.js";
|
|
3
|
+
import type { SpringOptions } from "./spring.js";
|
|
2
4
|
export interface FlipOptions {
|
|
3
5
|
/** Duration in milliseconds. Default 400. */
|
|
4
6
|
duration?: number;
|
|
@@ -42,3 +44,74 @@ export interface FlipOptions {
|
|
|
42
44
|
* ```
|
|
43
45
|
*/
|
|
44
46
|
export declare function animateFlip(ref: () => Element | null | undefined, mutate: () => void, options?: FlipOptions): AnimationControls;
|
|
47
|
+
export interface SharedLayoutOptions {
|
|
48
|
+
/**
|
|
49
|
+
* Shared identity. Elements with the same id hand off to each other
|
|
50
|
+
* across mounts: the newcomer flies in from the donor's last rect.
|
|
51
|
+
*/
|
|
52
|
+
id: string;
|
|
53
|
+
/**
|
|
54
|
+
* Spring physics for the handoff flight. Default
|
|
55
|
+
* `{ stiffness: 260, damping: 30 }`.
|
|
56
|
+
*/
|
|
57
|
+
spring?: SpringOptions;
|
|
58
|
+
/**
|
|
59
|
+
* Also morph the size delta as scale, so growing or shrinking elements
|
|
60
|
+
* morph instead of just sliding. Default true.
|
|
61
|
+
*/
|
|
62
|
+
scale?: boolean;
|
|
63
|
+
}
|
|
64
|
+
export interface SharedLayoutResult {
|
|
65
|
+
/** Horizontal corrective offset in pixels. */
|
|
66
|
+
x: Accessor<number>;
|
|
67
|
+
/** Vertical corrective offset in pixels. */
|
|
68
|
+
y: Accessor<number>;
|
|
69
|
+
/** Horizontal corrective scale. */
|
|
70
|
+
scaleX: Accessor<number>;
|
|
71
|
+
/** Vertical corrective scale. */
|
|
72
|
+
scaleY: Accessor<number>;
|
|
73
|
+
/** True while the handoff flight is running. */
|
|
74
|
+
flying: Accessor<boolean>;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Shared-element transition across mounts, the `layoutId` magic.
|
|
78
|
+
*
|
|
79
|
+
* Give the same `id` to elements that represent the same thing in
|
|
80
|
+
* different places: a tab pill, a card that opens into a detail view,
|
|
81
|
+
* a thumbnail that becomes a hero image. When the old element unmounts
|
|
82
|
+
* its rect is remembered; when the new one mounts it starts at the
|
|
83
|
+
* donor's rect and springs home, sliding and morphing into place.
|
|
84
|
+
*
|
|
85
|
+
* This is cross-mount FLIP: where `animateFlip` inverts the delta
|
|
86
|
+
* around a mutation on one element, `createSharedLayout` inverts the
|
|
87
|
+
* delta between two elements that share an identity. Bind the returned
|
|
88
|
+
* values as a transform on the mounted element.
|
|
89
|
+
*
|
|
90
|
+
* SSR-safe and reduced-motion safe: the element simply appears, with
|
|
91
|
+
* no flight. One active carrier per id: the first mount consumes the
|
|
92
|
+
* parked donor rect.
|
|
93
|
+
*
|
|
94
|
+
* ```tsx
|
|
95
|
+
* import { createSharedLayout } from "solid-drift"
|
|
96
|
+
*
|
|
97
|
+
* function TabPill(props: { id: string }) {
|
|
98
|
+
* let pill!: HTMLDivElement
|
|
99
|
+
* const { x, y, scaleX, scaleY } = createSharedLayout(() => pill, {
|
|
100
|
+
* id: props.id,
|
|
101
|
+
* })
|
|
102
|
+
* return (
|
|
103
|
+
* <div
|
|
104
|
+
* ref={pill}
|
|
105
|
+
* style={{
|
|
106
|
+
* transform: `translate(${x()}px, ${y()}px) scale(${scaleX()}, ${scaleY()})`,
|
|
107
|
+
* }}
|
|
108
|
+
* />
|
|
109
|
+
* )
|
|
110
|
+
* }
|
|
111
|
+
*
|
|
112
|
+
* // Only one pill is mounted at a time; it glides between tabs.
|
|
113
|
+
* <Show when={tab() === "a"}><TabPill id="pill" /></Show>
|
|
114
|
+
* <Show when={tab() === "b"}><TabPill id="pill" /></Show>
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export declare function createSharedLayout(ref: () => Element | null | undefined, options: SharedLayoutOptions): SharedLayoutResult;
|
package/dist/flip.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { createEffect, createSignal, onCleanup, } from "solid-js";
|
|
1
2
|
import { animate, } from "./animate.js";
|
|
3
|
+
import { now, schedule } from "./engine.js";
|
|
2
4
|
import { prefersReducedMotion } from "./reduced-motion.js";
|
|
3
5
|
/**
|
|
4
6
|
* FLIP layout animation around a DOM mutation.
|
|
@@ -106,3 +108,158 @@ export function animateFlip(ref, mutate, options = {}) {
|
|
|
106
108
|
}));
|
|
107
109
|
return controls;
|
|
108
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Donor registry for shared-element handoffs. When an element carrying a
|
|
113
|
+
* shared id unmounts, its last rect is parked here; the next element that
|
|
114
|
+
* mounts with the same id consumes it and flies in from that rect.
|
|
115
|
+
* One active carrier per id: consumption deletes the entry.
|
|
116
|
+
*/
|
|
117
|
+
const donors = new Map();
|
|
118
|
+
function rectOf(el) {
|
|
119
|
+
const r = el.getBoundingClientRect();
|
|
120
|
+
return { left: r.left, top: r.top, width: r.width, height: r.height };
|
|
121
|
+
}
|
|
122
|
+
const zero = () => 0;
|
|
123
|
+
const one = () => 1;
|
|
124
|
+
const no = () => false;
|
|
125
|
+
/**
|
|
126
|
+
* Shared-element transition across mounts, the `layoutId` magic.
|
|
127
|
+
*
|
|
128
|
+
* Give the same `id` to elements that represent the same thing in
|
|
129
|
+
* different places: a tab pill, a card that opens into a detail view,
|
|
130
|
+
* a thumbnail that becomes a hero image. When the old element unmounts
|
|
131
|
+
* its rect is remembered; when the new one mounts it starts at the
|
|
132
|
+
* donor's rect and springs home, sliding and morphing into place.
|
|
133
|
+
*
|
|
134
|
+
* This is cross-mount FLIP: where `animateFlip` inverts the delta
|
|
135
|
+
* around a mutation on one element, `createSharedLayout` inverts the
|
|
136
|
+
* delta between two elements that share an identity. Bind the returned
|
|
137
|
+
* values as a transform on the mounted element.
|
|
138
|
+
*
|
|
139
|
+
* SSR-safe and reduced-motion safe: the element simply appears, with
|
|
140
|
+
* no flight. One active carrier per id: the first mount consumes the
|
|
141
|
+
* parked donor rect.
|
|
142
|
+
*
|
|
143
|
+
* ```tsx
|
|
144
|
+
* import { createSharedLayout } from "solid-drift"
|
|
145
|
+
*
|
|
146
|
+
* function TabPill(props: { id: string }) {
|
|
147
|
+
* let pill!: HTMLDivElement
|
|
148
|
+
* const { x, y, scaleX, scaleY } = createSharedLayout(() => pill, {
|
|
149
|
+
* id: props.id,
|
|
150
|
+
* })
|
|
151
|
+
* return (
|
|
152
|
+
* <div
|
|
153
|
+
* ref={pill}
|
|
154
|
+
* style={{
|
|
155
|
+
* transform: `translate(${x()}px, ${y()}px) scale(${scaleX()}, ${scaleY()})`,
|
|
156
|
+
* }}
|
|
157
|
+
* />
|
|
158
|
+
* )
|
|
159
|
+
* }
|
|
160
|
+
*
|
|
161
|
+
* // Only one pill is mounted at a time; it glides between tabs.
|
|
162
|
+
* <Show when={tab() === "a"}><TabPill id="pill" /></Show>
|
|
163
|
+
* <Show when={tab() === "b"}><TabPill id="pill" /></Show>
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
export function createSharedLayout(ref, options) {
|
|
167
|
+
const { id, spring = {}, scale = true } = options;
|
|
168
|
+
if (typeof window === "undefined") {
|
|
169
|
+
return { x: zero, y: zero, scaleX: one, scaleY: one, flying: no };
|
|
170
|
+
}
|
|
171
|
+
const [x, setX] = createSignal(0);
|
|
172
|
+
const [y, setY] = createSignal(0);
|
|
173
|
+
const [scaleX, setScaleX] = createSignal(1);
|
|
174
|
+
const [scaleY, setScaleY] = createSignal(1);
|
|
175
|
+
const [flying, setFlying] = createSignal(false);
|
|
176
|
+
let cancelFlight = null;
|
|
177
|
+
const stopFlight = () => {
|
|
178
|
+
cancelFlight?.();
|
|
179
|
+
cancelFlight = null;
|
|
180
|
+
};
|
|
181
|
+
/** Spring flight from the inverted delta back to identity. */
|
|
182
|
+
const flyFrom = (dx, dy, sx, sy) => {
|
|
183
|
+
const { stiffness = 260, damping = 30 } = spring;
|
|
184
|
+
let cx = dx;
|
|
185
|
+
let cy = dy;
|
|
186
|
+
let csx = sx;
|
|
187
|
+
let csy = sy;
|
|
188
|
+
let vx = 0;
|
|
189
|
+
let vy = 0;
|
|
190
|
+
let vsx = 0;
|
|
191
|
+
let vsy = 0;
|
|
192
|
+
let last = now();
|
|
193
|
+
setX(cx);
|
|
194
|
+
setY(cy);
|
|
195
|
+
setScaleX(csx);
|
|
196
|
+
setScaleY(csy);
|
|
197
|
+
setFlying(true);
|
|
198
|
+
stopFlight();
|
|
199
|
+
const task = (t) => {
|
|
200
|
+
const dt = Math.min(Math.max((t - last) / 1000, 0), 0.064);
|
|
201
|
+
last = t;
|
|
202
|
+
// Semi-implicit Euler per channel, same integrator as createSpring.
|
|
203
|
+
vx += (-stiffness * cx - damping * vx) * dt;
|
|
204
|
+
vy += (-stiffness * cy - damping * vy) * dt;
|
|
205
|
+
vsx += (-stiffness * (csx - 1) - damping * vsx) * dt;
|
|
206
|
+
vsy += (-stiffness * (csy - 1) - damping * vsy) * dt;
|
|
207
|
+
cx += vx * dt;
|
|
208
|
+
cy += vy * dt;
|
|
209
|
+
csx += vsx * dt;
|
|
210
|
+
csy += vsy * dt;
|
|
211
|
+
setX(cx);
|
|
212
|
+
setY(cy);
|
|
213
|
+
setScaleX(csx);
|
|
214
|
+
setScaleY(csy);
|
|
215
|
+
const settled = Math.hypot(cx, cy) < 0.5 &&
|
|
216
|
+
Math.abs(csx - 1) < 0.003 &&
|
|
217
|
+
Math.abs(csy - 1) < 0.003 &&
|
|
218
|
+
Math.hypot(vx, vy) < 20 &&
|
|
219
|
+
Math.hypot(vsx, vsy) < 0.05;
|
|
220
|
+
if (settled) {
|
|
221
|
+
setX(0);
|
|
222
|
+
setY(0);
|
|
223
|
+
setScaleX(1);
|
|
224
|
+
setScaleY(1);
|
|
225
|
+
setFlying(false);
|
|
226
|
+
cancelFlight = null;
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
return true;
|
|
230
|
+
};
|
|
231
|
+
cancelFlight = schedule(task);
|
|
232
|
+
};
|
|
233
|
+
// Late-bound refs (Solid assigns `ref` after mount) still hand off.
|
|
234
|
+
createEffect(() => {
|
|
235
|
+
const el = ref();
|
|
236
|
+
if (!el)
|
|
237
|
+
return;
|
|
238
|
+
const mountRect = rectOf(el);
|
|
239
|
+
const donor = donors.get(id);
|
|
240
|
+
donors.delete(id);
|
|
241
|
+
if (donor && !prefersReducedMotion()) {
|
|
242
|
+
const own = mountRect;
|
|
243
|
+
const dx = donor.left - own.left;
|
|
244
|
+
const dy = donor.top - own.top;
|
|
245
|
+
const sx = scale && own.width > 0 ? donor.width / own.width : 1;
|
|
246
|
+
const sy = scale && own.height > 0 ? donor.height / own.height : 1;
|
|
247
|
+
const moved = Math.abs(dx) >= 0.5 ||
|
|
248
|
+
Math.abs(dy) >= 0.5 ||
|
|
249
|
+
Math.abs(sx - 1) >= 0.002 ||
|
|
250
|
+
Math.abs(sy - 1) >= 0.002;
|
|
251
|
+
if (moved)
|
|
252
|
+
flyFrom(dx, dy, sx, sy);
|
|
253
|
+
}
|
|
254
|
+
onCleanup(() => {
|
|
255
|
+
// In <Show>/<Switch>/&& branches the cleanup runs while the node
|
|
256
|
+
// is still attached, so this is the live rect. If the node was
|
|
257
|
+
// already detached (all-zero rect), keep the mount snapshot.
|
|
258
|
+
const r = rectOf(el);
|
|
259
|
+
const valid = r.width > 0 || r.height > 0 || r.left !== 0 || r.top !== 0;
|
|
260
|
+
donors.set(id, valid ? r : mountRect);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
onCleanup(() => stopFlight());
|
|
264
|
+
return { x, y, scaleX, scaleY, flying };
|
|
265
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export { createVelocity, type VelocityOptions } from "./velocity.js";
|
|
|
19
19
|
export { createMagnetic, type MagneticOptions, type MagneticResult, createTilt, type TiltOptions, type TiltResult, } from "./pointer.js";
|
|
20
20
|
export { createTrail, type TrailOptions } from "./trail.js";
|
|
21
21
|
export { createTimeline, type TimelineStep, type TimelineStatus, type TimelineControls, } from "./timeline.js";
|
|
22
|
-
export { animateFlip, type FlipOptions } from "./flip.js";
|
|
22
|
+
export { animateFlip, type FlipOptions, createSharedLayout, type SharedLayoutOptions, type SharedLayoutResult, } from "./flip.js";
|
|
23
23
|
export { createSquashStretch, type SquashStretchOptions, type SquashStretchResult, createFollowThrough, type FollowThroughOptions, createAnticipation, type AnticipationOptions, createWobble, type WobbleOptions, type WobbleResult, } from "./cartoon.js";
|
|
24
24
|
export { createGravity, type GravityOptions, type GravityResult, createPendulum, type PendulumOptions, type PendulumResult, createFling, type FlingOptions, type FlingResult, } from "./physics.js";
|
|
25
25
|
export { createFontSwap, type FontSwapOptions, type FontSwapResult, createTyping, type TypingOptions, type TypingResult, createTextPhysics, type TextPhysicsOptions, type TextPhysicsResult, createTextTunnel, type TextTunnelOptions, type TextTunnelResult, createTextCutout, type TextCutoutOptions, createTextGradient, type TextGradientOptions, createTextScramble, type TextScrambleOptions, type TextScrambleResult, createTextWave, type TextWaveOptions, } from "./typography.js";
|
package/dist/index.js
CHANGED
|
@@ -19,7 +19,7 @@ export { createVelocity } from "./velocity.js";
|
|
|
19
19
|
export { createMagnetic, createTilt, } from "./pointer.js";
|
|
20
20
|
export { createTrail } from "./trail.js";
|
|
21
21
|
export { createTimeline, } from "./timeline.js";
|
|
22
|
-
export { animateFlip } from "./flip.js";
|
|
22
|
+
export { animateFlip, createSharedLayout, } from "./flip.js";
|
|
23
23
|
export { createSquashStretch, createFollowThrough, createAnticipation, createWobble, } from "./cartoon.js";
|
|
24
24
|
export { createGravity, createPendulum, createFling, } from "./physics.js";
|
|
25
25
|
export { createFontSwap, createTyping, createTextPhysics, createTextTunnel, createTextCutout, createTextGradient, createTextScramble, createTextWave, } from "./typography.js";
|
package/package.json
CHANGED