mosaic 2.1.0 → 2.2.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 +26 -3
- package/dist/mosaic.js +14 -1
- 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
|
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;
|
|
@@ -114,7 +121,7 @@ function initialise(rootNode, config, store) {
|
|
|
114
121
|
});
|
|
115
122
|
}
|
|
116
123
|
store.initialise(state, (state) => {
|
|
117
|
-
for (const { targets, attribute, text, class: classes, sync } of elements) {
|
|
124
|
+
for (const { targets, attribute, text, class: classes, property, sync } of elements) {
|
|
118
125
|
for (const [index, target] of targets.entries()) {
|
|
119
126
|
if (store.isDestroyed())
|
|
120
127
|
return;
|
|
@@ -136,6 +143,12 @@ function initialise(rootNode, config, store) {
|
|
|
136
143
|
return;
|
|
137
144
|
writeClasses(target, value);
|
|
138
145
|
}
|
|
146
|
+
if (property) {
|
|
147
|
+
const value = property({ state, element: target, index });
|
|
148
|
+
if (store.isDestroyed())
|
|
149
|
+
return;
|
|
150
|
+
writeProperties(target, value);
|
|
151
|
+
}
|
|
139
152
|
if (sync !== undefined && !composing.has(target)) {
|
|
140
153
|
controls.get(target).write(state[sync]);
|
|
141
154
|
}
|
package/dist/mosaic.min.js
CHANGED
|
@@ -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){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
|
|
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};
|
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;
|