what-core 0.6.2 → 0.6.5

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.
Files changed (52) hide show
  1. package/README.md +2 -0
  2. package/compiler.d.ts +30 -0
  3. package/devtools.d.ts +2 -0
  4. package/dist/compiler.js +1787 -0
  5. package/dist/compiler.js.map +7 -0
  6. package/dist/compiler.min.js +2 -0
  7. package/dist/compiler.min.js.map +7 -0
  8. package/dist/devtools.js +10 -0
  9. package/dist/devtools.js.map +7 -0
  10. package/dist/devtools.min.js +2 -0
  11. package/dist/devtools.min.js.map +7 -0
  12. package/dist/index.js +330 -382
  13. package/dist/index.js.map +4 -4
  14. package/dist/index.min.js +62 -62
  15. package/dist/index.min.js.map +4 -4
  16. package/dist/render.js +262 -21
  17. package/dist/render.js.map +4 -4
  18. package/dist/render.min.js +58 -1
  19. package/dist/render.min.js.map +4 -4
  20. package/dist/testing.js +3 -0
  21. package/dist/testing.js.map +2 -2
  22. package/dist/testing.min.js +1 -1
  23. package/dist/testing.min.js.map +2 -2
  24. package/index.d.ts +176 -1
  25. package/jsx-runtime.d.ts +622 -0
  26. package/package.json +20 -2
  27. package/render.d.ts +1 -1
  28. package/src/agent-context.js +1 -1
  29. package/src/compiler.js +18 -0
  30. package/src/components.js +73 -27
  31. package/src/devtools.js +4 -0
  32. package/src/dom.js +7 -0
  33. package/src/guardrails.js +3 -4
  34. package/src/hooks.js +0 -11
  35. package/src/index.js +5 -9
  36. package/src/render.js +91 -24
  37. package/testing.d.ts +1 -1
  38. package/dist/a11y.js +0 -440
  39. package/dist/animation.js +0 -548
  40. package/dist/components.js +0 -229
  41. package/dist/data.js +0 -638
  42. package/dist/dom.js +0 -439
  43. package/dist/form.js +0 -509
  44. package/dist/h.js +0 -152
  45. package/dist/head.js +0 -51
  46. package/dist/helpers.js +0 -140
  47. package/dist/hooks.js +0 -210
  48. package/dist/reactive.js +0 -432
  49. package/dist/scheduler.js +0 -246
  50. package/dist/skeleton.js +0 -363
  51. package/dist/store.js +0 -83
  52. package/dist/what.js +0 -117
package/dist/animation.js DELETED
@@ -1,548 +0,0 @@
1
- // What Framework - Animation Primitives
2
- // Springs, tweens, gestures, and transition helpers
3
-
4
- import { signal, effect, untrack, batch } from './reactive.js';
5
- import { getCurrentComponent } from './dom.js';
6
- import { scheduleRead, scheduleWrite } from './scheduler.js';
7
-
8
- // Create an effect scoped to the current component's lifecycle
9
- function scopedEffect(fn) {
10
- const ctx = getCurrentComponent?.();
11
- const dispose = effect(fn);
12
- if (ctx) ctx.effects.push(dispose);
13
- return dispose;
14
- }
15
-
16
- // --- Spring Animation ---
17
- // Physics-based animation with natural feel
18
-
19
- export function spring(initialValue, options = {}) {
20
- const {
21
- stiffness = 100,
22
- damping = 10,
23
- mass = 1,
24
- precision = 0.01,
25
- } = options;
26
-
27
- const current = signal(initialValue);
28
- const target = signal(initialValue);
29
- const velocity = signal(0);
30
- const isAnimating = signal(false);
31
-
32
- let rafId = null;
33
- let lastTime = null;
34
-
35
- function tick(time) {
36
- if (lastTime === null) {
37
- lastTime = time;
38
- rafId = requestAnimationFrame(tick);
39
- return;
40
- }
41
-
42
- const dt = Math.min((time - lastTime) / 1000, 0.064); // Cap at ~15fps minimum
43
- lastTime = time;
44
-
45
- const currentVal = current.peek();
46
- const targetVal = target.peek();
47
- const vel = velocity.peek();
48
-
49
- // Spring physics
50
- const displacement = currentVal - targetVal;
51
- const springForce = -stiffness * displacement;
52
- const dampingForce = -damping * vel;
53
- const acceleration = (springForce + dampingForce) / mass;
54
-
55
- const newVelocity = vel + acceleration * dt;
56
- const newValue = currentVal + newVelocity * dt;
57
-
58
- batch(() => {
59
- velocity.set(newVelocity);
60
- current.set(newValue);
61
- });
62
-
63
- // Check if settled
64
- if (Math.abs(newVelocity) < precision && Math.abs(displacement) < precision) {
65
- batch(() => {
66
- current.set(targetVal);
67
- velocity.set(0);
68
- isAnimating.set(false);
69
- });
70
- rafId = null;
71
- lastTime = null;
72
- return;
73
- }
74
-
75
- rafId = requestAnimationFrame(tick);
76
- }
77
-
78
- function set(newTarget) {
79
- target.set(newTarget);
80
- if (!isAnimating.peek()) {
81
- isAnimating.set(true);
82
- lastTime = null;
83
- rafId = requestAnimationFrame(tick);
84
- }
85
- }
86
-
87
- function stop() {
88
- if (rafId) {
89
- cancelAnimationFrame(rafId);
90
- rafId = null;
91
- }
92
- isAnimating.set(false);
93
- lastTime = null;
94
- }
95
-
96
- function snap(value) {
97
- stop();
98
- batch(() => {
99
- current.set(value);
100
- target.set(value);
101
- velocity.set(0);
102
- });
103
- }
104
-
105
- // Register stop() as cleanup if inside a component
106
- const ctx = getCurrentComponent?.();
107
- if (ctx) {
108
- ctx._cleanupCallbacks = ctx._cleanupCallbacks || [];
109
- ctx._cleanupCallbacks.push(stop);
110
- }
111
-
112
- return {
113
- current: () => current(),
114
- target: () => target(),
115
- velocity: () => velocity(),
116
- isAnimating: () => isAnimating(),
117
- set,
118
- stop,
119
- snap,
120
- subscribe: current.subscribe,
121
- };
122
- }
123
-
124
- // --- Tween Animation ---
125
- // Easing-based animation
126
-
127
- export function tween(from, to, options = {}) {
128
- const {
129
- duration = 300,
130
- easing = (t) => t * (2 - t), // easeOutQuad
131
- onUpdate,
132
- onComplete,
133
- } = options;
134
-
135
- const progress = signal(0);
136
- const value = signal(from);
137
- const isAnimating = signal(true);
138
-
139
- let startTime = null;
140
- let rafId = null;
141
-
142
- function tick(time) {
143
- if (startTime === null) startTime = time;
144
-
145
- const elapsed = time - startTime;
146
- const t = Math.min(elapsed / duration, 1);
147
- const easedT = easing(t);
148
- const currentValue = from + (to - from) * easedT;
149
-
150
- batch(() => {
151
- progress.set(t);
152
- value.set(currentValue);
153
- });
154
-
155
- if (onUpdate) onUpdate(currentValue, t);
156
-
157
- if (t < 1) {
158
- rafId = requestAnimationFrame(tick);
159
- } else {
160
- isAnimating.set(false);
161
- if (onComplete) onComplete();
162
- }
163
- }
164
-
165
- rafId = requestAnimationFrame(tick);
166
-
167
- return {
168
- progress: () => progress(),
169
- value: () => value(),
170
- isAnimating: () => isAnimating(),
171
- cancel: () => {
172
- if (rafId) cancelAnimationFrame(rafId);
173
- isAnimating.set(false);
174
- },
175
- subscribe: value.subscribe,
176
- };
177
- }
178
-
179
- // --- Easing Functions ---
180
-
181
- export const easings = {
182
- linear: (t) => t,
183
- easeInQuad: (t) => t * t,
184
- easeOutQuad: (t) => t * (2 - t),
185
- easeInOutQuad: (t) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t,
186
- easeInCubic: (t) => t * t * t,
187
- easeOutCubic: (t) => (--t) * t * t + 1,
188
- easeInOutCubic: (t) => t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
189
- easeInElastic: (t) => t === 0 ? 0 : t === 1 ? 1 : -Math.pow(2, 10 * t - 10) * Math.sin((t * 10 - 10.75) * ((2 * Math.PI) / 3)),
190
- easeOutElastic: (t) => t === 0 ? 0 : t === 1 ? 1 : Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * ((2 * Math.PI) / 3)) + 1,
191
- easeOutBounce: (t) => {
192
- const n1 = 7.5625;
193
- const d1 = 2.75;
194
- if (t < 1 / d1) return n1 * t * t;
195
- if (t < 2 / d1) return n1 * (t -= 1.5 / d1) * t + 0.75;
196
- if (t < 2.5 / d1) return n1 * (t -= 2.25 / d1) * t + 0.9375;
197
- return n1 * (t -= 2.625 / d1) * t + 0.984375;
198
- },
199
- };
200
-
201
- // --- useTransition Hook ---
202
- // Animate between states
203
-
204
- export function useTransition(options = {}) {
205
- const { duration = 300, easing = easings.easeOutQuad } = options;
206
-
207
- const isTransitioning = signal(false);
208
- const progress = signal(0);
209
-
210
- async function start(callback) {
211
- isTransitioning.set(true);
212
- progress.set(0);
213
-
214
- return new Promise((resolve) => {
215
- const startTime = performance.now();
216
-
217
- function tick(time) {
218
- const elapsed = time - startTime;
219
- const t = Math.min(elapsed / duration, 1);
220
- progress.set(easing(t));
221
-
222
- if (t < 1) {
223
- requestAnimationFrame(tick);
224
- } else {
225
- isTransitioning.set(false);
226
- if (callback) callback();
227
- resolve();
228
- }
229
- }
230
-
231
- requestAnimationFrame(tick);
232
- });
233
- }
234
-
235
- return {
236
- isTransitioning: () => isTransitioning(),
237
- progress: () => progress(),
238
- start,
239
- };
240
- }
241
-
242
- // --- Gesture Handlers ---
243
-
244
- export function useGesture(element, handlers = {}) {
245
- const {
246
- onDrag,
247
- onDragStart,
248
- onDragEnd,
249
- onPinch,
250
- onSwipe,
251
- onTap,
252
- onLongPress,
253
- preventDefault = false, // Set to true to allow e.preventDefault() in touch handlers
254
- } = handlers;
255
-
256
- const state = {
257
- isDragging: signal(false),
258
- startX: 0,
259
- startY: 0,
260
- currentX: signal(0),
261
- currentY: signal(0),
262
- deltaX: signal(0),
263
- deltaY: signal(0),
264
- velocity: signal({ x: 0, y: 0 }),
265
- };
266
-
267
- let lastTime = 0;
268
- let lastX = 0;
269
- let lastY = 0;
270
- let longPressTimer = null;
271
-
272
- function handleStart(e) {
273
- const touch = e.touches ? e.touches[0] : e;
274
- state.startX = touch.clientX;
275
- state.startY = touch.clientY;
276
- lastX = touch.clientX;
277
- lastY = touch.clientY;
278
- lastTime = performance.now();
279
-
280
- state.isDragging.set(true);
281
- if (onDragStart) onDragStart({ x: state.startX, y: state.startY });
282
-
283
- // Long press detection
284
- if (onLongPress) {
285
- longPressTimer = setTimeout(() => {
286
- if (state.isDragging.peek()) {
287
- onLongPress({ x: lastX, y: lastY });
288
- }
289
- }, 500);
290
- }
291
- }
292
-
293
- function handleMove(e) {
294
- if (!state.isDragging.peek()) return;
295
-
296
- const touch = e.touches ? e.touches[0] : e;
297
- const x = touch.clientX;
298
- const y = touch.clientY;
299
- const now = performance.now();
300
- const dt = now - lastTime;
301
-
302
- batch(() => {
303
- state.currentX.set(x);
304
- state.currentY.set(y);
305
- state.deltaX.set(x - state.startX);
306
- state.deltaY.set(y - state.startY);
307
-
308
- if (dt > 0) {
309
- state.velocity.set({
310
- x: (x - lastX) / dt * 1000,
311
- y: (y - lastY) / dt * 1000,
312
- });
313
- }
314
- });
315
-
316
- lastX = x;
317
- lastY = y;
318
- lastTime = now;
319
-
320
- if (longPressTimer) {
321
- // Cancel long press if moved too much
322
- const distance = Math.sqrt(state.deltaX.peek() ** 2 + state.deltaY.peek() ** 2);
323
- if (distance > 10) {
324
- clearTimeout(longPressTimer);
325
- longPressTimer = null;
326
- }
327
- }
328
-
329
- if (onDrag) {
330
- onDrag({
331
- x,
332
- y,
333
- deltaX: state.deltaX.peek(),
334
- deltaY: state.deltaY.peek(),
335
- velocity: state.velocity.peek(),
336
- });
337
- }
338
- }
339
-
340
- function handleEnd(e) {
341
- if (!state.isDragging.peek()) return;
342
-
343
- if (longPressTimer) {
344
- clearTimeout(longPressTimer);
345
- longPressTimer = null;
346
- }
347
-
348
- const deltaX = state.deltaX.peek();
349
- const deltaY = state.deltaY.peek();
350
- const velocity = state.velocity.peek();
351
- const distance = Math.sqrt(deltaX ** 2 + deltaY ** 2);
352
-
353
- // Tap detection
354
- if (distance < 10 && onTap) {
355
- onTap({ x: state.startX, y: state.startY });
356
- }
357
-
358
- // Swipe detection
359
- if (onSwipe && (Math.abs(velocity.x) > 500 || Math.abs(velocity.y) > 500)) {
360
- const direction = Math.abs(velocity.x) > Math.abs(velocity.y)
361
- ? (velocity.x > 0 ? 'right' : 'left')
362
- : (velocity.y > 0 ? 'down' : 'up');
363
- onSwipe({ direction, velocity });
364
- }
365
-
366
- if (onDragEnd) {
367
- onDragEnd({
368
- deltaX,
369
- deltaY,
370
- velocity,
371
- });
372
- }
373
-
374
- state.isDragging.set(false);
375
- }
376
-
377
- // Pinch handling (touch only)
378
- let initialPinchDistance = null;
379
-
380
- function handlePinchMove(e) {
381
- if (!onPinch || e.touches.length !== 2) return;
382
-
383
- const touch1 = e.touches[0];
384
- const touch2 = e.touches[1];
385
- const distance = Math.sqrt(
386
- (touch2.clientX - touch1.clientX) ** 2 +
387
- (touch2.clientY - touch1.clientY) ** 2
388
- );
389
-
390
- if (initialPinchDistance === null) {
391
- initialPinchDistance = distance;
392
- }
393
-
394
- const scale = distance / initialPinchDistance;
395
- const centerX = (touch1.clientX + touch2.clientX) / 2;
396
- const centerY = (touch1.clientY + touch2.clientY) / 2;
397
-
398
- onPinch({ scale, centerX, centerY });
399
- }
400
-
401
- function handlePinchEnd() {
402
- initialPinchDistance = null;
403
- }
404
-
405
- // Attach listeners
406
- if (typeof element === 'function') {
407
- // Ref function
408
- scopedEffect(() => {
409
- const el = untrack(element);
410
- if (!el) return;
411
- return attachListeners(el);
412
- });
413
- } else if (element?.current !== undefined) {
414
- // Ref object
415
- scopedEffect(() => {
416
- const el = element.current;
417
- if (!el) return;
418
- return attachListeners(el);
419
- });
420
- } else if (element) {
421
- attachListeners(element);
422
- }
423
-
424
- function attachListeners(el) {
425
- el.addEventListener('mousedown', handleStart);
426
- el.addEventListener('touchstart', handleStart, { passive: !preventDefault });
427
- window.addEventListener('mousemove', handleMove);
428
- window.addEventListener('touchmove', handlePinchMove);
429
- window.addEventListener('touchmove', handleMove);
430
- window.addEventListener('mouseup', handleEnd);
431
- window.addEventListener('touchend', handleEnd);
432
- window.addEventListener('touchend', handlePinchEnd);
433
-
434
- return () => {
435
- el.removeEventListener('mousedown', handleStart);
436
- el.removeEventListener('touchstart', handleStart);
437
- window.removeEventListener('mousemove', handleMove);
438
- window.removeEventListener('touchmove', handlePinchMove);
439
- window.removeEventListener('touchmove', handleMove);
440
- window.removeEventListener('mouseup', handleEnd);
441
- window.removeEventListener('touchend', handleEnd);
442
- window.removeEventListener('touchend', handlePinchEnd);
443
- };
444
- }
445
-
446
- return state;
447
- }
448
-
449
- // --- useAnimatedValue Hook ---
450
- // Like React Native's Animated.Value
451
-
452
- export function useAnimatedValue(initialValue) {
453
- const value = signal(initialValue);
454
- const animations = [];
455
-
456
- return {
457
- value: () => value(),
458
- setValue: (v) => value.set(v),
459
-
460
- // Spring to target
461
- spring(toValue, config = {}) {
462
- const s = spring(value.peek(), config);
463
- s.set(toValue);
464
-
465
- const dispose = effect(() => {
466
- value.set(s.current());
467
- });
468
-
469
- return {
470
- stop: () => { s.stop(); dispose(); },
471
- };
472
- },
473
-
474
- // Tween to target
475
- timing(toValue, config = {}) {
476
- const t = tween(value.peek(), toValue, {
477
- ...config,
478
- onUpdate: (v) => value.set(v),
479
- });
480
-
481
- return {
482
- stop: () => t.cancel(),
483
- };
484
- },
485
-
486
- // Interpolate value
487
- interpolate(inputRange, outputRange) {
488
- return () => {
489
- const v = value();
490
- // Find segment
491
- for (let i = 0; i < inputRange.length - 1; i++) {
492
- if (v >= inputRange[i] && v <= inputRange[i + 1]) {
493
- const t = (v - inputRange[i]) / (inputRange[i + 1] - inputRange[i]);
494
- return outputRange[i] + (outputRange[i + 1] - outputRange[i]) * t;
495
- }
496
- }
497
- // Clamp
498
- if (v <= inputRange[0]) return outputRange[0];
499
- return outputRange[outputRange.length - 1];
500
- };
501
- },
502
-
503
- subscribe: value.subscribe,
504
- };
505
- }
506
-
507
- // --- CSS Transition Classes ---
508
-
509
- export function createTransitionClasses(name) {
510
- return {
511
- enter: `${name}-enter`,
512
- enterActive: `${name}-enter-active`,
513
- enterDone: `${name}-enter-done`,
514
- exit: `${name}-exit`,
515
- exitActive: `${name}-exit-active`,
516
- exitDone: `${name}-exit-done`,
517
- };
518
- }
519
-
520
- // Apply CSS transition
521
- export async function cssTransition(element, name, type = 'enter', duration = 300) {
522
- const classes = createTransitionClasses(name);
523
-
524
- return new Promise((resolve) => {
525
- scheduleWrite(() => {
526
- // Initial state
527
- element.classList.add(classes[type]);
528
-
529
- // Force reflow
530
- scheduleRead(() => {
531
- element.offsetHeight;
532
-
533
- scheduleWrite(() => {
534
- // Active state
535
- element.classList.add(classes[`${type}Active`]);
536
-
537
- setTimeout(() => {
538
- scheduleWrite(() => {
539
- element.classList.remove(classes[type], classes[`${type}Active`]);
540
- element.classList.add(classes[`${type}Done`]);
541
- resolve();
542
- });
543
- }, duration);
544
- });
545
- });
546
- });
547
- });
548
- }