what-core 0.4.2 → 0.5.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/components.js +213 -319
- package/dist/dom.js +730 -915
- package/dist/h.js +140 -191
- package/dist/head.js +42 -59
- package/dist/helpers.js +124 -187
- package/dist/hooks.js +186 -279
- package/dist/reactive.js +244 -317
- package/dist/store.js +73 -118
- package/dist/what.js +5 -3
- package/index.d.ts +391 -152
- package/package.json +4 -3
- package/render.d.ts +11 -0
- package/src/a11y.js +52 -6
- package/src/dom.js +91 -8
- package/src/form.js +85 -54
- package/src/helpers.js +1 -12
- package/src/hooks.js +11 -0
- package/src/index.js +1 -1
- package/src/render.js +114 -51
- package/src/store.js +6 -1
package/dist/store.js
CHANGED
|
@@ -1,128 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
// Lightweight global state management. Signal-based, type-safe, ergonomic.
|
|
3
|
-
// Like Zustand meets signals — define a store, use it anywhere.
|
|
4
|
-
|
|
5
|
-
import { signal, computed, batch } from './reactive.js';
|
|
6
|
-
|
|
7
|
-
// --- storeComputed ---
|
|
8
|
-
// Marker wrapper to explicitly tag a function as a computed in createStore.
|
|
9
|
-
// Without this, createStore can't distinguish computed(state => ...) from action(item => ...).
|
|
10
|
-
//
|
|
11
|
-
// Usage:
|
|
12
|
-
// const useCounter = createStore({
|
|
13
|
-
// count: 0,
|
|
14
|
-
// doubled: storeComputed(state => state.count * 2),
|
|
15
|
-
// addItem(item) { /* this is an action */ },
|
|
16
|
-
// });
|
|
17
|
-
|
|
1
|
+
import { signal, computed, batch, __DEV__ } from './reactive.js';
|
|
18
2
|
export function derived(fn) {
|
|
19
|
-
|
|
20
|
-
|
|
3
|
+
fn._storeComputed = true;
|
|
4
|
+
return fn;
|
|
21
5
|
}
|
|
22
|
-
|
|
23
|
-
// Deprecated alias — use derived() instead
|
|
24
6
|
let _storeComputedWarned = false;
|
|
25
7
|
export function storeComputed(fn) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
8
|
+
if (!_storeComputedWarned) {
|
|
9
|
+
_storeComputedWarned = true;
|
|
10
|
+
console.warn('[what] storeComputed() is deprecated. Use derived() instead.');
|
|
11
|
+
}
|
|
12
|
+
return derived(fn);
|
|
31
13
|
}
|
|
32
|
-
|
|
33
|
-
// --- createStore ---
|
|
34
|
-
// Creates a reactive store with actions. Each key becomes a signal.
|
|
35
|
-
//
|
|
36
|
-
// Usage:
|
|
37
|
-
// const useCounter = createStore({
|
|
38
|
-
// count: 0,
|
|
39
|
-
// doubled: storeComputed(state => state.count * 2), // computed
|
|
40
|
-
// increment() { this.count++; }, // action
|
|
41
|
-
// decrement() { this.count--; },
|
|
42
|
-
// addItem(item) { this.items.push(item); }, // action (not confused with computed)
|
|
43
|
-
// });
|
|
44
|
-
//
|
|
45
|
-
// function Counter() {
|
|
46
|
-
// const { count, doubled, increment } = useCounter();
|
|
47
|
-
// return h('div', null, count, ' / ', doubled, h('button', { onClick: increment }, '+'));
|
|
48
|
-
// }
|
|
49
|
-
|
|
50
14
|
export function createStore(definition) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
Object.defineProperty(result, key, { get: () => c(), enumerable: true });
|
|
112
|
-
}
|
|
113
|
-
for (const [key, fn] of Object.entries(actions)) {
|
|
114
|
-
result[key] = fn;
|
|
115
|
-
}
|
|
116
|
-
return result;
|
|
117
|
-
};
|
|
15
|
+
const signals = {};
|
|
16
|
+
const computeds = {};
|
|
17
|
+
const actions = {};
|
|
18
|
+
const state = {};
|
|
19
|
+
for (const [key, value] of Object.entries(definition)) {
|
|
20
|
+
if (typeof value === 'function' && value._storeComputed) {
|
|
21
|
+
if (__DEV__ && value.length === 0) {
|
|
22
|
+
console.warn(
|
|
23
|
+
`[what] derived() for "${key}" should accept the state parameter, e.g. derived(state => ...).`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
computeds[key] = value;
|
|
27
|
+
} else if (typeof value === 'function') {
|
|
28
|
+
actions[key] = value;
|
|
29
|
+
} else {
|
|
30
|
+
signals[key] = signal(value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
for (const [key, fn] of Object.entries(computeds)) {
|
|
34
|
+
const proxy = new Proxy({}, {
|
|
35
|
+
get(_, prop) {
|
|
36
|
+
if (signals[prop]) return signals[prop]();
|
|
37
|
+
if (computeds[prop]) return computeds[prop]();
|
|
38
|
+
return undefined;
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
computeds[key] = computed(() => fn(proxy));
|
|
42
|
+
}
|
|
43
|
+
for (const [key, fn] of Object.entries(actions)) {
|
|
44
|
+
actions[key] = (...args) => {
|
|
45
|
+
batch(() => {
|
|
46
|
+
const proxy = new Proxy({}, {
|
|
47
|
+
get(_, prop) {
|
|
48
|
+
if (signals[prop]) return signals[prop].peek();
|
|
49
|
+
if (computeds[prop]) return computeds[prop].peek();
|
|
50
|
+
if (actions[prop]) return actions[prop];
|
|
51
|
+
return undefined;
|
|
52
|
+
},
|
|
53
|
+
set(_, prop, val) {
|
|
54
|
+
if (signals[prop]) signals[prop].set(val);
|
|
55
|
+
return true;
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
fn.apply(proxy, args);
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return function useStore() {
|
|
63
|
+
const result = {};
|
|
64
|
+
for (const [key, s] of Object.entries(signals)) {
|
|
65
|
+
Object.defineProperty(result, key, { get: () => s(), enumerable: true });
|
|
66
|
+
}
|
|
67
|
+
for (const [key, c] of Object.entries(computeds)) {
|
|
68
|
+
Object.defineProperty(result, key, { get: () => c(), enumerable: true });
|
|
69
|
+
}
|
|
70
|
+
for (const [key, fn] of Object.entries(actions)) {
|
|
71
|
+
result[key] = fn;
|
|
72
|
+
}
|
|
73
|
+
return result;
|
|
74
|
+
};
|
|
118
75
|
}
|
|
119
|
-
|
|
120
|
-
// --- Simple atom --- [DEPRECATED: use signal() directly]
|
|
121
76
|
let _atomWarned = false;
|
|
122
77
|
export function atom(initial) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
return signal(initial);
|
|
78
|
+
if (!_atomWarned) {
|
|
79
|
+
_atomWarned = true;
|
|
80
|
+
console.warn('[what] atom() is deprecated. Use signal() directly instead.');
|
|
128
81
|
}
|
|
82
|
+
return signal(initial);
|
|
83
|
+
}
|
package/dist/what.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { signal, computed, effect, batch, untrack } from './reactive.js';
|
|
1
|
+
export { signal, computed, effect, memo as signalMemo, batch, untrack, flushSync, createRoot } from './reactive.js';
|
|
2
|
+
export { template, insert, mapArray, spread, delegateEvents, on, classList } from './render.js';
|
|
2
3
|
export { h, Fragment, html } from './h.js';
|
|
3
4
|
export { mount } from './dom.js';
|
|
4
5
|
export {
|
|
@@ -17,10 +18,9 @@ onCleanup,
|
|
|
17
18
|
createResource,
|
|
18
19
|
} from './hooks.js';
|
|
19
20
|
export { memo, lazy, Suspense, ErrorBoundary, Show, For, Switch, Match, Island } from './components.js';
|
|
20
|
-
export { createStore, storeComputed, atom } from './store.js';
|
|
21
|
+
export { createStore, derived, storeComputed, atom } from './store.js';
|
|
21
22
|
export { Head, clearHead } from './head.js';
|
|
22
23
|
export {
|
|
23
|
-
show,
|
|
24
24
|
each,
|
|
25
25
|
cls,
|
|
26
26
|
style,
|
|
@@ -28,6 +28,7 @@ debounce,
|
|
|
28
28
|
throttle,
|
|
29
29
|
useMediaQuery,
|
|
30
30
|
useLocalStorage,
|
|
31
|
+
useClickOutside,
|
|
31
32
|
Portal,
|
|
32
33
|
transition,
|
|
33
34
|
} from './helpers.js';
|
|
@@ -56,6 +57,7 @@ cssTransition,
|
|
|
56
57
|
} from './animation.js';
|
|
57
58
|
export {
|
|
58
59
|
useFocus,
|
|
60
|
+
useFocusRestore,
|
|
59
61
|
useFocusTrap,
|
|
60
62
|
FocusTrap,
|
|
61
63
|
announce,
|