@neovici/cosmoz-utils 6.23.0 → 6.24.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.
@@ -0,0 +1,3 @@
1
+ type Value = string | null | ((item: Element) => string | null);
2
+ export declare const forwardAttributes: (attrs: Record<string, Value>) => unknown;
3
+ export {};
@@ -0,0 +1,64 @@
1
+ import { noChange } from 'lit-html';
2
+ import { AsyncDirective, directive } from 'lit-html/async-directive.js';
3
+ import { invoke } from '../function';
4
+ const write = (item, name, value) => {
5
+ if (value == null) {
6
+ item.removeAttribute(name);
7
+ }
8
+ else {
9
+ item.setAttribute(name, value);
10
+ }
11
+ };
12
+ const forward = (attrs, item) => {
13
+ for (const [name, value] of Object.entries(attrs)) {
14
+ write(item, name, invoke(value, item));
15
+ }
16
+ };
17
+ /**
18
+ * Forwards attributes onto every flattened assigned element of the slot
19
+ * the directive is placed on — on each render (state changes re-render
20
+ * the slot) and on slot changes (different elements are assigned).
21
+ *
22
+ * Values are strings, nulls (removals), or functions of the assigned
23
+ * element. Placement is the configuration: the placed slot states which
24
+ * members receive the attributes. Slot semantics decide membership:
25
+ * slotting forwards through nested slots, wrappers do not — a
26
+ * non-forwarding wrapper receives the attributes itself without
27
+ * exposing state.
28
+ */
29
+ class ForwardAttributesDirective extends AsyncDirective {
30
+ _slot;
31
+ _attrs;
32
+ #forward = () => {
33
+ if (!this._slot || !this._attrs) {
34
+ return;
35
+ }
36
+ const items = this._slot.assignedElements({ flatten: true });
37
+ for (const item of items) {
38
+ forward(this._attrs, item);
39
+ }
40
+ };
41
+ #onSlotChange = () => this.#forward();
42
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
43
+ render(attrs) {
44
+ return noChange;
45
+ }
46
+ update(part, [attrs]) {
47
+ this._attrs = attrs;
48
+ const slot = part.element;
49
+ if (this._slot !== slot) {
50
+ this._slot?.removeEventListener('slotchange', this.#onSlotChange);
51
+ this._slot = slot;
52
+ slot.addEventListener('slotchange', this.#onSlotChange);
53
+ }
54
+ this.#forward();
55
+ return noChange;
56
+ }
57
+ disconnected() {
58
+ // the slot outlives renders; the listener is owned for its lifetime
59
+ // and removed when the part is cleared (template swap, disconnect)
60
+ this._slot?.removeEventListener('slotchange', this.#onSlotChange);
61
+ this._slot = undefined;
62
+ }
63
+ }
64
+ export const forwardAttributes = directive(ForwardAttributesDirective);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@neovici/cosmoz-utils",
3
- "version": "6.23.0",
3
+ "version": "6.24.0",
4
4
  "description": "Date, money and template management functions commonly needed in Cosmoz views.",
5
5
  "keywords": [
6
6
  "polymer",
@@ -60,7 +60,7 @@
60
60
  "./keybindings/*": "./dist/keybindings/*.js"
61
61
  },
62
62
  "dependencies": {
63
- "@pionjs/pion": "^2.7.1"
63
+ "@pionjs/pion": "^2.16.3"
64
64
  },
65
65
  "devDependencies": {
66
66
  "@changesets/cli": "^3.0.2",