@alwatr/action 9.16.0 → 9.17.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/README.md +249 -70
- package/dist/action.d.ts +93 -0
- package/dist/action.d.ts.map +1 -0
- package/dist/delegate.d.ts +28 -5
- package/dist/delegate.d.ts.map +1 -1
- package/dist/lib.d.ts +8 -6
- package/dist/lib.d.ts.map +1 -1
- package/dist/main.d.ts +30 -3
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +3 -3
- package/dist/main.js.map +6 -6
- package/dist/method.d.ts +62 -45
- package/dist/method.d.ts.map +1 -1
- package/dist/registry.d.ts +24 -12
- package/dist/registry.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/action.ts +97 -0
- package/src/delegate.ts +70 -19
- package/src/lib.ts +9 -6
- package/src/main.ts +30 -3
- package/src/method.ts +72 -54
- package/src/registry.ts +31 -17
package/src/registry.ts
CHANGED
|
@@ -1,38 +1,51 @@
|
|
|
1
|
+
import type {Action} from './action.js';
|
|
2
|
+
|
|
1
3
|
// ─── Type Definitions ────────────────────────────────────────────────────────
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
|
-
* A modifier handler used in `on
|
|
6
|
+
* A modifier handler used in `on-<eventType>` attribute syntax.
|
|
5
7
|
*
|
|
6
|
-
* Receives the triggering DOM `event
|
|
7
|
-
* `on
|
|
8
|
-
*
|
|
8
|
+
* Receives the triggering DOM `event`, the `element` that owns the
|
|
9
|
+
* `on-<eventType>` attribute, and the **mutable** `action` object being built.
|
|
10
|
+
* The handler may mutate `action.meta` to attach cross-cutting data (e.g. a
|
|
11
|
+
* trace ID, a timestamp, or an A/B flag) before the action reaches subscribers.
|
|
12
|
+
*
|
|
13
|
+
* Return `true` (or any truthy value) to allow the action to proceed, or
|
|
14
|
+
* `false` to cancel the dispatch entirely.
|
|
9
15
|
*
|
|
10
16
|
* Using explicit parameters instead of `this` binding makes handlers
|
|
11
17
|
* compatible with arrow functions and easier to test in isolation.
|
|
12
18
|
*
|
|
13
|
-
* @example
|
|
19
|
+
* @example — a modifier that stamps a timestamp into meta
|
|
20
|
+
* ```ts
|
|
21
|
+
* const timestampHandler: ModifierHandler = (_event, _element, action) => {
|
|
22
|
+
* action.meta ??= {};
|
|
23
|
+
* action.meta['timestamp'] = Date.now();
|
|
24
|
+
* return true;
|
|
25
|
+
* };
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @example — a modifier that cancels dispatch when the element is disabled
|
|
14
29
|
* ```ts
|
|
15
|
-
* // A modifier that only allows the action when the element is not disabled
|
|
16
30
|
* const notDisabledHandler: ModifierHandler = (_event, element) => {
|
|
17
31
|
* return !(element as HTMLButtonElement).disabled;
|
|
18
32
|
* };
|
|
19
33
|
* ```
|
|
20
34
|
*/
|
|
21
|
-
export type ModifierHandler = (event: Event, element: HTMLElement) => boolean;
|
|
35
|
+
export type ModifierHandler = (event: Event, element: HTMLElement, action: Action) => boolean;
|
|
22
36
|
|
|
23
37
|
/**
|
|
24
|
-
* A payload resolver used in `on
|
|
38
|
+
* A payload resolver used in `on-<eventType>` attribute syntax.
|
|
25
39
|
*
|
|
26
40
|
* Receives the triggering DOM `event` and the `element` that owns the
|
|
27
|
-
* `on
|
|
28
|
-
*
|
|
41
|
+
* `on-<eventType>` attribute. The return value becomes the `payload` field of
|
|
42
|
+
* the `Action` object passed to `onAction` subscribers.
|
|
29
43
|
*
|
|
30
44
|
* Using explicit parameters instead of `this` binding makes resolvers
|
|
31
45
|
* compatible with arrow functions and easier to test in isolation.
|
|
32
46
|
*
|
|
33
|
-
* @example
|
|
47
|
+
* @example — a resolver that returns the element's dataset id
|
|
34
48
|
* ```ts
|
|
35
|
-
* // A resolver that returns the element's dataset id
|
|
36
49
|
* const dataIdResolver: PayloadResolver = (_event, element) => {
|
|
37
50
|
* return (element as HTMLElement).dataset.id ?? null;
|
|
38
51
|
* };
|
|
@@ -89,7 +102,7 @@ modifierRegistry.set('prevent', (event) => {
|
|
|
89
102
|
* allowing native constraint-validation UI to surface errors. If no form is
|
|
90
103
|
* found the dispatch is also cancelled.
|
|
91
104
|
*
|
|
92
|
-
* Pair with
|
|
105
|
+
* Pair with `prevent` on `submit` events to avoid page reloads:
|
|
93
106
|
*
|
|
94
107
|
* @example `<form on-submit="submit_form:$formdata; prevent,validate" novalidate>`
|
|
95
108
|
*/
|
|
@@ -122,8 +135,8 @@ payloadRegistry.set('$value', (_event, element) => {
|
|
|
122
135
|
*
|
|
123
136
|
* @example `<form on-submit="submit_form:$formdata; prevent,validate">`
|
|
124
137
|
* ```ts
|
|
125
|
-
* onAction('submit_form', (
|
|
126
|
-
* console.log(
|
|
138
|
+
* onAction('submit_form', (action) => {
|
|
139
|
+
* console.log(action.payload); // {username: 'ali', password: '…'}
|
|
127
140
|
* });
|
|
128
141
|
* ```
|
|
129
142
|
*/
|
|
@@ -140,8 +153,9 @@ payloadRegistry.set('$formdata', (_event, element) => {
|
|
|
140
153
|
*
|
|
141
154
|
* @example `<input type="checkbox" on-change="toggle_feature:$checked" />`
|
|
142
155
|
* ```ts
|
|
143
|
-
* onAction('toggle_feature', (
|
|
144
|
-
*
|
|
156
|
+
* onAction('toggle_feature', (action) => {
|
|
157
|
+
* console.log(action.payload); // true or false
|
|
158
|
+
* featureSignal.set(action.payload);
|
|
145
159
|
* });
|
|
146
160
|
* ```
|
|
147
161
|
*/
|