stimeo-ui 0.8.0 → 0.9.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/CHANGELOG.md +89 -0
- package/dist/controllers/bulk_select_controller.d.ts +62 -15
- package/dist/controllers/bulk_select_controller.js +139 -28
- package/dist/controllers/bulk_select_controller.js.map +1 -1
- package/dist/controllers/clipboard_controller.d.ts +48 -15
- package/dist/controllers/clipboard_controller.js +102 -20
- package/dist/controllers/clipboard_controller.js.map +1 -1
- package/dist/controllers/color_picker_controller.d.ts +37 -6
- package/dist/controllers/color_picker_controller.js +180 -43
- package/dist/controllers/color_picker_controller.js.map +1 -1
- package/dist/controllers/data_grid_controller.d.ts +25 -9
- package/dist/controllers/data_grid_controller.js +150 -24
- package/dist/controllers/data_grid_controller.js.map +1 -1
- package/dist/controllers/editable_controller.d.ts +34 -9
- package/dist/controllers/editable_controller.js +83 -30
- package/dist/controllers/editable_controller.js.map +1 -1
- package/dist/controllers/filter_controller.d.ts +15 -3
- package/dist/controllers/filter_controller.js +32 -1
- package/dist/controllers/filter_controller.js.map +1 -1
- package/dist/controllers/masonry_controller.d.ts +34 -5
- package/dist/controllers/masonry_controller.js +129 -17
- package/dist/controllers/masonry_controller.js.map +1 -1
- package/dist/controllers/otp_controller.d.ts +4 -1
- package/dist/controllers/otp_controller.js +29 -16
- package/dist/controllers/otp_controller.js.map +1 -1
- package/dist/controllers/reset_before_cache_controller.d.ts +25 -2
- package/dist/controllers/reset_before_cache_controller.js +51 -5
- package/dist/controllers/reset_before_cache_controller.js.map +1 -1
- package/dist/controllers/resizable_controller.d.ts +23 -7
- package/dist/controllers/resizable_controller.js +128 -55
- package/dist/controllers/resizable_controller.js.map +1 -1
- package/dist/index.js +860 -339
- package/dist/index.js.map +1 -1
- package/dist/inspector/cli.js +2 -0
- package/dist/inspector/cli.js.map +1 -1
- package/dist/inspector/cli_bin.js +2 -0
- package/dist/inspector/cli_bin.js.map +1 -1
- package/dist/inspector/examples.json +22 -22
- package/dist/inspector/manifest.json +12 -8
- package/package.json +1 -1
|
@@ -2,6 +2,46 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
2
2
|
|
|
3
3
|
// src/controllers/clipboard_controller.ts
|
|
4
4
|
|
|
5
|
+
// src/utils/announce.ts
|
|
6
|
+
function announce(message, options = {}) {
|
|
7
|
+
const text = message.trim();
|
|
8
|
+
if (text.length === 0) return;
|
|
9
|
+
window.dispatchEvent(
|
|
10
|
+
new CustomEvent("stimeo--announcer:announce", {
|
|
11
|
+
detail: { message: text, assertive: options.assertive === true }
|
|
12
|
+
})
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/utils/before_cache_reset.ts
|
|
17
|
+
var BeforeCacheReset = class _BeforeCacheReset {
|
|
18
|
+
/** Every subscribed instance, iterated by the one shared document listener. */
|
|
19
|
+
static #subscribers = /* @__PURE__ */ new Set();
|
|
20
|
+
/** The shared listener; installed while at least one instance is subscribed. */
|
|
21
|
+
static #onBeforeCache = () => {
|
|
22
|
+
for (const subscriber of _BeforeCacheReset.#subscribers) subscriber.#rewind();
|
|
23
|
+
};
|
|
24
|
+
#rewind;
|
|
25
|
+
/** @param rewind - the pass that returns this controller's state to its initial form. */
|
|
26
|
+
constructor(rewind) {
|
|
27
|
+
this.#rewind = rewind;
|
|
28
|
+
}
|
|
29
|
+
/** Subscribes to `turbo:before-cache`; call from `connect()`. Idempotent. */
|
|
30
|
+
activate() {
|
|
31
|
+
const first = _BeforeCacheReset.#subscribers.size === 0;
|
|
32
|
+
_BeforeCacheReset.#subscribers.add(this);
|
|
33
|
+
if (first) {
|
|
34
|
+
document.addEventListener("turbo:before-cache", _BeforeCacheReset.#onBeforeCache);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Unsubscribes; call from `disconnect()`. Safe when never subscribed. */
|
|
38
|
+
deactivate() {
|
|
39
|
+
_BeforeCacheReset.#subscribers.delete(this);
|
|
40
|
+
if (_BeforeCacheReset.#subscribers.size > 0) return;
|
|
41
|
+
document.removeEventListener("turbo:before-cache", _BeforeCacheReset.#onBeforeCache);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
5
45
|
// src/utils/default_attribute.ts
|
|
6
46
|
function setDefaultAttribute(element, name, value) {
|
|
7
47
|
if (element.hasAttribute(name)) return false;
|
|
@@ -63,45 +103,59 @@ var SafeTimeout = class extends TimerRegistry {
|
|
|
63
103
|
};
|
|
64
104
|
|
|
65
105
|
// src/controllers/clipboard_controller.ts
|
|
106
|
+
var TRANSIENT_STATES = /* @__PURE__ */ new Set(["copied", "error"]);
|
|
66
107
|
var ClipboardController = class extends Controller {
|
|
67
108
|
static targets = ["source", "button", "feedback"];
|
|
68
109
|
static values = {
|
|
69
110
|
text: { type: String, default: "" },
|
|
70
111
|
feedbackDuration: { type: Number, default: 2e3 },
|
|
71
112
|
copiedLabel: { type: String, default: "Copied" },
|
|
72
|
-
errorLabel: { type: String, default: "Copy failed" }
|
|
113
|
+
errorLabel: { type: String, default: "Copy failed" },
|
|
114
|
+
announceCopiedText: { type: String, default: "" },
|
|
115
|
+
announceErrorText: { type: String, default: "" }
|
|
73
116
|
};
|
|
74
117
|
static actions = ["copy"];
|
|
75
118
|
static events = ["copy"];
|
|
76
|
-
/**
|
|
119
|
+
/**
|
|
120
|
+
* The pending return to idle — the only timer this controller schedules, so
|
|
121
|
+
* `clearAll()` is exactly "drop the auto-clear" and needs no id of its own.
|
|
122
|
+
*/
|
|
77
123
|
#timers = new SafeTimeout();
|
|
124
|
+
/** Returns the completion state to idle for the snapshot Turbo takes. */
|
|
125
|
+
#beforeCache = new BeforeCacheReset(() => this.#rewind());
|
|
78
126
|
/**
|
|
79
|
-
*
|
|
80
|
-
* a
|
|
81
|
-
*
|
|
127
|
+
* Whether this connection is still live. `copy()` suspends on the Clipboard API,
|
|
128
|
+
* and a teardown that lands while it is suspended must win: the continuation
|
|
129
|
+
* would otherwise write to an element nobody owns and arm a timer past the
|
|
130
|
+
* `clearAll()` that was supposed to be the last word.
|
|
82
131
|
*/
|
|
83
|
-
#
|
|
132
|
+
#connected = false;
|
|
84
133
|
connect() {
|
|
85
|
-
|
|
134
|
+
this.#connected = true;
|
|
135
|
+
this.#adopt();
|
|
136
|
+
this.#beforeCache.activate();
|
|
86
137
|
}
|
|
87
138
|
disconnect() {
|
|
139
|
+
this.#connected = false;
|
|
140
|
+
this.#beforeCache.deactivate();
|
|
88
141
|
this.#timers.clearAll();
|
|
89
142
|
}
|
|
90
143
|
/**
|
|
91
144
|
* Copies the resolved text and reports the outcome. Bound via `data-action`
|
|
92
|
-
* (click).
|
|
93
|
-
* — including on failure — so consumers can react either way.
|
|
145
|
+
* (click). Dispatches `stimeo--clipboard:copy` with `{ success, text }` once per
|
|
146
|
+
* completed attempt — including on failure — so consumers can react either way.
|
|
147
|
+
* An attempt whose connection ended while it was in flight reports nothing.
|
|
94
148
|
*/
|
|
95
149
|
async copy() {
|
|
96
150
|
const text = this.#resolveText();
|
|
97
151
|
let success = false;
|
|
98
152
|
try {
|
|
99
|
-
if (!navigator.clipboard?.writeText) throw new Error("Clipboard API unavailable");
|
|
100
153
|
await navigator.clipboard.writeText(text);
|
|
101
154
|
success = true;
|
|
102
155
|
} catch {
|
|
103
156
|
success = false;
|
|
104
157
|
}
|
|
158
|
+
if (!this.#connected) return;
|
|
105
159
|
this.#reportResult(success);
|
|
106
160
|
this.dispatch("copy", { detail: { success, text } });
|
|
107
161
|
}
|
|
@@ -118,24 +172,52 @@ var ClipboardController = class extends Controller {
|
|
|
118
172
|
}
|
|
119
173
|
return source.textContent ?? "";
|
|
120
174
|
}
|
|
121
|
-
/**
|
|
175
|
+
/**
|
|
176
|
+
* Reads the current state back from the DOM.
|
|
177
|
+
*
|
|
178
|
+
* A `copied` or `error` found at connect time is this controller's own output
|
|
179
|
+
* from a connection that is gone, and so is the timer that would have cleared it
|
|
180
|
+
* — nothing else would ever return the element to `idle`. Any other authored
|
|
181
|
+
* value belongs to the consumer and only a missing attribute takes the default.
|
|
182
|
+
*/
|
|
183
|
+
#adopt() {
|
|
184
|
+
if (this.#inTransientState()) {
|
|
185
|
+
this.#reset();
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
setDefaultAttribute(this.element, "data-state", "idle");
|
|
189
|
+
}
|
|
190
|
+
/** Whether `data-state` currently holds one of the values this controller writes. */
|
|
191
|
+
#inTransientState() {
|
|
192
|
+
const state = this.element.getAttribute("data-state");
|
|
193
|
+
return state !== null && TRANSIENT_STATES.has(state);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Returns the completion state to idle for the snapshot Turbo is about to take,
|
|
197
|
+
* so a page reached with the Back button does not report a copy that happened
|
|
198
|
+
* before the navigation. Only a state this controller wrote is rewound — an
|
|
199
|
+
* authored one is the consumer's and has to survive into the snapshot, exactly as
|
|
200
|
+
* `connect()` leaves it alone. State only — no `copy` is dispatched, which would
|
|
201
|
+
* claim a fresh copy ran.
|
|
202
|
+
*/
|
|
203
|
+
#rewind() {
|
|
204
|
+
if (!this.#inTransientState()) return;
|
|
205
|
+
this.#timers.clearAll();
|
|
206
|
+
this.#reset();
|
|
207
|
+
}
|
|
208
|
+
/** Reflects the result, announces it, and schedules the return to idle. */
|
|
122
209
|
#reportResult(success) {
|
|
123
210
|
this.element.setAttribute("data-state", success ? "copied" : "error");
|
|
124
211
|
if (this.hasFeedbackTarget) {
|
|
125
212
|
this.feedbackTarget.textContent = success ? this.copiedLabelValue : this.errorLabelValue;
|
|
126
213
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
this.#resetTimerId = null;
|
|
130
|
-
}
|
|
214
|
+
announce(success ? this.announceCopiedTextValue : this.announceErrorTextValue);
|
|
215
|
+
this.#timers.clearAll();
|
|
131
216
|
if (this.feedbackDurationValue > 0) {
|
|
132
|
-
this.#
|
|
133
|
-
this.#resetTimerId = null;
|
|
134
|
-
this.#reset();
|
|
135
|
-
}, this.feedbackDurationValue);
|
|
217
|
+
this.#timers.set(() => this.#reset(), this.feedbackDurationValue);
|
|
136
218
|
}
|
|
137
219
|
}
|
|
138
|
-
/** Returns to the idle state and
|
|
220
|
+
/** Returns to the idle state and empties the completion slot. */
|
|
139
221
|
#reset() {
|
|
140
222
|
this.element.setAttribute("data-state", "idle");
|
|
141
223
|
if (this.hasFeedbackTarget) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/default_attribute.ts","../../src/utils/safe_timeout.ts","../../src/controllers/clipboard_controller.ts"],"names":[],"mappings":";;;;;AAYO,SAAS,mBAAA,CAAoB,OAAA,EAAkB,IAAA,EAAc,KAAA,EAAwB;AAC1F,EAAA,IAAI,OAAA,CAAQ,YAAA,CAAa,IAAI,CAAA,EAAG,OAAO,KAAA;AACvC,EAAA,OAAA,CAAQ,YAAA,CAAa,MAAM,KAAK,CAAA;AAChC,EAAA,OAAO,IAAA;AACT;;;ACQA,IAAe,gBAAf,MAA6B;AAAA;AAAA,EAER,GAAA,uBAAU,GAAA,EAAY;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAczC,MAAM,EAAA,EAAkB;AACtB,IAAA,IAAI,IAAA,CAAK,GAAA,CAAI,MAAA,CAAO,EAAE,CAAA,EAAG;AACvB,MAAA,IAAA,CAAK,OAAO,EAAE,CAAA;AAAA,IAChB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,QAAA,GAAiB;AACf,IAAA,KAAA,MAAW,EAAA,IAAM,KAAK,GAAA,EAAK;AACzB,MAAA,IAAA,CAAK,OAAO,EAAE,CAAA;AAAA,IAChB;AACA,IAAA,IAAA,CAAK,IAAI,KAAA,EAAM;AAAA,EACjB;AAAA;AAAA,EAGA,IAAI,IAAA,GAAe;AACjB,IAAA,OAAO,KAAK,GAAA,CAAI,IAAA;AAAA,EAClB;AACF,CAAA;AAmBO,IAAM,WAAA,GAAN,cAA0B,aAAA,CAAc;AAAA;AAAA;AAAA;AAAA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YAAA,EAAc,OAAA,GAAU,WAAW,OAAO,CAAA;AACpE,IAAA,IAAI,KAAK,iBAAA,EAAmB;AAC1B,MAAA,IAAA,CAAK,cAAA,CAAe,WAAA,GAAc,OAAA,GAAU,IAAA,CAAK,mBAAmB,IAAA,CAAK,eAAA;AAAA,IAC3E;AAIA,IAAA,IAAI,IAAA,CAAK,kBAAkB,IAAA,EAAM;AAC/B,MAAA,IAAA,CAAK,OAAA,CAAQ,KAAA,CAAM,IAAA,CAAK,aAAa,CAAA;AACrC,MAAA,IAAA,CAAK,aAAA,GAAgB,IAAA;AAAA,IACvB;AACA,IAAA,IAAI,IAAA,CAAK,wBAAwB,CAAA,EAAG;AAClC,MAAA,IAAA,CAAK,aAAA,GAAgB,IAAA,CAAK,OAAA,CAAQ,GAAA,CAAI,MAAM;AAC1C,QAAA,IAAA,CAAK,aAAA,GAAgB,IAAA;AACrB,QAAA,IAAA,CAAK,MAAA,EAAO;AAAA,MACd,CAAA,EAAG,KAAK,qBAAqB,CAAA;AAAA,IAC/B;AAAA,EACF;AAAA;AAAA,EAGA,MAAA,GAAe;AACb,IAAA,IAAA,CAAK,OAAA,CAAQ,YAAA,CAAa,YAAA,EAAc,MAAM,CAAA;AAC9C,IAAA,IAAI,KAAK,iBAAA,EAAmB;AAC1B,MAAA,IAAA,CAAK,eAAe,WAAA,GAAc,EAAA;AAAA,IACpC;AAAA,EACF;AACF","file":"clipboard_controller.js","sourcesContent":["/**\n * Adds an attribute default without displacing an authored value.\n *\n * Attribute presence — including an authored empty string — is the ownership\n * boundary. The return value lets a caller remember that it supplied the\n * default when that caller must later restore the authored state.\n *\n * @param element - Element that owns the attribute\n * @param name - Attribute name\n * @param value - Value to write only when the attribute is absent\n * @returns Whether this call added the attribute\n */\nexport function setDefaultAttribute(element: Element, name: string, value: string): boolean {\n if (element.hasAttribute(name)) return false;\n element.setAttribute(name, value);\n return true;\n}\n","/**\n * Self-cleaning timer registries shared by Stimeo controllers.\n *\n * Stimulus controllers frequently schedule `setTimeout` / `setInterval` work\n * (auto-dismiss, debouncing, polling). When the element leaves the DOM — a\n * Turbo Drive navigation, a Turbo Stream replacement, or any `disconnect()` —\n * orphaned timers keep firing against a detached controller, leaking memory and\n * mutating stale state. {@link SafeTimeout} and {@link SafeInterval} track every\n * timer they create so a single {@link TimerRegistry.clearAll | clearAll()} call\n * in `disconnect()` tears them all down.\n *\n * These are intentionally low-level primitives: they own *registration and\n * cleanup only*. Higher-level policy (pause/resume, remaining-time accounting)\n * stays in the individual controllers so per-widget semantics are not flattened\n * into a lowest-common-denominator helper.\n */\n\n/**\n * Shared registry bookkeeping for the timeout/interval variants.\n *\n * Subclasses provide the scheduling primitive ({@link schedule}) and its matching\n * canceller ({@link cancel}); this base owns the set of live ids plus the\n * per-id and bulk teardown shared by both.\n */\nabstract class TimerRegistry {\n /** Live timer ids that have not yet been cleared (or, for timeouts, fired). */\n protected readonly ids = new Set<number>();\n\n /** Schedules the underlying platform timer and returns its id. */\n protected abstract schedule(callback: () => void, delay: number): number;\n\n /** Cancels the underlying platform timer for `id`. */\n protected abstract cancel(id: number): void;\n\n /**\n * Cancels a single tracked timer.\n *\n * No-ops if the id is unknown (already cleared, fired, or never owned by this\n * registry), so callers can clear defensively without guarding.\n */\n clear(id: number): void {\n if (this.ids.delete(id)) {\n this.cancel(id);\n }\n }\n\n /**\n * Cancels every tracked timer. Call this from a controller's `disconnect()`\n * to guarantee no timer outlives the element.\n */\n clearAll(): void {\n for (const id of this.ids) {\n this.cancel(id);\n }\n this.ids.clear();\n }\n\n /** Number of timers currently tracked (pending). */\n get size(): number {\n return this.ids.size;\n }\n}\n\n/**\n * `setTimeout` wrapper that auto-forgets each timer once it fires and supports\n * bulk teardown on disconnect.\n *\n * @example\n * ```ts\n * #timers = new SafeTimeout();\n *\n * connect() {\n * this.#timers.set(() => this.dismiss(), 5000);\n * }\n *\n * disconnect() {\n * this.#timers.clearAll();\n * }\n * ```\n */\nexport class SafeTimeout extends TimerRegistry {\n /**\n * Schedules `callback` after `delay` ms and returns the timer id.\n *\n * The id is removed from the registry automatically when the timeout fires,\n * so {@link TimerRegistry.size | size} reflects only still-pending timers.\n */\n set(callback: () => void, delay: number): number {\n const id = this.schedule(() => {\n this.ids.delete(id);\n callback();\n }, delay);\n this.ids.add(id);\n return id;\n }\n\n protected schedule(callback: () => void, delay: number): number {\n return window.setTimeout(callback, delay);\n }\n\n protected cancel(id: number): void {\n window.clearTimeout(id);\n }\n}\n\n/**\n * `setInterval` wrapper that tracks every interval for bulk teardown on\n * disconnect. Unlike {@link SafeTimeout}, intervals are retained until they are\n * explicitly cleared because they fire repeatedly.\n *\n * @example\n * ```ts\n * #intervals = new SafeInterval();\n *\n * connect() {\n * this.#intervals.set(() => this.tick(), 1000);\n * }\n *\n * disconnect() {\n * this.#intervals.clearAll();\n * }\n * ```\n */\nexport class SafeInterval extends TimerRegistry {\n /** Schedules a repeating `callback` every `delay` ms and returns the timer id. */\n set(callback: () => void, delay: number): number {\n const id = this.schedule(callback, delay);\n this.ids.add(id);\n return id;\n }\n\n protected schedule(callback: () => void, delay: number): number {\n return window.setInterval(callback, delay);\n }\n\n protected cancel(id: number): void {\n window.clearInterval(id);\n }\n}\n","import { Controller } from \"@hotwired/stimulus\";\nimport { setDefaultAttribute } from \"../utils/default_attribute\";\nimport { SafeTimeout } from \"../utils/safe_timeout\";\n\n/**\n * Headless copy-to-clipboard behavior with a live-region completion notice.\n *\n * Markup contract (identifier: `stimeo--clipboard`):\n * <div data-controller=\"stimeo--clipboard\"\n * data-stimeo--clipboard-feedback-duration-value=\"2000\">\n * <input type=\"text\" value=\"https://example.com\" readonly\n * data-stimeo--clipboard-target=\"source\">\n * <button type=\"button\" data-stimeo--clipboard-target=\"button\"\n * data-action=\"stimeo--clipboard#copy\">Copy</button>\n * <span role=\"status\" aria-live=\"polite\"\n * data-stimeo--clipboard-target=\"feedback\"></span>\n * </div>\n *\n * No dedicated APG pattern; this follows the Button + live-region practice. The\n * copy uses the standard `navigator.clipboard` API (no extra dependency); when\n * it is unavailable or rejects, the failure is surfaced rather than silently\n * swallowed, and never communicated by icon alone — the `role=\"status\"` region\n * carries text so screen readers announce the outcome.\n *\n * @remarks\n * Behavior only — icon swaps and styling are the consumer's, keyed off\n * `data-state` (`idle` / `copied` / `error`). The completion notice clears\n * itself after `feedbackDuration`; that timer is torn down on disconnect (Turbo)\n * via {@link SafeTimeout}.\n */\nexport class ClipboardController extends Controller<HTMLElement> {\n static override targets = [\"source\", \"button\", \"feedback\"];\n static override values = {\n text: { type: String, default: \"\" },\n feedbackDuration: { type: Number, default: 2000 },\n copiedLabel: { type: String, default: \"Copied\" },\n errorLabel: { type: String, default: \"Copy failed\" },\n };\n static actions = [\"copy\"] as const;\n static events = [\"copy\"] as const;\n\n declare readonly sourceTarget: HTMLElement;\n declare readonly buttonTarget: HTMLElement;\n declare readonly feedbackTarget: HTMLElement;\n declare readonly hasSourceTarget: boolean;\n declare readonly hasButtonTarget: boolean;\n declare readonly hasFeedbackTarget: boolean;\n\n declare textValue: string;\n declare feedbackDurationValue: number;\n declare copiedLabelValue: string;\n declare errorLabelValue: string;\n\n /** Auto-clear timer for the completion notice; torn down on disconnect. */\n #timers = new SafeTimeout();\n\n /**\n * The pending auto-clear timer id, or `null` when none is scheduled. Tracked so\n * a rapid second copy cancels the first window instead of letting a stale timer\n * reset the freshly-shown notice early.\n */\n #resetTimerId: number | null = null;\n\n override connect(): void {\n setDefaultAttribute(this.element, \"data-state\", \"idle\");\n }\n\n override disconnect(): void {\n this.#timers.clearAll();\n }\n\n /**\n * Copies the resolved text and reports the outcome. Bound via `data-action`\n * (click). Always dispatches `stimeo--clipboard:copy` with `{ success, text }`\n * — including on failure — so consumers can react either way.\n */\n async copy(): Promise<void> {\n const text = this.#resolveText();\n let success = false;\n try {\n if (!navigator.clipboard?.writeText) throw new Error(\"Clipboard API unavailable\");\n await navigator.clipboard.writeText(text);\n success = true;\n } catch {\n success = false;\n }\n\n this.#reportResult(success);\n this.dispatch(\"copy\", { detail: { success, text } });\n }\n\n /**\n * The text to copy: the explicit `text` value when set, otherwise the source\n * target's current value (inputs/textareas) or text content.\n */\n #resolveText(): string {\n if (this.textValue.length > 0) return this.textValue;\n if (!this.hasSourceTarget) return \"\";\n const source = this.sourceTarget;\n if (source instanceof HTMLInputElement || source instanceof HTMLTextAreaElement) {\n return source.value;\n }\n return source.textContent ?? \"\";\n }\n\n /** Reflects the result on `data-state`, announces it, and schedules a reset. */\n #reportResult(success: boolean): void {\n this.element.setAttribute(\"data-state\", success ? \"copied\" : \"error\");\n if (this.hasFeedbackTarget) {\n this.feedbackTarget.textContent = success ? this.copiedLabelValue : this.errorLabelValue;\n }\n\n // Cancel any in-flight reset so consecutive copies restart the full window\n // rather than having the earlier timer clear the new notice prematurely.\n if (this.#resetTimerId !== null) {\n this.#timers.clear(this.#resetTimerId);\n this.#resetTimerId = null;\n }\n if (this.feedbackDurationValue > 0) {\n this.#resetTimerId = this.#timers.set(() => {\n this.#resetTimerId = null;\n this.#reset();\n }, this.feedbackDurationValue);\n }\n }\n\n /** Returns to the idle state and clears the completion notice. */\n #reset(): void {\n this.element.setAttribute(\"data-state\", \"idle\");\n if (this.hasFeedbackTarget) {\n this.feedbackTarget.textContent = \"\";\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/announce.ts","../../src/utils/before_cache_reset.ts","../../src/utils/default_attribute.ts","../../src/utils/safe_timeout.ts","../../src/controllers/clipboard_controller.ts"],"names":[],"mappings":";;;;;AAoBO,SAAS,QAAA,CAAS,OAAA,EAAiB,OAAA,GAAmC,EAAC,EAAS;AACrF,EAAA,MAAM,IAAA,GAAO,QAAQ,IAAA,EAAK;AAC1B,EAAA,IAAI,IAAA,CAAK,WAAW,CAAA,EAAG;AACvB,EAAA,MAAA,CAAO,aAAA;AAAA,IACL,IAAI,YAAY,4BAAA,EAA8B;AAAA,MAC5C,QAAQ,EAAE,OAAA,EAAS,MAAM,SAAA,EAAW,OAAA,CAAQ,cAA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message to the page's shared `stimeo--announcer`.\n *\n * A component that has to reach assistive tech does not carry a live region of its\n * own: a region only announces what changes *after* assistive tech already knows\n * about it, which a region that appears (or is un-hidden) with its message cannot\n * satisfy. The one region that can is the announcer sitting in the page from the\n * start, so state changes are handed to it as an event and it does the reading.\n *\n * The event goes to `window` because the announcer is usually a sibling high in the\n * document rather than an ancestor of the component dispatching it.\n *\n * Wording comes from the consumer — the library ships no English strings — so an\n * empty message is silently dropped and nothing is announced.\n *\n * @example\n * ```ts\n * announce(this.announceTextValue, { assertive: false });\n * ```\n */\nexport function announce(message: string, options: { assertive?: boolean } = {}): void {\n const text = message.trim();\n if (text.length === 0) return;\n window.dispatchEvent(\n new CustomEvent(\"stimeo--announcer:announce\", {\n detail: { message: text, assertive: options.assertive === true },\n }),\n );\n}\n\n/**\n * Fills `{name}` placeholders in an announcement template from `values`.\n *\n * The same substitution the value-text templates use, so a consumer writes\n * `\"{percent}% complete\"` in one attribute and gets the same rules everywhere. A\n * placeholder with no matching entry is left as authored rather than blanked, which\n * keeps a typo visible instead of silently swallowing the word.\n */\nexport function fillTemplate(template: string, values: Record<string, string | number>): string {\n return template.replace(/\\{([a-zA-Z][a-zA-Z0-9]*)\\}/g, (match, name: string) => {\n const replacement = values[name];\n return replacement === undefined ? match : String(replacement);\n });\n}\n","/**\n * Runs a controller's \"return to the initial state\" pass just before Turbo\n * caches the page.\n *\n * **`disconnect()` cannot do this job, for two independent reasons.** Turbo\n * queues the clone from this event rather than taking it here, and the body swap\n * that runs the controller's `disconnect()` is queued separately — so which of\n * the two lands first is not something a controller can rely on, and a rewind\n * written in `disconnect()` may reach only the DOM being thrown away. In the\n * other direction, `disconnect()` also fires on an in-page move (Stimulus tears\n * down and reconnects the same element), where rewinding would wipe a\n * legitimately in-progress interaction — a spinner mid-load would vanish. One\n * timing is unreliable, the other is too eager; `turbo:before-cache` is the only\n * point that is exactly \"the page is about to be frozen\".\n *\n * Scope is the subscription only: registering on `activate()`, unregistering on\n * `deactivate()`, and one shared document listener no matter how many instances\n * are live. *What* to return to its initial state — which `data-state`, which\n * `hidden`, which `aria-busy` — stays in the controller, because no two\n * consumers answer it the same way (the `MicrotaskCoalescer` split).\n *\n * **Rewind state, not appearance.** The pass writes attributes the controller\n * itself owns; the visual result of those attributes is the consumer's CSS, and\n * a library that reached for style or class names would be guessing at markup\n * it does not own.\n *\n * Both entry points are idempotent, so the lifecycle hooks can call them\n * unconditionally: a second `activate()` does not double-subscribe and does not\n * make the callback run twice, and `deactivate()` on an instance that never\n * subscribed is a no-op.\n *\n * This file's own doc block is dropped from `dist`, but every member comment is\n * inlined into each consumer entry (`tsup` builds with `splitting: false`), so\n * rationale belongs here and only the contract belongs on the members.\n *\n * @example\n * ```ts\n * readonly #beforeCache = new BeforeCacheReset(() => this.#rewind());\n *\n * connect() { this.#beforeCache.activate(); }\n * disconnect() { this.#beforeCache.deactivate(); }\n * ```\n */\nexport class BeforeCacheReset {\n /** Every subscribed instance, iterated by the one shared document listener. */\n static readonly #subscribers = new Set<BeforeCacheReset>();\n\n /** The shared listener; installed while at least one instance is subscribed. */\n static readonly #onBeforeCache = (): void => {\n for (const subscriber of BeforeCacheReset.#subscribers) subscriber.#rewind();\n };\n\n readonly #rewind: () => void;\n\n /** @param rewind - the pass that returns this controller's state to its initial form. */\n constructor(rewind: () => void) {\n this.#rewind = rewind;\n }\n\n /** Subscribes to `turbo:before-cache`; call from `connect()`. Idempotent. */\n activate(): void {\n const first = BeforeCacheReset.#subscribers.size === 0;\n BeforeCacheReset.#subscribers.add(this);\n if (first) {\n document.addEventListener(\"turbo:before-cache\", BeforeCacheReset.#onBeforeCache);\n }\n }\n\n /** Unsubscribes; call from `disconnect()`. Safe when never subscribed. */\n deactivate(): void {\n BeforeCacheReset.#subscribers.delete(this);\n if (BeforeCacheReset.#subscribers.size > 0) return;\n document.removeEventListener(\"turbo:before-cache\", BeforeCacheReset.#onBeforeCache);\n }\n}\n","/**\n * Adds an attribute default without displacing an authored value.\n *\n * Attribute presence — including an authored empty string — is the ownership\n * boundary. The return value lets a caller remember that it supplied the\n * default when that caller must later restore the authored state.\n *\n * @param element - Element that owns the attribute\n * @param name - Attribute name\n * @param value - Value to write only when the attribute is absent\n * @returns Whether this call added the attribute\n */\nexport function setDefaultAttribute(element: Element, name: string, value: string): boolean {\n if (element.hasAttribute(name)) return false;\n element.setAttribute(name, value);\n return true;\n}\n","/**\n * Self-cleaning timer registries shared by Stimeo controllers.\n *\n * Stimulus controllers frequently schedule `setTimeout` / `setInterval` work\n * (auto-dismiss, debouncing, polling). When the element leaves the DOM — a\n * Turbo Drive navigation, a Turbo Stream replacement, or any `disconnect()` —\n * orphaned timers keep firing against a detached controller, leaking memory and\n * mutating stale state. {@link SafeTimeout} and {@link SafeInterval} track every\n * timer they create so a single {@link TimerRegistry.clearAll | clearAll()} call\n * in `disconnect()` tears them all down.\n *\n * These are intentionally low-level primitives: they own *registration and\n * cleanup only*. Higher-level policy (pause/resume, remaining-time accounting)\n * stays in the individual controllers so per-widget semantics are not flattened\n * into a lowest-common-denominator helper.\n */\n\n/**\n * Shared registry bookkeeping for the timeout/interval variants.\n *\n * Subclasses provide the scheduling primitive ({@link schedule}) and its matching\n * canceller ({@link cancel}); this base owns the set of live ids plus the\n * per-id and bulk teardown shared by both.\n */\nabstract class TimerRegistry {\n /** Live timer ids that have not yet been cleared (or, for timeouts, fired). */\n protected readonly ids = new Set<number>();\n\n /** Schedules the underlying platform timer and returns its id. */\n protected abstract schedule(callback: () => void, delay: number): number;\n\n /** Cancels the underlying platform timer for `id`. */\n protected abstract cancel(id: number): void;\n\n /**\n * Cancels a single tracked timer.\n *\n * No-ops if the id is unknown (already cleared, fired, or never owned by this\n * registry), so callers can clear defensively without guarding.\n */\n clear(id: number): void {\n if (this.ids.delete(id)) {\n this.cancel(id);\n }\n }\n\n /**\n * Cancels every tracked timer. Call this from a controller's `disconnect()`\n * to guarantee no timer outlives the element.\n */\n clearAll(): void {\n for (const id of this.ids) {\n this.cancel(id);\n }\n this.ids.clear();\n }\n\n /** Number of timers currently tracked (pending). */\n get size(): number {\n return this.ids.size;\n }\n}\n\n/**\n * `setTimeout` wrapper that auto-forgets each timer once it fires and supports\n * bulk teardown on disconnect.\n *\n * @example\n * ```ts\n * #timers = new SafeTimeout();\n *\n * connect() {\n * this.#timers.set(() => this.dismiss(), 5000);\n * }\n *\n * disconnect() {\n * this.#timers.clearAll();\n * }\n * ```\n */\nexport class SafeTimeout extends TimerRegistry {\n /**\n * Schedules `callback` after `delay` ms and returns the timer id.\n *\n * The id is removed from the registry automatically when the timeout fires,\n * so {@link TimerRegistry.size | size} reflects only still-pending timers.\n */\n set(callback: () => void, delay: number): number {\n const id = this.schedule(() => {\n this.ids.delete(id);\n callback();\n }, delay);\n this.ids.add(id);\n return id;\n }\n\n protected schedule(callback: () => void, delay: number): number {\n return window.setTimeout(callback, delay);\n }\n\n protected cancel(id: number): void {\n window.clearTimeout(id);\n }\n}\n\n/**\n * `setInterval` wrapper that tracks every interval for bulk teardown on\n * disconnect. Unlike {@link SafeTimeout}, intervals are retained until they are\n * explicitly cleared because they fire repeatedly.\n *\n * @example\n * ```ts\n * #intervals = new SafeInterval();\n *\n * connect() {\n * this.#intervals.set(() => this.tick(), 1000);\n * }\n *\n * disconnect() {\n * this.#intervals.clearAll();\n * }\n * ```\n */\nexport class SafeInterval extends TimerRegistry {\n /** Schedules a repeating `callback` every `delay` ms and returns the timer id. */\n set(callback: () => void, delay: number): number {\n const id = this.schedule(callback, delay);\n this.ids.add(id);\n return id;\n }\n\n protected schedule(callback: () => void, delay: number): number {\n return window.setInterval(callback, delay);\n }\n\n protected cancel(id: number): void {\n window.clearInterval(id);\n }\n}\n","import { Controller } from \"@hotwired/stimulus\";\nimport { announce } from \"../utils/announce\";\nimport { BeforeCacheReset } from \"../utils/before_cache_reset\";\nimport { setDefaultAttribute } from \"../utils/default_attribute\";\nimport { SafeTimeout } from \"../utils/safe_timeout\";\n\n/**\n * The `data-state` values this controller writes itself. Any other value on the\n * attribute was authored by the consumer and is left alone.\n */\nconst TRANSIENT_STATES = new Set([\"copied\", \"error\"]);\n\n/**\n * Headless copy-to-clipboard behavior with a completion slot and a screen-reader\n * announcement.\n *\n * Markup contract (identifier: `stimeo--clipboard`):\n * <div data-controller=\"stimeo--clipboard\"\n * data-stimeo--clipboard-feedback-duration-value=\"2000\"\n * data-stimeo--clipboard-announce-copied-text-value=\"Copied to clipboard\"\n * data-stimeo--clipboard-announce-error-text-value=\"Copy failed\">\n * <input type=\"text\" value=\"https://example.com\" readonly\n * data-stimeo--clipboard-target=\"source\">\n * <button type=\"button\" data-stimeo--clipboard-target=\"button\"\n * data-action=\"stimeo--clipboard#copy\">Copy</button>\n * <span data-stimeo--clipboard-target=\"feedback\"></span>\n * </div>\n *\n * No dedicated APG pattern; this follows the Button practice plus a status message\n * ({@link https://www.w3.org/WAI/WCAG22/Understanding/status-changes.html | WCAG 2.2 SC 4.1.3}).\n * The copy uses the standard `navigator.clipboard` API (no extra dependency); every\n * failure mode — an API the browser withholds outside a secure context as much as a\n * rejected permission — settles the same way, as `data-state=\"error\"` plus the\n * `copy` event's `success: false`.\n *\n * Targets: `source` (the text to copy, when `text` is not set), `button` (the\n * control that triggers `copy`), `feedback` (the **visible** completion slot).\n *\n * Values: `text` (copy this instead of reading `source`), `feedbackDuration` (ms the\n * completion state is held; `0` or less arms no timer, so it stands until the next\n * copy — a reconnect and the before-cache rewind still clear it), `copiedLabel` /\n * `errorLabel` (what the visible slot shows), `announceCopiedText` /\n * `announceErrorText` (what assistive tech hears; empty announces nothing).\n *\n * @remarks\n * Behavior only — icon swaps and styling are the consumer's, keyed off `data-state`\n * (`idle` / `copied` / `error`).\n *\n * **The `feedback` slot must not carry live-region semantics.** Announcing is the\n * page's shared `stimeo--announcer` job: it is seated before the change it reads,\n * which a slot filled on demand cannot be, and it already collapses a repeat of the\n * same wording so a second copy is still heard. A `role=\"status\"` on the slot as\n * well would say everything twice.\n *\n * `copied` and `error` are transient: the timer that clears them belongs to one\n * connection, so a fresh `connect()` that finds either — a restored snapshot, an\n * in-page move — returns to `idle`, and the state is rewound before Turbo caches\n * the page ({@link BeforeCacheReset}). The rewind is silent: it discards nothing a\n * reconnect does not derive again.\n */\nexport class ClipboardController extends Controller<HTMLElement> {\n static override targets = [\"source\", \"button\", \"feedback\"];\n static override values = {\n text: { type: String, default: \"\" },\n feedbackDuration: { type: Number, default: 2000 },\n copiedLabel: { type: String, default: \"Copied\" },\n errorLabel: { type: String, default: \"Copy failed\" },\n announceCopiedText: { type: String, default: \"\" },\n announceErrorText: { type: String, default: \"\" },\n };\n static actions = [\"copy\"] as const;\n static events = [\"copy\"] as const;\n\n declare readonly sourceTarget: HTMLElement;\n declare readonly buttonTarget: HTMLElement;\n declare readonly feedbackTarget: HTMLElement;\n declare readonly hasSourceTarget: boolean;\n declare readonly hasButtonTarget: boolean;\n declare readonly hasFeedbackTarget: boolean;\n\n declare textValue: string;\n declare feedbackDurationValue: number;\n declare copiedLabelValue: string;\n declare errorLabelValue: string;\n declare announceCopiedTextValue: string;\n declare announceErrorTextValue: string;\n\n /**\n * The pending return to idle — the only timer this controller schedules, so\n * `clearAll()` is exactly \"drop the auto-clear\" and needs no id of its own.\n */\n readonly #timers = new SafeTimeout();\n\n /** Returns the completion state to idle for the snapshot Turbo takes. */\n readonly #beforeCache = new BeforeCacheReset(() => this.#rewind());\n\n /**\n * Whether this connection is still live. `copy()` suspends on the Clipboard API,\n * and a teardown that lands while it is suspended must win: the continuation\n * would otherwise write to an element nobody owns and arm a timer past the\n * `clearAll()` that was supposed to be the last word.\n */\n #connected = false;\n\n override connect(): void {\n this.#connected = true;\n this.#adopt();\n this.#beforeCache.activate();\n }\n\n override disconnect(): void {\n this.#connected = false;\n this.#beforeCache.deactivate();\n this.#timers.clearAll();\n }\n\n /**\n * Copies the resolved text and reports the outcome. Bound via `data-action`\n * (click). Dispatches `stimeo--clipboard:copy` with `{ success, text }` once per\n * completed attempt — including on failure — so consumers can react either way.\n * An attempt whose connection ended while it was in flight reports nothing.\n */\n async copy(): Promise<void> {\n const text = this.#resolveText();\n let success = false;\n try {\n await navigator.clipboard.writeText(text);\n success = true;\n } catch {\n success = false;\n }\n if (!this.#connected) return;\n\n this.#reportResult(success);\n this.dispatch(\"copy\", { detail: { success, text } });\n }\n\n /**\n * The text to copy: the explicit `text` value when set, otherwise the source\n * target's current value (inputs/textareas) or text content.\n */\n #resolveText(): string {\n if (this.textValue.length > 0) return this.textValue;\n if (!this.hasSourceTarget) return \"\";\n const source = this.sourceTarget;\n if (source instanceof HTMLInputElement || source instanceof HTMLTextAreaElement) {\n return source.value;\n }\n return source.textContent ?? \"\";\n }\n\n /**\n * Reads the current state back from the DOM.\n *\n * A `copied` or `error` found at connect time is this controller's own output\n * from a connection that is gone, and so is the timer that would have cleared it\n * — nothing else would ever return the element to `idle`. Any other authored\n * value belongs to the consumer and only a missing attribute takes the default.\n */\n #adopt(): void {\n if (this.#inTransientState()) {\n this.#reset();\n return;\n }\n setDefaultAttribute(this.element, \"data-state\", \"idle\");\n }\n\n /** Whether `data-state` currently holds one of the values this controller writes. */\n #inTransientState(): boolean {\n const state = this.element.getAttribute(\"data-state\");\n return state !== null && TRANSIENT_STATES.has(state);\n }\n\n /**\n * Returns the completion state to idle for the snapshot Turbo is about to take,\n * so a page reached with the Back button does not report a copy that happened\n * before the navigation. Only a state this controller wrote is rewound — an\n * authored one is the consumer's and has to survive into the snapshot, exactly as\n * `connect()` leaves it alone. State only — no `copy` is dispatched, which would\n * claim a fresh copy ran.\n */\n #rewind(): void {\n if (!this.#inTransientState()) return;\n this.#timers.clearAll();\n this.#reset();\n }\n\n /** Reflects the result, announces it, and schedules the return to idle. */\n #reportResult(success: boolean): void {\n this.element.setAttribute(\"data-state\", success ? \"copied\" : \"error\");\n if (this.hasFeedbackTarget) {\n this.feedbackTarget.textContent = success ? this.copiedLabelValue : this.errorLabelValue;\n }\n announce(success ? this.announceCopiedTextValue : this.announceErrorTextValue);\n\n // Drop any in-flight reset so consecutive copies restart the full window\n // rather than having the earlier timer clear the new result prematurely.\n this.#timers.clearAll();\n if (this.feedbackDurationValue > 0) {\n this.#timers.set(() => this.#reset(), this.feedbackDurationValue);\n }\n }\n\n /** Returns to the idle state and empties the completion slot. */\n #reset(): void {\n this.element.setAttribute(\"data-state\", \"idle\");\n if (this.hasFeedbackTarget) {\n this.feedbackTarget.textContent = \"\";\n }\n }\n}\n"]}
|
|
@@ -8,6 +8,7 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
8
8
|
* data-stimeo--color-picker-value-value="#3366cc">
|
|
9
9
|
* <div role="slider" aria-label="Hue" data-channel="hue" tabindex="0"
|
|
10
10
|
* aria-valuemin="0" aria-valuemax="360" aria-valuenow="210"
|
|
11
|
+
* data-value-text="Hue {value} degrees"
|
|
11
12
|
* data-stimeo--color-picker-target="slider"
|
|
12
13
|
* data-action="keydown->stimeo--color-picker#onKeydown
|
|
13
14
|
* pointerdown->stimeo--color-picker#onPointerDown"></div>
|
|
@@ -38,18 +39,36 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
38
39
|
* here reads `direction`. A gradient has no logical `to` keyword, so mirroring
|
|
39
40
|
* one means swapping `to right`/`to left` under a `:dir(rtl)` selector.
|
|
40
41
|
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
42
|
+
* A channel slider announces its bounds from its own `aria-valuemin`/`aria-valuemax`,
|
|
43
|
+
* falling back per channel when they are absent or blank; the resolved pair is
|
|
44
|
+
* written back, so assistive tech never hears the `slider` role's 0–100 default
|
|
45
|
+
* over a hue that reaches 360. `aria-valuetext` is filled from the slider's
|
|
46
|
+
* `{@link VALUE_TEXT_ATTRIBUTE}` template — `{value}` is the channel value — which
|
|
47
|
+
* keeps the announced wording i18n-neutral; without a template the text is English.
|
|
48
|
+
*
|
|
49
|
+
* A drag belongs to the pointer that started it: only a primary button opens one,
|
|
50
|
+
* and {@link OwnedPointerSession} filters movement and termination by that
|
|
51
|
+
* `pointerId`, so a second finger neither steers nor cuts the gesture. Its
|
|
52
|
+
* listeners are released on drag end, when the slider leaves, and on `disconnect()`
|
|
53
|
+
* (Turbo navigation included).
|
|
54
|
+
*
|
|
55
|
+
* The `value` Value carries the color in both directions: every settled color is
|
|
56
|
+
* written back, so a Turbo cache restore and a form submission both carry the color
|
|
57
|
+
* the user picked. An outside write — application code or a morph — re-seeds the
|
|
58
|
+
* model and reports `reconcile`. The ARIA attributes, `--stimeo--color`, and the
|
|
59
|
+
* mirrored input values are this controller's own output and stay in the DOM as
|
|
60
|
+
* written, which is what makes the restored snapshot show the current color.
|
|
45
61
|
*
|
|
46
62
|
* The internal model is integer HSL(A), so a hex → HSL → hex round-trip is not
|
|
47
63
|
* exactly bijective: a typed hex can normalize to a near (not identical) value
|
|
48
64
|
* once the HSL sliders are touched. This keeps the model small and zero-dep; use a
|
|
49
65
|
* dedicated color library on the consumer side if exact hex preservation matters.
|
|
50
66
|
*
|
|
67
|
+
* While `alpha` is disabled the model stays opaque and an alpha slider authored
|
|
68
|
+
* anyway edits nothing, so the hex and `change`'s `rgba.a` never disagree.
|
|
69
|
+
*
|
|
51
70
|
* A color the user set through a slider or the hex input is reported as
|
|
52
|
-
* `stimeo--color-picker:change`.
|
|
71
|
+
* `stimeo--color-picker:change`. Changing `alpha` or `value` at runtime can move the
|
|
53
72
|
* committed color without a user edit, and that arrives as
|
|
54
73
|
* `stimeo--color-picker:reconcile` with the same detail. Neither fires on connect.
|
|
55
74
|
*/
|
|
@@ -86,9 +105,21 @@ declare class ColorPickerController extends Controller<HTMLElement> {
|
|
|
86
105
|
disconnect(): void;
|
|
87
106
|
/** Repaints when application code (or a Turbo morph) changes `alpha` at runtime. */
|
|
88
107
|
alphaValueChanged(): void;
|
|
108
|
+
/** Adopts a color application code (or a Turbo morph) put in `value` at runtime. */
|
|
109
|
+
valueValueChanged(): void;
|
|
110
|
+
/** Hydrates a channel slider inserted or replaced at runtime. */
|
|
111
|
+
sliderTargetConnected(slider: HTMLElement): void;
|
|
112
|
+
/** Ends a gesture whose geometry target disappeared or ceased being a target. */
|
|
113
|
+
sliderTargetDisconnected(slider: HTMLElement): void;
|
|
114
|
+
/** Fills a hex input inserted or replaced at runtime with the current color. */
|
|
115
|
+
hexTargetConnected(hex: HTMLInputElement): void;
|
|
116
|
+
/** Fills a form field inserted or replaced at runtime with the current color. */
|
|
117
|
+
fieldTargetConnected(field: HTMLInputElement): void;
|
|
118
|
+
/** Publishes the current color on a preview inserted or replaced at runtime. */
|
|
119
|
+
previewTargetConnected(preview: HTMLElement): void;
|
|
89
120
|
/** Keyboard stepping on the focused channel slider (APG Slider model). */
|
|
90
121
|
onKeydown(event: KeyboardEvent): void;
|
|
91
|
-
/** Begins a
|
|
122
|
+
/** Begins a primary-button drag on a channel slider, owned by its own pointer. */
|
|
92
123
|
onPointerDown(event: PointerEvent): void;
|
|
93
124
|
/** Parses the hex input on confirm and syncs every channel + surface. */
|
|
94
125
|
onHexInput(): void;
|