@bquery/bquery 1.3.0 → 1.4.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 +527 -501
- package/dist/{batch-4LAvfLE7.js → batch-x7b2eZST.js} +2 -2
- package/dist/{batch-4LAvfLE7.js.map → batch-x7b2eZST.js.map} +1 -1
- package/dist/component.es.mjs +1 -1
- package/dist/core/collection.d.ts +19 -3
- package/dist/core/collection.d.ts.map +1 -1
- package/dist/core/element.d.ts +23 -4
- package/dist/core/element.d.ts.map +1 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/utils/function.d.ts +21 -4
- package/dist/core/utils/function.d.ts.map +1 -1
- package/dist/{core-COenAZjD.js → core-BhpuvPhy.js} +62 -37
- package/dist/core-BhpuvPhy.js.map +1 -0
- package/dist/core.es.mjs +174 -131
- package/dist/core.es.mjs.map +1 -1
- package/dist/full.es.mjs +7 -7
- package/dist/full.iife.js +2 -2
- package/dist/full.iife.js.map +1 -1
- package/dist/full.umd.js +2 -2
- package/dist/full.umd.js.map +1 -1
- package/dist/index.es.mjs +7 -7
- package/dist/motion.es.mjs.map +1 -1
- package/dist/{persisted-Dz_ryNuC.js → persisted-DHoi3uEs.js} +4 -4
- package/dist/{persisted-Dz_ryNuC.js.map → persisted-DHoi3uEs.js.map} +1 -1
- package/dist/platform/storage.d.ts.map +1 -1
- package/dist/platform.es.mjs +12 -7
- package/dist/platform.es.mjs.map +1 -1
- package/dist/reactive/core.d.ts +12 -0
- package/dist/reactive/core.d.ts.map +1 -1
- package/dist/reactive/effect.d.ts.map +1 -1
- package/dist/reactive/internals.d.ts +6 -0
- package/dist/reactive/internals.d.ts.map +1 -1
- package/dist/reactive.es.mjs +6 -6
- package/dist/router.es.mjs +1 -1
- package/dist/{sanitize-1FBEPAFH.js → sanitize-Cxvxa-DX.js} +50 -39
- package/dist/sanitize-Cxvxa-DX.js.map +1 -0
- package/dist/security/sanitize-core.d.ts.map +1 -1
- package/dist/security.es.mjs +2 -2
- package/dist/store.es.mjs +2 -2
- package/dist/type-guards-BdKlYYlS.js +32 -0
- package/dist/type-guards-BdKlYYlS.js.map +1 -0
- package/dist/untrack-DNnnqdlR.js +6 -0
- package/dist/{untrack-BuEQKH7_.js.map → untrack-DNnnqdlR.js.map} +1 -1
- package/dist/view/evaluate.d.ts.map +1 -1
- package/dist/view.es.mjs +157 -151
- package/dist/view.es.mjs.map +1 -1
- package/dist/{watch-CXyaBC_9.js → watch-DXXv3iAI.js} +3 -3
- package/dist/{watch-CXyaBC_9.js.map → watch-DXXv3iAI.js.map} +1 -1
- package/package.json +132 -132
- package/src/core/collection.ts +628 -588
- package/src/core/element.ts +774 -746
- package/src/core/index.ts +48 -47
- package/src/core/utils/function.ts +151 -110
- package/src/motion/animate.ts +113 -113
- package/src/motion/flip.ts +176 -176
- package/src/motion/scroll.ts +57 -57
- package/src/motion/spring.ts +150 -150
- package/src/motion/timeline.ts +246 -246
- package/src/motion/transition.ts +51 -51
- package/src/platform/storage.ts +215 -208
- package/src/reactive/core.ts +114 -93
- package/src/reactive/effect.ts +54 -43
- package/src/reactive/internals.ts +122 -105
- package/src/security/sanitize-core.ts +364 -343
- package/src/view/evaluate.ts +290 -274
- package/dist/core-COenAZjD.js.map +0 -1
- package/dist/sanitize-1FBEPAFH.js.map +0 -1
- package/dist/type-guards-DRma3-Kc.js +0 -16
- package/dist/type-guards-DRma3-Kc.js.map +0 -1
- package/dist/untrack-BuEQKH7_.js +0 -6
package/src/motion/spring.ts
CHANGED
|
@@ -1,150 +1,150 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Spring physics helpers.
|
|
3
|
-
*
|
|
4
|
-
* @module bquery/motion
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { Spring, SpringConfig } from './types';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Default spring configuration values.
|
|
11
|
-
*/
|
|
12
|
-
const DEFAULT_SPRING_CONFIG: Required<SpringConfig> = {
|
|
13
|
-
stiffness: 100,
|
|
14
|
-
damping: 10,
|
|
15
|
-
mass: 1,
|
|
16
|
-
precision: 0.01,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Create a spring-based animation for smooth, physics-based motion.
|
|
21
|
-
*
|
|
22
|
-
* Uses variable frame rate timing based on `requestAnimationFrame` timestamps
|
|
23
|
-
* to ensure consistent animation speed across different devices and frame rates.
|
|
24
|
-
* Large time deltas (e.g., from tab backgrounding) are clamped to maintain
|
|
25
|
-
* simulation stability.
|
|
26
|
-
*
|
|
27
|
-
* @param initialValue - Starting value for the spring
|
|
28
|
-
* @param config - Spring physics configuration
|
|
29
|
-
* @returns Spring instance for controlling the animation
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```ts
|
|
33
|
-
* const x = spring(0, { stiffness: 120, damping: 14 });
|
|
34
|
-
* x.onChange((value) => {
|
|
35
|
-
* element.style.transform = `translateX(${value}px)`;
|
|
36
|
-
* });
|
|
37
|
-
* await x.to(100);
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export const spring = (initialValue: number, config: SpringConfig = {}): Spring => {
|
|
41
|
-
const { stiffness, damping, mass, precision } = {
|
|
42
|
-
...DEFAULT_SPRING_CONFIG,
|
|
43
|
-
...config,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
let current = initialValue;
|
|
47
|
-
let velocity = 0;
|
|
48
|
-
let target = initialValue;
|
|
49
|
-
let animationFrame: number | null = null;
|
|
50
|
-
let resolvePromise: (() => void) | null = null;
|
|
51
|
-
let lastTime: number | null = null;
|
|
52
|
-
const listeners = new Set<(value: number) => void>();
|
|
53
|
-
|
|
54
|
-
const notifyListeners = () => {
|
|
55
|
-
for (const listener of listeners) {
|
|
56
|
-
listener(current);
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const step = (timestamp: number) => {
|
|
61
|
-
// Calculate time delta (in seconds) from last frame
|
|
62
|
-
// If this is the first frame, use a sensible default (1/60s)
|
|
63
|
-
// This ensures the animation speed is independent of frame rate
|
|
64
|
-
const deltaTime = lastTime !== null ? (timestamp - lastTime) / 1000 : 1 / 60;
|
|
65
|
-
// Clamp large deltas to prevent instability (e.g. tab backgrounding)
|
|
66
|
-
// Maximum delta of 1/30s (~33ms) keeps simulation stable
|
|
67
|
-
const clampedDelta = Math.min(deltaTime, 1 / 30);
|
|
68
|
-
lastTime = timestamp;
|
|
69
|
-
|
|
70
|
-
// Spring physics calculation
|
|
71
|
-
const displacement = current - target;
|
|
72
|
-
const springForce = -stiffness * displacement;
|
|
73
|
-
const dampingForce = -damping * velocity;
|
|
74
|
-
const acceleration = (springForce + dampingForce) / mass;
|
|
75
|
-
|
|
76
|
-
velocity += acceleration * clampedDelta;
|
|
77
|
-
current += velocity * clampedDelta;
|
|
78
|
-
|
|
79
|
-
notifyListeners();
|
|
80
|
-
|
|
81
|
-
// Check if spring has settled
|
|
82
|
-
if (Math.abs(velocity) < precision && Math.abs(displacement) < precision) {
|
|
83
|
-
current = target;
|
|
84
|
-
velocity = 0;
|
|
85
|
-
animationFrame = null;
|
|
86
|
-
notifyListeners();
|
|
87
|
-
resolvePromise?.();
|
|
88
|
-
resolvePromise = null;
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
animationFrame = requestAnimationFrame(step);
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
return {
|
|
96
|
-
to(newTarget: number): Promise<void> {
|
|
97
|
-
target = newTarget;
|
|
98
|
-
|
|
99
|
-
if (animationFrame !== null) {
|
|
100
|
-
cancelAnimationFrame(animationFrame);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Resolve any pending promise from a previous to() call
|
|
104
|
-
// This ensures all returned promises eventually settle
|
|
105
|
-
resolvePromise?.();
|
|
106
|
-
|
|
107
|
-
// Reset lastTime to ensure clean start for new animation
|
|
108
|
-
lastTime = null;
|
|
109
|
-
|
|
110
|
-
return new Promise((resolve) => {
|
|
111
|
-
resolvePromise = resolve;
|
|
112
|
-
animationFrame = requestAnimationFrame(step);
|
|
113
|
-
});
|
|
114
|
-
},
|
|
115
|
-
|
|
116
|
-
current(): number {
|
|
117
|
-
return current;
|
|
118
|
-
},
|
|
119
|
-
|
|
120
|
-
stop(): void {
|
|
121
|
-
if (animationFrame !== null) {
|
|
122
|
-
cancelAnimationFrame(animationFrame);
|
|
123
|
-
animationFrame = null;
|
|
124
|
-
}
|
|
125
|
-
velocity = 0;
|
|
126
|
-
lastTime = null;
|
|
127
|
-
resolvePromise?.();
|
|
128
|
-
resolvePromise = null;
|
|
129
|
-
},
|
|
130
|
-
|
|
131
|
-
onChange(callback: (value: number) => void): () => void {
|
|
132
|
-
listeners.add(callback);
|
|
133
|
-
return () => listeners.delete(callback);
|
|
134
|
-
},
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Preset spring configurations for common use cases.
|
|
140
|
-
*/
|
|
141
|
-
export const springPresets = {
|
|
142
|
-
/** Gentle, slow-settling spring */
|
|
143
|
-
gentle: { stiffness: 80, damping: 15 } as SpringConfig,
|
|
144
|
-
/** Responsive, snappy spring */
|
|
145
|
-
snappy: { stiffness: 200, damping: 20 } as SpringConfig,
|
|
146
|
-
/** Bouncy, playful spring */
|
|
147
|
-
bouncy: { stiffness: 300, damping: 8 } as SpringConfig,
|
|
148
|
-
/** Stiff, quick spring with minimal overshoot */
|
|
149
|
-
stiff: { stiffness: 400, damping: 30 } as SpringConfig,
|
|
150
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Spring physics helpers.
|
|
3
|
+
*
|
|
4
|
+
* @module bquery/motion
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Spring, SpringConfig } from './types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Default spring configuration values.
|
|
11
|
+
*/
|
|
12
|
+
const DEFAULT_SPRING_CONFIG: Required<SpringConfig> = {
|
|
13
|
+
stiffness: 100,
|
|
14
|
+
damping: 10,
|
|
15
|
+
mass: 1,
|
|
16
|
+
precision: 0.01,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Create a spring-based animation for smooth, physics-based motion.
|
|
21
|
+
*
|
|
22
|
+
* Uses variable frame rate timing based on `requestAnimationFrame` timestamps
|
|
23
|
+
* to ensure consistent animation speed across different devices and frame rates.
|
|
24
|
+
* Large time deltas (e.g., from tab backgrounding) are clamped to maintain
|
|
25
|
+
* simulation stability.
|
|
26
|
+
*
|
|
27
|
+
* @param initialValue - Starting value for the spring
|
|
28
|
+
* @param config - Spring physics configuration
|
|
29
|
+
* @returns Spring instance for controlling the animation
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const x = spring(0, { stiffness: 120, damping: 14 });
|
|
34
|
+
* x.onChange((value) => {
|
|
35
|
+
* element.style.transform = `translateX(${value}px)`;
|
|
36
|
+
* });
|
|
37
|
+
* await x.to(100);
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export const spring = (initialValue: number, config: SpringConfig = {}): Spring => {
|
|
41
|
+
const { stiffness, damping, mass, precision } = {
|
|
42
|
+
...DEFAULT_SPRING_CONFIG,
|
|
43
|
+
...config,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
let current = initialValue;
|
|
47
|
+
let velocity = 0;
|
|
48
|
+
let target = initialValue;
|
|
49
|
+
let animationFrame: number | null = null;
|
|
50
|
+
let resolvePromise: (() => void) | null = null;
|
|
51
|
+
let lastTime: number | null = null;
|
|
52
|
+
const listeners = new Set<(value: number) => void>();
|
|
53
|
+
|
|
54
|
+
const notifyListeners = () => {
|
|
55
|
+
for (const listener of listeners) {
|
|
56
|
+
listener(current);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const step = (timestamp: number) => {
|
|
61
|
+
// Calculate time delta (in seconds) from last frame
|
|
62
|
+
// If this is the first frame, use a sensible default (1/60s)
|
|
63
|
+
// This ensures the animation speed is independent of frame rate
|
|
64
|
+
const deltaTime = lastTime !== null ? (timestamp - lastTime) / 1000 : 1 / 60;
|
|
65
|
+
// Clamp large deltas to prevent instability (e.g. tab backgrounding)
|
|
66
|
+
// Maximum delta of 1/30s (~33ms) keeps simulation stable
|
|
67
|
+
const clampedDelta = Math.min(deltaTime, 1 / 30);
|
|
68
|
+
lastTime = timestamp;
|
|
69
|
+
|
|
70
|
+
// Spring physics calculation
|
|
71
|
+
const displacement = current - target;
|
|
72
|
+
const springForce = -stiffness * displacement;
|
|
73
|
+
const dampingForce = -damping * velocity;
|
|
74
|
+
const acceleration = (springForce + dampingForce) / mass;
|
|
75
|
+
|
|
76
|
+
velocity += acceleration * clampedDelta;
|
|
77
|
+
current += velocity * clampedDelta;
|
|
78
|
+
|
|
79
|
+
notifyListeners();
|
|
80
|
+
|
|
81
|
+
// Check if spring has settled
|
|
82
|
+
if (Math.abs(velocity) < precision && Math.abs(displacement) < precision) {
|
|
83
|
+
current = target;
|
|
84
|
+
velocity = 0;
|
|
85
|
+
animationFrame = null;
|
|
86
|
+
notifyListeners();
|
|
87
|
+
resolvePromise?.();
|
|
88
|
+
resolvePromise = null;
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
animationFrame = requestAnimationFrame(step);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
to(newTarget: number): Promise<void> {
|
|
97
|
+
target = newTarget;
|
|
98
|
+
|
|
99
|
+
if (animationFrame !== null) {
|
|
100
|
+
cancelAnimationFrame(animationFrame);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Resolve any pending promise from a previous to() call
|
|
104
|
+
// This ensures all returned promises eventually settle
|
|
105
|
+
resolvePromise?.();
|
|
106
|
+
|
|
107
|
+
// Reset lastTime to ensure clean start for new animation
|
|
108
|
+
lastTime = null;
|
|
109
|
+
|
|
110
|
+
return new Promise((resolve) => {
|
|
111
|
+
resolvePromise = resolve;
|
|
112
|
+
animationFrame = requestAnimationFrame(step);
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
current(): number {
|
|
117
|
+
return current;
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
stop(): void {
|
|
121
|
+
if (animationFrame !== null) {
|
|
122
|
+
cancelAnimationFrame(animationFrame);
|
|
123
|
+
animationFrame = null;
|
|
124
|
+
}
|
|
125
|
+
velocity = 0;
|
|
126
|
+
lastTime = null;
|
|
127
|
+
resolvePromise?.();
|
|
128
|
+
resolvePromise = null;
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
onChange(callback: (value: number) => void): () => void {
|
|
132
|
+
listeners.add(callback);
|
|
133
|
+
return () => listeners.delete(callback);
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Preset spring configurations for common use cases.
|
|
140
|
+
*/
|
|
141
|
+
export const springPresets = {
|
|
142
|
+
/** Gentle, slow-settling spring */
|
|
143
|
+
gentle: { stiffness: 80, damping: 15 } as SpringConfig,
|
|
144
|
+
/** Responsive, snappy spring */
|
|
145
|
+
snappy: { stiffness: 200, damping: 20 } as SpringConfig,
|
|
146
|
+
/** Bouncy, playful spring */
|
|
147
|
+
bouncy: { stiffness: 300, damping: 8 } as SpringConfig,
|
|
148
|
+
/** Stiff, quick spring with minimal overshoot */
|
|
149
|
+
stiff: { stiffness: 400, damping: 30 } as SpringConfig,
|
|
150
|
+
};
|