dotquilt 0.0.0-stage → 0.1.1

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.
Files changed (183) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +64 -2
  3. package/dist/animations.d.ts +105 -0
  4. package/dist/animations.js +107 -0
  5. package/dist/catalog-styles.d.ts +4 -0
  6. package/dist/catalog-styles.js +12 -0
  7. package/dist/chart-animations.d.ts +36 -0
  8. package/dist/chart-animations.js +25 -0
  9. package/dist/chart-styles.d.ts +3 -0
  10. package/dist/chart-styles.js +23 -0
  11. package/dist/chart.css +4 -0
  12. package/dist/chart.d.ts +73 -0
  13. package/dist/chart.js +80 -0
  14. package/dist/circle.css +5 -0
  15. package/dist/index.d.ts +11 -0
  16. package/dist/index.js +9 -0
  17. package/dist/internal/binding.d.ts +13 -0
  18. package/dist/internal/binding.js +21 -0
  19. package/dist/internal/capture.d.ts +4 -0
  20. package/dist/internal/capture.js +13 -0
  21. package/dist/internal/chart-appearance.d.ts +12 -0
  22. package/dist/internal/chart-appearance.js +2 -0
  23. package/dist/internal/chart-definitions.d.ts +19 -0
  24. package/dist/internal/chart-definitions.js +149 -0
  25. package/dist/internal/chart-factory.d.ts +12 -0
  26. package/dist/internal/chart-factory.js +31 -0
  27. package/dist/internal/chart-field.d.ts +22 -0
  28. package/dist/internal/chart-field.js +74 -0
  29. package/dist/internal/chart-idle.d.ts +3 -0
  30. package/dist/internal/chart-idle.js +96 -0
  31. package/dist/internal/chart-math.d.ts +8 -0
  32. package/dist/internal/chart-math.js +19 -0
  33. package/dist/internal/chart-participant.d.ts +8 -0
  34. package/dist/internal/chart-participant.js +4 -0
  35. package/dist/internal/chart-projection.d.ts +11 -0
  36. package/dist/internal/chart-projection.js +69 -0
  37. package/dist/internal/content-box.d.ts +11 -0
  38. package/dist/internal/content-box.js +36 -0
  39. package/dist/internal/coordinated-handoff.d.ts +4 -0
  40. package/dist/internal/coordinated-handoff.js +158 -0
  41. package/dist/internal/count.d.ts +3 -0
  42. package/dist/internal/count.js +20 -0
  43. package/dist/internal/defaults.d.ts +11 -0
  44. package/dist/internal/defaults.js +11 -0
  45. package/dist/internal/dense/managed-painter.d.ts +4 -0
  46. package/dist/internal/dense/managed-painter.js +361 -0
  47. package/dist/internal/dense/runner.d.ts +2 -0
  48. package/dist/internal/dense/runner.js +5 -0
  49. package/dist/internal/dense-loader.d.ts +83 -0
  50. package/dist/internal/dense-loader.js +331 -0
  51. package/dist/internal/dissolve.d.ts +27 -0
  52. package/dist/internal/dissolve.js +216 -0
  53. package/dist/internal/dom.d.ts +23 -0
  54. package/dist/internal/dom.js +104 -0
  55. package/dist/internal/effect.d.ts +2 -0
  56. package/dist/internal/effect.js +17 -0
  57. package/dist/internal/element.d.ts +89 -0
  58. package/dist/internal/element.js +384 -0
  59. package/dist/internal/event-element.d.ts +8 -0
  60. package/dist/internal/event-element.js +20 -0
  61. package/dist/internal/frames.d.ts +1 -0
  62. package/dist/internal/frames.js +51 -0
  63. package/dist/internal/handoff.d.ts +56 -0
  64. package/dist/internal/handoff.js +102 -0
  65. package/dist/internal/ledger.d.ts +13 -0
  66. package/dist/internal/ledger.js +78 -0
  67. package/dist/internal/loading-controller.d.ts +62 -0
  68. package/dist/internal/loading-controller.js +698 -0
  69. package/dist/internal/marks.d.ts +153 -0
  70. package/dist/internal/marks.js +227 -0
  71. package/dist/internal/math.d.ts +2 -0
  72. package/dist/internal/math.js +2 -0
  73. package/dist/internal/morph-marks.d.ts +62 -0
  74. package/dist/internal/morph-marks.js +205 -0
  75. package/dist/internal/morph.d.ts +35 -0
  76. package/dist/internal/morph.js +488 -0
  77. package/dist/internal/paint.d.ts +29 -0
  78. package/dist/internal/paint.js +1 -0
  79. package/dist/internal/painter.d.ts +20 -0
  80. package/dist/internal/painter.js +155 -0
  81. package/dist/internal/preset-data.d.ts +12 -0
  82. package/dist/internal/preset-data.js +31 -0
  83. package/dist/internal/preset.d.ts +22 -0
  84. package/dist/internal/preset.js +86 -0
  85. package/dist/internal/pulse.d.ts +7 -0
  86. package/dist/internal/pulse.js +74 -0
  87. package/dist/internal/reveal-effect.d.ts +7 -0
  88. package/dist/internal/reveal-effect.js +24 -0
  89. package/dist/internal/reveal.d.ts +5 -0
  90. package/dist/internal/reveal.js +11 -0
  91. package/dist/internal/runner.d.ts +9 -0
  92. package/dist/internal/runner.js +70 -0
  93. package/dist/internal/services.d.ts +12 -0
  94. package/dist/internal/services.js +159 -0
  95. package/dist/internal/skeleton-handoff.d.ts +26 -0
  96. package/dist/internal/skeleton-handoff.js +6 -0
  97. package/dist/internal/skeleton-source.d.ts +5 -0
  98. package/dist/internal/skeleton-source.js +106 -0
  99. package/dist/internal/surface.d.ts +4 -0
  100. package/dist/internal/surface.js +21 -0
  101. package/dist/internal/table-definitions.d.ts +27 -0
  102. package/dist/internal/table-definitions.js +139 -0
  103. package/dist/internal/table-factory.d.ts +12 -0
  104. package/dist/internal/table-factory.js +44 -0
  105. package/dist/internal/table-field.d.ts +12 -0
  106. package/dist/internal/table-field.js +75 -0
  107. package/dist/internal/table-model.d.ts +21 -0
  108. package/dist/internal/table-model.js +117 -0
  109. package/dist/internal/table-projection.d.ts +7 -0
  110. package/dist/internal/table-projection.js +110 -0
  111. package/dist/internal/target-changes.d.ts +3 -0
  112. package/dist/internal/target-changes.js +2 -0
  113. package/dist/internal/target-tracker.d.ts +18 -0
  114. package/dist/internal/target-tracker.js +33 -0
  115. package/dist/internal/targets.d.ts +3 -0
  116. package/dist/internal/targets.js +15 -0
  117. package/dist/internal/text-definitions.d.ts +141 -0
  118. package/dist/internal/text-definitions.js +228 -0
  119. package/dist/internal/text-glyphs.d.ts +4 -0
  120. package/dist/internal/text-glyphs.js +32 -0
  121. package/dist/internal/text-morph.d.ts +32 -0
  122. package/dist/internal/text-morph.js +113 -0
  123. package/dist/internal/text-playback.d.ts +4 -0
  124. package/dist/internal/text-playback.js +119 -0
  125. package/dist/internal/text-scramble.d.ts +18 -0
  126. package/dist/internal/text-scramble.js +47 -0
  127. package/dist/internal/time.d.ts +9 -0
  128. package/dist/internal/time.js +13 -0
  129. package/dist/internal/unstyled.d.ts +8 -0
  130. package/dist/internal/unstyled.js +8 -0
  131. package/dist/line.css +5 -0
  132. package/dist/region-css.d.ts +1 -0
  133. package/dist/region-css.js +6 -0
  134. package/dist/region-styles.d.ts +3 -0
  135. package/dist/region-styles.js +15 -0
  136. package/dist/region.css +1 -0
  137. package/dist/region.d.ts +31 -0
  138. package/dist/region.js +100 -0
  139. package/dist/register-all.d.ts +2 -0
  140. package/dist/register-all.js +33 -0
  141. package/dist/server.d.ts +63 -0
  142. package/dist/server.js +141 -0
  143. package/dist/skeleton-animations.d.ts +10 -0
  144. package/dist/skeleton-animations.js +35 -0
  145. package/dist/skeleton-block.d.ts +50 -0
  146. package/dist/skeleton-block.js +260 -0
  147. package/dist/skeleton-circle.d.ts +5 -0
  148. package/dist/skeleton-circle.js +11 -0
  149. package/dist/skeleton-family.css +13 -0
  150. package/dist/skeleton-line.d.ts +5 -0
  151. package/dist/skeleton-line.js +11 -0
  152. package/dist/skeleton-text.css +5 -0
  153. package/dist/skeleton-text.d.ts +22 -0
  154. package/dist/skeleton-text.js +115 -0
  155. package/dist/skeleton.css +4 -0
  156. package/dist/skeleton.d.ts +5 -0
  157. package/dist/skeleton.js +4 -0
  158. package/dist/static-catalog-styles.d.ts +8 -0
  159. package/dist/static-catalog-styles.js +44 -0
  160. package/dist/styles.d.ts +4 -0
  161. package/dist/styles.js +18 -0
  162. package/dist/table-animations.d.ts +11 -0
  163. package/dist/table-animations.js +4 -0
  164. package/dist/table-styles.d.ts +2 -0
  165. package/dist/table-styles.js +12 -0
  166. package/dist/table.css +3 -0
  167. package/dist/table.d.ts +88 -0
  168. package/dist/table.js +126 -0
  169. package/dist/text-animations.d.ts +31 -0
  170. package/dist/text-animations.js +40 -0
  171. package/dist/text-styles.d.ts +3 -0
  172. package/dist/text-styles.js +24 -0
  173. package/dist/text.css +3 -0
  174. package/dist/text.d.ts +42 -0
  175. package/dist/text.js +402 -0
  176. package/dist/types.d.ts +56 -0
  177. package/dist/types.js +18 -0
  178. package/dist/typing-styles.d.ts +1 -0
  179. package/dist/typing-styles.js +7 -0
  180. package/dist/typing.css +4 -0
  181. package/dist/typing.d.ts +35 -0
  182. package/dist/typing.js +164 -0
  183. package/package.json +100 -4
@@ -0,0 +1,104 @@
1
+ import { LoaderRegistrationError } from '../types.js';
2
+ /** Lets a host's Canvas painter re-read colours after a stylesheet change. */
3
+ export const styleRefresh = Symbol('dotquilt.style-refresh');
4
+ export const BaseElement = (globalThis.HTMLElement ??
5
+ class {
6
+ });
7
+ export function register(name, elementClass) {
8
+ if (typeof customElements === 'undefined')
9
+ throw new Error('Registration requires a browser');
10
+ const existing = customElements.get(name);
11
+ // One copy per page (AUDIT.md decision 5): only this exact class counts.
12
+ if (existing === elementClass)
13
+ return;
14
+ if (existing)
15
+ throw new LoaderRegistrationError(name);
16
+ customElements.define(name, elementClass);
17
+ }
18
+ export function node(doc, tag, attrs = {}) {
19
+ const el = doc.createElement(tag);
20
+ for (const key of Object.keys(attrs))
21
+ el.setAttribute(key, attrs[key]);
22
+ return el;
23
+ }
24
+ export function installStyles(root, css, policy) {
25
+ if (policy.mode === 'external')
26
+ root.append(node(root.ownerDocument, 'link', {
27
+ rel: 'stylesheet',
28
+ href: policy.href,
29
+ }));
30
+ else
31
+ for (const text of css) {
32
+ const style = node(root.ownerDocument, 'style');
33
+ if (policy.nonce)
34
+ style.setAttribute('nonce', policy.nonce);
35
+ style.textContent = text;
36
+ root.append(style);
37
+ }
38
+ }
39
+ export function upgradeProperties(host, keys) {
40
+ for (const key of keys)
41
+ if (Object.hasOwn(host, key)) {
42
+ const value = Reflect.get(host, key);
43
+ Reflect.deleteProperty(host, key);
44
+ Reflect.set(host, key, value);
45
+ }
46
+ }
47
+ export function emit(host, type, detail) {
48
+ const Event = host.ownerDocument.defaultView?.CustomEvent ?? CustomEvent;
49
+ host.dispatchEvent(new Event(type, {
50
+ detail: Object.freeze(detail),
51
+ bubbles: true,
52
+ composed: true,
53
+ }));
54
+ }
55
+ export function composedParent(node) {
56
+ return (node.assignedSlot ??
57
+ node.parentNode ??
58
+ node.host ??
59
+ null);
60
+ }
61
+ export function motionRestricted(host) {
62
+ return [host, ...ancestors(host)].some((element) => element.matches('[hidden],[data-dotquilt-offscreen],[data-dotquilt-disabled],[data-dotquilt-paused],[motion]:not([motion=system])'));
63
+ }
64
+ export function ancestors(host) {
65
+ const result = [];
66
+ let parent = composedParent(host);
67
+ while (parent) {
68
+ if (parent.nodeType === 1)
69
+ result.push(parent);
70
+ parent = composedParent(parent);
71
+ }
72
+ return result;
73
+ }
74
+ export function activeWithin(host) {
75
+ let active = host.ownerDocument.activeElement;
76
+ while (active?.shadowRoot?.activeElement)
77
+ active = active.shadowRoot.activeElement;
78
+ return (!!active &&
79
+ (active === host || ancestors(active).includes(host)));
80
+ }
81
+ const mediaQueries = new WeakMap();
82
+ /** Whether a media query matches, reusing one MediaQueryList per window and query. */
83
+ export function media(win, query) {
84
+ if (!win)
85
+ return false;
86
+ const queries = mediaQueries.get(win) ?? new Map();
87
+ mediaQueries.set(win, queries);
88
+ let list = queries.get(query);
89
+ if (!list) {
90
+ list = win.matchMedia(query);
91
+ queries.set(query, list);
92
+ }
93
+ return list.matches;
94
+ }
95
+ /**
96
+ * Where painters append their drawing: the host's frame when it has one (a
97
+ * grid spanning exactly the content box), otherwise the shadow root itself.
98
+ */
99
+ export function paintLayer(root) {
100
+ for (const child of root.children)
101
+ if (child.hasAttribute('data-dotquilt-frame'))
102
+ return child;
103
+ return root;
104
+ }
@@ -0,0 +1,2 @@
1
+ /** Update one owned, CSP-safe effect; identical frames do not touch the animation. */
2
+ export declare function createEffect(element: HTMLElement, own: (animation: Animation) => void): (values: PropertyIndexedKeyframes) => void;
@@ -0,0 +1,17 @@
1
+ /** Update one owned, CSP-safe effect; identical frames do not touch the animation. */
2
+ export function createEffect(element, own) {
3
+ let animation;
4
+ let previous = '';
5
+ return (values) => {
6
+ const key = JSON.stringify(values);
7
+ if (key === previous)
8
+ return;
9
+ if (animation)
10
+ animation.effect.setKeyframes(values);
11
+ else {
12
+ animation = element.animate(values, { duration: 1, fill: 'both' });
13
+ own(animation);
14
+ }
15
+ previous = key;
16
+ };
17
+ }
@@ -0,0 +1,89 @@
1
+ import { type Mark, type Motion, type StylePolicy } from '../types.js';
2
+ import { BaseElement } from './dom.js';
3
+ import type { Preset, PresetKind } from './preset.js';
4
+ import { subscribe } from './services.js';
5
+ export interface Activity {
6
+ started: number | undefined;
7
+ }
8
+ export { now, remaining } from './time.js';
9
+ export declare abstract class ManagedElement<K extends PresetKind = 'skeleton'> extends BaseElement {
10
+ #private;
11
+ static observedAttributes: string[];
12
+ protected policy: StylePolicy;
13
+ protected ownActivity: Activity;
14
+ protected resources: ReturnType<typeof subscribe> | undefined;
15
+ protected deadline: ReturnType<typeof setTimeout> | undefined;
16
+ protected abstract capturePreset(value: Preset<K>): Preset<K>;
17
+ protected presetChanged(): void;
18
+ get preset(): Preset<K> | undefined;
19
+ set preset(value: Preset<K> | undefined);
20
+ get motion(): Motion;
21
+ set motion(value: Motion | undefined);
22
+ /** The shape this loader draws. Styles and painters read the attribute. */
23
+ get mark(): Mark;
24
+ set mark(value: Mark | undefined);
25
+ /** Forgets values cached between environment changes. */
26
+ protected forget(): void;
27
+ /** The owning region (or other owner), computed once per environment change. */
28
+ protected cachedOwner<T>(compute: () => T): T;
29
+ get stylePolicy(): StylePolicy;
30
+ set stylePolicy(value: StylePolicy);
31
+ /**
32
+ * Attaches the shadow root with this element's styles.
33
+ *
34
+ * An external stylesheet loads asynchronously, and until it applies the
35
+ * shadow tree would render unstyled and shift the layout that the static
36
+ * (server) markup reserved. So until it loads, the element keeps its static
37
+ * presentation instead: it matches `:state(unstyled)`, which the static
38
+ * rules treat like `:not(:defined)`; with `projectFallback` it renders its
39
+ * light-DOM fallback through a leading slot and hides what it adds to its
40
+ * shadow root; motion waits; and `whenStyled` work is deferred. A stylesheet
41
+ * that fails to load leaves the static presentation in place.
42
+ *
43
+ * A loaded stylesheet applies before its `load` event, possibly a frame
44
+ * earlier, and its `display` rules then override `hidden`. So the hold also
45
+ * ends in the first animation frame that finds the stylesheet loaded.
46
+ */
47
+ protected attachStyledShadow(css: readonly string[], projectFallback: boolean): ShadowRoot;
48
+ /** Runs `work` once the shadow stylesheet applies (now, if it has). */
49
+ protected whenStyled(work: () => void): void;
50
+ connectedCallback(): void;
51
+ protected abstract upgrade(): void;
52
+ disconnectedCallback(): void;
53
+ adoptedCallback(): void;
54
+ attributeChangedCallback(name: string, _old: string | null, value: string | null): void;
55
+ protected environmentChanged(): void;
56
+ protected clearDeadline(): void;
57
+ protected arm(activity: Activity): void;
58
+ /**
59
+ * Whether this element may animate at all: styled, admitted, and not
60
+ * stilled by reduced motion or a pause. A handoff needs only this, so the
61
+ * reveal animates however long the load took.
62
+ */
63
+ protected canAnimate(): boolean;
64
+ /**
65
+ * Whether idle motion may run: it also stops when the loading period's
66
+ * 4-second window is used up, so a long load never loops indefinitely.
67
+ */
68
+ protected allowed(activity: Activity): boolean;
69
+ /** The property names update() applies; subclasses add their own. */
70
+ protected get optionNames(): readonly string[];
71
+ /**
72
+ * Applies the keys of `patch` that are options of this element. Any other
73
+ * key is reported as `unknown-option` instead of being stored as an expando.
74
+ */
75
+ protected applyOptions(patch: object): void;
76
+ /** Reports a value outside an option's allowed set, by its property name. */
77
+ protected invalidOption(attribute: string): void;
78
+ protected report(code: string, field: string): void;
79
+ /** `field` now holds a valid value: its configuration errors may report again. */
80
+ protected accept(field: string): void;
81
+ /**
82
+ * A new loading cycle in `owner`: failures while loading may report again,
83
+ * from the owner and from every loader inside it.
84
+ */
85
+ protected static newCycle(owner: Element): void;
86
+ protected requestRender(): void;
87
+ protected abstract render(): void;
88
+ }
89
+ export declare function refreshStyles(scope: ParentNode): void;
@@ -0,0 +1,384 @@
1
+ import { marks } from '../types.js';
2
+ import { BaseElement, emit, installStyles, motionRestricted, node, styleRefresh, } from './dom.js';
3
+ import { subscribe } from './services.js';
4
+ import { now, remaining } from './time.js';
5
+ export { now, remaining } from './time.js';
6
+ /**
7
+ * Failures while loading, rather than configuration mistakes. They report
8
+ * once per loading cycle (AUDIT.md decision 10); configuration errors report
9
+ * again only after the field held a valid value in between.
10
+ */
11
+ const runtimeCodes = new Set([
12
+ 'animation-failed',
13
+ 'invalid-geometry',
14
+ 'missing-placeholder',
15
+ 'missing-content',
16
+ 'focus-fallback-unavailable',
17
+ ]);
18
+ const codeOf = (key) => key.slice(0, key.indexOf(':'));
19
+ const fieldOf = (key) => key.slice(key.indexOf(':') + 1);
20
+ /** `pointerResponse` → `pointer-response`. */
21
+ const attributeOf = (field) => field.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
22
+ export class ManagedElement extends BaseElement {
23
+ static observedAttributes = ['motion', 'mark'];
24
+ policy = { mode: 'inline' };
25
+ ownActivity = { started: undefined };
26
+ resources;
27
+ deadline;
28
+ #queued = false;
29
+ /** Problems reported and not yet cleared, as `code:field`. */
30
+ #issues = new Set();
31
+ /** The attribute value each configuration field had when it was reported. */
32
+ #invalid = new Map();
33
+ #hold;
34
+ #internals;
35
+ // Cached between environment changes so animation frames don't walk ancestors.
36
+ #restricted;
37
+ #owner;
38
+ #preset;
39
+ presetChanged() { }
40
+ get preset() {
41
+ return this.#preset;
42
+ }
43
+ set preset(value) {
44
+ try {
45
+ this.#preset =
46
+ value === undefined ? undefined : this.capturePreset(value);
47
+ this.accept('preset');
48
+ }
49
+ catch {
50
+ this.#preset = undefined;
51
+ this.report('invalid-preset', 'preset');
52
+ }
53
+ this.presetChanged();
54
+ this.#motionChanged();
55
+ }
56
+ get motion() {
57
+ const value = this.getAttribute('motion') ?? this.#preset?.motion ?? 'system';
58
+ return value === 'system' ? 'system' : 'paused';
59
+ }
60
+ set motion(value) {
61
+ if (value === undefined)
62
+ this.removeAttribute('motion');
63
+ else
64
+ this.setAttribute('motion', String(value));
65
+ }
66
+ /** The shape this loader draws. Styles and painters read the attribute. */
67
+ get mark() {
68
+ const value = this.getAttribute('mark');
69
+ return marks.includes(value) ? value : 'dot';
70
+ }
71
+ set mark(value) {
72
+ if (value === undefined)
73
+ this.removeAttribute('mark');
74
+ else
75
+ this.setAttribute('mark', String(value));
76
+ }
77
+ /** Forgets values cached between environment changes. */
78
+ forget() {
79
+ this.#restricted = undefined;
80
+ this.#owner = undefined;
81
+ }
82
+ /** The owning region (or other owner), computed once per environment change. */
83
+ cachedOwner(compute) {
84
+ this.#owner ??= { value: compute() };
85
+ return this.#owner.value;
86
+ }
87
+ #motionChanged() {
88
+ this.forget();
89
+ this.toggleAttribute('data-dotquilt-paused', !this.hasAttribute('motion') && this.#preset?.motion === 'paused');
90
+ this.requestRender();
91
+ }
92
+ get stylePolicy() {
93
+ return this.policy;
94
+ }
95
+ set stylePolicy(value) {
96
+ if (this.shadowRoot) {
97
+ try {
98
+ if (JSON.stringify(value) !== JSON.stringify(this.policy))
99
+ this.report('late-style-policy', 'stylePolicy');
100
+ }
101
+ catch {
102
+ this.report('invalid-style-policy', 'stylePolicy');
103
+ }
104
+ return;
105
+ }
106
+ if (!value ||
107
+ !['inline', 'external'].includes(value.mode) ||
108
+ (value.mode === 'external' &&
109
+ (typeof value.href !== 'string' ||
110
+ value.href.length > 2048 ||
111
+ !/^(https?:\/\/|\/[^/]|\.\.?\/)/.test(value.href))) ||
112
+ (value.mode === 'inline' &&
113
+ value.nonce !== undefined &&
114
+ (typeof value.nonce !== 'string' || value.nonce.length > 256))) {
115
+ this.report('invalid-style-policy', 'stylePolicy');
116
+ return;
117
+ }
118
+ this.policy = Object.freeze({ ...value });
119
+ this.accept('stylePolicy');
120
+ }
121
+ /**
122
+ * Attaches the shadow root with this element's styles.
123
+ *
124
+ * An external stylesheet loads asynchronously, and until it applies the
125
+ * shadow tree would render unstyled and shift the layout that the static
126
+ * (server) markup reserved. So until it loads, the element keeps its static
127
+ * presentation instead: it matches `:state(unstyled)`, which the static
128
+ * rules treat like `:not(:defined)`; with `projectFallback` it renders its
129
+ * light-DOM fallback through a leading slot and hides what it adds to its
130
+ * shadow root; motion waits; and `whenStyled` work is deferred. A stylesheet
131
+ * that fails to load leaves the static presentation in place.
132
+ *
133
+ * A loaded stylesheet applies before its `load` event, possibly a frame
134
+ * earlier, and its `display` rules then override `hidden`. So the hold also
135
+ * ends in the first animation frame that finds the stylesheet loaded.
136
+ */
137
+ attachStyledShadow(css, projectFallback) {
138
+ const root = this.attachShadow({ mode: 'open' });
139
+ installStyles(root, css, this.policy);
140
+ const link = root.lastElementChild;
141
+ if (this.policy.mode !== 'external' || !link || Reflect.get(link, 'sheet'))
142
+ return root;
143
+ try {
144
+ this.#internals ??= this.attachInternals();
145
+ this.#internals.states.add('unstyled');
146
+ }
147
+ catch {
148
+ // Without custom states the static rules cannot follow the element.
149
+ return root;
150
+ }
151
+ const hold = {
152
+ link,
153
+ slot: undefined,
154
+ observer: undefined,
155
+ hidden: new Set(),
156
+ deferred: [],
157
+ frame: undefined,
158
+ loaded: (event) => {
159
+ if (event.target === link)
160
+ this.#release();
161
+ },
162
+ };
163
+ if (projectFallback) {
164
+ const slot = node(this.ownerDocument, 'slot');
165
+ root.append(slot);
166
+ const hide = () => {
167
+ for (const child of root.children)
168
+ if (child !== link &&
169
+ child !== slot &&
170
+ !child.hasAttribute('hidden')) {
171
+ child.setAttribute('hidden', '');
172
+ hold.hidden.add(child);
173
+ }
174
+ };
175
+ hold.slot = slot;
176
+ hold.observer = new MutationObserver(hide);
177
+ hold.observer.observe(root, { childList: true });
178
+ }
179
+ // Capturing on the root runs before the elements' own load listeners,
180
+ // which measure the styled tree.
181
+ root.addEventListener('load', hold.loaded, true);
182
+ this.#hold = hold;
183
+ this.#watch();
184
+ return root;
185
+ }
186
+ /** Runs `work` once the shadow stylesheet applies (now, if it has). */
187
+ whenStyled(work) {
188
+ if (this.#hold)
189
+ this.#hold.deferred.push(work);
190
+ else
191
+ work();
192
+ }
193
+ /** Checks each frame, while connected, whether the stylesheet has loaded. */
194
+ #watch() {
195
+ const hold = this.#hold;
196
+ const win = this.ownerDocument.defaultView;
197
+ if (!hold || !win || hold.frame !== undefined)
198
+ return;
199
+ hold.frame = win.requestAnimationFrame(() => {
200
+ hold.frame = undefined;
201
+ if (Reflect.get(hold.link, 'sheet'))
202
+ this.#release();
203
+ else if (this.isConnected)
204
+ this.#watch();
205
+ });
206
+ }
207
+ #release() {
208
+ const hold = this.#hold;
209
+ if (!hold)
210
+ return;
211
+ this.#hold = undefined;
212
+ this.shadowRoot?.removeEventListener('load', hold.loaded, true);
213
+ if (hold.frame !== undefined)
214
+ this.ownerDocument.defaultView?.cancelAnimationFrame(hold.frame);
215
+ hold.observer?.disconnect();
216
+ hold.slot?.remove();
217
+ for (const child of hold.hidden)
218
+ child.removeAttribute('hidden');
219
+ this.#internals?.states.delete('unstyled');
220
+ for (const work of hold.deferred)
221
+ work();
222
+ this.forget();
223
+ this.requestRender();
224
+ }
225
+ connectedCallback() {
226
+ // A stylesheet can finish loading while the element is disconnected.
227
+ if (this.#hold && Reflect.get(this.#hold.link, 'sheet'))
228
+ this.#release();
229
+ else
230
+ this.#watch();
231
+ const href = this.getAttribute('data-dotquilt-style-href');
232
+ if (!this.shadowRoot && (href !== null || this.nonce))
233
+ this.stylePolicy =
234
+ href !== null
235
+ ? { mode: 'external', href }
236
+ : { mode: 'inline', nonce: this.nonce };
237
+ this.forget();
238
+ this.upgrade();
239
+ this.resources = subscribe({
240
+ host: this,
241
+ changed: () => {
242
+ this.forget();
243
+ this.environmentChanged();
244
+ },
245
+ });
246
+ this.#motionChanged();
247
+ }
248
+ disconnectedCallback() {
249
+ this.forget();
250
+ this.resources?.release();
251
+ this.resources = undefined;
252
+ this.clearDeadline();
253
+ }
254
+ adoptedCallback() {
255
+ if (this.resources) {
256
+ this.disconnectedCallback();
257
+ this.connectedCallback();
258
+ }
259
+ }
260
+ attributeChangedCallback(name, _old, value) {
261
+ this.forget();
262
+ if (name === 'motion' &&
263
+ value !== null &&
264
+ !['system', 'paused'].includes(value))
265
+ this.invalidOption('motion');
266
+ if (name === 'mark' && value !== null && !marks.includes(value))
267
+ this.invalidOption('mark');
268
+ // A value no check reported is valid (or the default): the field's
269
+ // configuration errors may report again.
270
+ const field = name.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
271
+ if (this.#invalid.get(field) !== value)
272
+ this.accept(field);
273
+ if (name === 'motion')
274
+ this.#motionChanged();
275
+ else
276
+ this.requestRender();
277
+ }
278
+ environmentChanged() {
279
+ this.requestRender();
280
+ }
281
+ clearDeadline() {
282
+ clearTimeout(this.deadline);
283
+ this.deadline = undefined;
284
+ }
285
+ arm(activity) {
286
+ this.clearDeadline();
287
+ if (activity.started === undefined)
288
+ activity.started = now(this);
289
+ this.deadline = setTimeout(() => {
290
+ this.deadline = undefined;
291
+ this.requestRender();
292
+ }, remaining(this, activity));
293
+ }
294
+ /**
295
+ * Whether this element may animate at all: styled, admitted, and not
296
+ * stilled by reduced motion or a pause. A handoff needs only this, so the
297
+ * reveal animates however long the load took.
298
+ */
299
+ canAnimate() {
300
+ // No motion on an unstyled shadow tree.
301
+ if (this.#hold)
302
+ return false;
303
+ if (!this.resources?.eligible())
304
+ return false;
305
+ this.#restricted ??= motionRestricted(this);
306
+ return !this.#restricted;
307
+ }
308
+ /**
309
+ * Whether idle motion may run: it also stops when the loading period's
310
+ * 4-second window is used up, so a long load never loops indefinitely.
311
+ */
312
+ allowed(activity) {
313
+ return this.canAnimate() && remaining(this, activity) > 0;
314
+ }
315
+ /** The property names update() applies; subclasses add their own. */
316
+ get optionNames() {
317
+ return ['motion', 'mark', 'stylePolicy', 'preset'];
318
+ }
319
+ /**
320
+ * Applies the keys of `patch` that are options of this element. Any other
321
+ * key is reported as `unknown-option` instead of being stored as an expando.
322
+ */
323
+ applyOptions(patch) {
324
+ const names = this.optionNames;
325
+ for (const [key, value] of Object.entries(patch))
326
+ if (names.includes(key))
327
+ Reflect.set(this, key, value);
328
+ else
329
+ this.report('unknown-option', key);
330
+ }
331
+ /** Reports a value outside an option's allowed set, by its property name. */
332
+ invalidOption(attribute) {
333
+ this.report('invalid-option', attribute.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()));
334
+ }
335
+ report(code, field) {
336
+ const key = `${code}:${field}`;
337
+ if (!runtimeCodes.has(code))
338
+ this.#invalid.set(field, this.getAttribute(attributeOf(field)));
339
+ if (this.#issues.has(key))
340
+ return;
341
+ this.#issues.add(key);
342
+ emit(this, 'dotquilt-error', { code, field });
343
+ }
344
+ /** `field` now holds a valid value: its configuration errors may report again. */
345
+ accept(field) {
346
+ this.#invalid.delete(field);
347
+ for (const key of this.#issues)
348
+ if (fieldOf(key) === field && !runtimeCodes.has(codeOf(key)))
349
+ this.#issues.delete(key);
350
+ }
351
+ /**
352
+ * A new loading cycle in `owner`: failures while loading may report again,
353
+ * from the owner and from every loader inside it.
354
+ */
355
+ static newCycle(owner) {
356
+ for (const element of [owner, ...owner.querySelectorAll('*')])
357
+ if (#issues in element)
358
+ for (const key of element.#issues)
359
+ if (runtimeCodes.has(codeOf(key)))
360
+ element.#issues.delete(key);
361
+ }
362
+ requestRender() {
363
+ if (this.#queued)
364
+ return;
365
+ this.#queued = true;
366
+ queueMicrotask(() => {
367
+ this.#queued = false;
368
+ if (this.resources)
369
+ this.render();
370
+ });
371
+ }
372
+ }
373
+ export function refreshStyles(scope) {
374
+ const hosts = [
375
+ ...scope.querySelectorAll('[data-dotquilt-surface],dotquilt-region,dotquilt-text-loader'),
376
+ ];
377
+ if ('matches' in scope &&
378
+ scope.matches('[data-dotquilt-surface],dotquilt-region,dotquilt-text-loader'))
379
+ hosts.push(scope);
380
+ for (const host of hosts) {
381
+ host[styleRefresh]?.();
382
+ host.refresh?.();
383
+ }
384
+ }
@@ -0,0 +1,8 @@
1
+ import { ManagedElement } from './element.js';
2
+ import type { PresetKind } from './preset.js';
3
+ /** Event ownership is needed by controllers and dense surfaces, not decoration. */
4
+ export declare abstract class EventElement<K extends PresetKind> extends ManagedElement<K> {
5
+ #private;
6
+ protected listen(target: EventTarget | null | undefined, type: string, listener: EventListener, capture?: boolean): void;
7
+ disconnectedCallback(): void;
8
+ }
@@ -0,0 +1,20 @@
1
+ import { ManagedElement } from './element.js';
2
+ /** Event ownership is needed by controllers and dense surfaces, not decoration. */
3
+ export class EventElement extends ManagedElement {
4
+ #listeners;
5
+ listen(target, type, listener, capture = false) {
6
+ if (!target)
7
+ return;
8
+ this.#listeners ??= new AbortController();
9
+ target.addEventListener(type, listener, {
10
+ capture,
11
+ signal: this.#listeners.signal,
12
+ });
13
+ }
14
+ disconnectedCallback() {
15
+ // Private listener cleanup has no error result to report.
16
+ this.#listeners?.abort(null);
17
+ this.#listeners = undefined;
18
+ super.disconnectedCallback();
19
+ }
20
+ }
@@ -0,0 +1 @@
1
+ export declare function frames(host: HTMLElement, callback: () => void): () => void;
@@ -0,0 +1,51 @@
1
+ const key = Symbol.for('dotquilt.frames.v1');
2
+ export function frames(host, callback) {
3
+ const doc = host.ownerDocument, win = doc.defaultView;
4
+ if (!win)
5
+ return () => { };
6
+ let service = Reflect.get(doc, key);
7
+ if (!service) {
8
+ const clients = new Set();
9
+ let raf = 0;
10
+ let snapshot;
11
+ const tick = () => {
12
+ raf = 0;
13
+ // Membership changes invalidate the next tick, not the snapshot in progress.
14
+ snapshot ??= [...clients];
15
+ for (const fn of snapshot) {
16
+ if (!clients.has(fn))
17
+ continue;
18
+ try {
19
+ fn();
20
+ }
21
+ catch (error) {
22
+ // One failing client must not stop every other loader's motion.
23
+ clients.delete(fn);
24
+ snapshot = undefined;
25
+ win.reportError?.(error);
26
+ }
27
+ }
28
+ if (clients.size && !raf)
29
+ raf = win.requestAnimationFrame(tick);
30
+ };
31
+ service = {
32
+ add(fn) {
33
+ clients.add(fn);
34
+ snapshot = undefined;
35
+ if (!raf)
36
+ raf = win.requestAnimationFrame(tick);
37
+ return () => {
38
+ clients.delete(fn);
39
+ snapshot = undefined;
40
+ if (!clients.size) {
41
+ win.cancelAnimationFrame(raf);
42
+ raf = 0;
43
+ Reflect.deleteProperty(doc, key);
44
+ }
45
+ };
46
+ },
47
+ };
48
+ Reflect.set(doc, key, service);
49
+ }
50
+ return service.add(callback);
51
+ }