@dumiflex/wp-kit 0.12.0 → 0.13.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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.13.0](https://github.com/DumiFlex/wp-kit/compare/v0.12.0...v0.13.0) (2026-11-03)
4
+
5
+
6
+ ### Features
7
+
8
+ * v-toggle-row, so a row's words flip its one checkbox or switch, and SettingRow rows do it ([#102](https://github.com/DumiFlex/wp-kit/issues/102)) ([1d3309b](https://github.com/DumiFlex/wp-kit/commit/1d3309b6f2a7da6f2dff82a41aed8bc46d1c779a))
9
+
3
10
  ## [0.12.0](https://github.com/DumiFlex/wp-kit/compare/v0.11.0...v0.12.0) (2026-11-03)
4
11
 
5
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dumiflex/wp-kit",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "Vue 3 component kit shared by the Wildcard Pipeline manager, canvas and community site.",
5
5
  "keywords": [
6
6
  "vue",
@@ -14,7 +14,13 @@
14
14
  * control column, like a form. `recommended` names the value most people
15
15
  * should pick ("On"). `for` makes the label a real `<label for>`. Slots:
16
16
  * `#badge` after the label, `#extra` under the hint (a status line).
17
+ *
18
+ * A row whose control is one checkbox or switch flips it on a click anywhere
19
+ * on the row's words (v-toggle-row), like a label; a WpSwitch is a span, so a
20
+ * `<label for>` alone can't reach it.
17
21
  */
22
+ import { vToggleRow } from "./toggle-row";
23
+
18
24
  const props = defineProps<{
19
25
  label: string;
20
26
  hint?: string | undefined;
@@ -32,6 +38,7 @@ const props = defineProps<{
32
38
  <template>
33
39
  <div
34
40
  :id="settingKey"
41
+ v-toggle-row
35
42
  class="wp-set-row"
36
43
  :class="{ 'wp-set-row--stacked': stacked, 'wp-set-row--form': form, 'wp-set-row--target': highlighted }"
37
44
  :data-setting="settingKey"
@@ -0,0 +1,7 @@
1
+ /* v-toggle-row (toggle-row.ts): the row's words flip its one toggle, so the
2
+ * whole row shows the hand and its words don't get picked up as a selection
3
+ * on a quick double click. Layered, so a host's own cursor rule wins. */
4
+ @layer wp-extension {
5
+ .wp-toggle-row { cursor: pointer; user-select: none; -webkit-user-select: none; }
6
+ .wp-toggle-row--disabled { cursor: not-allowed; }
7
+ }
@@ -0,0 +1,104 @@
1
+ /**
2
+ * `v-toggle-row`: a click on a row's words flips the row's one checkbox or
3
+ * switch, the way a `<label>` ticks its input. For a row that lays out its
4
+ * label and its control itself (a label on the left, a switch on the right)
5
+ * instead of passing the words to the Checkbox or WpSwitch.
6
+ *
7
+ * <div v-toggle-row class="row">
8
+ * <span>Override base seed</span>
9
+ * <WpSwitch v-model="override" aria-label="Override base seed" />
10
+ * </div>
11
+ *
12
+ * It acts only when the row holds exactly one `role="checkbox"` or
13
+ * `role="switch"` and no other value control (an input, a select, a slider,
14
+ * radios), so a row with two switches or a switch beside a number field stays
15
+ * as it is. A click on anything interactive inside the row (the toggle
16
+ * itself, a button, a link, a native `<label>`, a labelled Checkbox) is left
17
+ * to that element. The control keeps the keyboard stop and gets the focus.
18
+ * SettingRow uses it on every row.
19
+ *
20
+ * The row gets the `wp-toggle-row` class while it has a single toggle, for
21
+ * the pointer cursor (toggle-row.css). State lives in a WeakMap (no expando
22
+ * keys), so the row's own attributes and serialization stay untouched.
23
+ */
24
+ import type { Directive } from "vue";
25
+ import "./toggle-row.css";
26
+
27
+ const TOGGLE = '[role="checkbox"], [role="switch"]';
28
+ /** Value controls that make "the row's control" ambiguous. */
29
+ const OTHER_CONTROL =
30
+ 'input:not([type="hidden"]), select, textarea, [role="combobox"], [role="slider"], [role="spinbutton"], [role="radio"], [role="listbox"], [contenteditable=""], [contenteditable="true"]';
31
+ /** A click that lands on one of these belongs to it, not to the row. */
32
+ const INTERACTIVE =
33
+ 'button, a[href], input, select, textarea, label, summary, [role="button"], [role="link"], [role="checkbox"], [role="switch"], [role="radio"], [role="tab"], [role="menuitem"], [role="option"], [role="combobox"], [role="slider"], [role="spinbutton"], [contenteditable=""], [contenteditable="true"], .wp-checkbox';
34
+
35
+ interface State {
36
+ off: () => void;
37
+ }
38
+
39
+ const states = new WeakMap<HTMLElement, State>();
40
+ /** One click flips one toggle, even with rows nested in rows. */
41
+ const handled = new WeakSet<Event>();
42
+
43
+ /** The row's one toggle, or null when it has none, several, or another control beside it. */
44
+ export function rowToggle(row: HTMLElement): HTMLElement | null {
45
+ const toggles = row.querySelectorAll<HTMLElement>(TOGGLE);
46
+ if (toggles.length !== 1) return null;
47
+ if (row.querySelector(OTHER_CONTROL)) return null;
48
+ return toggles[0] ?? null;
49
+ }
50
+
51
+ function isDisabled(el: HTMLElement): boolean {
52
+ return el.getAttribute("aria-disabled") === "true" || (el instanceof HTMLButtonElement && el.disabled);
53
+ }
54
+
55
+ function sync(row: HTMLElement): void {
56
+ const toggle = rowToggle(row);
57
+ row.classList.toggle("wp-toggle-row", !!toggle);
58
+ row.classList.toggle("wp-toggle-row--disabled", !!toggle && isDisabled(toggle));
59
+ }
60
+
61
+ /** Flip the row's toggle for a click on the row's words. Exported for tests. */
62
+ export function onRowClick(row: HTMLElement, event: MouseEvent): void {
63
+ if (handled.has(event) || event.button !== 0) return;
64
+ const target = event.target;
65
+ if (!(target instanceof Element)) return;
66
+ const hit = target.closest(INTERACTIVE);
67
+ if (hit && hit !== row && row.contains(hit)) return;
68
+ // Text the reader was selecting is not a click on the label.
69
+ const selection = row.ownerDocument.getSelection?.();
70
+ if (selection && !selection.isCollapsed && row.contains(selection.anchorNode)) return;
71
+ const toggle = rowToggle(row);
72
+ if (!toggle || isDisabled(toggle)) return;
73
+ handled.add(event);
74
+ toggle.click();
75
+ toggle.focus({ preventScroll: true });
76
+ }
77
+
78
+ function attach(el: HTMLElement): void {
79
+ if (states.has(el)) return;
80
+ const click = (e: MouseEvent): void => onRowClick(el, e);
81
+ el.addEventListener("click", click);
82
+ states.set(el, { off: () => el.removeEventListener("click", click) });
83
+ }
84
+
85
+ function detach(el: HTMLElement): void {
86
+ states.get(el)?.off();
87
+ states.delete(el);
88
+ el.classList.remove("wp-toggle-row", "wp-toggle-row--disabled");
89
+ }
90
+
91
+ /** `v-toggle-row="false"` turns it off for one row (a row whose words do something else). */
92
+ export const vToggleRow: Directive<HTMLElement, boolean | undefined> = {
93
+ mounted(el, binding) {
94
+ if (binding.value === false) return;
95
+ attach(el);
96
+ sync(el);
97
+ },
98
+ updated(el, binding) {
99
+ if (binding.value === false) return detach(el);
100
+ attach(el);
101
+ sync(el);
102
+ },
103
+ unmounted: detach,
104
+ };