mosaic 2.1.0 → 2.3.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 +41 -11
- package/dist/mosaic.js +37 -5
- package/dist/mosaic.min.js +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/types.d.ts +2 -0
- package/dist/types/xo.d.ts +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -45,8 +45,8 @@ console.log(controller.getState().count); // 2, with the DOM updated
|
|
|
45
45
|
controller.destroy();
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
`$` is an alias of `Mosaic`. A minified ESM entry is available at
|
|
49
|
-
`mosaic/dist/mosaic.min.js`, including for use through an ESM-compatible CDN.
|
|
48
|
+
<!-- `$` is an alias of `Mosaic`. A minified ESM entry is available at
|
|
49
|
+
`mosaic/dist/mosaic.min.js`, including for use through an ESM-compatible CDN. -->
|
|
50
50
|
|
|
51
51
|
## Configuration and selectors
|
|
52
52
|
|
|
@@ -102,6 +102,7 @@ element and `index` is its position in the captured collection for that binding.
|
|
|
102
102
|
| `attribute` | Map of exact DOM attribute names to values | Sets or removes the returned attributes |
|
|
103
103
|
| `text` | String, number, boolean, `null`, or `undefined` | Writes plain text content |
|
|
104
104
|
| `class` | Map of class names to booleans | Toggles only returned class names |
|
|
105
|
+
| `property` | Map of DOM property names to arbitrary JavaScript values | Assigns the returned properties directly |
|
|
105
106
|
|
|
106
107
|
```ts
|
|
107
108
|
elements: {
|
|
@@ -109,11 +110,11 @@ elements: {
|
|
|
109
110
|
attribute: ({ state }) => ({
|
|
110
111
|
"aria-expanded": String(state.open),
|
|
111
112
|
"data-status": state.status,
|
|
112
|
-
disabled: state.busy,
|
|
113
113
|
tabindex: 0,
|
|
114
114
|
}),
|
|
115
115
|
text: ({ state }) => state.label,
|
|
116
116
|
class: ({ state }) => ({ active: state.open }),
|
|
117
|
+
property: ({ state }) => ({ disabled: state.busy }),
|
|
117
118
|
},
|
|
118
119
|
}
|
|
119
120
|
```
|
|
@@ -131,6 +132,28 @@ including child elements. It writes text rather than parsing HTML.
|
|
|
131
132
|
also remain untouched; return `false` explicitly to remove a class. Nullish class
|
|
132
133
|
or attribute maps leave the element unchanged.
|
|
133
134
|
|
|
135
|
+
`property` writes DOM properties, not HTML attributes. For example,
|
|
136
|
+
`property: ({ state }) => ({ value: state.query })` updates an input's live `.value`.
|
|
137
|
+
It is strictly one-way: state supplies the initial and subsequent rendered values;
|
|
138
|
+
DOM edits and input/change events do not update state. A later render restores the
|
|
139
|
+
state-derived values.
|
|
140
|
+
|
|
141
|
+
Nullish property maps do nothing, and omitted properties remain untouched.
|
|
142
|
+
Explicit entries, including `null` and `undefined`, are assigned directly. Mosaic
|
|
143
|
+
does not normalise, validate, or coerce property values. Native or custom property
|
|
144
|
+
setters may coerce values, reflect attributes, or have other side effects. An
|
|
145
|
+
assignment is skipped only when `Object.is` compares the live property value and
|
|
146
|
+
the requested value as equal.
|
|
147
|
+
|
|
148
|
+
For each matched element, rendering proceeds in this order:
|
|
149
|
+
`attribute` → `text` → `class` → `property` → `sync`, regardless of configuration
|
|
150
|
+
field order. Bindings targeting the same property can overwrite one another;
|
|
151
|
+
for example, `property` and `sync` do not independently own `.value`. `sync` keeps
|
|
152
|
+
its existing behaviour, including composition handling, and has the final write
|
|
153
|
+
when it updates a managed property. Property ownership and avoiding conflicting
|
|
154
|
+
writes remain application responsibilities; Mosaic does not detect or resolve
|
|
155
|
+
conflicts.
|
|
156
|
+
|
|
134
157
|
## Events
|
|
135
158
|
|
|
136
159
|
`on` maps DOM event names to callbacks or action names. Event callbacks receive
|
|
@@ -172,10 +195,11 @@ from user input requires no action or event handler:
|
|
|
172
195
|
|
|
173
196
|
```ts
|
|
174
197
|
const controller = Mosaic(root, {
|
|
175
|
-
state: { query: "", enabled: false },
|
|
198
|
+
state: { query: "", enabled: false, delivery: "" },
|
|
176
199
|
elements: {
|
|
177
200
|
"input.search": { sync: "query" },
|
|
178
201
|
'input[type="checkbox"]': { sync: "enabled" },
|
|
202
|
+
'input[type="radio"][name="delivery"]': { sync: "delivery" },
|
|
179
203
|
},
|
|
180
204
|
});
|
|
181
205
|
```
|
|
@@ -183,18 +207,24 @@ const controller = Mosaic(root, {
|
|
|
183
207
|
Typing updates `query`; checking or unchecking the checkbox updates `enabled`.
|
|
184
208
|
String-valued inputs, textareas, and single-select controls use `.value` and require
|
|
185
209
|
string state fields. Checkboxes use `.checked` and require boolean state fields.
|
|
186
|
-
Checkbox `.value` and `.indeterminate` are left untouched. Radio
|
|
187
|
-
|
|
210
|
+
Checkbox `.value` and `.indeterminate` are left untouched. Radio groups synchronise
|
|
211
|
+
the selected input's `.value` with a string state field and use `.checked` to
|
|
212
|
+
reflect the selection. Bind all radios in a same-name group together, using a
|
|
213
|
+
separate binding and state key for each group. File inputs and multi-selects cause
|
|
214
|
+
a mount error.
|
|
188
215
|
|
|
189
216
|
At mount, the DOM wins over configured state. When controls share a key, the first
|
|
190
|
-
bound control supplies its initial value and the rest mirror it.
|
|
217
|
+
bound control supplies its initial value and the rest mirror it. For radio groups,
|
|
218
|
+
the checked option supplies the initial value, or `""` if no option is checked.
|
|
219
|
+
A state value that matches no radio option leaves the whole group unchecked. Binding order
|
|
191
220
|
follows the `elements` object's entry order, then document order within each binding. Keys with
|
|
192
221
|
no matching controls retain their configured state. Multiple checkboxes sharing a
|
|
193
222
|
key mirror one boolean; they do not collect an array of selected values. Mixing
|
|
194
|
-
checkbox and string-value controls on the same key causes a mount error.
|
|
223
|
+
checkbox and string-value controls (including radios) on the same key causes a mount error.
|
|
195
224
|
|
|
196
|
-
|
|
197
|
-
`change`.
|
|
225
|
+
Text inputs, textareas, and single-selects update state on `input`; checkboxes
|
|
226
|
+
update on both `input` and `change`. Radios update only on `change`, and only when
|
|
227
|
+
the event's radio is checked. Sync runs before consumer handlers for those events. Actions can also
|
|
198
228
|
update control values or checked states on the next render. Equal values are left
|
|
199
229
|
untouched to avoid redundant assignments and preserve cursor position.
|
|
200
230
|
|
|
@@ -217,7 +247,7 @@ see the current boolean.
|
|
|
217
247
|
During IME composition, input events continue updating state, but Mosaic leaves
|
|
218
248
|
the composing string control's value untouched. Other controls sharing the key can
|
|
219
249
|
still update. On composition end, the final DOM value is committed and mirrored.
|
|
220
|
-
Numeric coercion, array-valued checkbox groups,
|
|
250
|
+
Numeric coercion, array-valued checkbox groups, and automatic
|
|
221
251
|
form-reset handling belong in consumer code.
|
|
222
252
|
|
|
223
253
|
## State, completion, and cleanup
|
package/dist/mosaic.js
CHANGED
|
@@ -21,6 +21,13 @@ function writeClasses(node, classes) {
|
|
|
21
21
|
node.classList.toggle(name, enabled);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
+
function writeProperties(node, properties) {
|
|
25
|
+
const target = node;
|
|
26
|
+
for (const [name, value] of Object.entries(properties ?? {})) {
|
|
27
|
+
if (!Object.is(target[name], value))
|
|
28
|
+
target[name] = value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
24
31
|
|
|
25
32
|
function initialise(rootNode, config, store) {
|
|
26
33
|
const { controller } = store;
|
|
@@ -54,7 +61,7 @@ function initialise(rootNode, config, store) {
|
|
|
54
61
|
if (sync === undefined)
|
|
55
62
|
continue;
|
|
56
63
|
for (const target of binding.targets) {
|
|
57
|
-
const control = syncControl(target);
|
|
64
|
+
const control = syncControl(target, binding.targets);
|
|
58
65
|
controls.set(target, control);
|
|
59
66
|
const kind = initialisedKeys.get(sync);
|
|
60
67
|
if (kind !== undefined && kind !== control.kind) {
|
|
@@ -70,7 +77,13 @@ function initialise(rootNode, config, store) {
|
|
|
70
77
|
store.patch({ [sync]: value });
|
|
71
78
|
}
|
|
72
79
|
};
|
|
73
|
-
if (
|
|
80
|
+
if (target.localName === "input" && target.type === "radio") {
|
|
81
|
+
listen(target, "change", () => {
|
|
82
|
+
if (target.checked)
|
|
83
|
+
update();
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else if (control.kind === "boolean") {
|
|
74
87
|
listen(target, "input", update);
|
|
75
88
|
listen(target, "change", update);
|
|
76
89
|
}
|
|
@@ -114,7 +127,7 @@ function initialise(rootNode, config, store) {
|
|
|
114
127
|
});
|
|
115
128
|
}
|
|
116
129
|
store.initialise(state, (state) => {
|
|
117
|
-
for (const { targets, attribute, text, class: classes, sync } of elements) {
|
|
130
|
+
for (const { targets, attribute, text, class: classes, property, sync } of elements) {
|
|
118
131
|
for (const [index, target] of targets.entries()) {
|
|
119
132
|
if (store.isDestroyed())
|
|
120
133
|
return;
|
|
@@ -136,6 +149,12 @@ function initialise(rootNode, config, store) {
|
|
|
136
149
|
return;
|
|
137
150
|
writeClasses(target, value);
|
|
138
151
|
}
|
|
152
|
+
if (property) {
|
|
153
|
+
const value = property({ state, element: target, index });
|
|
154
|
+
if (store.isDestroyed())
|
|
155
|
+
return;
|
|
156
|
+
writeProperties(target, value);
|
|
157
|
+
}
|
|
139
158
|
if (sync !== undefined && !composing.has(target)) {
|
|
140
159
|
controls.get(target).write(state[sync]);
|
|
141
160
|
}
|
|
@@ -143,10 +162,23 @@ function initialise(rootNode, config, store) {
|
|
|
143
162
|
}
|
|
144
163
|
});
|
|
145
164
|
}
|
|
146
|
-
function syncControl(target) {
|
|
165
|
+
function syncControl(target, targets) {
|
|
147
166
|
const name = target.localName;
|
|
148
167
|
const input = target;
|
|
149
168
|
if (target.namespaceURI === "http://www.w3.org/1999/xhtml") {
|
|
169
|
+
if (name === "input" && input.type === "radio") {
|
|
170
|
+
const radios = targets.filter((target) => target.localName === "input" && target.type === "radio" &&
|
|
171
|
+
target.name === input.name);
|
|
172
|
+
return {
|
|
173
|
+
kind: "string",
|
|
174
|
+
read: () => radios.find((radio) => radio.checked)?.value ?? "",
|
|
175
|
+
write: (value) => {
|
|
176
|
+
const checked = input.value === value;
|
|
177
|
+
if (input.checked !== checked)
|
|
178
|
+
input.checked = checked;
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
|
150
182
|
if (name === "input" && input.type === "checkbox") {
|
|
151
183
|
return {
|
|
152
184
|
kind: "boolean",
|
|
@@ -171,7 +203,7 @@ function syncControl(target) {
|
|
|
171
203
|
};
|
|
172
204
|
}
|
|
173
205
|
}
|
|
174
|
-
throw new TypeError("sync requires a checkbox, string-valued input, textarea, or single-select control");
|
|
206
|
+
throw new TypeError("sync requires a radio, checkbox, string-valued input, textarea, or single-select control");
|
|
175
207
|
}
|
|
176
208
|
|
|
177
209
|
const Message = () => {
|
package/dist/mosaic.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function
|
|
1
|
+
function e(e,t){for(const[n,r]of Object.entries(t??{}))if(!1===r||null==r)e.hasAttribute(n)&&e.removeAttribute(n);else{const t=!0===r?"":String(r);e.getAttribute(n)!==t&&e.setAttribute(n,t)}}function t(e,t){const n=null==t?"":String(t);e.textContent!==n&&(e.textContent=n)}function n(e,t){for(const[n,r]of Object.entries(t??{}))e.classList.toggle(n,r)}function r(e,t){const n=e;for(const[e,r]of Object.entries(t??{}))Object.is(n[e],r)||(n[e]=r)}function o(e,t){const n=e.localName,r=e;if("http://www.w3.org/1999/xhtml"===e.namespaceURI){if("input"===n&&"radio"===r.type){const e=t.filter(e=>"input"===e.localName&&"radio"===e.type&&e.name===r.name);return{kind:"string",read:()=>e.find(e=>e.checked)?.value??"",write:e=>{const t=r.value===e;r.checked!==t&&(r.checked=t)}}}if("input"===n&&"checkbox"===r.type)return{kind:"boolean",read:()=>r.checked,write:e=>{r.checked!==e&&(r.checked=e)}};if("textarea"===n||"select"===n&&!e.multiple||"input"===n&&!["radio","file"].includes(r.type)){const t=e;return{kind:"string",read:()=>t.value,write:e=>{t.value!==e&&(t.value=e)}}}}throw new TypeError("sync requires a radio, checkbox, string-valued input, textarea, or single-select control")}function c({actions:e={},getState:t=e=>e}){let n,r,o=null,c=[],s=[],i=!1;const a=[],l=()=>new DOMException("Mosaic has been destroyed","AbortError"),u=(()=>{const e=new Set;return{clear(){e.clear()},subscribe(t){const n={callback:t};return e.add(n),()=>{e.delete(n)}},publish(...t){const n=[];for(const r of[...e])if(e.has(r))try{r.callback(...t)}catch(e){n.push(e)}return n}}})();function f(e){n=t({...e})}function d(){return{...n}}function h(){i||null!==o||(o=requestAnimationFrame(p))}function p(){if(o=null,i)return;const e=c;c=[],s=e;const t=d(),n=[];try{r(t)}catch(e){n.push(e)}if(i||n.push(...u.publish(t)),s=[],!i)if(n.length){const t=1===n.length?n[0]:new AggregateError(n,"Errors while publishing a state update");e.forEach(({reject:e})=>e(t)),0===e.length&&reportError(t)}else e.forEach(({resolve:e})=>e())}return{controller:{dispatch:(t,...n)=>{try{if(i)throw l();if(!Object.hasOwn(e,t))throw new Error(`Unknown action "${t}"`);if("function"!=typeof e[t])throw new TypeError(`Action "${t}" is not a function`);const r=e[t].call(e,d(),n[0]);if(i)throw l();return f(r),new Promise((e,t)=>{c.push({resolve:e,reject:t}),h()})}catch(e){return Promise.reject(e)}},getState:d,subscribe:e=>i?()=>{}:u.subscribe(e),destroy(){if(i)return;i=!0,null!==o&&cancelAnimationFrame(o),o=null;const e=l();[...s,...c].forEach(({reject:t})=>t(e)),s=[],c=[],u.clear(),a.splice(0).forEach(e=>e()),r=()=>{}}},own:e=>{a.push(e)},isDestroyed:()=>i,initialise:(e,t)=>{f(e),r=t,r(d())},patch:e=>{i||(f({...d(),...e}),h())}}}const s=(s,i)=>{const a=c({...i});try{!function(c,s,i){const{controller:a}=i;let l=s.state??{};if(Array.isArray(s.elements))throw new TypeError("elements must be a selector-keyed object, not an array; move each select property to an object key");const u=Object.entries(s.elements??{}).map(([e,t])=>({...t,targets:[...c.querySelectorAll(e)]}));function f(e,t,n){e.addEventListener(t,n),i.own(()=>e.removeEventListener(t,n))}function d(e){e&&e.catch(e=>{i.isDestroyed()&&e instanceof DOMException&&"AbortError"===e.name||reportError(e)})}const h=new Map,p=new WeakMap,y=new WeakSet;for(const e of u){const{sync:t}=e;if(void 0!==t)for(const n of e.targets){const r=o(n,e.targets);p.set(n,r);const c=h.get(t);if(void 0!==c&&c!==r.kind)throw new TypeError(`sync key "${t}" cannot mix checkbox and string controls`);void 0===c&&(l={...l,[t]:r.read()},h.set(t,r.kind));const s=()=>{const e=r.read();a.getState()[t]!==e&&i.patch({[t]:e})};"input"===n.localName&&"radio"===n.type?f(n,"change",()=>{n.checked&&s()}):"boolean"===r.kind?(f(n,"input",s),f(n,"change",s)):(f(n,"compositionstart",()=>{y.add(n)}),f(n,"input",e=>{e.isComposing&&y.add(n),s()}),f(n,"compositionend",()=>{y.delete(n),s()}))}}for(const e of u)e.targets.forEach((t,n)=>{for(const[r,o]of Object.entries(e.on??{}))o&&f(t,r,e=>{if(i.isDestroyed())return;const r={state:a.getState(),element:t,index:n,event:e,dispatch:a.dispatch,getState:a.getState};d("string"==typeof o?(0,a.dispatch)(o,r):o(r))})});i.initialise(l,o=>{for(const{targets:c,attribute:s,text:a,class:l,property:f,sync:d}of u)for(const[u,h]of c.entries()){if(i.isDestroyed())return;if(s){const t=s({state:o,element:h,index:u});if(i.isDestroyed())return;e(h,t)}if(a){const e=a({state:o,element:h,index:u});if(i.isDestroyed())return;t(h,e)}if(l){const e=l({state:o,element:h,index:u});if(i.isDestroyed())return;n(h,e)}if(f){const e=f({state:o,element:h,index:u});if(i.isDestroyed())return;r(h,e)}void 0===d||y.has(h)||p.get(h).write(o[d])}})}(s,i,a)}catch(e){throw a.controller.destroy(),e}return a.controller},i=s;export{i as $,s as Mosaic};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { ActionHandlers, Controller, MosaicConfig } from "./types.js";
|
|
2
|
-
export type { ActionHandlers, RenderContext, EventContext, Attributes, AttributeValue, Classes, TextValue, Controller, Dispatch, ElementConfig, EventBindings, MosaicConfig, StoreConfig, StringKey, SyncKey, } from "./types.js";
|
|
2
|
+
export type { ActionHandlers, RenderContext, EventContext, Attributes, AttributeValue, Classes, Properties, TextValue, Controller, Dispatch, ElementConfig, EventBindings, MosaicConfig, StoreConfig, StringKey, SyncKey, } from "./types.js";
|
|
3
3
|
export declare const Mosaic: <State extends object = Record<string, never>, Actions extends ActionHandlers<State> = {}>(rootNode: Element, config: MosaicConfig<State, Actions>) => Controller<State, Actions>;
|
|
4
4
|
export declare const $: typeof Mosaic;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export type AttributeValue = string | number | boolean | null | undefined;
|
|
|
11
11
|
export type Attributes = Record<string, AttributeValue>;
|
|
12
12
|
export type TextValue = string | number | boolean | null | undefined;
|
|
13
13
|
export type Classes = Record<string, boolean>;
|
|
14
|
+
export type Properties = Record<string, unknown>;
|
|
14
15
|
export type StringKey<State> = {
|
|
15
16
|
[Key in keyof State]-?: State[Key] extends string ? Key : never;
|
|
16
17
|
}[keyof State] & string;
|
|
@@ -38,6 +39,7 @@ export interface ElementConfig<State extends object, Actions = {}> {
|
|
|
38
39
|
attribute?: (context: RenderContext<State>) => Attributes | null | undefined;
|
|
39
40
|
text?: (context: RenderContext<State>) => TextValue;
|
|
40
41
|
class?: (context: RenderContext<State>) => Classes | null | undefined;
|
|
42
|
+
property?: (context: RenderContext<State>) => Properties | null | undefined;
|
|
41
43
|
on?: EventBindings<State, Actions>;
|
|
42
44
|
sync?: SyncKey<State>;
|
|
43
45
|
}
|
package/dist/types/xo.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Attributes, Classes, TextValue } from "./types.js";
|
|
1
|
+
import type { Attributes, Classes, Properties, TextValue } from "./types.js";
|
|
2
2
|
export declare function write(node: Element, attributes: Attributes | null | undefined): void;
|
|
3
3
|
export declare function writeText(node: Element, value: TextValue): void;
|
|
4
4
|
export declare function writeClasses(node: Element, classes: Classes | null | undefined): void;
|
|
5
|
+
export declare function writeProperties(node: Element, properties: Properties | null | undefined): void;
|