@glasshome/ui 1.42.0 → 1.44.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 (153) hide show
  1. package/SPEC.md +8 -5
  2. package/dist/index.js +2 -2
  3. package/dist/lib/anchor-placement.d.ts +4 -0
  4. package/dist/lib/balance-rows.d.ts +10 -4
  5. package/dist/lib/behavior/anchored.d.ts +16 -3
  6. package/dist/lib/behavior/button-root.d.ts +7 -0
  7. package/dist/lib/behavior/collection.d.ts +8 -0
  8. package/dist/lib/behavior/dismiss.d.ts +6 -4
  9. package/dist/lib/behavior/focus-scope.d.ts +9 -1
  10. package/dist/lib/behavior/hide-outside.d.ts +1 -1
  11. package/dist/lib/behavior/layer-stack.d.ts +3 -2
  12. package/dist/lib/behavior/menu.d.ts +62 -0
  13. package/dist/lib/behavior/modal-root.d.ts +2 -0
  14. package/dist/lib/behavior/range.d.ts +44 -0
  15. package/dist/lib/color/color.d.ts +41 -0
  16. package/dist/lib/color/index.d.ts +2 -0
  17. package/dist/lib/color/intl.d.ts +40 -0
  18. package/dist/lib/dock-slot-classes.d.ts +2 -0
  19. package/dist/lib/hold.d.ts +1 -1
  20. package/dist/lib/locale.d.ts +3 -0
  21. package/dist/lib/menu-classes.d.ts +2 -0
  22. package/dist/lib/motion-classes.d.ts +2 -1
  23. package/dist/lib/overlay-classes.d.ts +1 -0
  24. package/dist/lib/scroll-track.d.ts +3 -2
  25. package/dist/lib/section-tokens.d.ts +1 -1
  26. package/dist/lib/segment-classes.d.ts +2 -0
  27. package/dist/lib/shadow-layer.d.ts +0 -45
  28. package/dist/lib/toggle-set.d.ts +44 -0
  29. package/dist/lib/visually-hidden.d.ts +3 -0
  30. package/dist/solid/accordion.d.ts +35 -11
  31. package/dist/solid/avatar.d.ts +8 -4
  32. package/dist/solid/bottom-sheet/bottom-sheet.d.ts +1 -1
  33. package/dist/solid/button.d.ts +5 -4
  34. package/dist/solid/charts.d.ts +1 -1
  35. package/dist/solid/checkbox.d.ts +14 -4
  36. package/dist/solid/collapsible.d.ts +33 -5
  37. package/dist/solid/color-disc.d.ts +1 -1
  38. package/dist/solid/color-slider.d.ts +1 -1
  39. package/dist/solid/color-wheel.d.ts +1 -1
  40. package/dist/solid/context-menu.d.ts +58 -24
  41. package/dist/solid/data-table.d.ts +4 -2
  42. package/dist/solid/dialog-parts.d.ts +8 -8
  43. package/dist/solid/dropdown-menu.d.ts +55 -24
  44. package/dist/solid/empty.d.ts +1 -1
  45. package/dist/solid/form.d.ts +5 -6
  46. package/dist/solid/hover-card.d.ts +65 -9
  47. package/dist/solid/index.d.ts +1 -1
  48. package/dist/solid/index.js +7752 -13493
  49. package/dist/solid/input-group.d.ts +6 -6
  50. package/dist/solid/item.d.ts +1 -1
  51. package/dist/solid/menu-parts.d.ts +175 -24
  52. package/dist/solid/option-card.d.ts +1 -4
  53. package/dist/solid/picker-row.d.ts +2 -0
  54. package/dist/solid/popover.d.ts +77 -12
  55. package/dist/solid/progress.d.ts +11 -3
  56. package/dist/solid/radio-group.d.ts +31 -6
  57. package/dist/solid/responsive-dialog.d.ts +1 -1
  58. package/dist/solid/scroll-area.d.ts +1 -1
  59. package/dist/solid/section-card.d.ts +2 -3
  60. package/dist/solid/select.d.ts +107 -10
  61. package/dist/solid/selection.d.ts +10 -1
  62. package/dist/solid/sheet.d.ts +1 -1
  63. package/dist/solid/sliding-indicator.d.ts +1 -1
  64. package/dist/solid/sonner.d.ts +1 -1
  65. package/dist/solid/toggle.d.ts +11 -3
  66. package/dist/solid/tooltip.d.ts +22 -22
  67. package/dist/tokens/index.js +54 -51
  68. package/dist/{utils-CxAii-e9.js → utils-BbIeGt7o.js} +376 -314
  69. package/package.json +3 -6
  70. package/src/lib/anchor-placement.ts +10 -3
  71. package/src/lib/balance-rows.ts +115 -40
  72. package/src/lib/behavior/anchored.ts +41 -9
  73. package/src/lib/behavior/button-root.tsx +103 -0
  74. package/src/lib/behavior/collection.ts +56 -28
  75. package/src/lib/behavior/dismiss.ts +61 -21
  76. package/src/lib/behavior/focus-scope.ts +75 -9
  77. package/src/lib/behavior/hide-outside.ts +2 -2
  78. package/src/lib/behavior/layer-stack.ts +33 -8
  79. package/src/lib/behavior/menu.ts +462 -0
  80. package/src/lib/behavior/modal-root.tsx +24 -18
  81. package/src/lib/behavior/polymorphic.tsx +1 -1
  82. package/src/lib/behavior/presence.ts +1 -1
  83. package/src/lib/behavior/range.ts +212 -0
  84. package/src/lib/clipboard.ts +2 -3
  85. package/src/lib/color/color.ts +746 -0
  86. package/src/lib/color/index.ts +2 -0
  87. package/src/lib/color/intl.ts +60 -0
  88. package/src/lib/dock-slot-classes.ts +3 -0
  89. package/src/lib/hold.ts +1 -1
  90. package/src/lib/locale.ts +62 -0
  91. package/src/lib/menu-classes.ts +3 -0
  92. package/src/lib/motion-classes.ts +8 -9
  93. package/src/lib/overlay-classes.ts +1 -3
  94. package/src/lib/scroll-track.ts +11 -6
  95. package/src/lib/section-tokens.ts +1 -1
  96. package/src/lib/segment-classes.ts +3 -0
  97. package/src/lib/shadow-layer.ts +0 -118
  98. package/src/lib/toggle-set.ts +133 -0
  99. package/src/lib/visually-hidden.ts +15 -0
  100. package/src/solid/accordion.tsx +294 -20
  101. package/src/solid/area-picker.tsx +4 -4
  102. package/src/solid/avatar.tsx +98 -24
  103. package/src/solid/bottom-sheet/arbitration.ts +4 -11
  104. package/src/solid/bottom-sheet/bottom-sheet.tsx +4 -5
  105. package/src/solid/bottom-sheet/drag-controller.ts +7 -20
  106. package/src/solid/button.tsx +12 -6
  107. package/src/solid/charts.tsx +1 -1
  108. package/src/solid/checkbox.tsx +167 -44
  109. package/src/solid/collapsible.tsx +260 -12
  110. package/src/solid/color-disc.tsx +11 -12
  111. package/src/solid/color-slider.tsx +114 -47
  112. package/src/solid/color-wheel.tsx +137 -39
  113. package/src/solid/context-menu.tsx +285 -94
  114. package/src/solid/data-table.tsx +48 -22
  115. package/src/solid/dialog-parts.tsx +39 -17
  116. package/src/solid/dock.tsx +44 -43
  117. package/src/solid/dropdown-menu.tsx +252 -115
  118. package/src/solid/empty.tsx +1 -1
  119. package/src/solid/entity-selector.tsx +3 -3
  120. package/src/solid/face-toggle.tsx +43 -18
  121. package/src/solid/form.tsx +5 -6
  122. package/src/solid/glyph-tile.tsx +48 -21
  123. package/src/solid/hover-card.tsx +287 -25
  124. package/src/solid/icon-picker.tsx +1 -2
  125. package/src/solid/image-picker.tsx +2 -2
  126. package/src/solid/index.ts +1 -3
  127. package/src/solid/input-group.tsx +2 -2
  128. package/src/solid/item.tsx +1 -1
  129. package/src/solid/menu-parts.tsx +610 -155
  130. package/src/solid/option-card.tsx +7 -13
  131. package/src/solid/picker-row.tsx +5 -1
  132. package/src/solid/popover.tsx +423 -80
  133. package/src/solid/preview-tile.tsx +1 -1
  134. package/src/solid/progress.tsx +84 -15
  135. package/src/solid/radio-group.tsx +271 -34
  136. package/src/solid/responsive-dialog.tsx +1 -1
  137. package/src/solid/schema-form.tsx +1 -6
  138. package/src/solid/scroll-area.tsx +1 -1
  139. package/src/solid/section-card.tsx +3 -4
  140. package/src/solid/select.tsx +939 -95
  141. package/src/solid/selection.tsx +165 -74
  142. package/src/solid/sheet.tsx +1 -1
  143. package/src/solid/slider.tsx +115 -65
  144. package/src/solid/sliding-indicator.tsx +4 -16
  145. package/src/solid/sonner.tsx +1 -1
  146. package/src/solid/tabs.tsx +12 -6
  147. package/src/solid/term.tsx +3 -4
  148. package/src/solid/toggle-group.tsx +34 -101
  149. package/src/solid/toggle.tsx +43 -9
  150. package/src/solid/tooltip.tsx +24 -24
  151. package/src/styles/globals.css +61 -50
  152. package/src/styles/theme.css +12 -6
  153. package/src/tokens/material.ts +11 -2
@@ -0,0 +1,2 @@
1
+ export { type Color, type ColorChannel, parseColor } from "./color.js";
2
+ export { COLOR_INTL_TRANSLATIONS } from "./intl.js";
@@ -0,0 +1,60 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ *
12
+ * Ported from @internationalized/color by way of Kobalte's colors module (MIT).
13
+ */
14
+
15
+ export const COLOR_INTL_TRANSLATIONS = {
16
+ hue: "Hue",
17
+ saturation: "Saturation",
18
+ lightness: "Lightness",
19
+ brightness: "Brightness",
20
+ red: "Red",
21
+ green: "Green",
22
+ blue: "Blue",
23
+ alpha: "Alpha",
24
+ colorName: (lightness: string, chroma: string, hue: string) => `${lightness} ${chroma} ${hue}`,
25
+ transparentColorName: (
26
+ lightness: string,
27
+ chroma: string,
28
+ hue: string,
29
+ percentTransparent: string,
30
+ ) => `${lightness} ${chroma} ${hue}, ${percentTransparent} transparent`,
31
+ "very dark": "very dark",
32
+ dark: "dark",
33
+ light: "light",
34
+ "very light": "very light",
35
+ pale: "pale",
36
+ grayish: "grayish",
37
+ vibrant: "vibrant",
38
+ black: "black",
39
+ white: "white",
40
+ gray: "gray",
41
+ pink: "pink",
42
+ "pink red": "pink red",
43
+ "red orange": "red orange",
44
+ brown: "brown",
45
+ orange: "orange",
46
+ "orange yellow": "orange yellow",
47
+ "brown yellow": "brown yellow",
48
+ yellow: "yellow",
49
+ "yellow green": "yellow green",
50
+ "green cyan": "green cyan",
51
+ cyan: "cyan",
52
+ "cyan blue": "cyan blue",
53
+ "blue purple": "blue purple",
54
+ purple: "purple",
55
+ "purple magenta": "purple magenta",
56
+ magenta: "magenta",
57
+ "magenta pink": "magenta pink",
58
+ };
59
+
60
+ export type ColorIntlTranslations = typeof COLOR_INTL_TRANSLATIONS;
@@ -1,4 +1,7 @@
1
1
  /* The footprint of a dock slot: the dock's items, its add slot, and the slot buttons
2
2
  * that stand for them in a form, so a pick looks like what lands in the dock. */
3
+ /** The rounded-lg in DOCK_SLOT_SHAPE, for a selection flyer that crosses slots. */
4
+ export const DOCK_SLOT_RADIUS = "var(--radius-lg)";
5
+
3
6
  export const DOCK_SLOT_SHAPE =
4
7
  "flex size-11 touch-manipulation items-center justify-center rounded-lg sm:size-12";
package/src/lib/hold.ts CHANGED
@@ -28,7 +28,7 @@ function spendPress(): void {
28
28
 
29
29
  /** A fired hold spends the press: the browser's own long press (text selection, context menu)
30
30
  * must not land on what the hold opened under the finger. Lasts until the release.
31
- * @deprecated use createHold, which also spends the release. */
31
+ * @deprecated `createHold`; removed in 2.0.0. */
32
32
  export function spendLongPress(): void {
33
33
  spendPress();
34
34
  }
@@ -0,0 +1,62 @@
1
+ import { isServer } from "solid-js/web";
2
+
3
+ export type ReadingDirection = "ltr" | "rtl";
4
+
5
+ const RTL_SCRIPTS = new Set([
6
+ "Arab",
7
+ "Syrc",
8
+ "Samr",
9
+ "Mand",
10
+ "Thaa",
11
+ "Mend",
12
+ "Nkoo",
13
+ "Adlm",
14
+ "Rohg",
15
+ "Hebr",
16
+ ]);
17
+
18
+ const RTL_LANGS = new Set([
19
+ "ae",
20
+ "ar",
21
+ "arc",
22
+ "bcc",
23
+ "bqi",
24
+ "ckb",
25
+ "dv",
26
+ "fa",
27
+ "glk",
28
+ "he",
29
+ "ku",
30
+ "mzn",
31
+ "nqo",
32
+ "pnb",
33
+ "ps",
34
+ "sd",
35
+ "ug",
36
+ "ur",
37
+ "yi",
38
+ ]);
39
+
40
+ function directionOf(locale: string): ReadingDirection {
41
+ if (typeof Intl.Locale === "function") {
42
+ const script = new Intl.Locale(locale).maximize().script ?? "";
43
+ return RTL_SCRIPTS.has(script) ? "rtl" : "ltr";
44
+ }
45
+ return RTL_LANGS.has(locale.split("-")[0] ?? "") ? "rtl" : "ltr";
46
+ }
47
+
48
+ function browserLocale(): string {
49
+ const language = typeof navigator === "undefined" ? undefined : navigator.language;
50
+ if (!language) return "en-US";
51
+ try {
52
+ Intl.DateTimeFormat.supportedLocalesOf([language]);
53
+ return language;
54
+ } catch {
55
+ return "en-US";
56
+ }
57
+ }
58
+
59
+ // The server renders en-US, as Kobalte did.
60
+ export const locale: string = isServer ? "en-US" : browserLocale();
61
+
62
+ export const readingDirection: ReadingDirection = directionOf(locale);
@@ -12,6 +12,9 @@ export const MENU_ITEM_BASE =
12
12
  // structural-ok: guards an svg color rule on a sibling data attribute this package always renders, not a class the consumer must also scan
13
13
  "relative flex cursor-default select-none items-center gap-2 rounded-sm py-1.5 text-sm outline-hidden transition-colors data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&_svg:not([class*='size-'])]:size-4 [&:not([data-tone])_svg:not([class*='text-'])]:text-muted-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&>[data-slot=icon]]:shrink-0 [&>[data-slot=icon]]:text-[16px]";
14
14
 
15
+ /** The rounded-sm in MENU_ITEM_BASE, for a selection flyer that crosses rows. */
16
+ export const MENU_ITEM_RADIUS = "var(--radius-sm)";
17
+
15
18
  export const MENU_ITEM_X = "px-2";
16
19
 
17
20
  export const MENU_ITEM = `${MENU_ITEM_BASE} ${MENU_ITEM_X}`;
@@ -8,18 +8,17 @@ const LEAVE =
8
8
  "data-[closed]:animate-out data-[closed]:duration-(--duration-micro) data-[closed]:ease-(--ease-contract)";
9
9
 
10
10
  /* Anchored panels (menu, popover, tooltip, hover card): unfold from the
11
- * trigger edge. Kobalte writes the origin per placement. */
11
+ * trigger edge; each panel sets its origin from data-side or the pointer. */
12
12
  export const OVERLAY_MOTION = `origin-(--kb-popper-content-transform-origin) ${ARRIVE} data-[expanded]:fade-in-0 data-[expanded]:zoom-in-90 ${LEAVE} data-[closed]:fade-out-0 data-[closed]:zoom-out-95`;
13
13
 
14
- /* Field pickers: paired with gutter={0} + anchorToTriggerTop, the clip-path
15
- * reveal is what makes the panel read as the field expanding, not a box
16
- * dropping in. */
14
+ /* Field pickers: the clip-path reveal is what makes the panel read as the
15
+ * field expanding, not a box dropping in. */
17
16
  export const FIELD_MOTION = "data-[closed]:animate-select-out data-[expanded]:animate-select-in";
18
17
 
19
18
  /* Anchored panels that own their trigger (dropdown menu): the panel covers
20
- * the trigger (gutter 0 + anchorToTriggerTop) and unrolls out of the
19
+ * the trigger and unrolls out of the
21
20
  * trigger's box; the content writes --morph-h/--morph-radius from the
22
- * trigger it replaces, --morph-w is kobalte's anchor width. */
21
+ * trigger it replaces, --morph-w is the anchor width the menu writes. */
23
22
  export const MORPH_MOTION =
24
23
  "[--morph-w:var(--kb-popper-anchor-width)] min-w-[var(--kb-popper-anchor-width)] data-[expanded]:animate-morph-in data-[closed]:animate-morph-out";
25
24
 
@@ -28,8 +27,7 @@ export const MORPH_MOTION =
28
27
  export const ANCHORED_MOTION =
29
28
  "data-[expanded]:animate-anchored-in data-[closed]:animate-morph-out";
30
29
 
31
- /* Children of a panel arrive staggered through the `gh-stagger` class
32
- * (globals.css): one door, no per-row index, mount-only. */
30
+ /** @deprecated Rows arrive with their panel; the class does nothing. Removed in 2.0.0. */
33
31
  export const STAGGER = "gh-stagger";
34
32
 
35
33
  /* An exit that holds its last frame: a closed surface still mounted (kept, or waiting
@@ -85,7 +83,7 @@ export const TRAVEL_MOTION =
85
83
  /* The longest exit (delay plus duration) each door's data-[closed] classes run;
86
84
  * tests/lib/motion-exit.test.tsx holds these to the computed styles. */
87
85
  export const MOTION_EXIT_MS: Record<
88
- "overlay" | "modal" | "anchored" | "scrim" | "field" | "sheet",
86
+ "overlay" | "modal" | "anchored" | "scrim" | "field" | "sheet" | "disclosure",
89
87
  number
90
88
  > = {
91
89
  overlay: 120,
@@ -94,4 +92,5 @@ export const MOTION_EXIT_MS: Record<
94
92
  scrim: 120,
95
93
  field: 120,
96
94
  sheet: 500,
95
+ disclosure: 200,
97
96
  };
@@ -40,9 +40,7 @@ export const FLOATING_PANEL_SURFACE = `${OVERLAY_SURFACE} ${PANEL_TAIL}`;
40
40
  * menus add p-1, popovers p-4, pickers none. */
41
41
  export const FLOATING_PANEL = `${OVERLAY_SURFACE} relative ${PANEL_TAIL}`;
42
42
 
43
- /* Zero-height rect on the trigger's top edge. With gutter 0 and the panel
44
- * width bound to --kb-popper-anchor-width, the panel covers the trigger and
45
- * grows downward from it. */
43
+ /** @deprecated Ignored; removed in 2.0.0. */
46
44
  export const anchorToTriggerTop = (anchor?: HTMLElement) => {
47
45
  const r = anchor?.getBoundingClientRect();
48
46
  return r
@@ -1,3 +1,6 @@
1
+ import type { Accessor } from "solid-js";
2
+ import { effect } from "./behavior/solid.js";
3
+
1
4
  /* A row of segments that scrolls sideways (tabs, a scrolling toggle group):
2
5
  * the selected segment stays in view and a clipped edge fades out, so the row
3
6
  * reads as "more this way" instead of ending mid-label. */
@@ -12,9 +15,13 @@ function writeClip(list: HTMLElement): void {
12
15
  list.toggleAttribute("data-clip-end", end > 1);
13
16
  }
14
17
 
15
- /** Centres the segment matching `selected` whenever it changes; drives
18
+ /** Centres the segment matching `selected` whenever `selection` changes; drives
16
19
  * scrollLeft, since scrollIntoView would scroll the page with it. */
17
- export function trackScroll(list: HTMLElement, selected: string, attribute: string): () => void {
20
+ export function trackScroll(
21
+ list: HTMLElement,
22
+ selected: string,
23
+ selection: Accessor<unknown>,
24
+ ): () => void {
18
25
  const reveal = () => {
19
26
  writeClip(list);
20
27
  if (list.scrollWidth <= list.clientWidth) return;
@@ -29,18 +36,16 @@ export function trackScroll(list: HTMLElement, selected: string, attribute: stri
29
36
  const clip = () => writeClip(list);
30
37
  // A frame late: on mount the track has no width yet, and during a press
31
38
  // the focus scroll cancels a smooth scroll started in the same task.
32
- let frame = requestAnimationFrame(reveal);
33
- const observer = new MutationObserver(() => {
39
+ let frame = 0;
40
+ effect(selection, () => {
34
41
  cancelAnimationFrame(frame);
35
42
  frame = requestAnimationFrame(reveal);
36
43
  });
37
- observer.observe(list, { attributes: true, attributeFilter: [attribute], subtree: true });
38
44
  const resize = new ResizeObserver(clip);
39
45
  resize.observe(list);
40
46
  list.addEventListener("scroll", clip, { passive: true });
41
47
  return () => {
42
48
  cancelAnimationFrame(frame);
43
- observer.disconnect();
44
49
  resize.disconnect();
45
50
  list.removeEventListener("scroll", clip);
46
51
  };
@@ -9,5 +9,5 @@ export const SECTION_PADDING = "p-3";
9
9
  * only for the deprecated class below. */
10
10
  const LEGACY_SECTION_ROW_SURFACE = `${SECTION_INNER_RADIUS} border border-border/60 bg-card/60`;
11
11
 
12
- /** @deprecated Render `SectionRow`, or compose `SECTION_ROW_SURFACE` from card-classes. */
12
+ /** @deprecated `SectionRow`, or `SECTION_ROW_SURFACE` from card-classes; removed in 2.0.0. */
13
13
  export const SECTION_ROW_CLASS = `${LEGACY_SECTION_ROW_SURFACE} ${SECTION_PADDING}`;
@@ -4,4 +4,7 @@ import { CONTROL_H, FOCUS_RING } from "./input-classes.js";
4
4
  * toggleVariants' base): both sit inside a TRACK_SURFACE track and pick one
5
5
  * of a few siblings, so they share height, type and focus ring. Callers still
6
6
  * layer their own variant/size classes on top through cn()'s tailwind-merge. */
7
+ /** The rounded-md of a segment (SEGMENT_ITEM, a toggle-group item), for a selection flyer that crosses segments. */
8
+ export const SEGMENT_RADIUS = "var(--radius-md)";
9
+
7
10
  export const SEGMENT_ITEM = `inline-flex items-center justify-center gap-1.5 whitespace-nowrap rounded-md ${CONTROL_H.sm} px-3 font-medium text-sm outline-none transition-glass active:scale-[0.97] disabled:pointer-events-none disabled:opacity-50 ${FOCUS_RING}`;
@@ -1,121 +1,3 @@
1
- import { type Accessor, createContext, createSignal, onCleanup, useContext } from "solid-js";
2
- import { createDismiss } from "./behavior/dismiss.js";
3
- import { effect } from "./behavior/solid.js";
4
- import { focusWithoutTracking } from "./behavior/focus-scope.js";
5
- import { registerLayer } from "./behavior/layer-stack.js";
6
-
7
1
  /** True for a node inside a shadow root, where page-level layer logic sees only the root's host. */
8
2
  export const inShadowRoot = (node: Node | undefined): boolean =>
9
3
  typeof ShadowRoot !== "undefined" && node?.getRootNode() instanceof ShadowRoot;
10
-
11
- export const ReportCovered = createContext<(covered: Accessor<boolean>) => void>();
12
-
13
- /** True while a layer of this Kobalte root sits under an owned modal; the root drops `modal` then. */
14
- export function createCoveredRoot(): {
15
- covered: Accessor<boolean>;
16
- report: (covered: Accessor<boolean>) => void;
17
- } {
18
- const [reporters, setReporters] = createSignal<readonly Accessor<boolean>[]>([]);
19
- return {
20
- covered: () => reporters().some((covered) => covered()),
21
- report: (covered) => {
22
- setReporters((list) => [...list, covered]);
23
- onCleanup(() => setReporters((list) => list.filter((other) => other !== covered)));
24
- },
25
- };
26
- }
27
-
28
- type OutsideEvent = { target: EventTarget | null; preventDefault(): void };
29
- type AutoFocusEvent = { preventDefault(): void };
30
-
31
- export interface LayerHandlers {
32
- onInteractOutside: (e: OutsideEvent) => void;
33
- onCloseAutoFocus: (e: AutoFocusEvent) => void;
34
- /** The shadow root's host while the layer is in one: what the page sees as the target of anything inside. */
35
- host: () => Element | undefined;
36
- /** An owned modal sits above the layer: the modal owns every press and focus until it closes. */
37
- covered: Accessor<boolean>;
38
- }
39
-
40
- interface ShadowLayerOptions {
41
- content: Accessor<HTMLElement | undefined>;
42
- open: Accessor<boolean>;
43
- /** Absent for layers that close on pointer leave (tooltips, hover cards): they never join the stack. */
44
- dismiss?: () => void;
45
- /** A press on it is the trigger's own toggle, never an outside press. */
46
- trigger?: Accessor<Element | undefined>;
47
- /** Menus: any press outside the layer and the layers above it dismisses it, newest or not, as Kobalte's menus do. */
48
- tree?: boolean;
49
- consumer: {
50
- onInteractOutside?: (e: never) => void;
51
- onCloseAutoFocus?: (e: never) => void;
52
- };
53
- }
54
-
55
- /**
56
- * Kobalte decides "outside" from the page, where a press or focus inside a shadow root is one on
57
- * the root's host, so a layer portaled into a shadow root would dismiss on every press inside it.
58
- * The handlers ignore those; while the layer is open in a shadow root, a press elsewhere in that
59
- * root dismisses the newest layer from a listener on the root itself. Outside a shadow root the
60
- * handlers only pass the consumer's own through.
61
- */
62
- export function shadowLayer(options: ShadowLayerOptions): LayerHandlers {
63
- let dismissedIn: ShadowRoot | undefined;
64
- const handle = registerLayer({
65
- element: options.content,
66
- open: options.open,
67
- kind: !options.dismiss ? "tooltip" : options.tree ? "menu" : "popover",
68
- escape: () => false,
69
- readsOwnEscape: true,
70
- dismiss: options.dismiss,
71
- trigger: options.trigger,
72
- });
73
- effect(options.open, (open) => {
74
- if (open) dismissedIn = undefined;
75
- });
76
- const covered = () => options.open() && handle.above().some((layer) => layer.kind === "modal");
77
- useContext(ReportCovered)?.(covered);
78
- createDismiss({
79
- layer: handle,
80
- outsidePress: () => !!options.dismiss && inShadowRoot(options.content()),
81
- onDismiss: () => {
82
- const root = options.content()?.getRootNode();
83
- dismissedIn = root instanceof ShadowRoot ? root : undefined;
84
- options.dismiss?.();
85
- // The caller refused the close (a controlled `open`): the next close is not this one.
86
- if (options.open()) dismissedIn = undefined;
87
- },
88
- });
89
- const shadowHost = () => {
90
- const root = options.content()?.getRootNode();
91
- return typeof ShadowRoot !== "undefined" && root instanceof ShadowRoot ? root.host : undefined;
92
- };
93
- return {
94
- host: shadowHost,
95
- covered,
96
- onInteractOutside: (e) => {
97
- const host = shadowHost();
98
- if (covered() || (host && e.target === host)) {
99
- e.preventDefault();
100
- return;
101
- }
102
- options.consumer.onInteractOutside?.(e as never);
103
- },
104
- onCloseAutoFocus: (e) => {
105
- const root = dismissedIn;
106
- dismissedIn = undefined;
107
- if (root) {
108
- // Dismissed by a press elsewhere in the root: focus stays where that press put it, even
109
- // for the Kobalte layers that focus their trigger past preventDefault.
110
- e.preventDefault();
111
- const kept = root.activeElement;
112
- queueMicrotask(() => {
113
- if (kept instanceof HTMLElement && kept.isConnected && root.activeElement !== kept) {
114
- focusWithoutTracking(kept);
115
- }
116
- });
117
- }
118
- options.consumer.onCloseAutoFocus?.(e as never);
119
- },
120
- };
121
- }
@@ -0,0 +1,133 @@
1
+ import { type Accessor, createSignal, createUniqueId, onCleanup } from "solid-js";
2
+ import { type Collection, createCollection } from "./behavior/collection.js";
3
+ import { focusWithoutTracking, trackFocus } from "./behavior/focus-scope.js";
4
+ import { effect } from "./behavior/solid.js";
5
+
6
+ type Orientation = "horizontal" | "vertical";
7
+
8
+ export interface ToggleSet {
9
+ id: Accessor<string>;
10
+ orientation: Accessor<Orientation>;
11
+ isPressed: (value: string) => boolean;
12
+ toggle: (value: string) => void;
13
+ collection: Collection;
14
+ /** Changes as items mount and unmount. */
15
+ members: Accessor<number>;
16
+ joined: (delta: 1 | -1) => void;
17
+ }
18
+
19
+ /** The shared core of a row of toggles: which are pressed, what a press does, and the roving focus between them. */
20
+ export function createToggleSet(options: {
21
+ root: Accessor<HTMLElement | undefined>;
22
+ itemSelector: string;
23
+ values: Accessor<readonly string[]>;
24
+ setValues: (next: string[]) => void;
25
+ multiple: Accessor<boolean>;
26
+ orientation?: Accessor<Orientation>;
27
+ id?: Accessor<string | undefined>;
28
+ disabledFocusable?: boolean;
29
+ }): ToggleSet {
30
+ const orientation = options.orientation ?? (() => "horizontal" as const);
31
+ const fallbackId = `group-${createUniqueId()}`;
32
+ const [members, setMembers] = createSignal(0);
33
+ const collection = createCollection({
34
+ root: options.root,
35
+ itemSelector: options.itemSelector,
36
+ orientation,
37
+ focus: "roving",
38
+ loop: () => true,
39
+ dir: () => (options.root()?.closest("[dir]")?.getAttribute("dir") === "rtl" ? "rtl" : "ltr"),
40
+ preferred: options.values,
41
+ disabledFocusable: options.disabledFocusable,
42
+ });
43
+ return {
44
+ id: () => options.id?.() ?? fallbackId,
45
+ orientation,
46
+ isPressed: (value) => options.values().includes(value),
47
+ toggle: (value) => {
48
+ const current = options.values();
49
+ const pressed = current.includes(value);
50
+ if (options.multiple()) {
51
+ options.setValues(pressed ? current.filter((v) => v !== value) : [...current, value]);
52
+ } else options.setValues(pressed ? [] : [value]);
53
+ },
54
+ collection,
55
+ members,
56
+ joined: (delta) => setMembers((n) => n + delta),
57
+ };
58
+ }
59
+
60
+ /** One toggle in a set: its ref, its pressed and roving-focus attributes, and its press, bound on the element so it works inside a closed shadow root. */
61
+ export function createToggleItem(
62
+ set: ToggleSet,
63
+ item: { value: Accessor<string>; disabled: Accessor<boolean>; locked?: Accessor<boolean> },
64
+ ) {
65
+ const id = `${set.id()}-item-${createUniqueId()}`;
66
+ const [el, setEl] = createSignal<HTMLElement>();
67
+ effect(item.disabled, () => set.collection.refresh());
68
+ onCleanup(() => set.collection.refresh());
69
+ const pressed = () => set.isPressed(item.value());
70
+
71
+ effect(el, (node) => {
72
+ if (!node) return;
73
+ set.joined(1);
74
+ let mousePressed = false;
75
+ const focused = trackFocus(node);
76
+ const choose = () => {
77
+ if (item.disabled() || item.locked?.()) return;
78
+ set.toggle(item.value());
79
+ set.collection.highlight(item.value(), "pointer");
80
+ };
81
+ // A mouse toggles on press, so focus lands on an item already toggled and is announced so.
82
+ const pointerdown = (event: PointerEvent) => {
83
+ mousePressed = event.pointerType === "mouse" && event.button === 0;
84
+ if (mousePressed) choose();
85
+ };
86
+ const click = (event: MouseEvent) => {
87
+ // WebKit leaves a clicked button unfocused.
88
+ if (!item.disabled() && !focused.has()) focusWithoutTracking(node);
89
+ if (event.detail === 0 || !mousePressed) choose();
90
+ mousePressed = false;
91
+ };
92
+ const focus = () => set.collection.highlight(item.value(), "pointer");
93
+ node.addEventListener("pointerdown", pointerdown);
94
+ node.addEventListener("click", click);
95
+ node.addEventListener("focus", focus);
96
+ return () => {
97
+ set.joined(-1);
98
+ focused.stop();
99
+ node.removeEventListener("pointerdown", pointerdown);
100
+ node.removeEventListener("click", click);
101
+ node.removeEventListener("focus", focus);
102
+ };
103
+ });
104
+
105
+ return {
106
+ ref: setEl,
107
+ pressed,
108
+ props: {
109
+ id,
110
+ get tabIndex() {
111
+ return set.collection.tabIndex(item.value());
112
+ },
113
+ get disabled() {
114
+ return item.disabled();
115
+ },
116
+ get "aria-pressed"() {
117
+ return pressed();
118
+ },
119
+ get "data-pressed"() {
120
+ return pressed() ? "" : undefined;
121
+ },
122
+ get "data-disabled"() {
123
+ return item.disabled() ? "" : undefined;
124
+ },
125
+ get "data-key"() {
126
+ return item.value();
127
+ },
128
+ get "data-orientation"() {
129
+ return set.orientation();
130
+ },
131
+ },
132
+ };
133
+ }
@@ -0,0 +1,15 @@
1
+ import type { JSX } from "solid-js";
2
+
3
+ /** Kept for assistive tech and form submission, gone from sight and layout. */
4
+ export const VISUALLY_HIDDEN: JSX.CSSProperties = {
5
+ border: "0",
6
+ clip: "rect(0, 0, 0, 0)",
7
+ "clip-path": "inset(50%)",
8
+ height: "1px",
9
+ margin: "0 -1px -1px 0",
10
+ overflow: "hidden",
11
+ padding: "0",
12
+ position: "absolute",
13
+ width: "1px",
14
+ "white-space": "nowrap",
15
+ };