@snupo/jmc 1.2.0 → 1.4.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/src/props.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import type { J } from './h.js'
2
+ import { Thunk } from './base.js'
2
3
 
3
4
  /** settings to tweak jmc */
4
5
  export const jmcconfig = {
5
6
  /** marks component roots with a comp="Name" attribute */
6
7
  debug: false,
7
8
  /** prop names always set as attributes, never as properties */
8
- attributes: new Set(['selected', 'viewBox']),
9
+ attributes: new Set(['selected', 'viewBox', 'list']),
9
10
  }
10
11
 
11
12
  const isproperty = (e: Element, name: string, value: any) =>
@@ -16,6 +17,7 @@ const isproperty = (e: Element, name: string, value: any) =>
16
17
  name !== 'style'))
17
18
 
18
19
  let removeprop = (e: Element & J, p: string, old: any) => {
20
+ if (p.startsWith('on') && typeof old == 'function') return e.removeEventListener(p.slice(2), old)
19
21
  if (isproperty(e, p, old)) e[p] = typeof old === 'boolean' ? false : ''
20
22
  if (!(old instanceof Object)) e.removeAttribute(p) // reflected properties (title, id, ...) leave an empty attribute otherwise
21
23
  }
@@ -23,7 +25,7 @@ let removeprop = (e: Element & J, p: string, old: any) => {
23
25
  /** applies now over old and returns the applied values, with thunks evaluated, to serve as old on the next call */
24
26
  export let setprops = (e: Element & J, old: J = {}, now: J): J => {
25
27
  // evaluate thunks into a copy: now is h.p, which gets rendered again and must keep its thunks
26
- now = Object.fromEntries(Object.entries(now).map(([k, v]) => [k, v?.thunk ? v() : v]))
28
+ now = Object.fromEntries(Object.entries(now).map(([k, v]) => [k, v instanceof Thunk ? v.f() : v]))
27
29
 
28
30
  // removals
29
31
  for (let p in old)
@@ -34,6 +36,11 @@ export let setprops = (e: Element & J, old: J = {}, now: J): J => {
34
36
  let v = now[p]
35
37
  if (v !== old[p] && !(v == null && old[p] == null)) { // strict: 0 vs '' or 1 vs true are changes. null vs undefined is not
36
38
  if (v == null) removeprop(e, p, old[p])
39
+ else if (p.startsWith('on') && typeof v == 'function') {
40
+ // listeners, not on* properties: those fail silently for events the element has no property for
41
+ if (typeof old[p] == 'function') e.removeEventListener(p.slice(2), old[p])
42
+ e.addEventListener(p.slice(2), v)
43
+ }
37
44
  else if (isproperty(e, p, v)) e[p] = v
38
45
  else if (v === false && !p.startsWith('aria-')) e.removeAttribute(p) // aria-*="false" is meaningful
39
46
  else e.setAttribute(p, v)
package/dist/events.d.ts DELETED
@@ -1,9 +0,0 @@
1
- declare global {
2
- interface Element {
3
- eventmap?: {
4
- [type: string]: EventListener | null;
5
- };
6
- }
7
- }
8
- export declare function setevent(el: Element, type: string, handler: EventListener | null): void;
9
- //# sourceMappingURL=events.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAEA,OAAO,CAAC,MAAM,CAAC;IACX,UAAU,OAAO;QACb,QAAQ,CAAC,EAAE;YAAE,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI,CAAA;SAAE,CAAA;KACtD;CACJ;AAED,wBAAgB,QAAQ,CACpB,EAAE,EAAE,OAAO,EACX,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,aAAa,GAAG,IAAI,QAahC"}
package/dist/events.js DELETED
@@ -1,17 +0,0 @@
1
- // future work: maybe lifecycle callbacks should occur in a phase 2 after a phase 1 that does all dom updates
2
- export function setevent(el, type, handler) {
3
- let map = el.eventmap ??= {};
4
- const old = map[type];
5
- if (old === handler)
6
- return;
7
- if (old)
8
- el.removeEventListener(type, old);
9
- if (handler) {
10
- el.addEventListener(type, handler);
11
- map[type] = handler;
12
- }
13
- else {
14
- delete map[type];
15
- }
16
- }
17
- //# sourceMappingURL=events.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAAA,6GAA6G;AAQ7G,MAAM,UAAU,QAAQ,CACpB,EAAW,EACX,IAAY,EACZ,OAA6B;IAE7B,IAAI,GAAG,GAAG,EAAE,CAAC,QAAQ,KAAK,EAAE,CAAA;IAE5B,MAAM,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC,CAAA;IACrB,IAAI,GAAG,KAAK,OAAO;QAAE,OAAM;IAC3B,IAAI,GAAG;QAAE,EAAE,CAAC,mBAAmB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IAC1C,IAAI,OAAO,EAAE,CAAC;QACV,EAAE,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QAClC,GAAG,CAAC,IAAI,CAAC,GAAG,OAAO,CAAA;IACvB,CAAC;SAAM,CAAC;QACJ,OAAO,GAAG,CAAC,IAAI,CAAC,CAAA;IACpB,CAAC;AACL,CAAC","sourcesContent":["// future work: maybe lifecycle callbacks should occur in a phase 2 after a phase 1 that does all dom updates\r\n\r\ndeclare global {\r\n interface Element {\r\n eventmap?: { [type: string]: EventListener | null }\r\n }\r\n}\r\n\r\nexport function setevent(\r\n el: Element,\r\n type: string,\r\n handler: EventListener | null) {\r\n\r\n let map = el.eventmap ??= {}\r\n\r\n const old = map[type]\r\n if (old === handler) return\r\n if (old) el.removeEventListener(type, old)\r\n if (handler) {\r\n el.addEventListener(type, handler)\r\n map[type] = handler\r\n } else {\r\n delete map[type]\r\n }\r\n}"]}
package/src/events.ts DELETED
@@ -1,25 +0,0 @@
1
- // future work: maybe lifecycle callbacks should occur in a phase 2 after a phase 1 that does all dom updates
2
-
3
- declare global {
4
- interface Element {
5
- eventmap?: { [type: string]: EventListener | null }
6
- }
7
- }
8
-
9
- export function setevent(
10
- el: Element,
11
- type: string,
12
- handler: EventListener | null) {
13
-
14
- let map = el.eventmap ??= {}
15
-
16
- const old = map[type]
17
- if (old === handler) return
18
- if (old) el.removeEventListener(type, old)
19
- if (handler) {
20
- el.addEventListener(type, handler)
21
- map[type] = handler
22
- } else {
23
- delete map[type]
24
- }
25
- }