@glasshome/ui 1.42.0 → 1.43.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.
Files changed (147) hide show
  1. package/dist/index.js +2 -2
  2. package/dist/lib/anchor-placement.d.ts +4 -0
  3. package/dist/lib/balance-rows.d.ts +10 -4
  4. package/dist/lib/behavior/anchored.d.ts +16 -2
  5. package/dist/lib/behavior/button-root.d.ts +7 -0
  6. package/dist/lib/behavior/collection.d.ts +8 -0
  7. package/dist/lib/behavior/dismiss.d.ts +6 -4
  8. package/dist/lib/behavior/focus-scope.d.ts +7 -1
  9. package/dist/lib/behavior/hide-outside.d.ts +1 -1
  10. package/dist/lib/behavior/layer-stack.d.ts +3 -2
  11. package/dist/lib/behavior/menu.d.ts +59 -0
  12. package/dist/lib/behavior/modal-root.d.ts +2 -0
  13. package/dist/lib/behavior/range.d.ts +44 -0
  14. package/dist/lib/color/color.d.ts +41 -0
  15. package/dist/lib/color/index.d.ts +2 -0
  16. package/dist/lib/color/intl.d.ts +40 -0
  17. package/dist/lib/dock-slot-classes.d.ts +2 -0
  18. package/dist/lib/hold.d.ts +1 -1
  19. package/dist/lib/locale.d.ts +3 -0
  20. package/dist/lib/menu-classes.d.ts +2 -0
  21. package/dist/lib/motion-classes.d.ts +1 -1
  22. package/dist/lib/overlay-classes.d.ts +1 -0
  23. package/dist/lib/scroll-track.d.ts +3 -2
  24. package/dist/lib/section-tokens.d.ts +1 -1
  25. package/dist/lib/segment-classes.d.ts +2 -0
  26. package/dist/lib/shadow-layer.d.ts +0 -45
  27. package/dist/lib/toggle-set.d.ts +44 -0
  28. package/dist/lib/visually-hidden.d.ts +3 -0
  29. package/dist/solid/accordion.d.ts +35 -11
  30. package/dist/solid/avatar.d.ts +8 -4
  31. package/dist/solid/bottom-sheet/bottom-sheet.d.ts +1 -1
  32. package/dist/solid/button.d.ts +5 -4
  33. package/dist/solid/charts.d.ts +1 -1
  34. package/dist/solid/checkbox.d.ts +14 -4
  35. package/dist/solid/collapsible.d.ts +33 -5
  36. package/dist/solid/color-disc.d.ts +1 -1
  37. package/dist/solid/color-slider.d.ts +1 -1
  38. package/dist/solid/color-wheel.d.ts +1 -1
  39. package/dist/solid/context-menu.d.ts +58 -24
  40. package/dist/solid/dialog-parts.d.ts +8 -8
  41. package/dist/solid/dropdown-menu.d.ts +55 -24
  42. package/dist/solid/empty.d.ts +1 -1
  43. package/dist/solid/form.d.ts +5 -6
  44. package/dist/solid/hover-card.d.ts +65 -9
  45. package/dist/solid/index.d.ts +1 -1
  46. package/dist/solid/index.js +7725 -13458
  47. package/dist/solid/input-group.d.ts +6 -6
  48. package/dist/solid/item.d.ts +1 -1
  49. package/dist/solid/menu-parts.d.ts +175 -24
  50. package/dist/solid/option-card.d.ts +1 -4
  51. package/dist/solid/picker-row.d.ts +2 -0
  52. package/dist/solid/popover.d.ts +77 -12
  53. package/dist/solid/progress.d.ts +11 -3
  54. package/dist/solid/radio-group.d.ts +31 -6
  55. package/dist/solid/responsive-dialog.d.ts +1 -1
  56. package/dist/solid/scroll-area.d.ts +1 -1
  57. package/dist/solid/section-card.d.ts +2 -3
  58. package/dist/solid/select.d.ts +107 -10
  59. package/dist/solid/selection.d.ts +10 -1
  60. package/dist/solid/sheet.d.ts +1 -1
  61. package/dist/solid/sliding-indicator.d.ts +1 -1
  62. package/dist/solid/sonner.d.ts +1 -1
  63. package/dist/solid/toggle.d.ts +11 -3
  64. package/dist/solid/tooltip.d.ts +22 -22
  65. package/dist/tokens/index.js +54 -51
  66. package/dist/{utils-CxAii-e9.js → utils-3Codx-NZ.js} +527 -489
  67. package/package.json +3 -6
  68. package/src/lib/anchor-placement.ts +10 -3
  69. package/src/lib/balance-rows.ts +115 -40
  70. package/src/lib/behavior/anchored.ts +26 -8
  71. package/src/lib/behavior/button-root.tsx +103 -0
  72. package/src/lib/behavior/collection.ts +66 -29
  73. package/src/lib/behavior/dismiss.ts +61 -21
  74. package/src/lib/behavior/focus-scope.ts +31 -6
  75. package/src/lib/behavior/hide-outside.ts +2 -2
  76. package/src/lib/behavior/layer-stack.ts +33 -8
  77. package/src/lib/behavior/menu.ts +470 -0
  78. package/src/lib/behavior/modal-root.tsx +24 -18
  79. package/src/lib/behavior/polymorphic.tsx +1 -1
  80. package/src/lib/behavior/presence.ts +1 -1
  81. package/src/lib/behavior/range.ts +212 -0
  82. package/src/lib/clipboard.ts +2 -3
  83. package/src/lib/color/color.ts +746 -0
  84. package/src/lib/color/index.ts +2 -0
  85. package/src/lib/color/intl.ts +60 -0
  86. package/src/lib/dock-slot-classes.ts +3 -0
  87. package/src/lib/hold.ts +1 -1
  88. package/src/lib/locale.ts +62 -0
  89. package/src/lib/menu-classes.ts +3 -0
  90. package/src/lib/motion-classes.ts +7 -7
  91. package/src/lib/overlay-classes.ts +1 -3
  92. package/src/lib/scroll-track.ts +11 -6
  93. package/src/lib/section-tokens.ts +1 -1
  94. package/src/lib/segment-classes.ts +3 -0
  95. package/src/lib/shadow-layer.ts +0 -118
  96. package/src/lib/toggle-set.ts +133 -0
  97. package/src/lib/visually-hidden.ts +15 -0
  98. package/src/solid/accordion.tsx +294 -20
  99. package/src/solid/area-picker.tsx +3 -2
  100. package/src/solid/avatar.tsx +98 -24
  101. package/src/solid/bottom-sheet/bottom-sheet.tsx +1 -1
  102. package/src/solid/button.tsx +12 -6
  103. package/src/solid/charts.tsx +1 -1
  104. package/src/solid/checkbox.tsx +167 -44
  105. package/src/solid/collapsible.tsx +260 -12
  106. package/src/solid/color-disc.tsx +11 -12
  107. package/src/solid/color-slider.tsx +114 -47
  108. package/src/solid/color-wheel.tsx +137 -39
  109. package/src/solid/context-menu.tsx +285 -94
  110. package/src/solid/dialog-parts.tsx +9 -9
  111. package/src/solid/dock.tsx +3 -2
  112. package/src/solid/dropdown-menu.tsx +252 -115
  113. package/src/solid/empty.tsx +1 -1
  114. package/src/solid/entity-selector.tsx +3 -2
  115. package/src/solid/face-toggle.tsx +43 -18
  116. package/src/solid/form.tsx +5 -6
  117. package/src/solid/glyph-tile.tsx +48 -21
  118. package/src/solid/hover-card.tsx +287 -25
  119. package/src/solid/image-picker.tsx +2 -2
  120. package/src/solid/index.ts +1 -3
  121. package/src/solid/input-group.tsx +2 -2
  122. package/src/solid/item.tsx +1 -1
  123. package/src/solid/menu-parts.tsx +619 -145
  124. package/src/solid/option-card.tsx +6 -10
  125. package/src/solid/picker-row.tsx +5 -1
  126. package/src/solid/popover.tsx +424 -79
  127. package/src/solid/preview-tile.tsx +1 -1
  128. package/src/solid/progress.tsx +84 -15
  129. package/src/solid/radio-group.tsx +271 -34
  130. package/src/solid/responsive-dialog.tsx +1 -1
  131. package/src/solid/schema-form.tsx +1 -6
  132. package/src/solid/scroll-area.tsx +1 -1
  133. package/src/solid/section-card.tsx +3 -4
  134. package/src/solid/select.tsx +950 -95
  135. package/src/solid/selection.tsx +142 -66
  136. package/src/solid/sheet.tsx +1 -1
  137. package/src/solid/slider.tsx +115 -65
  138. package/src/solid/sliding-indicator.tsx +1 -1
  139. package/src/solid/sonner.tsx +1 -1
  140. package/src/solid/tabs.tsx +12 -6
  141. package/src/solid/term.tsx +3 -4
  142. package/src/solid/toggle-group.tsx +29 -99
  143. package/src/solid/toggle.tsx +43 -9
  144. package/src/solid/tooltip.tsx +24 -24
  145. package/src/styles/globals.css +61 -7
  146. package/src/styles/theme.css +7 -4
  147. package/src/tokens/material.ts +11 -2
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@glasshome/ui",
3
- "version": "1.42.0",
4
- "description": "SolidJS component library for GlassHome, built on Kobalte",
3
+ "version": "1.43.0",
4
+ "description": "SolidJS component library for GlassHome",
5
5
  "keywords": [
6
6
  "components",
7
7
  "glasshome",
8
8
  "home-assistant",
9
- "kobalte",
10
9
  "smart-home",
11
10
  "solidjs",
12
11
  "tailwindcss",
@@ -100,7 +99,6 @@
100
99
  "dependencies": {
101
100
  "@iconify/types": "^2.0.0",
102
101
  "@iconify/utils": "^3.1.4",
103
- "@kobalte/core": "^0.13.13",
104
102
  "@types/json-schema": "^7.0.15",
105
103
  "clsx": "^2.1.1",
106
104
  "cva": "^1.0.0-beta.4",
@@ -154,7 +152,6 @@
154
152
  "node": ">=20"
155
153
  },
156
154
  "patchedDependencies": {
157
- "@guidepup/virtual-screen-reader@0.33.0": "patches/@guidepup%2Fvirtual-screen-reader@0.33.0.patch",
158
- "@kobalte/core@0.13.13": "patches/@kobalte%2Fcore@0.13.13.patch"
155
+ "@guidepup/virtual-screen-reader@0.33.0": "patches/@guidepup%2Fvirtual-screen-reader@0.33.0.patch"
159
156
  }
160
157
  }
@@ -16,6 +16,8 @@ export interface AnchorPlace {
16
16
  align: Align;
17
17
  offset: number;
18
18
  matchAnchorWidth: boolean;
19
+ /** False keeps the chosen side even without room; the part only shifts to stay inside the viewport. */
20
+ flip?: boolean;
19
21
  }
20
22
  export interface Placement {
21
23
  left: number;
@@ -50,7 +52,10 @@ export function placeBeside(
50
52
  const vertical = place.side === "top" || place.side === "bottom";
51
53
  const need = vertical ? floating.height : floating.width;
52
54
  const other = OPPOSITE[place.side];
53
- const side = rooms[place.side] >= need || rooms[other] <= rooms[place.side] ? place.side : other;
55
+ const side =
56
+ place.flip === false || rooms[place.side] >= need || rooms[other] <= rooms[place.side]
57
+ ? place.side
58
+ : other;
54
59
  const maxWidth = viewport.width - 2 * EDGE;
55
60
  const maxHeight = viewport.height - 2 * EDGE;
56
61
  const width = Math.min(
@@ -95,13 +100,15 @@ function anchoredBox(
95
100
  return { left: x, top: y, width: w, height: h };
96
101
  }
97
102
 
103
+ /** The corner a control takes (rounded-lg is --radius), clamped to a capsule at its height; read from the theme so opening a panel asks the page for no style. */
104
+ export const controlRadius = (height: number): string => `min(var(--radius), ${height / 2}px)`;
105
+
98
106
  /** A panel that grows out of the element that opened it. The --morph-* values start the panel
99
107
  * clipped to the element's box (MORPH_MOTION's keyframes), so it unrolls out of it. */
100
108
  export function overAnchor(anchor: HTMLElement, width: number, height?: number): JSX.CSSProperties {
101
109
  const box = anchor.getBoundingClientRect();
102
110
  const vh = window.innerHeight;
103
111
  const placed = anchoredBox(box, { width: window.innerWidth, height: vh }, width, height);
104
- const radius = getComputedStyle(anchor).borderTopLeftRadius || "0px";
105
112
  return {
106
113
  left: `${placed.left}px`,
107
114
  top: `${placed.top}px`,
@@ -114,6 +121,6 @@ export function overAnchor(anchor: HTMLElement, width: number, height?: number):
114
121
  "--morph-y": `${box.top - placed.top}px`,
115
122
  "--morph-w": `${box.width}px`,
116
123
  "--morph-h": `${box.height}px`,
117
- "--morph-radius": radius,
124
+ "--morph-radius": controlRadius(box.height),
118
125
  } as JSX.CSSProperties;
119
126
  }
@@ -1,62 +1,137 @@
1
+ import type { Accessor } from "solid-js";
2
+ import { effect } from "./behavior/solid.js";
3
+
1
4
  /* A row of segments that runs out of width wraps into a grid whose rows hold an even count
2
5
  * (four modes go two by two, five go three and two), never a full row and an orphan. */
3
6
 
4
- /** Classes for the element holding the items: one row, then the balanced grid once `data-wrapped` is set. */
5
- export const BALANCED_ROWS =
6
- "grid grid-flow-col auto-cols-[minmax(max-content,1fr)] data-[wrapped]:grid-flow-row data-[wrapped]:grid-cols-[repeat(var(--balance-columns),minmax(max-content,1fr))]";
7
-
8
- const fits = (items: HTMLElement) => items.scrollWidth <= items.clientWidth;
9
-
10
- function fit(sizer: HTMLElement, items: HTMLElement, count: number): void {
11
- const setColumns = (columns: number) =>
12
- items.style.setProperty("--balance-columns", String(columns));
13
- sizer.removeAttribute("data-wrapped");
14
- items.removeAttribute("data-wrapped");
15
- items.style.removeProperty("--balance-columns");
16
- if (count < 2 || fits(items)) return;
17
- sizer.setAttribute("data-wrapped", "");
18
- items.setAttribute("data-wrapped", "");
7
+ /** Marks the element holding the items: a wrapping row until the column count is known, then a grid of that many equal columns (globals.css). */
8
+ export const BALANCED_ROWS = "data-balanced-rows";
9
+
10
+ const BALANCED = "data-balanced";
11
+
12
+ /** Marks the node inside each item that sizes to its content in either layout. */
13
+ export const BALANCE_CONTENT = "data-balance-content";
14
+
15
+ const SLACK_PX = 0.5;
16
+
17
+ const total = (widths: number[]) => widths.reduce((sum, w) => sum + w, 0);
18
+
19
+ /** Columns of the balanced grid, from each item's own width; every item when one row holds them all. */
20
+ function balancedColumns(room: number, widths: number[]): number {
21
+ const count = widths.length;
22
+ if (count < 2 || total(widths) <= room + SLACK_PX) return count;
19
23
  let columns = count - 1;
20
24
  for (; columns > 1; columns--) {
21
- setColumns(columns);
22
- if (fits(items)) break;
25
+ let span = 0;
26
+ for (let column = 0; column < columns; column++) {
27
+ let widest = 0;
28
+ for (let i = column; i < count; i += columns) widest = Math.max(widest, widths[i] ?? 0);
29
+ span += widest;
30
+ }
31
+ if (span <= room + SLACK_PX) break;
23
32
  }
24
- setColumns(Math.ceil(count / Math.ceil(count / Math.max(columns, 1))));
33
+ return Math.ceil(count / Math.ceil(count / Math.max(columns, 1)));
25
34
  }
26
35
 
36
+ const inline = (sizes: readonly ResizeObserverSize[] | undefined, fallback: number) =>
37
+ sizes?.[0]?.inlineSize ?? fallback;
38
+
27
39
  /**
28
40
  * Lays `items` out in `BALANCED_ROWS`. `sizer` is the element that fills its line once wrapped
29
- * (it takes `data-wrapped` too); `item` selects the items among `items`' children.
41
+ * (it takes `data-wrapped` too); `item` selects the items among `items`' children, each holding
42
+ * one `BALANCE_CONTENT` node. Sizes come from a ResizeObserver only, never a layout read: an
43
+ * item's own width is its content node's width plus its padding and border, the same in either
44
+ * layout, so only the column count waits a frame, and the wrapping row never overflows meanwhile.
30
45
  */
31
- export function balanceRows(sizer: HTMLElement, items: HTMLElement, item: string): () => void {
32
- const each = () => items.querySelectorAll<HTMLElement>(`:scope > ${item}`);
33
- const run = () => {
34
- if (sizer.clientWidth === 0) return;
35
- fit(sizer, items, each().length);
36
- };
37
- run();
38
- // A frame late: a fit inside the observer's callback resizes what it observes.
46
+ export function balanceRows(
47
+ sizer: HTMLElement,
48
+ items: HTMLElement,
49
+ item: string,
50
+ members: Accessor<unknown>,
51
+ ): () => void {
52
+ const content = `[${BALANCE_CONTENT}]`;
53
+ const each = () => [...items.querySelectorAll<HTMLElement>(`:scope > ${item}`)];
54
+ const own = new WeakMap<Element, number>();
55
+ const chrome = new WeakMap<Element, number>();
56
+ let columns = 0;
39
57
  let frame = 0;
40
- const later = () => {
58
+
59
+ const widths = (): number[] | undefined => {
60
+ const out: number[] = [];
61
+ for (const el of each()) {
62
+ const inner = el.querySelector(content);
63
+ const width = inner ? own.get(inner) : undefined;
64
+ const edge = chrome.get(el);
65
+ if (width === undefined || edge === undefined) return undefined;
66
+ out.push(width + edge);
67
+ }
68
+ return out;
69
+ };
70
+
71
+ const layout = (next: number, count: number) => {
72
+ columns = next;
73
+ const wrapped = next > 0 && next < count;
74
+ for (const el of [sizer, items]) {
75
+ if (wrapped) el.setAttribute("data-wrapped", "");
76
+ else el.removeAttribute("data-wrapped");
77
+ }
78
+ if (next > 0) {
79
+ items.setAttribute(BALANCED, "");
80
+ items.style.setProperty("--balance-columns", String(next));
81
+ } else {
82
+ items.removeAttribute(BALANCED);
83
+ items.style.removeProperty("--balance-columns");
84
+ }
85
+ };
86
+
87
+ // A layout change inside any ResizeObserver callback resizes what other observers watch and loops, so it waits a frame.
88
+ const decide = () => {
41
89
  cancelAnimationFrame(frame);
42
- frame = requestAnimationFrame(run);
90
+ frame = requestAnimationFrame(() => {
91
+ const known = widths();
92
+ if (!known) return;
93
+ const space = room();
94
+ const next = space > 0 ? balancedColumns(space, known) : 0;
95
+ if (next !== columns) layout(next, known.length);
96
+ });
43
97
  };
44
- // Items too: an icon or font that loads late widens a label without resizing the group.
45
- const resize = new ResizeObserver(later);
46
- const observe = () => {
98
+
99
+ const parent = sizer.parentElement;
100
+ let line = 0;
101
+ let sizerWidth = 0;
102
+ let itemsWidth = 0;
103
+ // The room is the line the sizer may fill, whatever width it fits to now.
104
+ const room = () => (parent ? line - (sizerWidth - itemsWidth) : itemsWidth);
105
+
106
+ const resize = new ResizeObserver((entries) => {
107
+ for (const entry of entries) {
108
+ const border = inline(entry.borderBoxSize, entry.contentRect.width);
109
+ if (entry.target === parent) line = entry.contentRect.width;
110
+ if (entry.target === sizer) sizerWidth = border;
111
+ if (entry.target === items) itemsWidth = entry.contentRect.width;
112
+ if (entry.target.hasAttribute(BALANCE_CONTENT)) own.set(entry.target, border);
113
+ else if (entry.target.parentElement === items)
114
+ chrome.set(entry.target, border - inline(entry.contentBoxSize, entry.contentRect.width));
115
+ }
116
+ decide();
117
+ });
118
+
119
+ const watch = () => {
47
120
  resize.disconnect();
121
+ if (parent) resize.observe(parent);
48
122
  resize.observe(sizer);
49
- for (const el of each()) resize.observe(el);
123
+ resize.observe(items);
124
+ for (const el of each()) {
125
+ resize.observe(el);
126
+ const inner = el.querySelector(content);
127
+ if (inner) resize.observe(inner);
128
+ }
50
129
  };
51
- observe();
52
- const children = new MutationObserver(() => {
53
- observe();
54
- later();
55
- });
56
- children.observe(items, { childList: true });
130
+
131
+ layout(0, 0);
132
+ effect(members, watch);
57
133
  return () => {
58
134
  cancelAnimationFrame(frame);
59
135
  resize.disconnect();
60
- children.disconnect();
61
136
  };
62
137
  }
@@ -1,18 +1,30 @@
1
1
  import type { Accessor } from "solid-js";
2
- import { type Align, placeBeside, type Side } from "../anchor-placement";
3
- import { effect } from "./solid";
2
+ import { type Align, placeBeside, type Side } from "../anchor-placement.js";
3
+ import { effect } from "./solid.js";
4
+
5
+ /** An element, or a point or box with no element of its own: a context menu opens at the pointer. */
6
+ export interface AnchorLike {
7
+ getBoundingClientRect(): { left: number; top: number; width: number; height: number };
8
+ getRootNode?(): Node;
9
+ }
4
10
 
5
11
  export interface AnchoredOptions {
6
- anchor: Accessor<Element | undefined>;
12
+ anchor: Accessor<AnchorLike | undefined>;
7
13
  floating: Accessor<HTMLElement | undefined>;
8
14
  open: Accessor<boolean>;
9
15
  side?: Accessor<Side>;
10
16
  align?: Accessor<Align>;
11
17
  offset?: number;
12
18
  matchAnchorWidth?: boolean;
19
+ /** The anchor counts as its top edge and the part never flips: placed below, it covers the anchor. */
20
+ coverAnchor?: boolean;
21
+ /** A custom property the part gets the anchor's width in, for styles sized by the anchor. */
22
+ widthVar?: string;
13
23
  }
14
24
 
15
25
  const SHADOW_ROOT_NODE = 11;
26
+ // The widget realm's tap guard reads this marker as a placed panel.
27
+ const ANCHORED = "data-anchored";
16
28
 
17
29
  export function createAnchored(options: AnchoredOptions): void {
18
30
  effect(
@@ -26,8 +38,10 @@ export function createAnchored(options: AnchoredOptions): void {
26
38
  ] as const,
27
39
  ([open, anchor, floating, side, align]) => {
28
40
  if (!open || !anchor || !floating) return;
41
+ floating.setAttribute(ANCHORED, "");
29
42
  const offset = options.offset ?? 0;
30
43
  const matchAnchorWidth = options.matchAnchorWidth ?? false;
44
+ const cover = options.coverAnchor ?? false;
31
45
  const written = new Map<string, string>();
32
46
  const write = (name: string, value: string) => {
33
47
  if (written.get(name) === value) return;
@@ -42,16 +56,19 @@ export function createAnchored(options: AnchoredOptions): void {
42
56
  write("margin", "0");
43
57
  write("right", "auto");
44
58
  write("bottom", "auto");
45
- size = { width: floating.offsetWidth, height: floating.offsetHeight };
46
59
  }
47
60
  const box = anchor.getBoundingClientRect();
61
+ if (options.widthVar) write(options.widthVar, `${box.width}px`);
62
+ if (measure) size = { width: floating.offsetWidth, height: floating.offsetHeight };
48
63
  const placed = placeBeside(
49
- { left: box.left, top: box.top, width: box.width, height: box.height },
64
+ { left: box.left, top: box.top, width: box.width, height: cover ? 0 : box.height },
50
65
  size,
51
66
  { width: window.innerWidth, height: window.innerHeight },
52
- { side, align, offset, matchAnchorWidth },
67
+ { side, align, offset, matchAnchorWidth, flip: !cover },
53
68
  );
54
- write("top", `${placed.top}px`);
69
+ // A covering part may sit as near the edge as its anchor already does.
70
+ const top = cover && box.top >= 0 ? Math.min(placed.top, box.top + offset) : placed.top;
71
+ write("top", `${top}px`);
55
72
  write("left", `${placed.left}px`);
56
73
  write("max-width", `${placed.maxWidth}px`);
57
74
  write("max-height", `${placed.maxHeight}px`);
@@ -71,7 +88,7 @@ export function createAnchored(options: AnchoredOptions): void {
71
88
  };
72
89
  const onResize = () => place(true);
73
90
 
74
- const root = typeof anchor.getRootNode === "function" ? anchor.getRootNode() : document;
91
+ const root = anchor.getRootNode?.() ?? document;
75
92
  const scrollTargets: EventTarget[] =
76
93
  root.nodeType === SHADOW_ROOT_NODE ? [document, root] : [document];
77
94
  for (const target of scrollTargets)
@@ -82,6 +99,7 @@ export function createAnchored(options: AnchoredOptions): void {
82
99
  target.removeEventListener("scroll", onScroll, { capture: true });
83
100
  window.removeEventListener("resize", onResize);
84
101
  if (frame !== undefined) cancelAnimationFrame(frame);
102
+ floating.removeAttribute(ANCHORED);
85
103
  };
86
104
  },
87
105
  );
@@ -0,0 +1,103 @@
1
+ import { type ComponentProps, createSignal, type JSX, type ValidComponent } from "solid-js";
2
+ import { Polymorphic } from "./polymorphic.js";
3
+ import { callEventHandler, effect, merge, omit } from "./solid.js";
4
+
5
+ export type ButtonRootProps<T extends ValidComponent = "button"> = Omit<
6
+ ComponentProps<T>,
7
+ "as" | "disabled"
8
+ > & {
9
+ as?: T;
10
+ disabled?: boolean;
11
+ };
12
+
13
+ type Passed = {
14
+ onClick?: JSX.EventHandlerUnion<HTMLElement, MouseEvent>;
15
+ ref?: unknown;
16
+ href?: unknown;
17
+ };
18
+
19
+ const NATIVE = new Set(["button", "input"]);
20
+
21
+ /** A press target: a native button, a link, or any other element made one (role, Tab stop, Enter and Space). */
22
+ export function ButtonRoot<T extends ValidComponent = "button">(props: ButtonRootProps<T>) {
23
+ const own = props as ButtonRootProps<ValidComponent> & Passed;
24
+ const [el, setEl] = createSignal<HTMLElement>();
25
+ const tag = () =>
26
+ typeof own.as === "string" ? own.as : own.as === undefined ? "button" : el()?.localName;
27
+ const native = () => NATIVE.has(tag() ?? "");
28
+ const made = () => !native() && !(tag() === "a" && own.href !== undefined);
29
+
30
+ // Bound on the element, since Solid's delegated handlers never reach inside a closed shadow root.
31
+ effect(el, (node) => {
32
+ if (!node) return;
33
+ const click = (event: MouseEvent) => {
34
+ if (own.disabled && !native()) {
35
+ event.preventDefault();
36
+ event.stopImmediatePropagation();
37
+ return;
38
+ }
39
+ callEventHandler(
40
+ event as MouseEvent & { currentTarget: HTMLElement; target: Element },
41
+ own.onClick,
42
+ );
43
+ };
44
+ const keydown = (event: KeyboardEvent) => {
45
+ if (!made() || own.disabled || event.target !== node) return;
46
+ if (event.key === "Enter") {
47
+ event.preventDefault();
48
+ node.click();
49
+ } else if (event.key === " ") event.preventDefault();
50
+ };
51
+ const keyup = (event: KeyboardEvent) => {
52
+ if (!made() || own.disabled || event.target !== node || event.key !== " ") return;
53
+ event.preventDefault();
54
+ node.click();
55
+ };
56
+ node.addEventListener("click", click);
57
+ node.addEventListener("keydown", keydown);
58
+ node.addEventListener("keyup", keyup);
59
+ return () => {
60
+ node.removeEventListener("click", click);
61
+ node.removeEventListener("keydown", keydown);
62
+ node.removeEventListener("keyup", keyup);
63
+ };
64
+ });
65
+
66
+ return (
67
+ <Polymorphic
68
+ {...merge(
69
+ {
70
+ get type() {
71
+ return native() ? "button" : undefined;
72
+ },
73
+ get role() {
74
+ return made() ? "button" : undefined;
75
+ },
76
+ get tabIndex() {
77
+ return made() && !own.disabled ? 0 : undefined;
78
+ },
79
+ get disabled() {
80
+ return native() ? own.disabled : undefined;
81
+ },
82
+ get "aria-disabled"() {
83
+ return !native() && own.disabled ? true : undefined;
84
+ },
85
+ get "data-disabled"() {
86
+ return own.disabled ? "" : undefined;
87
+ },
88
+ },
89
+ omit(own, "as", "disabled", "onClick", "ref"),
90
+ {
91
+ get as() {
92
+ return own.as;
93
+ },
94
+ defaultAs: "button" as ValidComponent,
95
+ ref: (node: HTMLElement) => {
96
+ setEl(node);
97
+ if (typeof own.ref === "function") own.ref(node);
98
+ },
99
+ },
100
+ )}
101
+ />
102
+ );
103
+ }
@@ -1,6 +1,6 @@
1
1
  import { type Accessor, createSignal, onCleanup } from "solid-js";
2
- import { focusWithoutTracking } from "./focus-scope";
3
- import { effect } from "./solid";
2
+ import { activeOf, focusWithoutTracking } from "./focus-scope.js";
3
+ import { effect } from "./solid.js";
4
4
 
5
5
  export const TYPEAHEAD_MS = 1000;
6
6
 
@@ -16,6 +16,8 @@ export interface CollectionOptions {
16
16
  dir?: Accessor<"ltr" | "rtl">;
17
17
  /** Keys that hold the tab stop while focus is outside, first present wins (a selected tab). */
18
18
  preferred?: Accessor<readonly string[]>;
19
+ /** APG's focusable when disabled: an `aria-disabled` item stays in the arrow order and can hold the tab stop. */
20
+ disabledFocusable?: boolean;
19
21
  }
20
22
 
21
23
  export interface Collection {
@@ -34,11 +36,6 @@ const isDisabled = (el: HTMLElement) =>
34
36
 
35
37
  const keyOf = (el: HTMLElement) => el.dataset.key ?? "";
36
38
 
37
- function focusIsWithin(root: HTMLElement): boolean {
38
- const active = (root.getRootNode() as Document | ShadowRoot).activeElement;
39
- return !!active && root.contains(active);
40
- }
41
-
42
39
  function step(
43
40
  items: HTMLElement[],
44
41
  from: number,
@@ -58,25 +55,55 @@ function forwardKeys(orientation: Orientation, rtl: boolean): { next: string; pr
58
55
  : { next: "ArrowRight", prev: "ArrowLeft" };
59
56
  }
60
57
 
61
- const textOf = (el: HTMLElement) => (el.textContent ?? "").trim().toLowerCase();
58
+ const textOf = (el: HTMLElement) => el.textContent ?? "";
62
59
 
63
- function matchText(items: HTMLElement[], from: number, buffer: string): HTMLElement | undefined {
60
+ function matchText(texts: readonly string[], from: number, buffer: string): number {
64
61
  const first = buffer.charAt(0);
65
62
  const cycling = buffer.length > 1 && buffer === first.repeat(buffer.length);
66
63
  const search = cycling ? first : buffer;
67
64
  const start = cycling || buffer.length === 1 ? from + 1 : Math.max(from, 0);
68
- for (let i = 0; i < items.length; i++) {
69
- const item = items[(start + i) % items.length];
70
- if (item && textOf(item).startsWith(search)) return item;
65
+ for (let i = 0; i < texts.length; i++) {
66
+ const index = (start + i) % texts.length;
67
+ if ((texts[index] ?? "").trim().toLowerCase().startsWith(search)) return index;
71
68
  }
72
- return undefined;
69
+ return -1;
70
+ }
71
+
72
+ export interface Typeahead {
73
+ /** The index in `texts` the key leads to: -1 when nothing matches, `undefined` when the key types nothing. */
74
+ match: (event: KeyboardEvent, texts: readonly string[], from: number) => number | undefined;
75
+ }
76
+
77
+ /** The typing buffer alone, for a list whose items are not in the DOM (a closed Select's trigger). */
78
+ export function createTypeahead(): Typeahead {
79
+ let buffer = "";
80
+ let timer: ReturnType<typeof setTimeout> | undefined;
81
+ onCleanup(() => clearTimeout(timer));
82
+ return {
83
+ match(event, texts, from) {
84
+ const char = event.key;
85
+ if (char.length !== 1 || (char === " " && buffer === "")) return undefined;
86
+ buffer += char.toLowerCase();
87
+ clearTimeout(timer);
88
+ timer = setTimeout(() => (buffer = ""), TYPEAHEAD_MS);
89
+ return matchText(texts, from, buffer);
90
+ },
91
+ };
73
92
  }
74
93
 
75
94
  export function createCollection(options: CollectionOptions): Collection {
76
95
  const [own, setOwn] = createSignal<string>();
77
96
  const highlighted = () => own() ?? options.preferred?.()[0];
97
+ const skipped = options.disabledFocusable
98
+ ? (el: HTMLElement) => el.matches(":disabled")
99
+ : isDisabled;
78
100
  const enabledItems = (root: HTMLElement) =>
79
- [...root.querySelectorAll<HTMLElement>(options.itemSelector)].filter((el) => !isDisabled(el));
101
+ [...root.querySelectorAll<HTMLElement>(options.itemSelector)].filter((el) => !skipped(el));
102
+
103
+ // Tracked from focus events, which a sealed widget already receives; reading activeElement asks the page.
104
+ let focused: Element | null = null;
105
+ const focusIsWithin = (root: HTMLElement) =>
106
+ !!focused && focused.isConnected && root.contains(focused);
80
107
 
81
108
  function placeStop(root: HTMLElement | undefined, preferred: readonly string[]) {
82
109
  if (!root || focusIsWithin(root)) return;
@@ -103,23 +130,36 @@ export function createCollection(options: CollectionOptions): Collection {
103
130
  }
104
131
 
105
132
  if (options.focus === "roving") {
106
- effect(
107
- () => [options.root(), options.preferred?.() ?? []] as const,
108
- ([root, preferred]) => placeStop(root, preferred),
109
- );
110
133
  effect(options.root, (root) => {
111
134
  if (!root) return;
135
+ // One read when the listeners attach, so an item focused before them (autofocus) keeps the stop.
136
+ const current = activeOf(root);
137
+ focused = current && root.contains(current) ? current : null;
138
+ const item = focused?.closest<HTMLElement>(options.itemSelector);
139
+ if (item && root.contains(item)) setOwn(keyOf(item));
140
+ const enter = (event: FocusEvent) => {
141
+ focused = event.target instanceof Element ? event.target : null;
142
+ };
112
143
  const leave = (event: FocusEvent) => {
113
- if (!(event.relatedTarget instanceof Node && root.contains(event.relatedTarget))) refresh();
144
+ const next = event.relatedTarget;
145
+ if (next instanceof Element && root.contains(next)) return;
146
+ focused = null;
147
+ refresh();
114
148
  };
149
+ root.addEventListener("focusin", enter);
115
150
  root.addEventListener("focusout", leave);
116
- return () => root.removeEventListener("focusout", leave);
151
+ return () => {
152
+ root.removeEventListener("focusin", enter);
153
+ root.removeEventListener("focusout", leave);
154
+ };
117
155
  });
156
+ effect(
157
+ () => [options.root(), options.preferred?.() ?? []] as const,
158
+ ([root, preferred]) => placeStop(root, preferred),
159
+ );
118
160
  }
119
161
 
120
- let buffer = "";
121
- let timer: ReturnType<typeof setTimeout> | undefined;
122
- onCleanup(() => clearTimeout(timer));
162
+ const typer = createTypeahead();
123
163
 
124
164
  function moveTo(el: HTMLElement | undefined) {
125
165
  if (!el) return;
@@ -139,12 +179,9 @@ export function createCollection(options: CollectionOptions): Collection {
139
179
  }
140
180
 
141
181
  function typeahead(event: KeyboardEvent, items: HTMLElement[], current: number): boolean {
142
- const char = event.key;
143
- if (char.length !== 1 || (char === " " && buffer === "")) return false;
144
- buffer += char.toLowerCase();
145
- clearTimeout(timer);
146
- timer = setTimeout(() => (buffer = ""), TYPEAHEAD_MS);
147
- moveTo(matchText(items, current, buffer));
182
+ const index = typer.match(event, items.map(textOf), current);
183
+ if (index === undefined) return false;
184
+ moveTo(items[index]);
148
185
  return true;
149
186
  }
150
187