coily 0.10.1 → 0.11.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,121 @@
1
+ import { n as SpringConfig, t as createSpringSystem } from "./system-Dxxv4ee7.js";
2
+ import { computed, customRef, defineComponent, inject, onBeforeUnmount, onMounted, 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() {
10
+ const system = createSpringSystem();
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
+ function useSpring(target, options) {
27
+ const system = inject(SpringSystemKey);
28
+ if (!system) throw new Error("No SpringSystem found");
29
+ const config = computed(() => {
30
+ const opts = toValue(options);
31
+ if (opts instanceof SpringConfig) return opts;
32
+ return new SpringConfig(opts ?? defaultOptions);
33
+ });
34
+ const spring = system.createSpring(toValue(target), config.value);
35
+ watchSyncEffect(() => {
36
+ spring.configure(config.value);
37
+ });
38
+ let triggerValue;
39
+ let triggerVelocity;
40
+ spring.onUpdate(() => {
41
+ triggerValue?.();
42
+ triggerVelocity?.();
43
+ });
44
+ const value = customRef((track, trigger) => {
45
+ triggerValue = trigger;
46
+ return {
47
+ get() {
48
+ track();
49
+ return spring.value;
50
+ },
51
+ set(value) {
52
+ spring.value = value;
53
+ trigger();
54
+ }
55
+ };
56
+ });
57
+ const velocity = customRef((track, trigger) => {
58
+ triggerVelocity = trigger;
59
+ return {
60
+ get() {
61
+ track();
62
+ return spring.velocity;
63
+ },
64
+ set(value) {
65
+ spring.velocity = value;
66
+ trigger();
67
+ }
68
+ };
69
+ });
70
+ const resting = customRef((track, trigger) => {
71
+ spring.onStart(trigger);
72
+ spring.onStop(trigger);
73
+ return {
74
+ get() {
75
+ track();
76
+ return spring.resting;
77
+ },
78
+ set() {}
79
+ };
80
+ });
81
+ watchSyncEffect(() => {
82
+ spring.target = toValue(target);
83
+ });
84
+ return {
85
+ value,
86
+ velocity,
87
+ resting,
88
+ jumpTo: (value) => {
89
+ spring.jumpTo(value);
90
+ }
91
+ };
92
+ }
93
+ //#endregion
94
+ //#region src/vue/component.ts
95
+ const SpringValue = defineComponent((props, { slots }) => {
96
+ const { target, mass, tension, damping, precision } = toRefs(props);
97
+ const spring = useSpring(target, () => ({
98
+ mass: mass?.value ?? 1,
99
+ tension: tension?.value ?? 100,
100
+ damping: damping?.value ?? 10,
101
+ precision: precision?.value ?? 2
102
+ }));
103
+ return () => {
104
+ return slots.default?.({
105
+ value: spring.value.value,
106
+ velocity: spring.velocity.value,
107
+ resting: spring.resting.value
108
+ }) ?? null;
109
+ };
110
+ }, {
111
+ name: "SpringValue",
112
+ props: [
113
+ "target",
114
+ "mass",
115
+ "tension",
116
+ "damping",
117
+ "precision"
118
+ ]
119
+ });
120
+ //#endregion
121
+ export { SpringValue, provideSpringSystem, useSpring, useSpringSystem };
package/package.json CHANGED
@@ -1,47 +1,66 @@
1
1
  {
2
2
  "name": "coily",
3
- "version": "0.10.1",
3
+ "version": "0.11.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 };