coily 0.10.1 → 0.12.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/dist/vue.js ADDED
@@ -0,0 +1,367 @@
1
+ import { n as SpringConfig, t as createSpringSystem } from "./system-DPaqec-Z.js";
2
+ import { computed, customRef, defineComponent, getCurrentScope, inject, onBeforeUnmount, onMounted, onScopeDispose, provide, toRefs, toValue, watchSyncEffect } from "vue";
3
+ //#region src/vue/system.ts
4
+ const SpringSystemKey = Symbol.for("coily/spring-system");
5
+ function provideSpringSystem(system, app) {
6
+ if (app) app.provide(SpringSystemKey, system);
7
+ else provide(SpringSystemKey, system);
8
+ }
9
+ function useSpringSystem(options) {
10
+ const system = createSpringSystem(options);
11
+ provideSpringSystem(system);
12
+ onMounted(() => {
13
+ system.start();
14
+ });
15
+ onBeforeUnmount(() => {
16
+ system.stop();
17
+ });
18
+ }
19
+ //#endregion
20
+ //#region src/vue/spring.ts
21
+ const defaultOptions = {
22
+ tension: 100,
23
+ damping: 10,
24
+ precision: 2
25
+ };
26
+ /** @internal Symbol to access the underlying Spring from a SpringRef */
27
+ const SpringInstanceKey = Symbol("spring");
28
+ function hasSpringInstance(value) {
29
+ return typeof value === "object" && value !== null && SpringInstanceKey in value;
30
+ }
31
+ function useSpring(target, options) {
32
+ if (Array.isArray(target)) return Array.from(target, (t) => createSpringRef(t, options));
33
+ if (hasSpringInstance(target)) return createLinkedSpringRef(target, options);
34
+ return createSpringRef(target, options);
35
+ }
36
+ function createSpringRef(target, options) {
37
+ const system = inject(SpringSystemKey);
38
+ if (!system) throw new Error("No SpringSystem found");
39
+ const config = computed(() => {
40
+ const opts = toValue(options);
41
+ if (opts instanceof SpringConfig) return opts;
42
+ return new SpringConfig(opts ?? defaultOptions);
43
+ });
44
+ const spring = system.createSpring(toValue(target), config.value);
45
+ watchSyncEffect(() => {
46
+ spring.configure(config.value);
47
+ });
48
+ let triggerValue;
49
+ let triggerVelocity;
50
+ let triggerTimeRemaining;
51
+ spring.onUpdate(() => {
52
+ triggerValue?.();
53
+ triggerVelocity?.();
54
+ triggerTimeRemaining?.();
55
+ });
56
+ const value = customRef((track, trigger) => ({
57
+ get() {
58
+ triggerValue ??= trigger;
59
+ track();
60
+ return spring.value;
61
+ },
62
+ set(value) {
63
+ spring.value = value;
64
+ trigger();
65
+ }
66
+ }));
67
+ const velocity = customRef((track, trigger) => ({
68
+ get() {
69
+ triggerVelocity ??= trigger;
70
+ track();
71
+ return spring.velocity;
72
+ },
73
+ set(value) {
74
+ spring.velocity = value;
75
+ trigger();
76
+ }
77
+ }));
78
+ const timeRemaining = customRef((track, trigger) => ({
79
+ get() {
80
+ triggerTimeRemaining ??= trigger;
81
+ track();
82
+ return spring.timeRemaining;
83
+ },
84
+ set() {}
85
+ }));
86
+ const isResting = customRef((track, trigger) => {
87
+ spring.onStart(trigger);
88
+ spring.onStop(trigger);
89
+ return {
90
+ get() {
91
+ track();
92
+ return spring.isResting;
93
+ },
94
+ set() {}
95
+ };
96
+ });
97
+ watchSyncEffect(() => {
98
+ spring.target = toValue(target);
99
+ });
100
+ if (getCurrentScope()) onScopeDispose(() => spring.dispose());
101
+ const ref = Object.assign(value, {
102
+ velocity,
103
+ timeRemaining,
104
+ isResting,
105
+ jumpTo: (value) => spring.jumpTo(value)
106
+ });
107
+ Object.defineProperty(ref, SpringInstanceKey, { value: spring });
108
+ return ref;
109
+ }
110
+ function createLinkedSpringRef(leaderRef, options) {
111
+ const system = inject(SpringSystemKey);
112
+ if (!system) throw new Error("No SpringSystem found");
113
+ const leaderSpring = leaderRef[SpringInstanceKey];
114
+ const config = computed(() => {
115
+ const opts = toValue(options);
116
+ if (!opts) return void 0;
117
+ if (opts instanceof SpringConfig) return opts;
118
+ return new SpringConfig(opts);
119
+ });
120
+ const spring = system.createSpring({ target: leaderSpring }, config.value);
121
+ watchSyncEffect(() => {
122
+ const c = config.value;
123
+ if (c) spring.configure(c);
124
+ else spring.clearConfigOverride();
125
+ });
126
+ let triggerValue;
127
+ let triggerVelocity;
128
+ let triggerTimeRemaining;
129
+ spring.onUpdate(() => {
130
+ triggerValue?.();
131
+ triggerVelocity?.();
132
+ triggerTimeRemaining?.();
133
+ });
134
+ const value = customRef((track, trigger) => ({
135
+ get() {
136
+ triggerValue ??= trigger;
137
+ track();
138
+ return spring.value;
139
+ },
140
+ set(value) {
141
+ spring.value = value;
142
+ trigger();
143
+ }
144
+ }));
145
+ const velocity = customRef((track, trigger) => ({
146
+ get() {
147
+ triggerVelocity ??= trigger;
148
+ track();
149
+ return spring.velocity;
150
+ },
151
+ set(value) {
152
+ spring.velocity = value;
153
+ trigger();
154
+ }
155
+ }));
156
+ const timeRemaining = customRef((track, trigger) => ({
157
+ get() {
158
+ triggerTimeRemaining ??= trigger;
159
+ track();
160
+ return spring.timeRemaining;
161
+ },
162
+ set() {}
163
+ }));
164
+ const isResting = customRef((track, trigger) => {
165
+ spring.onStart(trigger);
166
+ spring.onStop(trigger);
167
+ return {
168
+ get() {
169
+ track();
170
+ return spring.isResting;
171
+ },
172
+ set() {}
173
+ };
174
+ });
175
+ if (getCurrentScope()) onScopeDispose(() => spring.dispose());
176
+ const ref = Object.assign(value, {
177
+ velocity,
178
+ timeRemaining,
179
+ isResting,
180
+ jumpTo: (value) => spring.jumpTo(value)
181
+ });
182
+ Object.defineProperty(ref, SpringInstanceKey, { value: spring });
183
+ return ref;
184
+ }
185
+ /** @internal Symbol to access the underlying Spring2D from a SpringRef2D */
186
+ const Spring2DInstanceKey = Symbol("spring2d");
187
+ function hasSpring2DInstance(value) {
188
+ return typeof value === "object" && value !== null && Spring2DInstanceKey in value;
189
+ }
190
+ function useSpring2D(target, options) {
191
+ if (hasSpring2DInstance(target)) return createLinkedSpringRef2D(target, options);
192
+ return createSpringRef2D(target, options);
193
+ }
194
+ function createSpringRef2D(target, options) {
195
+ const system = inject(SpringSystemKey);
196
+ if (!system) throw new Error("No SpringSystem found");
197
+ const config = computed(() => {
198
+ const opts = toValue(options);
199
+ if (opts instanceof SpringConfig) return opts;
200
+ return new SpringConfig(opts ?? defaultOptions);
201
+ });
202
+ const spring = system.createSpring2D(toValue(target), config.value);
203
+ watchSyncEffect(() => {
204
+ spring.configure(config.value);
205
+ });
206
+ let triggerValue;
207
+ let triggerVelocity;
208
+ let triggerTimeRemaining;
209
+ spring.onUpdate(() => {
210
+ triggerValue?.();
211
+ triggerVelocity?.();
212
+ triggerTimeRemaining?.();
213
+ });
214
+ const value = customRef((track, trigger) => ({
215
+ get() {
216
+ triggerValue ??= trigger;
217
+ track();
218
+ return spring.value;
219
+ },
220
+ set(value) {
221
+ spring.value = value;
222
+ trigger();
223
+ }
224
+ }));
225
+ const velocity = customRef((track, trigger) => ({
226
+ get() {
227
+ triggerVelocity ??= trigger;
228
+ track();
229
+ return spring.velocity;
230
+ },
231
+ set(value) {
232
+ spring.velocity = value;
233
+ trigger();
234
+ }
235
+ }));
236
+ const timeRemaining = customRef((track, trigger) => ({
237
+ get() {
238
+ triggerTimeRemaining ??= trigger;
239
+ track();
240
+ return spring.timeRemaining;
241
+ },
242
+ set() {}
243
+ }));
244
+ const isResting = customRef((track, trigger) => {
245
+ spring.onStart(trigger);
246
+ spring.onStop(trigger);
247
+ return {
248
+ get() {
249
+ track();
250
+ return spring.isResting;
251
+ },
252
+ set() {}
253
+ };
254
+ });
255
+ watchSyncEffect(() => {
256
+ spring.target = toValue(target);
257
+ });
258
+ if (getCurrentScope()) onScopeDispose(() => spring.dispose());
259
+ const ref = Object.assign(value, {
260
+ velocity,
261
+ timeRemaining,
262
+ isResting,
263
+ jumpTo: (value) => spring.jumpTo(value)
264
+ });
265
+ Object.defineProperty(ref, Spring2DInstanceKey, { value: spring });
266
+ return ref;
267
+ }
268
+ function createLinkedSpringRef2D(leaderRef, options) {
269
+ const system = inject(SpringSystemKey);
270
+ if (!system) throw new Error("No SpringSystem found");
271
+ const leaderSpring = leaderRef[Spring2DInstanceKey];
272
+ const config = computed(() => {
273
+ const opts = toValue(options);
274
+ if (!opts) return void 0;
275
+ if (opts instanceof SpringConfig) return opts;
276
+ return new SpringConfig(opts);
277
+ });
278
+ const spring = system.createSpring2D({ target: leaderSpring }, config.value);
279
+ watchSyncEffect(() => {
280
+ const c = config.value;
281
+ if (c) spring.configure(c);
282
+ else spring.clearConfigOverride();
283
+ });
284
+ let triggerValue;
285
+ let triggerVelocity;
286
+ let triggerTimeRemaining;
287
+ spring.onUpdate(() => {
288
+ triggerValue?.();
289
+ triggerVelocity?.();
290
+ triggerTimeRemaining?.();
291
+ });
292
+ const value = customRef((track, trigger) => ({
293
+ get() {
294
+ triggerValue ??= trigger;
295
+ track();
296
+ return spring.value;
297
+ },
298
+ set(value) {
299
+ spring.value = value;
300
+ trigger();
301
+ }
302
+ }));
303
+ const velocity = customRef((track, trigger) => ({
304
+ get() {
305
+ triggerVelocity ??= trigger;
306
+ track();
307
+ return spring.velocity;
308
+ },
309
+ set(value) {
310
+ spring.velocity = value;
311
+ trigger();
312
+ }
313
+ }));
314
+ const timeRemaining = customRef((track, trigger) => ({
315
+ get() {
316
+ triggerTimeRemaining ??= trigger;
317
+ track();
318
+ return spring.timeRemaining;
319
+ },
320
+ set() {}
321
+ }));
322
+ const isResting = customRef((track, trigger) => {
323
+ spring.onStart(trigger);
324
+ spring.onStop(trigger);
325
+ return {
326
+ get() {
327
+ track();
328
+ return spring.isResting;
329
+ },
330
+ set() {}
331
+ };
332
+ });
333
+ if (getCurrentScope()) onScopeDispose(() => spring.dispose());
334
+ const ref = Object.assign(value, {
335
+ velocity,
336
+ timeRemaining,
337
+ isResting,
338
+ jumpTo: (value) => spring.jumpTo(value)
339
+ });
340
+ Object.defineProperty(ref, Spring2DInstanceKey, { value: spring });
341
+ return ref;
342
+ }
343
+ //#endregion
344
+ //#region src/vue/component.ts
345
+ const SpringValue = defineComponent((props, { slots, expose }) => {
346
+ const { target, config } = toRefs(props);
347
+ const spring = useSpring(target, config);
348
+ expose({
349
+ value: spring,
350
+ velocity: spring.velocity,
351
+ isResting: spring.isResting,
352
+ timeRemaining: spring.timeRemaining,
353
+ jumpTo: spring.jumpTo
354
+ });
355
+ return () => slots.default?.({
356
+ value: spring.value,
357
+ velocity: spring.velocity.value,
358
+ isResting: spring.isResting.value,
359
+ timeRemaining: spring.timeRemaining.value,
360
+ jumpTo: spring.jumpTo
361
+ }) ?? null;
362
+ }, {
363
+ name: "SpringValue",
364
+ props: ["target", "config"]
365
+ });
366
+ //#endregion
367
+ export { SpringValue, provideSpringSystem, useSpring, useSpring2D, useSpringSystem };
package/package.json CHANGED
@@ -1,47 +1,66 @@
1
1
  {
2
2
  "name": "coily",
3
- "version": "0.10.1",
3
+ "version": "0.12.0",
4
4
  "description": "Simulate values using spring physics",
5
- "author": "Tim Morris <timthewebguy@gmail.com>",
6
5
  "homepage": "https://github.com/tkofh/coily#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/tkofh/coily/issues"
8
+ },
7
9
  "license": "MIT",
8
- "sideEffects": false,
9
- "type": "module",
10
- "module": "./dist/api.mjs",
11
- "types": "./dist/api.d.mts",
12
- "exports": {
13
- ".": {
14
- "import": "./dist/api.mjs",
15
- "types": "./dist/api.d.mts"
16
- },
17
- "./loop": {
18
- "import": "./dist/loop.mjs",
19
- "types": "./dist/loop.d.mts"
20
- }
10
+ "author": "Tim Morris <timthewebguy@gmail.com>",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/tkofh/coily.git"
21
14
  },
22
15
  "files": [
23
16
  "dist"
24
17
  ],
18
+ "type": "module",
19
+ "sideEffects": false,
20
+ "exports": {
21
+ ".": "./dist/index.js",
22
+ "./nuxt": "./dist/nuxt.js",
23
+ "./vue": "./dist/vue.js",
24
+ "./package.json": "./package.json"
25
+ },
25
26
  "publishConfig": {
26
27
  "access": "public"
27
28
  },
28
- "repository": {
29
- "type": "git",
30
- "url": "git+https://github.com/tkofh/coily.git"
29
+ "devDependencies": {
30
+ "@nuxt/kit": "^4.4.2",
31
+ "@nuxt/schema": "^4.4.2",
32
+ "@vitejs/plugin-vue": "^6.0.5",
33
+ "@vitest/browser-playwright": "^4.1.0",
34
+ "@vitest/coverage-v8": "^4.1.0",
35
+ "@vue/test-utils": "^2.4.6",
36
+ "fast-check": "^4.6.0",
37
+ "happy-dom": "^20.8.4",
38
+ "tsdown": "^0.21.3",
39
+ "vitest": "^4.1.0",
40
+ "vitest-browser-vue": "^2.1.0",
41
+ "vue": "^3.5.30"
31
42
  },
32
- "bugs": {
33
- "url": "https://github.com/tkofh/coily/issues"
43
+ "peerDependencies": {
44
+ "@nuxt/kit": "^4.0.0",
45
+ "vue": "^3.4.0"
34
46
  },
35
- "devDependencies": {
36
- "unbuild": "^2.0.0",
37
- "vitest": "^2.1.4",
38
- "@repo/tsconfig": "0.0.0"
47
+ "peerDependenciesMeta": {
48
+ "vue": {
49
+ "optional": true
50
+ },
51
+ "@nuxt/kit": {
52
+ "optional": true
53
+ }
39
54
  },
40
- "dependencies": {
41
- "tickloop": "^0.5.0"
55
+ "engines": {
56
+ "node": ">=24.10.0"
42
57
  },
43
58
  "scripts": {
44
- "build": "unbuild",
45
- "test": "vitest run"
59
+ "build": "tsdown",
60
+ "test": "vitest run",
61
+ "test:coverage": "vitest run --coverage",
62
+ "bench": "vitest bench --run",
63
+ "bench:baseline": "vitest bench --run --outputJson bench-baseline.json",
64
+ "bench:compare": "vitest bench --run --compare bench-baseline.json"
46
65
  }
47
66
  }
package/dist/api.d.mts DELETED
@@ -1,110 +0,0 @@
1
- interface SolverOptions {
2
- mass: number;
3
- tension: number;
4
- damping: number;
5
- position: number;
6
- velocity: number;
7
- precision: number;
8
- }
9
- declare class Solver {
10
- #private;
11
- constructor(options: SolverOptions);
12
- get mass(): number;
13
- set mass(value: number);
14
- get tension(): number;
15
- set tension(value: number);
16
- get damping(): number;
17
- set damping(value: number);
18
- get position(): number;
19
- set position(value: number);
20
- get velocity(): number;
21
- set velocity(value: number);
22
- get precision(): number;
23
- set precision(value: number);
24
- get resting(): boolean;
25
- get naturalFrequency(): number;
26
- get criticalDamping(): number;
27
- get dampingRatio(): number;
28
- tick(dt: number, emit?: boolean): void;
29
- onUpdate(callback: () => void): () => void;
30
- onStart(callback: () => void): () => void;
31
- onStop(callback: () => void): () => void;
32
- }
33
-
34
- declare class Scheduler {
35
- #private;
36
- tick(delta: number): void;
37
- add(solver: Solver): void;
38
- remove(solver: Solver): void;
39
- has(solver: Solver): boolean;
40
- }
41
-
42
- interface SpringOptions {
43
- /**
44
- * The mass of the spring
45
- *
46
- * must be greater than 0
47
- */
48
- mass: number;
49
- /**
50
- * The tension of the spring
51
- *
52
- * must be greater than 0
53
- */
54
- tension: number;
55
- /**
56
- * The damping of the spring
57
- *
58
- * must be greater than or equal to 0
59
- */
60
- damping: number;
61
- /**
62
- * The target of the spring
63
- *
64
- * defaults to 0
65
- */
66
- target?: number;
67
- /**
68
- * The current value of the spring
69
- *
70
- * defaults to the target
71
- */
72
- value?: number;
73
- /**
74
- * The precision of the solver
75
- *
76
- * defaults to 2
77
- */
78
- precision?: number;
79
- }
80
- declare class Spring {
81
- #private;
82
- constructor(scheduler: Scheduler, options: SpringOptions);
83
- get target(): number;
84
- set target(value: number);
85
- get value(): number;
86
- set value(value: number);
87
- get velocity(): number;
88
- set velocity(value: number);
89
- get mass(): number;
90
- set mass(value: number);
91
- get tension(): number;
92
- set tension(value: number);
93
- get damping(): number;
94
- set damping(value: number);
95
- get precision(): number;
96
- set precision(value: number);
97
- get resting(): boolean;
98
- onUpdate(callback: () => void): () => void;
99
- onStart(callback: () => void): () => void;
100
- onStop(callback: () => void): () => void;
101
- jumpTo(value: number): void;
102
- }
103
-
104
- interface SpringSystem {
105
- createSpring(options: SpringOptions): Spring;
106
- tick(dt: number): void;
107
- }
108
- declare function createSpringSystem(): SpringSystem;
109
-
110
- export { Spring, type SpringSystem, createSpringSystem };
package/dist/api.d.ts DELETED
@@ -1,110 +0,0 @@
1
- interface SolverOptions {
2
- mass: number;
3
- tension: number;
4
- damping: number;
5
- position: number;
6
- velocity: number;
7
- precision: number;
8
- }
9
- declare class Solver {
10
- #private;
11
- constructor(options: SolverOptions);
12
- get mass(): number;
13
- set mass(value: number);
14
- get tension(): number;
15
- set tension(value: number);
16
- get damping(): number;
17
- set damping(value: number);
18
- get position(): number;
19
- set position(value: number);
20
- get velocity(): number;
21
- set velocity(value: number);
22
- get precision(): number;
23
- set precision(value: number);
24
- get resting(): boolean;
25
- get naturalFrequency(): number;
26
- get criticalDamping(): number;
27
- get dampingRatio(): number;
28
- tick(dt: number, emit?: boolean): void;
29
- onUpdate(callback: () => void): () => void;
30
- onStart(callback: () => void): () => void;
31
- onStop(callback: () => void): () => void;
32
- }
33
-
34
- declare class Scheduler {
35
- #private;
36
- tick(delta: number): void;
37
- add(solver: Solver): void;
38
- remove(solver: Solver): void;
39
- has(solver: Solver): boolean;
40
- }
41
-
42
- interface SpringOptions {
43
- /**
44
- * The mass of the spring
45
- *
46
- * must be greater than 0
47
- */
48
- mass: number;
49
- /**
50
- * The tension of the spring
51
- *
52
- * must be greater than 0
53
- */
54
- tension: number;
55
- /**
56
- * The damping of the spring
57
- *
58
- * must be greater than or equal to 0
59
- */
60
- damping: number;
61
- /**
62
- * The target of the spring
63
- *
64
- * defaults to 0
65
- */
66
- target?: number;
67
- /**
68
- * The current value of the spring
69
- *
70
- * defaults to the target
71
- */
72
- value?: number;
73
- /**
74
- * The precision of the solver
75
- *
76
- * defaults to 2
77
- */
78
- precision?: number;
79
- }
80
- declare class Spring {
81
- #private;
82
- constructor(scheduler: Scheduler, options: SpringOptions);
83
- get target(): number;
84
- set target(value: number);
85
- get value(): number;
86
- set value(value: number);
87
- get velocity(): number;
88
- set velocity(value: number);
89
- get mass(): number;
90
- set mass(value: number);
91
- get tension(): number;
92
- set tension(value: number);
93
- get damping(): number;
94
- set damping(value: number);
95
- get precision(): number;
96
- set precision(value: number);
97
- get resting(): boolean;
98
- onUpdate(callback: () => void): () => void;
99
- onStart(callback: () => void): () => void;
100
- onStop(callback: () => void): () => void;
101
- jumpTo(value: number): void;
102
- }
103
-
104
- interface SpringSystem {
105
- createSpring(options: SpringOptions): Spring;
106
- tick(dt: number): void;
107
- }
108
- declare function createSpringSystem(): SpringSystem;
109
-
110
- export { Spring, type SpringSystem, createSpringSystem };