mosaic 2.2.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 CHANGED
@@ -195,10 +195,11 @@ from user input requires no action or event handler:
195
195
 
196
196
  ```ts
197
197
  const controller = Mosaic(root, {
198
- state: { query: "", enabled: false },
198
+ state: { query: "", enabled: false, delivery: "" },
199
199
  elements: {
200
200
  "input.search": { sync: "query" },
201
201
  'input[type="checkbox"]': { sync: "enabled" },
202
+ 'input[type="radio"][name="delivery"]': { sync: "delivery" },
202
203
  },
203
204
  });
204
205
  ```
@@ -206,18 +207,24 @@ const controller = Mosaic(root, {
206
207
  Typing updates `query`; checking or unchecking the checkbox updates `enabled`.
207
208
  String-valued inputs, textareas, and single-select controls use `.value` and require
208
209
  string state fields. Checkboxes use `.checked` and require boolean state fields.
209
- Checkbox `.value` and `.indeterminate` are left untouched. Radio buttons, file
210
- inputs, and multi-selects cause a mount error.
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.
211
215
 
212
216
  At mount, the DOM wins over configured state. When controls share a key, the first
213
- bound control supplies its initial value and the rest mirror it. Binding order
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
214
220
  follows the `elements` object's entry order, then document order within each binding. Keys with
215
221
  no matching controls retain their configured state. Multiple checkboxes sharing a
216
222
  key mirror one boolean; they do not collect an array of selected values. Mixing
217
- 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.
218
224
 
219
- String controls update state on `input`; checkboxes update on both `input` and
220
- `change`. Sync runs before consumer handlers for those events. Actions can also
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
221
228
  update control values or checked states on the next render. Equal values are left
222
229
  untouched to avoid redundant assignments and preserve cursor position.
223
230
 
@@ -240,7 +247,7 @@ see the current boolean.
240
247
  During IME composition, input events continue updating state, but Mosaic leaves
241
248
  the composing string control's value untouched. Other controls sharing the key can
242
249
  still update. On composition end, the final DOM value is committed and mirrored.
243
- Numeric coercion, array-valued checkbox groups, radio groups, and automatic
250
+ Numeric coercion, array-valued checkbox groups, and automatic
244
251
  form-reset handling belong in consumer code.
245
252
 
246
253
  ## State, completion, and cleanup
package/dist/mosaic.js CHANGED
@@ -61,7 +61,7 @@ function initialise(rootNode, config, store) {
61
61
  if (sync === undefined)
62
62
  continue;
63
63
  for (const target of binding.targets) {
64
- const control = syncControl(target);
64
+ const control = syncControl(target, binding.targets);
65
65
  controls.set(target, control);
66
66
  const kind = initialisedKeys.get(sync);
67
67
  if (kind !== undefined && kind !== control.kind) {
@@ -77,7 +77,13 @@ function initialise(rootNode, config, store) {
77
77
  store.patch({ [sync]: value });
78
78
  }
79
79
  };
80
- if (control.kind === "boolean") {
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") {
81
87
  listen(target, "input", update);
82
88
  listen(target, "change", update);
83
89
  }
@@ -156,10 +162,23 @@ function initialise(rootNode, config, store) {
156
162
  }
157
163
  });
158
164
  }
159
- function syncControl(target) {
165
+ function syncControl(target, targets) {
160
166
  const name = target.localName;
161
167
  const input = target;
162
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
+ }
163
182
  if (name === "input" && input.type === "checkbox") {
164
183
  return {
165
184
  kind: "boolean",
@@ -184,7 +203,7 @@ function syncControl(target) {
184
203
  };
185
204
  }
186
205
  }
187
- 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");
188
207
  }
189
208
 
190
209
  const Message = () => {
@@ -1 +1 @@
1
- function t(t,e){for(const[n,r]of Object.entries(e??{}))if(!1===r||null==r)t.hasAttribute(n)&&t.removeAttribute(n);else{const e=!0===r?"":String(r);t.getAttribute(n)!==e&&t.setAttribute(n,e)}}function e(t,e){const n=null==e?"":String(e);t.textContent!==n&&(t.textContent=n)}function n(t,e){for(const[n,r]of Object.entries(e??{}))t.classList.toggle(n,r)}function r(t,e){const n=t;for(const[t,r]of Object.entries(e??{}))Object.is(n[t],r)||(n[t]=r)}function o(t){const e=t.localName,n=t;if("http://www.w3.org/1999/xhtml"===t.namespaceURI){if("input"===e&&"checkbox"===n.type)return{kind:"boolean",read:()=>n.checked,write:t=>{n.checked!==t&&(n.checked=t)}};if("textarea"===e||"select"===e&&!t.multiple||"input"===e&&!["radio","file"].includes(n.type)){const e=t;return{kind:"string",read:()=>e.value,write:t=>{e.value!==t&&(e.value=t)}}}}throw new TypeError("sync requires a checkbox, string-valued input, textarea, or single-select control")}function s({actions:t={},getState:e=t=>t}){let n,r,o=null,s=[],c=[],i=!1;const a=[],l=()=>new DOMException("Mosaic has been destroyed","AbortError"),u=(()=>{const t=new Set;return{clear(){t.clear()},subscribe(e){const n={callback:e};return t.add(n),()=>{t.delete(n)}},publish(...e){const n=[];for(const r of[...t])if(t.has(r))try{r.callback(...e)}catch(t){n.push(t)}return n}}})();function f(t){n=e({...t})}function d(){return{...n}}function h(){i||null!==o||(o=requestAnimationFrame(p))}function p(){if(o=null,i)return;const t=s;s=[],c=t;const e=d(),n=[];try{r(e)}catch(t){n.push(t)}if(i||n.push(...u.publish(e)),c=[],!i)if(n.length){const e=1===n.length?n[0]:new AggregateError(n,"Errors while publishing a state update");t.forEach(({reject:t})=>t(e)),0===t.length&&reportError(e)}else t.forEach(({resolve:t})=>t())}return{controller:{dispatch:(e,...n)=>{try{if(i)throw l();if(!Object.hasOwn(t,e))throw new Error(`Unknown action "${e}"`);if("function"!=typeof t[e])throw new TypeError(`Action "${e}" is not a function`);const r=t[e].call(t,d(),n[0]);if(i)throw l();return f(r),new Promise((t,e)=>{s.push({resolve:t,reject:e}),h()})}catch(t){return Promise.reject(t)}},getState:d,subscribe:t=>i?()=>{}:u.subscribe(t),destroy(){if(i)return;i=!0,null!==o&&cancelAnimationFrame(o),o=null;const t=l();[...c,...s].forEach(({reject:e})=>e(t)),c=[],s=[],u.clear(),a.splice(0).forEach(t=>t()),r=()=>{}}},own:t=>{a.push(t)},isDestroyed:()=>i,initialise:(t,e)=>{f(t),r=e,r(d())},patch:t=>{i||(f({...d(),...t}),h())}}}const c=(c,i)=>{const a=s({...i});try{!function(s,c,i){const{controller:a}=i;let l=c.state??{};if(Array.isArray(c.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(c.elements??{}).map(([t,e])=>({...e,targets:[...s.querySelectorAll(t)]}));function f(t,e,n){t.addEventListener(e,n),i.own(()=>t.removeEventListener(e,n))}function d(t){t&&t.catch(t=>{i.isDestroyed()&&t instanceof DOMException&&"AbortError"===t.name||reportError(t)})}const h=new Map,p=new WeakMap,y=new WeakSet;for(const t of u){const{sync:e}=t;if(void 0!==e)for(const n of t.targets){const t=o(n);p.set(n,t);const r=h.get(e);if(void 0!==r&&r!==t.kind)throw new TypeError(`sync key "${e}" cannot mix checkbox and string controls`);void 0===r&&(l={...l,[e]:t.read()},h.set(e,t.kind));const s=()=>{const n=t.read();a.getState()[e]!==n&&i.patch({[e]:n})};"boolean"===t.kind?(f(n,"input",s),f(n,"change",s)):(f(n,"compositionstart",()=>{y.add(n)}),f(n,"input",t=>{t.isComposing&&y.add(n),s()}),f(n,"compositionend",()=>{y.delete(n),s()}))}}for(const t of u)t.targets.forEach((e,n)=>{for(const[r,o]of Object.entries(t.on??{}))o&&f(e,r,t=>{if(i.isDestroyed())return;const r={state:a.getState(),element:e,index:n,event:t,dispatch:a.dispatch,getState:a.getState};d("string"==typeof o?(0,a.dispatch)(o,r):o(r))})});i.initialise(l,o=>{for(const{targets:s,attribute:c,text:a,class:l,property:f,sync:d}of u)for(const[u,h]of s.entries()){if(i.isDestroyed())return;if(c){const e=c({state:o,element:h,index:u});if(i.isDestroyed())return;t(h,e)}if(a){const t=a({state:o,element:h,index:u});if(i.isDestroyed())return;e(h,t)}if(l){const t=l({state:o,element:h,index:u});if(i.isDestroyed())return;n(h,t)}if(f){const t=f({state:o,element:h,index:u});if(i.isDestroyed())return;r(h,t)}void 0===d||y.has(h)||p.get(h).write(o[d])}})}(c,i,a)}catch(t){throw a.controller.destroy(),t}return a.controller},i=c;export{i as $,c as Mosaic};
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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mosaic",
3
3
  "type": "module",
4
- "version": "2.2.0",
4
+ "version": "2.3.0",
5
5
  "description": "A JavaScript library for progressive enhancing HTML",
6
6
  "main": "dist/mosaic.js",
7
7
  "browser": "dist/mosaic.js",