coily 0.11.0 → 0.12.1

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 CHANGED
@@ -1,13 +1,13 @@
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";
1
+ import { n as SpringConfig, t as createSpringSystem } from "./system-BD52hyd8.js";
2
+ import { computed, customRef, defineComponent, getCurrentScope, inject, onBeforeUnmount, onMounted, onScopeDispose, provide, toRefs, toValue, watchSyncEffect } from "vue";
3
3
  //#region src/vue/system.ts
4
4
  const SpringSystemKey = Symbol.for("coily/spring-system");
5
5
  function provideSpringSystem(system, app) {
6
6
  if (app) app.provide(SpringSystemKey, system);
7
7
  else provide(SpringSystemKey, system);
8
8
  }
9
- function useSpringSystem() {
10
- const system = createSpringSystem();
9
+ function useSpringSystem(options) {
10
+ const system = createSpringSystem(options);
11
11
  provideSpringSystem(system);
12
12
  onMounted(() => {
13
13
  system.start();
@@ -18,62 +18,238 @@ function useSpringSystem() {
18
18
  }
19
19
  //#endregion
20
20
  //#region src/vue/spring.ts
21
- const defaultOptions = {
22
- tension: 100,
23
- damping: 10,
24
- precision: 2
25
- };
21
+ /** @internal Symbol to access the underlying Spring from a SpringRef */
22
+ const SpringInstanceKey = Symbol("spring");
23
+ function hasSpringInstance(value) {
24
+ return typeof value === "object" && value !== null && SpringInstanceKey in value;
25
+ }
26
26
  function useSpring(target, options) {
27
+ if (Array.isArray(target)) return Array.from(target, (t) => createSpringRef(t, options));
28
+ if (hasSpringInstance(target)) return createLinkedSpringRef(target, options);
29
+ return createSpringRef(target, options);
30
+ }
31
+ function createSpringRef(target, options) {
27
32
  const system = inject(SpringSystemKey);
28
33
  if (!system) throw new Error("No SpringSystem found");
29
34
  const config = computed(() => {
30
35
  const opts = toValue(options);
31
36
  if (opts instanceof SpringConfig) return opts;
32
- return new SpringConfig(opts ?? defaultOptions);
37
+ return opts ? new SpringConfig(opts) : void 0;
33
38
  });
34
- const spring = system.createSpring(toValue(target), config.value);
39
+ const spring = system.createSpring(toValue(target), config.value ?? void 0);
35
40
  watchSyncEffect(() => {
36
- spring.configure(config.value);
41
+ const c = config.value;
42
+ if (c) spring.config = c;
43
+ else spring.config = null;
37
44
  });
38
45
  let triggerValue;
39
46
  let triggerVelocity;
47
+ let triggerTimeRemaining;
40
48
  spring.onUpdate(() => {
41
49
  triggerValue?.();
42
50
  triggerVelocity?.();
51
+ triggerTimeRemaining?.();
43
52
  });
44
- const value = customRef((track, trigger) => {
45
- triggerValue = trigger;
53
+ const value = customRef((track, trigger) => ({
54
+ get() {
55
+ triggerValue ??= trigger;
56
+ track();
57
+ return spring.value;
58
+ },
59
+ set(value) {
60
+ spring.value = value;
61
+ trigger();
62
+ }
63
+ }));
64
+ const velocity = customRef((track, trigger) => ({
65
+ get() {
66
+ triggerVelocity ??= trigger;
67
+ track();
68
+ return spring.velocity;
69
+ },
70
+ set(value) {
71
+ spring.velocity = value;
72
+ trigger();
73
+ }
74
+ }));
75
+ const timeRemaining = customRef((track, trigger) => ({
76
+ get() {
77
+ triggerTimeRemaining ??= trigger;
78
+ track();
79
+ return spring.timeRemaining;
80
+ },
81
+ set() {}
82
+ }));
83
+ const isResting = customRef((track, trigger) => {
84
+ spring.onStart(trigger);
85
+ spring.onStop(trigger);
46
86
  return {
47
87
  get() {
48
88
  track();
49
- return spring.value;
89
+ return spring.isResting;
50
90
  },
51
- set(value) {
52
- spring.value = value;
53
- trigger();
54
- }
91
+ set() {}
55
92
  };
56
93
  });
57
- const velocity = customRef((track, trigger) => {
58
- triggerVelocity = trigger;
94
+ watchSyncEffect(() => {
95
+ spring.target = toValue(target);
96
+ });
97
+ if (getCurrentScope()) onScopeDispose(() => spring.dispose());
98
+ const ref = Object.assign(value, {
99
+ velocity,
100
+ timeRemaining,
101
+ isResting,
102
+ jumpTo: (value) => spring.jumpTo(value)
103
+ });
104
+ Object.defineProperty(ref, SpringInstanceKey, { value: spring });
105
+ return ref;
106
+ }
107
+ function createLinkedSpringRef(leaderRef, options) {
108
+ const system = inject(SpringSystemKey);
109
+ if (!system) throw new Error("No SpringSystem found");
110
+ const leaderSpring = leaderRef[SpringInstanceKey];
111
+ const config = computed(() => {
112
+ const opts = toValue(options);
113
+ if (!opts) return void 0;
114
+ if (opts instanceof SpringConfig) return opts;
115
+ return new SpringConfig(opts);
116
+ });
117
+ const spring = system.createSpring({ target: leaderSpring }, config.value);
118
+ watchSyncEffect(() => {
119
+ const c = config.value;
120
+ if (c) spring.config = c;
121
+ else spring.config = null;
122
+ });
123
+ let triggerValue;
124
+ let triggerVelocity;
125
+ let triggerTimeRemaining;
126
+ spring.onUpdate(() => {
127
+ triggerValue?.();
128
+ triggerVelocity?.();
129
+ triggerTimeRemaining?.();
130
+ });
131
+ const value = customRef((track, trigger) => ({
132
+ get() {
133
+ triggerValue ??= trigger;
134
+ track();
135
+ return spring.value;
136
+ },
137
+ set(value) {
138
+ spring.value = value;
139
+ trigger();
140
+ }
141
+ }));
142
+ const velocity = customRef((track, trigger) => ({
143
+ get() {
144
+ triggerVelocity ??= trigger;
145
+ track();
146
+ return spring.velocity;
147
+ },
148
+ set(value) {
149
+ spring.velocity = value;
150
+ trigger();
151
+ }
152
+ }));
153
+ const timeRemaining = customRef((track, trigger) => ({
154
+ get() {
155
+ triggerTimeRemaining ??= trigger;
156
+ track();
157
+ return spring.timeRemaining;
158
+ },
159
+ set() {}
160
+ }));
161
+ const isResting = customRef((track, trigger) => {
162
+ spring.onStart(trigger);
163
+ spring.onStop(trigger);
59
164
  return {
60
165
  get() {
61
166
  track();
62
- return spring.velocity;
167
+ return spring.isResting;
63
168
  },
64
- set(value) {
65
- spring.velocity = value;
66
- trigger();
67
- }
169
+ set() {}
68
170
  };
69
171
  });
70
- const resting = customRef((track, trigger) => {
172
+ if (getCurrentScope()) onScopeDispose(() => spring.dispose());
173
+ const ref = Object.assign(value, {
174
+ velocity,
175
+ timeRemaining,
176
+ isResting,
177
+ jumpTo: (value) => spring.jumpTo(value)
178
+ });
179
+ Object.defineProperty(ref, SpringInstanceKey, { value: spring });
180
+ return ref;
181
+ }
182
+ //#endregion
183
+ //#region src/vue/spring2d.ts
184
+ /** @internal Symbol to access the underlying Spring2D from a SpringRef2D */
185
+ const Spring2DInstanceKey = Symbol("spring2d");
186
+ function hasSpring2DInstance(value) {
187
+ return typeof value === "object" && value !== null && Spring2DInstanceKey in value;
188
+ }
189
+ function useSpring2D(target, options) {
190
+ if (Array.isArray(target)) return Array.from(target, (t) => createSpringRef2D(t, 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 opts ? new SpringConfig(opts) : void 0;
201
+ });
202
+ const spring = system.createSpring2D(toValue(target), config.value ?? void 0);
203
+ watchSyncEffect(() => {
204
+ const c = config.value;
205
+ if (c) spring.config = c;
206
+ else spring.config = null;
207
+ });
208
+ let triggerValue;
209
+ let triggerVelocity;
210
+ let triggerTimeRemaining;
211
+ spring.onUpdate(() => {
212
+ triggerValue?.();
213
+ triggerVelocity?.();
214
+ triggerTimeRemaining?.();
215
+ });
216
+ const value = customRef((track, trigger) => ({
217
+ get() {
218
+ triggerValue ??= trigger;
219
+ track();
220
+ return spring.value;
221
+ },
222
+ set(value) {
223
+ spring.value = value;
224
+ trigger();
225
+ }
226
+ }));
227
+ const velocity = customRef((track, trigger) => ({
228
+ get() {
229
+ triggerVelocity ??= trigger;
230
+ track();
231
+ return spring.velocity;
232
+ },
233
+ set(value) {
234
+ spring.velocity = value;
235
+ trigger();
236
+ }
237
+ }));
238
+ const timeRemaining = customRef((track, trigger) => ({
239
+ get() {
240
+ triggerTimeRemaining ??= trigger;
241
+ track();
242
+ return spring.timeRemaining;
243
+ },
244
+ set() {}
245
+ }));
246
+ const isResting = customRef((track, trigger) => {
71
247
  spring.onStart(trigger);
72
248
  spring.onStop(trigger);
73
249
  return {
74
250
  get() {
75
251
  track();
76
- return spring.resting;
252
+ return spring.isResting;
77
253
  },
78
254
  set() {}
79
255
  };
@@ -81,41 +257,113 @@ function useSpring(target, options) {
81
257
  watchSyncEffect(() => {
82
258
  spring.target = toValue(target);
83
259
  });
84
- return {
85
- value,
260
+ if (getCurrentScope()) onScopeDispose(() => spring.dispose());
261
+ const ref = Object.assign(value, {
86
262
  velocity,
87
- resting,
88
- jumpTo: (value) => {
89
- spring.jumpTo(value);
263
+ timeRemaining,
264
+ isResting,
265
+ jumpTo: (value) => spring.jumpTo(value)
266
+ });
267
+ Object.defineProperty(ref, Spring2DInstanceKey, { value: spring });
268
+ return ref;
269
+ }
270
+ function createLinkedSpringRef2D(leaderRef, options) {
271
+ const system = inject(SpringSystemKey);
272
+ if (!system) throw new Error("No SpringSystem found");
273
+ const leaderSpring = leaderRef[Spring2DInstanceKey];
274
+ const config = computed(() => {
275
+ const opts = toValue(options);
276
+ if (!opts) return void 0;
277
+ if (opts instanceof SpringConfig) return opts;
278
+ return new SpringConfig(opts);
279
+ });
280
+ const spring = system.createSpring2D({ target: leaderSpring }, config.value);
281
+ watchSyncEffect(() => {
282
+ const c = config.value;
283
+ if (c) spring.config = c;
284
+ else spring.config = null;
285
+ });
286
+ let triggerValue;
287
+ let triggerVelocity;
288
+ let triggerTimeRemaining;
289
+ spring.onUpdate(() => {
290
+ triggerValue?.();
291
+ triggerVelocity?.();
292
+ triggerTimeRemaining?.();
293
+ });
294
+ const value = customRef((track, trigger) => ({
295
+ get() {
296
+ triggerValue ??= trigger;
297
+ track();
298
+ return spring.value;
299
+ },
300
+ set(value) {
301
+ spring.value = value;
302
+ trigger();
303
+ }
304
+ }));
305
+ const velocity = customRef((track, trigger) => ({
306
+ get() {
307
+ triggerVelocity ??= trigger;
308
+ track();
309
+ return spring.velocity;
310
+ },
311
+ set(value) {
312
+ spring.velocity = value;
313
+ trigger();
90
314
  }
91
- };
315
+ }));
316
+ const timeRemaining = customRef((track, trigger) => ({
317
+ get() {
318
+ triggerTimeRemaining ??= trigger;
319
+ track();
320
+ return spring.timeRemaining;
321
+ },
322
+ set() {}
323
+ }));
324
+ const isResting = customRef((track, trigger) => {
325
+ spring.onStart(trigger);
326
+ spring.onStop(trigger);
327
+ return {
328
+ get() {
329
+ track();
330
+ return spring.isResting;
331
+ },
332
+ set() {}
333
+ };
334
+ });
335
+ if (getCurrentScope()) onScopeDispose(() => spring.dispose());
336
+ const ref = Object.assign(value, {
337
+ velocity,
338
+ timeRemaining,
339
+ isResting,
340
+ jumpTo: (value) => spring.jumpTo(value)
341
+ });
342
+ Object.defineProperty(ref, Spring2DInstanceKey, { value: spring });
343
+ return ref;
92
344
  }
93
345
  //#endregion
94
346
  //#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
- };
347
+ const SpringValue = defineComponent((props, { slots, expose }) => {
348
+ const { target, config } = toRefs(props);
349
+ const spring = useSpring(target, config);
350
+ expose({
351
+ value: spring,
352
+ velocity: spring.velocity,
353
+ isResting: spring.isResting,
354
+ timeRemaining: spring.timeRemaining,
355
+ jumpTo: spring.jumpTo
356
+ });
357
+ return () => slots.default?.({
358
+ value: spring.value,
359
+ velocity: spring.velocity.value,
360
+ isResting: spring.isResting.value,
361
+ timeRemaining: spring.timeRemaining.value,
362
+ jumpTo: spring.jumpTo
363
+ }) ?? null;
110
364
  }, {
111
365
  name: "SpringValue",
112
- props: [
113
- "target",
114
- "mass",
115
- "tension",
116
- "damping",
117
- "precision"
118
- ]
366
+ props: ["target", "config"]
119
367
  });
120
368
  //#endregion
121
- export { SpringValue, provideSpringSystem, useSpring, useSpringSystem };
369
+ export { SpringValue, provideSpringSystem, useSpring, useSpring2D, useSpringSystem };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coily",
3
- "version": "0.11.0",
3
+ "version": "0.12.1",
4
4
  "description": "Simulate values using spring physics",
5
5
  "homepage": "https://github.com/tkofh/coily#readme",
6
6
  "bugs": {