what-core 0.14.0 → 0.15.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/LICENSE +21 -0
- package/README.md +64 -38
- package/a11y.d.ts +128 -0
- package/data.d.ts +294 -0
- package/dist/a11y.js +510 -0
- package/dist/a11y.min.js +413 -0
- package/dist/actions.js +158 -0
- package/dist/actions.min.js +112 -0
- package/dist/animation.js +481 -0
- package/dist/animation.min.js +295 -0
- package/dist/async.js +180 -0
- package/dist/async.min.js +134 -0
- package/dist/components.js +577 -0
- package/dist/components.min.js +413 -0
- package/dist/data.js +1212 -0
- package/dist/data.min.js +918 -0
- package/dist/diagnostics.js +187 -0
- package/dist/diagnostics.min.js +134 -0
- package/dist/dom.js +796 -0
- package/dist/dom.min.js +603 -0
- package/dist/entries/a11y.js +43 -0
- package/dist/entries/a11y.min.js +43 -0
- package/dist/entries/data.js +29 -0
- package/dist/entries/data.min.js +29 -0
- package/dist/entries/dom.js +45 -0
- package/dist/entries/dom.min.js +45 -0
- package/dist/entries/form.js +31 -0
- package/dist/entries/form.min.js +31 -0
- package/dist/entries/internal.js +123 -0
- package/dist/entries/internal.min.js +123 -0
- package/dist/entries/motion.js +23 -0
- package/dist/entries/motion.min.js +23 -0
- package/dist/entries/ui.js +25 -0
- package/dist/entries/ui.min.js +25 -0
- package/dist/env.js +5 -0
- package/dist/env.min.js +5 -0
- package/dist/errors.js +1518 -0
- package/dist/errors.min.js +548 -0
- package/dist/form.js +936 -0
- package/dist/form.min.js +703 -0
- package/dist/guardrails.js +84 -0
- package/dist/guardrails.min.js +55 -0
- package/dist/h.js +232 -0
- package/dist/h.min.js +171 -0
- package/dist/head.js +241 -0
- package/dist/head.min.js +207 -0
- package/dist/helpers.js +126 -0
- package/dist/helpers.min.js +98 -0
- package/dist/hooks.js +255 -0
- package/dist/hooks.min.js +153 -0
- package/dist/hot.js +215 -0
- package/dist/hot.min.js +24 -0
- package/dist/hydration-data.js +495 -0
- package/dist/hydration-data.min.js +371 -0
- package/dist/import-changes.js +273 -0
- package/dist/import-changes.min.js +272 -0
- package/dist/index.js +95 -0
- package/dist/index.min.js +95 -227
- package/dist/jsx-dev-runtime.js +10 -0
- package/dist/jsx-dev-runtime.min.js +10 -1
- package/dist/jsx-runtime.js +27 -0
- package/dist/jsx-runtime.min.js +15 -1
- package/dist/live.js +54 -0
- package/dist/live.min.js +49 -0
- package/dist/markup-rules.js +250 -0
- package/dist/markup-rules.min.js +213 -0
- package/dist/reactive.js +3334 -0
- package/dist/reactive.min.js +2033 -0
- package/dist/render.js +4746 -0
- package/dist/render.min.js +2797 -1
- package/dist/scheduler.js +225 -0
- package/dist/scheduler.min.js +162 -0
- package/dist/selector.js +11 -0
- package/dist/selector.min.js +10 -0
- package/dist/server-context.js +148 -0
- package/dist/server-context.min.js +121 -0
- package/dist/skeleton.js +342 -0
- package/dist/skeleton.min.js +310 -0
- package/dist/store.js +72 -0
- package/dist/store.min.js +50 -0
- package/dist/testing.js +720 -0
- package/dist/testing.min.js +458 -1
- package/dist/vnode-adoption.js +19 -0
- package/dist/vnode-adoption.min.js +19 -0
- package/dist/vnode-renderer.js +61 -0
- package/dist/vnode-renderer.min.js +47 -0
- package/dom.d.ts +44 -0
- package/form.d.ts +218 -0
- package/hot.d.ts +35 -0
- package/index.d.ts +421 -736
- package/internal.d.ts +284 -0
- package/jsx-runtime.d.ts +160 -137
- package/markup-rules.d.ts +83 -0
- package/motion.d.ts +156 -0
- package/package.json +117 -3
- package/render.d.ts +278 -29
- package/src/a11y.js +64 -59
- package/src/actions.js +226 -0
- package/src/animation.js +105 -57
- package/src/async.js +259 -0
- package/src/components.js +495 -144
- package/src/data.js +1410 -1159
- package/src/diagnostics.js +263 -0
- package/src/dom.js +1159 -872
- package/src/entries/a11y.js +24 -0
- package/src/entries/data.js +17 -0
- package/src/entries/dom.js +29 -0
- package/src/entries/form.js +16 -0
- package/src/entries/internal.js +63 -0
- package/src/entries/motion.js +13 -0
- package/src/entries/ui.js +13 -0
- package/src/env-development.js +12 -0
- package/src/env.js +33 -0
- package/src/errors.js +1144 -168
- package/src/form.js +723 -225
- package/src/guardrails.js +38 -126
- package/src/h.js +227 -152
- package/src/head.js +189 -19
- package/src/helpers.js +47 -75
- package/src/hooks.js +233 -248
- package/src/hot.js +316 -0
- package/src/hydration-data.js +859 -16
- package/src/import-changes.js +156 -0
- package/src/index.js +38 -188
- package/src/jsx-dev-runtime.js +7 -14
- package/src/jsx-runtime.js +25 -9
- package/src/live.js +80 -0
- package/src/markup-rules.js +387 -0
- package/src/reactive.js +4400 -862
- package/src/render.js +6502 -2570
- package/src/scheduler.js +56 -21
- package/src/selector.js +31 -0
- package/src/server-context.js +252 -22
- package/src/skeleton.js +22 -12
- package/src/store.js +17 -31
- package/src/testing.js +389 -63
- package/src/vnode-adoption.js +48 -0
- package/src/vnode-renderer.js +114 -0
- package/testing.d.ts +81 -1
- package/ui.d.ts +20 -0
- package/dist/chunk-6YOUZ6T4.min.js +0 -1
- package/dist/chunk-FN6HXME3.min.js +0 -11
- package/dist/chunk-VKZY7JPN.min.js +0 -1
- package/src/agent-context.js +0 -127
- package/src/warnings.js +0 -193
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
// src/animation.js
|
|
2
|
+
import { signal, effect, batch, onCleanup, createRoot, untrack, _userEffect } from "./reactive.js";
|
|
3
|
+
import { scheduleRead, scheduleWrite } from "./scheduler.js";
|
|
4
|
+
function spring(initialValue, options = {}) {
|
|
5
|
+
const {
|
|
6
|
+
stiffness = 100,
|
|
7
|
+
damping = 10,
|
|
8
|
+
mass = 1,
|
|
9
|
+
precision = 0.01
|
|
10
|
+
} = options;
|
|
11
|
+
const current = signal(initialValue);
|
|
12
|
+
const target = signal(initialValue);
|
|
13
|
+
const velocity = signal(0);
|
|
14
|
+
const isAnimating = signal(false);
|
|
15
|
+
let rafId = null;
|
|
16
|
+
let lastTime = null;
|
|
17
|
+
function tick(time) {
|
|
18
|
+
if (lastTime === null) {
|
|
19
|
+
lastTime = time;
|
|
20
|
+
rafId = requestAnimationFrame(tick);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const dt = Math.min((time - lastTime) / 1e3, 0.064);
|
|
24
|
+
lastTime = time;
|
|
25
|
+
const currentVal = current.peek();
|
|
26
|
+
const targetVal = target.peek();
|
|
27
|
+
const vel = velocity.peek();
|
|
28
|
+
const displacement = currentVal - targetVal;
|
|
29
|
+
const springForce = -stiffness * displacement;
|
|
30
|
+
const dampingForce = -damping * vel;
|
|
31
|
+
const acceleration = (springForce + dampingForce) / mass;
|
|
32
|
+
const newVelocity = vel + acceleration * dt;
|
|
33
|
+
const newValue = currentVal + newVelocity * dt;
|
|
34
|
+
batch(() => {
|
|
35
|
+
velocity.set(newVelocity);
|
|
36
|
+
current.set(newValue);
|
|
37
|
+
});
|
|
38
|
+
if (Math.abs(newVelocity) < precision && Math.abs(displacement) < precision) {
|
|
39
|
+
batch(() => {
|
|
40
|
+
current.set(targetVal);
|
|
41
|
+
velocity.set(0);
|
|
42
|
+
isAnimating.set(false);
|
|
43
|
+
});
|
|
44
|
+
rafId = null;
|
|
45
|
+
lastTime = null;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
rafId = requestAnimationFrame(tick);
|
|
49
|
+
}
|
|
50
|
+
function set(newTarget) {
|
|
51
|
+
target.set(newTarget);
|
|
52
|
+
if (rafId === null) {
|
|
53
|
+
isAnimating.set(true);
|
|
54
|
+
lastTime = null;
|
|
55
|
+
rafId = requestAnimationFrame(tick);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function stop() {
|
|
59
|
+
if (rafId) {
|
|
60
|
+
cancelAnimationFrame(rafId);
|
|
61
|
+
rafId = null;
|
|
62
|
+
}
|
|
63
|
+
isAnimating.set(false);
|
|
64
|
+
lastTime = null;
|
|
65
|
+
}
|
|
66
|
+
function snap(value) {
|
|
67
|
+
stop();
|
|
68
|
+
batch(() => {
|
|
69
|
+
current.set(value);
|
|
70
|
+
target.set(value);
|
|
71
|
+
velocity.set(0);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
onCleanup(stop);
|
|
75
|
+
return {
|
|
76
|
+
current: () => current(),
|
|
77
|
+
target: () => target(),
|
|
78
|
+
velocity: () => velocity(),
|
|
79
|
+
isAnimating: () => isAnimating(),
|
|
80
|
+
set,
|
|
81
|
+
stop,
|
|
82
|
+
snap,
|
|
83
|
+
subscribe: current.subscribe
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function tween(from, to, options = {}) {
|
|
87
|
+
const {
|
|
88
|
+
duration = 300,
|
|
89
|
+
easing = (t) => t * (2 - t),
|
|
90
|
+
// easeOutQuad
|
|
91
|
+
onUpdate,
|
|
92
|
+
onComplete
|
|
93
|
+
} = options;
|
|
94
|
+
const progress = signal(0);
|
|
95
|
+
const value = signal(from);
|
|
96
|
+
const isAnimating = signal(true);
|
|
97
|
+
let startTime = null;
|
|
98
|
+
let rafId = null;
|
|
99
|
+
function tick(time) {
|
|
100
|
+
if (startTime === null) startTime = time;
|
|
101
|
+
const t0 = (
|
|
102
|
+
/** @type {number} */
|
|
103
|
+
startTime
|
|
104
|
+
);
|
|
105
|
+
const elapsed = time - t0;
|
|
106
|
+
const t = duration > 0 ? Math.min(elapsed / duration, 1) : 1;
|
|
107
|
+
const easedT = easing(t);
|
|
108
|
+
const currentValue = from + (to - from) * easedT;
|
|
109
|
+
batch(() => {
|
|
110
|
+
progress.set(t);
|
|
111
|
+
value.set(currentValue);
|
|
112
|
+
});
|
|
113
|
+
if (onUpdate) onUpdate(currentValue, t);
|
|
114
|
+
if (t < 1) {
|
|
115
|
+
rafId = requestAnimationFrame(tick);
|
|
116
|
+
} else {
|
|
117
|
+
isAnimating.set(false);
|
|
118
|
+
if (onComplete) onComplete();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
rafId = requestAnimationFrame(tick);
|
|
122
|
+
function cancel() {
|
|
123
|
+
if (rafId) cancelAnimationFrame(rafId);
|
|
124
|
+
isAnimating.set(false);
|
|
125
|
+
}
|
|
126
|
+
onCleanup(cancel);
|
|
127
|
+
return {
|
|
128
|
+
progress: () => progress(),
|
|
129
|
+
value: () => value(),
|
|
130
|
+
isAnimating: () => isAnimating(),
|
|
131
|
+
cancel,
|
|
132
|
+
subscribe: value.subscribe
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
var easings = {
|
|
136
|
+
linear: (t) => t,
|
|
137
|
+
easeInQuad: (t) => t * t,
|
|
138
|
+
easeOutQuad: (t) => t * (2 - t),
|
|
139
|
+
easeInOutQuad: (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t,
|
|
140
|
+
easeInCubic: (t) => t * t * t,
|
|
141
|
+
easeOutCubic: (t) => --t * t * t + 1,
|
|
142
|
+
easeInOutCubic: (t) => t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
|
|
143
|
+
easeInElastic: (t) => t === 0 ? 0 : t === 1 ? 1 : -Math.pow(2, 10 * t - 10) * Math.sin((t * 10 - 10.75) * (2 * Math.PI / 3)),
|
|
144
|
+
easeOutElastic: (t) => t === 0 ? 0 : t === 1 ? 1 : Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * (2 * Math.PI / 3)) + 1,
|
|
145
|
+
easeOutBounce: (t) => {
|
|
146
|
+
const n1 = 7.5625;
|
|
147
|
+
const d1 = 2.75;
|
|
148
|
+
if (t < 1 / d1) return n1 * t * t;
|
|
149
|
+
if (t < 2 / d1) return n1 * (t -= 1.5 / d1) * t + 0.75;
|
|
150
|
+
if (t < 2.5 / d1) return n1 * (t -= 2.25 / d1) * t + 0.9375;
|
|
151
|
+
return n1 * (t -= 2.625 / d1) * t + 0.984375;
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
function useTween(options = {}) {
|
|
155
|
+
const { duration = 300, easing = easings.easeOutQuad } = options;
|
|
156
|
+
const isTransitioning = signal(false);
|
|
157
|
+
const progress = signal(0);
|
|
158
|
+
let run = null;
|
|
159
|
+
const stopRun = () => {
|
|
160
|
+
const stopped = run;
|
|
161
|
+
if (stopped === null) return;
|
|
162
|
+
run = null;
|
|
163
|
+
cancelAnimationFrame(stopped.frame);
|
|
164
|
+
stopped.resolve();
|
|
165
|
+
};
|
|
166
|
+
onCleanup(stopRun);
|
|
167
|
+
async function start(callback) {
|
|
168
|
+
stopRun();
|
|
169
|
+
isTransitioning.set(true);
|
|
170
|
+
progress.set(0);
|
|
171
|
+
return (
|
|
172
|
+
/** @type {Promise<void>} */
|
|
173
|
+
new Promise((resolve) => {
|
|
174
|
+
const startTime = performance.now();
|
|
175
|
+
const own = { frame: 0, resolve };
|
|
176
|
+
run = own;
|
|
177
|
+
function tick(time) {
|
|
178
|
+
if (run !== own) return;
|
|
179
|
+
const elapsed = time - startTime;
|
|
180
|
+
const t = Math.min(elapsed / duration, 1);
|
|
181
|
+
progress.set(easing(t));
|
|
182
|
+
if (t < 1) {
|
|
183
|
+
own.frame = requestAnimationFrame(tick);
|
|
184
|
+
} else {
|
|
185
|
+
run = null;
|
|
186
|
+
isTransitioning.set(false);
|
|
187
|
+
if (callback) callback();
|
|
188
|
+
resolve();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
own.frame = requestAnimationFrame(tick);
|
|
192
|
+
})
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
isTransitioning: () => isTransitioning(),
|
|
197
|
+
progress: () => progress(),
|
|
198
|
+
start
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function useGesture(element, handlers = {}) {
|
|
202
|
+
const {
|
|
203
|
+
onDrag,
|
|
204
|
+
onDragStart,
|
|
205
|
+
onDragEnd,
|
|
206
|
+
onPinch,
|
|
207
|
+
onSwipe,
|
|
208
|
+
onTap,
|
|
209
|
+
onLongPress,
|
|
210
|
+
preventDefault = false
|
|
211
|
+
// Set to true to allow e.preventDefault() in touch handlers
|
|
212
|
+
} = handlers;
|
|
213
|
+
const state = {
|
|
214
|
+
isDragging: signal(false),
|
|
215
|
+
startX: 0,
|
|
216
|
+
startY: 0,
|
|
217
|
+
currentX: signal(0),
|
|
218
|
+
currentY: signal(0),
|
|
219
|
+
deltaX: signal(0),
|
|
220
|
+
deltaY: signal(0),
|
|
221
|
+
velocity: signal({ x: 0, y: 0 })
|
|
222
|
+
};
|
|
223
|
+
let lastTime = 0;
|
|
224
|
+
let lastX = 0;
|
|
225
|
+
let lastY = 0;
|
|
226
|
+
let longPressTimer = null;
|
|
227
|
+
let longPressed = false;
|
|
228
|
+
function handleStart(e) {
|
|
229
|
+
const touch = e.touches ? e.touches[0] : e;
|
|
230
|
+
state.startX = touch.clientX;
|
|
231
|
+
state.startY = touch.clientY;
|
|
232
|
+
lastX = touch.clientX;
|
|
233
|
+
lastY = touch.clientY;
|
|
234
|
+
lastTime = performance.now();
|
|
235
|
+
batch(() => {
|
|
236
|
+
state.currentX.set(touch.clientX);
|
|
237
|
+
state.currentY.set(touch.clientY);
|
|
238
|
+
state.deltaX.set(0);
|
|
239
|
+
state.deltaY.set(0);
|
|
240
|
+
state.velocity.set({ x: 0, y: 0 });
|
|
241
|
+
});
|
|
242
|
+
state.isDragging.set(true);
|
|
243
|
+
if (onDragStart) onDragStart({ x: state.startX, y: state.startY });
|
|
244
|
+
longPressed = false;
|
|
245
|
+
if (onLongPress) {
|
|
246
|
+
longPressTimer = setTimeout(() => {
|
|
247
|
+
longPressTimer = null;
|
|
248
|
+
if (state.isDragging.peek()) {
|
|
249
|
+
longPressed = true;
|
|
250
|
+
onLongPress({ x: lastX, y: lastY });
|
|
251
|
+
}
|
|
252
|
+
}, 500);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function handleMove(e) {
|
|
256
|
+
if (!state.isDragging.peek()) return;
|
|
257
|
+
const touch = e.touches ? e.touches[0] : e;
|
|
258
|
+
const x = touch.clientX;
|
|
259
|
+
const y = touch.clientY;
|
|
260
|
+
const now = performance.now();
|
|
261
|
+
const dt = now - lastTime;
|
|
262
|
+
batch(() => {
|
|
263
|
+
state.currentX.set(x);
|
|
264
|
+
state.currentY.set(y);
|
|
265
|
+
state.deltaX.set(x - state.startX);
|
|
266
|
+
state.deltaY.set(y - state.startY);
|
|
267
|
+
if (dt > 0) {
|
|
268
|
+
state.velocity.set({
|
|
269
|
+
x: (x - lastX) / dt * 1e3,
|
|
270
|
+
y: (y - lastY) / dt * 1e3
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
lastX = x;
|
|
275
|
+
lastY = y;
|
|
276
|
+
lastTime = now;
|
|
277
|
+
if (longPressTimer) {
|
|
278
|
+
const distance = Math.sqrt(state.deltaX.peek() ** 2 + state.deltaY.peek() ** 2);
|
|
279
|
+
if (distance > 10) {
|
|
280
|
+
clearTimeout(longPressTimer);
|
|
281
|
+
longPressTimer = null;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (onDrag) {
|
|
285
|
+
onDrag({
|
|
286
|
+
x,
|
|
287
|
+
y,
|
|
288
|
+
deltaX: state.deltaX.peek(),
|
|
289
|
+
deltaY: state.deltaY.peek(),
|
|
290
|
+
velocity: state.velocity.peek()
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
function handleEnd(_e) {
|
|
295
|
+
if (!state.isDragging.peek()) return;
|
|
296
|
+
if (longPressTimer) {
|
|
297
|
+
clearTimeout(longPressTimer);
|
|
298
|
+
longPressTimer = null;
|
|
299
|
+
}
|
|
300
|
+
const deltaX = state.deltaX.peek();
|
|
301
|
+
const deltaY = state.deltaY.peek();
|
|
302
|
+
const velocity = state.velocity.peek();
|
|
303
|
+
const distance = Math.sqrt(deltaX ** 2 + deltaY ** 2);
|
|
304
|
+
if (distance < 10 && onTap && !longPressed) {
|
|
305
|
+
onTap({ x: state.startX, y: state.startY });
|
|
306
|
+
}
|
|
307
|
+
if (onSwipe && (Math.abs(velocity.x) > 500 || Math.abs(velocity.y) > 500)) {
|
|
308
|
+
const direction = Math.abs(velocity.x) > Math.abs(velocity.y) ? velocity.x > 0 ? "right" : "left" : velocity.y > 0 ? "down" : "up";
|
|
309
|
+
onSwipe({ direction, velocity });
|
|
310
|
+
}
|
|
311
|
+
if (onDragEnd) {
|
|
312
|
+
onDragEnd({
|
|
313
|
+
deltaX,
|
|
314
|
+
deltaY,
|
|
315
|
+
velocity
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
state.isDragging.set(false);
|
|
319
|
+
}
|
|
320
|
+
let initialPinchDistance = null;
|
|
321
|
+
function handlePinchMove(e) {
|
|
322
|
+
if (!onPinch || e.touches.length !== 2) return;
|
|
323
|
+
const touch1 = e.touches[0];
|
|
324
|
+
const touch2 = e.touches[1];
|
|
325
|
+
const distance = Math.sqrt(
|
|
326
|
+
(touch2.clientX - touch1.clientX) ** 2 + (touch2.clientY - touch1.clientY) ** 2
|
|
327
|
+
);
|
|
328
|
+
if (initialPinchDistance === null) {
|
|
329
|
+
initialPinchDistance = distance;
|
|
330
|
+
}
|
|
331
|
+
const scale = distance / initialPinchDistance;
|
|
332
|
+
const centerX = (touch1.clientX + touch2.clientX) / 2;
|
|
333
|
+
const centerY = (touch1.clientY + touch2.clientY) / 2;
|
|
334
|
+
onPinch({ scale, centerX, centerY });
|
|
335
|
+
}
|
|
336
|
+
function handlePinchEnd() {
|
|
337
|
+
initialPinchDistance = null;
|
|
338
|
+
}
|
|
339
|
+
if (typeof element === "function" || element?.current !== void 0) {
|
|
340
|
+
_userEffect(() => {
|
|
341
|
+
const el = typeof element === "function" ? element() : element.current;
|
|
342
|
+
if (!el) return;
|
|
343
|
+
return attachListeners(el);
|
|
344
|
+
});
|
|
345
|
+
} else if (element) {
|
|
346
|
+
onCleanup(attachListeners(element));
|
|
347
|
+
}
|
|
348
|
+
function attachListeners(el) {
|
|
349
|
+
el.addEventListener("mousedown", handleStart);
|
|
350
|
+
el.addEventListener("touchstart", handleStart, { passive: !preventDefault });
|
|
351
|
+
window.addEventListener("mousemove", handleMove);
|
|
352
|
+
window.addEventListener("touchmove", handlePinchMove);
|
|
353
|
+
window.addEventListener("touchmove", handleMove);
|
|
354
|
+
window.addEventListener("mouseup", handleEnd);
|
|
355
|
+
window.addEventListener("touchend", handleEnd);
|
|
356
|
+
window.addEventListener("touchend", handlePinchEnd);
|
|
357
|
+
return () => {
|
|
358
|
+
el.removeEventListener("mousedown", handleStart);
|
|
359
|
+
el.removeEventListener("touchstart", handleStart);
|
|
360
|
+
window.removeEventListener("mousemove", handleMove);
|
|
361
|
+
window.removeEventListener("touchmove", handlePinchMove);
|
|
362
|
+
window.removeEventListener("touchmove", handleMove);
|
|
363
|
+
window.removeEventListener("mouseup", handleEnd);
|
|
364
|
+
window.removeEventListener("touchend", handleEnd);
|
|
365
|
+
window.removeEventListener("touchend", handlePinchEnd);
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
return state;
|
|
369
|
+
}
|
|
370
|
+
function useAnimatedValue(initialValue) {
|
|
371
|
+
const value = signal(initialValue);
|
|
372
|
+
let running = null;
|
|
373
|
+
const stopRunning = () => {
|
|
374
|
+
const stop = running;
|
|
375
|
+
running = null;
|
|
376
|
+
if (stop !== null) stop();
|
|
377
|
+
};
|
|
378
|
+
onCleanup(stopRunning);
|
|
379
|
+
const animate = (start) => {
|
|
380
|
+
stopRunning();
|
|
381
|
+
const stop = createRoot((dispose) => {
|
|
382
|
+
const end = () => {
|
|
383
|
+
if (running === end) running = null;
|
|
384
|
+
dispose();
|
|
385
|
+
};
|
|
386
|
+
untrack(() => start(end));
|
|
387
|
+
return end;
|
|
388
|
+
});
|
|
389
|
+
running = stop;
|
|
390
|
+
return { stop: () => {
|
|
391
|
+
if (running === stop) stopRunning();
|
|
392
|
+
} };
|
|
393
|
+
};
|
|
394
|
+
return {
|
|
395
|
+
value: () => value(),
|
|
396
|
+
setValue: (v) => value.set(v),
|
|
397
|
+
// Spring to target
|
|
398
|
+
spring(toValue, config = {}) {
|
|
399
|
+
return animate((end) => {
|
|
400
|
+
const s = spring(value.peek(), config);
|
|
401
|
+
s.set(toValue);
|
|
402
|
+
effect(() => {
|
|
403
|
+
value.set(s.current());
|
|
404
|
+
if (!s.isAnimating()) end();
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
},
|
|
408
|
+
// Tween to target
|
|
409
|
+
timing(toValue, config = {}) {
|
|
410
|
+
return animate((end) => {
|
|
411
|
+
tween(value.peek(), toValue, {
|
|
412
|
+
...config,
|
|
413
|
+
onUpdate: (v) => value.set(v),
|
|
414
|
+
onComplete: () => {
|
|
415
|
+
end();
|
|
416
|
+
if (config.onComplete) config.onComplete();
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
},
|
|
421
|
+
// Interpolate value
|
|
422
|
+
interpolate(inputRange, outputRange) {
|
|
423
|
+
return () => {
|
|
424
|
+
const v = value();
|
|
425
|
+
for (let i = 0; i < inputRange.length - 1; i++) {
|
|
426
|
+
if (v >= inputRange[i] && v <= inputRange[i + 1]) {
|
|
427
|
+
const t = (v - inputRange[i]) / (inputRange[i + 1] - inputRange[i]);
|
|
428
|
+
return outputRange[i] + (outputRange[i + 1] - outputRange[i]) * t;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
if (v <= inputRange[0]) return outputRange[0];
|
|
432
|
+
return outputRange[outputRange.length - 1];
|
|
433
|
+
};
|
|
434
|
+
},
|
|
435
|
+
subscribe: value.subscribe
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
function createTransitionClasses(name) {
|
|
439
|
+
return {
|
|
440
|
+
enter: `${name}-enter`,
|
|
441
|
+
enterActive: `${name}-enter-active`,
|
|
442
|
+
enterDone: `${name}-enter-done`,
|
|
443
|
+
exit: `${name}-exit`,
|
|
444
|
+
exitActive: `${name}-exit-active`,
|
|
445
|
+
exitDone: `${name}-exit-done`
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
async function cssTransition(element, name, type = "enter", duration = 300) {
|
|
449
|
+
const classes = createTransitionClasses(name);
|
|
450
|
+
return (
|
|
451
|
+
/** @type {Promise<void>} */
|
|
452
|
+
new Promise((resolve) => {
|
|
453
|
+
scheduleWrite(() => {
|
|
454
|
+
element.classList.add(classes[type]);
|
|
455
|
+
scheduleRead(() => {
|
|
456
|
+
element.offsetHeight;
|
|
457
|
+
scheduleWrite(() => {
|
|
458
|
+
element.classList.add(classes[`${type}Active`]);
|
|
459
|
+
setTimeout(() => {
|
|
460
|
+
scheduleWrite(() => {
|
|
461
|
+
element.classList.remove(classes[type], classes[`${type}Active`]);
|
|
462
|
+
element.classList.add(classes[`${type}Done`]);
|
|
463
|
+
resolve();
|
|
464
|
+
});
|
|
465
|
+
}, duration);
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
});
|
|
469
|
+
})
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
export {
|
|
473
|
+
createTransitionClasses,
|
|
474
|
+
cssTransition,
|
|
475
|
+
easings,
|
|
476
|
+
spring,
|
|
477
|
+
tween,
|
|
478
|
+
useAnimatedValue,
|
|
479
|
+
useGesture,
|
|
480
|
+
useTween
|
|
481
|
+
};
|