@glasshome/ui 1.41.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 (188) hide show
  1. package/SPEC.md +5 -0
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js +3 -3
  4. package/dist/lib/anchor-placement.d.ts +34 -0
  5. package/dist/lib/balance-rows.d.ts +10 -4
  6. package/dist/lib/behavior/anchored.d.ts +26 -0
  7. package/dist/lib/behavior/button-root.d.ts +7 -0
  8. package/dist/lib/behavior/collection.d.ts +32 -0
  9. package/dist/lib/behavior/controllable.d.ts +8 -0
  10. package/dist/lib/behavior/dismiss.d.ts +13 -0
  11. package/dist/lib/behavior/focus-scope.d.ts +24 -0
  12. package/dist/lib/behavior/hide-outside.d.ts +7 -0
  13. package/dist/lib/behavior/layer-stack.d.ts +34 -0
  14. package/dist/lib/behavior/menu.d.ts +59 -0
  15. package/dist/lib/behavior/modal-root.d.ts +68 -0
  16. package/dist/lib/behavior/polymorphic.d.ts +6 -0
  17. package/dist/lib/behavior/presence.d.ts +18 -0
  18. package/dist/lib/behavior/range.d.ts +44 -0
  19. package/dist/lib/behavior/solid.d.ts +13 -0
  20. package/dist/lib/color/color.d.ts +41 -0
  21. package/dist/lib/color/index.d.ts +2 -0
  22. package/dist/lib/color/intl.d.ts +40 -0
  23. package/dist/lib/dock-slot-classes.d.ts +2 -0
  24. package/dist/lib/hold.d.ts +1 -1
  25. package/dist/lib/locale.d.ts +3 -0
  26. package/dist/lib/menu-classes.d.ts +2 -0
  27. package/dist/lib/motion-classes.d.ts +5 -2
  28. package/dist/lib/overlay-classes.d.ts +4 -0
  29. package/dist/lib/page-cover.d.ts +8 -0
  30. package/dist/lib/scroll-track.d.ts +3 -2
  31. package/dist/lib/section-tokens.d.ts +1 -1
  32. package/dist/lib/segment-classes.d.ts +2 -0
  33. package/dist/lib/shadow-layer.d.ts +0 -43
  34. package/dist/lib/toggle-set.d.ts +44 -0
  35. package/dist/lib/visually-hidden.d.ts +3 -0
  36. package/dist/solid/accordion.d.ts +35 -11
  37. package/dist/solid/alert-dialog.d.ts +5 -8
  38. package/dist/solid/avatar.d.ts +8 -4
  39. package/dist/solid/bottom-sheet/bottom-sheet.d.ts +2 -2
  40. package/dist/solid/bottom-sheet/constants.d.ts +1 -1
  41. package/dist/solid/button.d.ts +5 -4
  42. package/dist/solid/charts.d.ts +1 -1
  43. package/dist/solid/checkbox.d.ts +14 -4
  44. package/dist/solid/collapsible.d.ts +33 -5
  45. package/dist/solid/color-disc.d.ts +1 -1
  46. package/dist/solid/color-slider.d.ts +1 -1
  47. package/dist/solid/color-wheel.d.ts +1 -1
  48. package/dist/solid/context-menu.d.ts +58 -24
  49. package/dist/solid/dialog-parts.d.ts +53 -16
  50. package/dist/solid/dialog.d.ts +7 -10
  51. package/dist/solid/dropdown-menu.d.ts +55 -24
  52. package/dist/solid/empty.d.ts +1 -1
  53. package/dist/solid/form.d.ts +5 -6
  54. package/dist/solid/hover-card.d.ts +65 -9
  55. package/dist/solid/index.d.ts +5 -2
  56. package/dist/solid/index.js +8671 -14317
  57. package/dist/solid/input-group.d.ts +6 -6
  58. package/dist/solid/item.d.ts +1 -1
  59. package/dist/solid/menu-parts.d.ts +175 -24
  60. package/dist/solid/option-card.d.ts +1 -4
  61. package/dist/solid/picker-row.d.ts +2 -0
  62. package/dist/solid/popover.d.ts +77 -12
  63. package/dist/solid/progress.d.ts +11 -3
  64. package/dist/solid/radio-group.d.ts +31 -6
  65. package/dist/solid/responsive-dialog.d.ts +3 -1
  66. package/dist/solid/scroll-area.d.ts +1 -1
  67. package/dist/solid/section-card.d.ts +2 -3
  68. package/dist/solid/select.d.ts +107 -10
  69. package/dist/solid/selection.d.ts +10 -1
  70. package/dist/solid/sheet.d.ts +6 -9
  71. package/dist/solid/sliding-indicator.d.ts +1 -1
  72. package/dist/solid/sonner.d.ts +1 -1
  73. package/dist/solid/stale-veil.d.ts +7 -0
  74. package/dist/solid/tabs.d.ts +28 -9
  75. package/dist/solid/toggle-group.d.ts +30 -5
  76. package/dist/solid/toggle.d.ts +11 -3
  77. package/dist/solid/tooltip.d.ts +71 -9
  78. package/dist/tokens/index.js +54 -51
  79. package/dist/{utils-CGN4X-2O.js → utils-3Codx-NZ.js} +848 -618
  80. package/package.json +6 -7
  81. package/src/index.ts +1 -0
  82. package/src/lib/anchor-placement.ts +77 -4
  83. package/src/lib/balance-rows.ts +115 -40
  84. package/src/lib/behavior/anchored.ts +106 -0
  85. package/src/lib/behavior/button-root.tsx +103 -0
  86. package/src/lib/behavior/collection.ts +218 -0
  87. package/src/lib/behavior/controllable.ts +22 -0
  88. package/src/lib/behavior/dismiss.ts +185 -0
  89. package/src/lib/behavior/focus-scope.ts +227 -0
  90. package/src/lib/behavior/hide-outside.ts +85 -0
  91. package/src/lib/behavior/layer-stack.ts +150 -0
  92. package/src/lib/behavior/menu.ts +470 -0
  93. package/src/lib/behavior/modal-root.tsx +452 -0
  94. package/src/lib/behavior/polymorphic.tsx +30 -0
  95. package/src/lib/behavior/presence.ts +89 -0
  96. package/src/lib/behavior/range.ts +212 -0
  97. package/src/lib/behavior/solid.ts +30 -0
  98. package/src/lib/clipboard.ts +4 -4
  99. package/src/lib/color/color.ts +746 -0
  100. package/src/lib/color/index.ts +2 -0
  101. package/src/lib/color/intl.ts +60 -0
  102. package/src/lib/dock-slot-classes.ts +3 -0
  103. package/src/lib/hold.ts +1 -1
  104. package/src/lib/locale.ts +62 -0
  105. package/src/lib/menu-classes.ts +3 -0
  106. package/src/lib/motion-classes.ts +33 -8
  107. package/src/lib/overlay-classes.ts +8 -3
  108. package/src/lib/page-cover.ts +47 -0
  109. package/src/lib/scroll-track.ts +11 -6
  110. package/src/lib/section-tokens.ts +1 -1
  111. package/src/lib/segment-classes.ts +3 -0
  112. package/src/lib/shadow-layer.ts +0 -136
  113. package/src/lib/toggle-set.ts +133 -0
  114. package/src/lib/visually-hidden.ts +15 -0
  115. package/src/solid/accordion.tsx +294 -20
  116. package/src/solid/alert-dialog.tsx +36 -85
  117. package/src/solid/area-picker.tsx +5 -3
  118. package/src/solid/avatar.tsx +98 -24
  119. package/src/solid/badge.tsx +5 -5
  120. package/src/solid/bottom-sheet/bottom-sheet.tsx +73 -129
  121. package/src/solid/bottom-sheet/constants.ts +3 -1
  122. package/src/solid/bottom-sheet/drag-controller.ts +11 -0
  123. package/src/solid/breadcrumb.tsx +4 -4
  124. package/src/solid/button-group.tsx +4 -4
  125. package/src/solid/button.tsx +12 -6
  126. package/src/solid/card.tsx +5 -4
  127. package/src/solid/charts.tsx +1 -1
  128. package/src/solid/checkbox.tsx +167 -44
  129. package/src/solid/collapsible.tsx +260 -12
  130. package/src/solid/color-disc.tsx +11 -12
  131. package/src/solid/color-slider.tsx +114 -47
  132. package/src/solid/color-wheel.tsx +137 -39
  133. package/src/solid/context-menu.tsx +287 -86
  134. package/src/solid/dialog-parts.tsx +135 -46
  135. package/src/solid/dialog.tsx +44 -76
  136. package/src/solid/dock-edit.tsx +5 -4
  137. package/src/solid/dock.tsx +6 -5
  138. package/src/solid/dropdown-menu.tsx +252 -107
  139. package/src/solid/empty.tsx +1 -1
  140. package/src/solid/entity-selector.tsx +5 -3
  141. package/src/solid/face-toggle.tsx +43 -18
  142. package/src/solid/form.tsx +9 -9
  143. package/src/solid/glyph-tile.tsx +48 -21
  144. package/src/solid/hover-card.tsx +288 -21
  145. package/src/solid/icon-picker.tsx +2 -1
  146. package/src/solid/image-picker.tsx +6 -5
  147. package/src/solid/index.ts +5 -4
  148. package/src/solid/input-group.tsx +7 -5
  149. package/src/solid/input-otp.tsx +2 -1
  150. package/src/solid/item.tsx +5 -5
  151. package/src/solid/logo.tsx +5 -4
  152. package/src/solid/menu-parts.tsx +619 -145
  153. package/src/solid/option-card.tsx +6 -10
  154. package/src/solid/picker-row.tsx +10 -5
  155. package/src/solid/picker-search.tsx +2 -1
  156. package/src/solid/popover.tsx +424 -71
  157. package/src/solid/preview-tile.tsx +1 -1
  158. package/src/solid/progress.tsx +84 -15
  159. package/src/solid/radio-group.tsx +271 -34
  160. package/src/solid/responsive-dialog.tsx +10 -10
  161. package/src/solid/schema-form.tsx +1 -6
  162. package/src/solid/scroll-area.tsx +1 -1
  163. package/src/solid/section-card.tsx +3 -4
  164. package/src/solid/select.tsx +950 -95
  165. package/src/solid/selection.tsx +142 -66
  166. package/src/solid/sheet.tsx +48 -80
  167. package/src/solid/slider.tsx +115 -65
  168. package/src/solid/sliding-indicator.tsx +1 -1
  169. package/src/solid/sonner.tsx +1 -1
  170. package/src/solid/spotlight.tsx +23 -11
  171. package/src/solid/stale-veil.tsx +20 -0
  172. package/src/solid/switch.tsx +8 -6
  173. package/src/solid/tabs.tsx +377 -52
  174. package/src/solid/term.tsx +3 -4
  175. package/src/solid/toggle-group.tsx +237 -95
  176. package/src/solid/toggle.tsx +43 -9
  177. package/src/solid/tooltip.tsx +285 -26
  178. package/src/styles/globals.css +64 -10
  179. package/src/styles/theme.css +12 -4
  180. package/src/tokens/material.ts +11 -2
  181. package/dist/lib/when-animations-settle.d.ts +0 -4
  182. package/dist/solid/bottom-sheet/focus-trap.d.ts +0 -6
  183. package/dist/solid/bottom-sheet/scroll-lock.d.ts +0 -2
  184. package/dist/solid/bottom-sheet/sheet-motion.d.ts +0 -1
  185. package/src/lib/when-animations-settle.ts +0 -12
  186. package/src/solid/bottom-sheet/focus-trap.ts +0 -63
  187. package/src/solid/bottom-sheet/scroll-lock.ts +0 -66
  188. package/src/solid/bottom-sheet/sheet-motion.ts +0 -4
@@ -1,17 +1,19 @@
1
- import { Dialog as DialogPrimitive, useDialogContext } from "@kobalte/core/dialog";
2
1
  import { usePortalMount } from "./portal-mount.js";
3
2
  import type { VariantProps } from "cva";
4
3
  import {
5
4
  type Component,
6
5
  type ComponentProps,
7
6
  createEffect,
7
+ createMemo,
8
8
  createSignal,
9
+ type JSX,
9
10
  onCleanup,
10
11
  type ParentComponent,
11
12
  splitProps,
12
13
  } from "solid-js";
13
14
  import { overAnchor } from "../lib/anchor-placement.js";
14
- import { inShadowRoot, shadowLayer } from "../lib/shadow-layer.js";
15
+ import { ModalFrame, useDialogContext } from "../lib/behavior/modal-root.js";
16
+ import { MOTION_EXIT_MS } from "../lib/motion-classes.js";
15
17
  import { buttonVariants } from "../lib/button-variants.js";
16
18
  import { cn } from "../lib/utils.js";
17
19
  import {
@@ -19,33 +21,30 @@ import {
19
21
  createModalDismiss,
20
22
  createModalHead,
21
23
  createModalParts,
24
+ createModalRootSkin,
25
+ createModalTextParts,
22
26
  MODAL_ANCHOR,
23
- MODAL_DESCRIPTION,
24
27
  MODAL_PANEL,
25
28
  MODAL_SCRIM,
26
- MODAL_TITLE,
27
29
  MODAL_WIDTH,
30
+ type ModalContentProps,
28
31
  type ModalDismissProps,
29
- ModalScrollLock,
32
+ ModalCover,
30
33
  type ModalSize,
31
34
  } from "./dialog-parts.js";
32
35
 
33
- /* Kobalte's own scroll lock is off everywhere: one refcounted lock
34
- * (bottom-sheet/scroll-lock.ts) serves every modal family, so a nested modal
35
- * cannot release the page early. */
36
- const Dialog: ParentComponent<ComponentProps<typeof DialogPrimitive>> = (props) => {
37
- const mount = usePortalMount();
38
- return <DialogPrimitive modal={!inShadowRoot(mount)} {...props} preventScroll={false} />;
39
- };
36
+ const Dialog = createModalRootSkin("dialog");
40
37
 
41
- const DialogTrigger: Component<ComponentProps<typeof DialogPrimitive.Trigger>> = (props) => (
42
- <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
43
- );
38
+ const {
39
+ Trigger: DialogTrigger,
40
+ Title: DialogTitle,
41
+ Description: DialogDescription,
42
+ } = createModalTextParts("dialog");
44
43
 
45
44
  type DialogCloseProps = ModalDismissProps &
46
45
  Partial<Pick<VariantProps<typeof buttonVariants>, "variant" | "size">>;
47
46
 
48
- const DialogDismiss = createModalDismiss("dialog-close");
47
+ const DialogDismiss = createModalDismiss("dialog-close", useDialogContext);
49
48
 
50
49
  /** Already the outline button, so `as` is for a non-button element (a link),
51
50
  * never for `Button`: two buttonVariants() calls leave tone knobs side by side
@@ -65,7 +64,7 @@ const DialogClose: ParentComponent<DialogCloseProps> = (props) => {
65
64
  );
66
65
  };
67
66
 
68
- type DialogContentProps = ComponentProps<typeof DialogPrimitive.Content> & {
67
+ type DialogContentProps = ModalContentProps & {
69
68
  size?: ModalSize;
70
69
  /** Names a panel that has no `DialogTitle`. A registered Title wins. */
71
70
  ariaLabel?: string;
@@ -92,97 +91,66 @@ const DialogContent: ParentComponent<DialogContentProps> = (props) => {
92
91
  "anchor",
93
92
  "style",
94
93
  "ref",
95
- "onInteractOutside",
96
- "onCloseAutoFocus",
97
- "onOpenAutoFocus",
94
+ "onFocusOutside",
95
+ "forceMount",
98
96
  ]);
99
- const context = useDialogContext();
97
+ const ctx = useDialogContext();
100
98
  const [viewport, setViewport] = createSignal(0);
101
- const [panel, setPanel] = createSignal<HTMLElement>();
102
- const layer = shadowLayer({
103
- content: panel,
104
- open: () => context.isOpen(),
105
- dismiss: () => context.close(),
106
- trapsFocus: true,
107
- consumer: local,
108
- });
109
99
  const [height, setHeight] = createSignal<number>();
110
100
  createEffect(() => {
111
- if (!local.anchor || !context.isOpen()) return;
101
+ if (!local.anchor || !ctx.isOpen()) return;
112
102
  const measure = () => setViewport((n) => n + 1);
113
103
  window.addEventListener("resize", measure);
114
104
  onCleanup(() => window.removeEventListener("resize", measure));
115
105
  });
116
106
  createEffect(() => {
117
- const el = panel();
107
+ const el = ctx.panel();
118
108
  if (!local.anchor || !el) return;
119
109
  setHeight(el.offsetHeight);
120
110
  const observer = new ResizeObserver(() => setHeight(el.offsetHeight));
121
111
  observer.observe(el);
122
112
  onCleanup(() => observer.disconnect());
123
113
  });
124
- const placement = () => {
114
+ const placement = createMemo<JSX.CSSProperties | undefined>((last) => {
115
+ if (!ctx.isOpen()) return last;
125
116
  viewport();
126
117
  const a = local.anchor;
127
118
  return a ? overAnchor(a, ANCHORED_WIDTH[local.size ?? "lg"], height()) : undefined;
128
- };
119
+ });
129
120
  return (
130
- <DialogPrimitive.Portal mount={usePortalMount()}>
131
- <ModalScrollLock />
132
- <DialogPrimitive.Overlay data-slot="dialog-overlay" class={MODAL_SCRIM} />
133
- <DialogPrimitive.Content
134
- ref={(el: HTMLElement) => {
135
- setPanel(el);
136
- if (typeof local.ref === "function") local.ref(el);
137
- }}
138
- data-slot="dialog-content"
139
- role="dialog"
140
- aria-label={context.titleId() ? undefined : local.ariaLabel}
141
- class={cn(
121
+ <>
122
+ <ModalCover panel={ctx.panel} open={ctx.isOpen} dormant={ctx.dormant} />
123
+ <ModalFrame
124
+ slot="dialog"
125
+ mount={usePortalMount()}
126
+ exitMs={local.anchor ? MOTION_EXIT_MS.anchored : MOTION_EXIT_MS.modal}
127
+ scrimClass={MODAL_SCRIM}
128
+ panelClass={cn(
142
129
  local.anchor ? ANCHORED_PANEL : MODAL_PANEL,
143
130
  !local.anchor && MODAL_WIDTH[local.size ?? "lg"],
144
131
  !local.anchor && MODAL_ANCHOR[local.size ?? "lg"],
145
132
  local.class,
146
133
  )}
147
- style={{ ...(typeof local.style === "object" ? local.style : {}), ...placement() }}
148
- onInteractOutside={layer.onInteractOutside}
149
- onCloseAutoFocus={layer.onCloseAutoFocus}
150
- onOpenAutoFocus={layer.onOpenAutoFocus}
134
+ panelStyle={{ ...(typeof local.style === "object" ? local.style : {}), ...placement() }}
135
+ ariaLabel={local.ariaLabel}
136
+ ref={(el: HTMLElement) => {
137
+ if (typeof local.ref === "function") local.ref(el as HTMLDivElement);
138
+ }}
151
139
  {...others}
152
140
  >
153
141
  {local.children}
154
- </DialogPrimitive.Content>
155
- </DialogPrimitive.Portal>
142
+ </ModalFrame>
143
+ </>
156
144
  );
157
145
  };
158
146
 
159
147
  const { Header: DialogHeader, Body: DialogBody, Footer: DialogFooter } = createModalParts("dialog");
160
148
 
161
- const DialogTitle: Component<ComponentProps<typeof DialogPrimitive.Title>> = (props) => {
162
- const [local, others] = splitProps(props, ["class"]);
163
- return (
164
- <DialogPrimitive.Title
165
- data-slot="dialog-title"
166
- class={cn(MODAL_TITLE, local.class)}
167
- {...others}
168
- />
169
- );
170
- };
171
-
172
- const DialogDescription: Component<ComponentProps<typeof DialogPrimitive.Description>> = (
173
- props,
174
- ) => {
175
- const [local, others] = splitProps(props, ["class"]);
176
- return (
177
- <DialogPrimitive.Description
178
- data-slot="dialog-description"
179
- class={cn(MODAL_DESCRIPTION, local.class)}
180
- {...others}
181
- />
182
- );
183
- };
184
-
185
- const DialogHead = createModalHead(DialogHeader, DialogTitle, DialogDescription);
149
+ const DialogHead = createModalHead(
150
+ DialogHeader,
151
+ DialogTitle as Component<ComponentProps<"h2">>,
152
+ DialogDescription as Component<ComponentProps<"p">>,
153
+ );
186
154
 
187
155
  export {
188
156
  Dialog,
@@ -1,5 +1,6 @@
1
1
  import { createSignal, For, onCleanup, Show } from "solid-js";
2
2
  import { Portal } from "solid-js/web";
3
+ import { focusWithoutTracking } from "../lib/behavior/focus-scope.js";
3
4
  import { Z_CLASS } from "../lib/layers.js";
4
5
  import { cn } from "../lib/utils.js";
5
6
  import { type EdgeZone, edgeZone, moveId, slotAt } from "./dock-paging.js";
@@ -136,10 +137,10 @@ export function createDockReorder(options: ReorderOptions) {
136
137
  if (ids.indexOf(id) < 0 || to < 0 || to >= ids.length) return;
137
138
  options.onReorder(moveId(ids, id, to));
138
139
  requestAnimationFrame(() =>
139
- options
140
- .bar()
141
- ?.querySelector<HTMLElement>(`[data-dock-id="${CSS.escape(id)}"]`)
142
- ?.focus(),
140
+ focusWithoutTracking(
141
+ options.bar()?.querySelector<HTMLElement>(`[data-dock-id="${CSS.escape(id)}"]`),
142
+ { preventScroll: false },
143
+ ),
143
144
  );
144
145
  }
145
146
 
@@ -13,9 +13,9 @@ import {
13
13
  Show,
14
14
  splitProps,
15
15
  } from "solid-js";
16
- import { Dynamic } from "solid-js/web";
16
+ import { Polymorphic } from "../lib/behavior/polymorphic.js";
17
17
  import { CARD_SURFACE } from "../lib/card-classes.js";
18
- import { DOCK_SLOT_SHAPE } from "../lib/dock-slot-classes.js";
18
+ import { DOCK_SLOT_RADIUS, DOCK_SLOT_SHAPE } from "../lib/dock-slot-classes.js";
19
19
  import {
20
20
  DOCK_ARRIVE_MOTION,
21
21
  DOCK_EDIT_MOTION,
@@ -119,7 +119,7 @@ const DockIconButton: Component<DockIconButtonProps> = (props) => {
119
119
  data-arrived={local.arrived ? "" : undefined}
120
120
  {...rest}
121
121
  >
122
- <SelectionHighlight />
122
+ <SelectionHighlight active={local.lit ?? false} />
123
123
  <Show when={local.arrived}>
124
124
  <span data-slot="dock-item-arrival" aria-hidden="true" class={DOCK_ARRIVE_MOTION} />
125
125
  </Show>
@@ -133,8 +133,8 @@ const DockIconButton: Component<DockIconButtonProps> = (props) => {
133
133
  {isElement() ? (
134
134
  (local.icon as JSX.Element)
135
135
  ) : (
136
- <Dynamic
137
- component={local.icon as Component<{ class?: string }>}
136
+ <Polymorphic
137
+ defaultAs={local.icon as Component<{ class?: string }>}
138
138
  class="h-5 w-5 sm:h-6 sm:w-6"
139
139
  />
140
140
  )}
@@ -511,6 +511,7 @@ const Dock: Component<DockProps> = (props) => {
511
511
  >
512
512
  <SelectionScope
513
513
  marker="lit"
514
+ radius={DOCK_SLOT_RADIUS}
514
515
  class={cn("flex items-center gap-0.5 sm:gap-1", vertical() && "flex-col")}
515
516
  >
516
517
  <Index each={local.items}>
@@ -1,143 +1,262 @@
1
- import { DropdownMenu as DropdownMenuPrimitive } from "@kobalte/core/dropdown-menu";
2
- import { usePortalMount } from "./portal-mount.js";
3
- import { useMenuContext } from "@kobalte/core/menu";
4
1
  import {
5
2
  type Component,
6
3
  type ComponentProps,
7
4
  createSignal,
8
- type ParentComponent,
5
+ createUniqueId,
6
+ type JSX,
9
7
  splitProps,
8
+ untrack,
9
+ type ValidComponent,
10
10
  } from "solid-js";
11
- import { MORPH_MOTION } from "../lib/motion-classes.js";
12
- import { inShadowRoot, shadowLayer } from "../lib/shadow-layer.js";
13
- import { anchorToTriggerTop } from "../lib/overlay-classes.js";
11
+ import type { AnchorLike } from "../lib/behavior/anchored.js";
12
+ import { ButtonRoot, type ButtonRootProps } from "../lib/behavior/button-root.js";
13
+ import { createControllableSignal } from "../lib/behavior/controllable.js";
14
+ import { createMenuButton } from "../lib/behavior/menu.js";
15
+ import { callEventHandler, effect, merge } from "../lib/behavior/solid.js";
16
+ import { MORPH_MOTION, MOTION_EXIT_MS } from "../lib/motion-classes.js";
14
17
  import { cn } from "../lib/utils.js";
15
18
  import {
16
- MENU_CONTENT_CLASS,
19
+ createMenuLevel,
20
+ type DeprecatedMenuRootProps,
21
+ MenuArrow,
22
+ type MenuCheckboxItemProps,
17
23
  MenuCheckboxItemPart,
18
- MenuContentIndicator,
24
+ MenuContentPart,
25
+ type MenuContentProps,
26
+ MenuGroup,
27
+ MenuItemIndicator,
28
+ type MenuItemProps,
19
29
  MenuItemPart,
30
+ type MenuLabelProps,
20
31
  MenuLabelPart,
32
+ MenuLevelProvider,
33
+ type MenuPlacement,
34
+ MenuPortal,
35
+ MenuRadioGroup,
36
+ type MenuRadioItemProps,
21
37
  MenuRadioItemPart,
22
38
  MenuSeparatorPart,
39
+ MenuSub,
23
40
  MenuSubContentPart,
41
+ type MenuSubTriggerProps,
24
42
  MenuSubTriggerPart,
43
+ MenuTextPart,
44
+ parsePlacement,
45
+ useMenuLevel,
25
46
  } from "./menu-parts.js";
47
+ import { controlRadius } from "../lib/anchor-placement.js";
26
48
 
27
- /* The panel covers the trigger (see MORPH_MOTION); statics carried across so
28
- * `DropdownMenu.Portal` keeps resolving. */
29
- const DropdownMenuRoot: Component<ComponentProps<typeof DropdownMenuPrimitive>> = (props) => {
30
- // Modal in a shadow root, Kobalte would aria-hide the realm's host, menu included.
31
- const inShadow = inShadowRoot(usePortalMount());
32
- return (
33
- <DropdownMenuPrimitive
34
- gutter={0}
35
- getAnchorRect={anchorToTriggerTop}
36
- {...props}
37
- modal={inShadow ? false : props.modal}
38
- />
39
- );
49
+ type AnchorRect = { x?: number; y?: number; width?: number; height?: number };
50
+
51
+ interface DropdownMenuRootProps extends DeprecatedMenuRootProps {
52
+ open?: boolean;
53
+ defaultOpen?: boolean;
54
+ onOpenChange?: (open: boolean) => void;
55
+ placement?: MenuPlacement;
56
+ gutter?: number;
57
+ /** The box the panel sits beside, read from the trigger; unset, the panel covers the trigger. */
58
+ getAnchorRect?: (anchor?: HTMLElement) => AnchorRect | undefined;
59
+ children?: JSX.Element;
60
+ }
61
+
62
+ function rectOf(trigger: HTMLElement, read: (el?: HTMLElement) => AnchorRect | undefined) {
63
+ const anchor: AnchorLike = {
64
+ getBoundingClientRect: () => {
65
+ const rect = read(trigger) ?? {};
66
+ return {
67
+ left: rect.x ?? 0,
68
+ top: rect.y ?? 0,
69
+ width: rect.width ?? 0,
70
+ height: rect.height ?? 0,
71
+ };
72
+ },
73
+ getRootNode: () => trigger.getRootNode(),
74
+ };
75
+ return anchor;
76
+ }
77
+
78
+ const DropdownMenuRoot: Component<DropdownMenuRootProps> = (props) => {
79
+ const [open, setOpen] = createControllableSignal<boolean>({
80
+ value: () => props.open,
81
+ defaultValue: () => props.defaultOpen ?? false,
82
+ onChange: (next) => props.onOpenChange?.(next),
83
+ });
84
+ const placement = () => parsePlacement(props.placement ?? "bottom-start");
85
+ const level = createMenuLevel({
86
+ open,
87
+ onOpenChange: setOpen,
88
+ anchor: (trigger) => {
89
+ const read = props.getAnchorRect;
90
+ return read && trigger ? rectOf(trigger, read) : trigger;
91
+ },
92
+ coverAnchor: !props.getAnchorRect,
93
+ side: () => placement()[0],
94
+ align: () => placement()[1],
95
+ offset: untrack(() => props.gutter ?? 0),
96
+ });
97
+ return <MenuLevelProvider value={level}>{props.children}</MenuLevelProvider>;
40
98
  };
41
- const DropdownMenu = Object.assign(DropdownMenuRoot, DropdownMenuPrimitive) as Component<
42
- ComponentProps<typeof DropdownMenuPrimitive>
43
- > &
44
- typeof DropdownMenuPrimitive;
45
- const DropdownMenuGroup = DropdownMenuPrimitive.Group;
46
- const DropdownMenuSub = DropdownMenuPrimitive.Sub;
47
- const DropdownMenuRadioGroup = DropdownMenuPrimitive.RadioGroup;
48
-
49
- const DropdownMenuTrigger: Component<ComponentProps<typeof DropdownMenuPrimitive.Trigger>> = (
50
- props,
51
- ) => {
52
- return <DropdownMenuPrimitive.Trigger data-slot="dropdown-menu-trigger" {...props} />;
99
+
100
+ type TriggerHandlers = {
101
+ id?: string;
102
+ ref?: unknown;
103
+ onPointerDown?: JSX.EventHandlerUnion<HTMLElement, PointerEvent>;
104
+ onKeyDown?: JSX.EventHandlerUnion<HTMLElement, KeyboardEvent>;
105
+ onClick?: JSX.EventHandlerUnion<HTMLElement, MouseEvent>;
53
106
  };
54
107
 
55
- const DropdownMenuContent: Component<ComponentProps<typeof DropdownMenuPrimitive.Content>> = (
56
- props,
57
- ) => {
58
- const [local, others] = splitProps(props, [
59
- "class",
60
- "children",
108
+ type Bound<E extends Event> = E & { currentTarget: HTMLElement; target: Element };
109
+
110
+ function DropdownMenuTrigger<T extends ValidComponent = "button">(
111
+ props: ButtonRootProps<T>,
112
+ ): JSX.Element {
113
+ const level = useMenuLevel();
114
+ const menu = level.menu;
115
+ const [local, rest] = splitProps(props as ButtonRootProps<ValidComponent> & TriggerHandlers, [
116
+ "id",
61
117
  "ref",
62
- "onInteractOutside",
63
- "onCloseAutoFocus",
118
+ "disabled",
119
+ "onPointerDown",
120
+ "onKeyDown",
121
+ "onClick",
64
122
  ]);
65
- const menu = useMenuContext();
66
- const [panel, setPanel] = createSignal<HTMLElement>();
67
- const layer = shadowLayer({
68
- content: panel,
69
- open: () => menu.isOpen(),
70
- dismiss: () => menu.close(true),
71
- trigger: () => menu.triggerRef(),
72
- tree: true,
73
- consumer: local,
123
+ const fallback = createUniqueId();
124
+ const id = (): string => local.id ?? fallback;
125
+ effect(id, (value) => {
126
+ level.setTriggerId(value);
127
+ return () => level.setTriggerId(undefined);
74
128
  });
75
- // The morph starts as the trigger's box: its height and radius are read
76
- // from the trigger itself, its width is kobalte's anchor width.
77
- const writeMorphStart = (el: HTMLElement) => {
78
- const trigger = menu.triggerRef();
79
- if (!trigger) return;
80
- const height = trigger.getBoundingClientRect().height;
81
- // A pill's radius is "infinite"; the rendered one is half its height.
82
- const radius = Math.min(
83
- Number.parseFloat(getComputedStyle(trigger).borderTopLeftRadius) || 0,
84
- height / 2,
85
- );
86
- el.style.setProperty("--morph-h", `${height}px`);
87
- el.style.setProperty("--morph-radius", `${radius}px`);
88
- };
129
+ const button = createMenuButton(menu, () => local.disabled ?? false);
130
+ const [el, setEl] = createSignal<HTMLElement>();
131
+
132
+ // Bound on the element, since Solid's delegated handlers never reach inside a closed shadow root.
133
+ effect(el, (node) => {
134
+ if (!node) return;
135
+ const down = (event: PointerEvent) => {
136
+ callEventHandler(event as Bound<PointerEvent>, local.onPointerDown);
137
+ button.onPointerDown(event);
138
+ };
139
+ const key = (event: KeyboardEvent) => {
140
+ callEventHandler(event as Bound<KeyboardEvent>, local.onKeyDown);
141
+ button.onKeyDown(event);
142
+ };
143
+ node.addEventListener("pointerdown", down);
144
+ node.addEventListener("keydown", key);
145
+ return () => {
146
+ node.removeEventListener("pointerdown", down);
147
+ node.removeEventListener("keydown", key);
148
+ if (untrack(menu.trigger) === node) menu.setTrigger(undefined);
149
+ };
150
+ });
151
+
152
+ return (
153
+ <ButtonRoot
154
+ {...merge({ "data-slot": "dropdown-menu-trigger" }, rest, {
155
+ get disabled() {
156
+ return local.disabled;
157
+ },
158
+ get id() {
159
+ return id();
160
+ },
161
+ ref: (node: HTMLElement) => {
162
+ setEl(node);
163
+ menu.setTrigger(node);
164
+ if (typeof local.ref === "function") local.ref(node);
165
+ },
166
+ "aria-haspopup": "true",
167
+ get "aria-expanded"() {
168
+ return menu.open();
169
+ },
170
+ get "aria-controls"() {
171
+ return menu.open() ? level.contentId() : undefined;
172
+ },
173
+ get "data-expanded"() {
174
+ return menu.open() ? "" : undefined;
175
+ },
176
+ get "data-closed"() {
177
+ return menu.open() ? undefined : "";
178
+ },
179
+ onClick: (event: Bound<MouseEvent>) => {
180
+ callEventHandler(event, local.onClick);
181
+ button.onClick();
182
+ },
183
+ })}
184
+ />
185
+ );
186
+ }
187
+
188
+ /* The panel opens as its trigger's box. */
189
+ function writeMorphStart(trigger: HTMLElement | undefined, el: HTMLElement) {
190
+ if (!trigger) return;
191
+ const box = trigger.getBoundingClientRect();
192
+ el.style.setProperty("--kb-popper-anchor-width", `${box.width}px`);
193
+ el.style.setProperty("--morph-h", `${box.height}px`);
194
+ el.style.setProperty("--morph-radius", controlRadius(box.height));
195
+ }
196
+
197
+ const DropdownMenuContent: Component<MenuContentProps> = (props) => {
198
+ const level = useMenuLevel();
89
199
  return (
90
- <DropdownMenuPrimitive.Portal mount={usePortalMount()}>
91
- <DropdownMenuPrimitive.Content
92
- ref={(el: HTMLElement) => {
93
- setPanel(el);
94
- writeMorphStart(el);
95
- if (typeof local.ref === "function") local.ref(el);
96
- }}
97
- onInteractOutside={layer.onInteractOutside}
98
- onCloseAutoFocus={layer.onCloseAutoFocus}
99
- data-slot="dropdown-menu-content"
100
- class={cn(MENU_CONTENT_CLASS, MORPH_MOTION, local.class)}
101
- {...others}
102
- >
103
- <MenuContentIndicator>{local.children}</MenuContentIndicator>
104
- </DropdownMenuPrimitive.Content>
105
- </DropdownMenuPrimitive.Portal>
200
+ <MenuContentPart
201
+ slotName="dropdown-menu-content"
202
+ motionClass={MORPH_MOTION}
203
+ exitMs={MOTION_EXIT_MS.anchored}
204
+ onPanel={(el) => writeMorphStart(untrack(level.menu.trigger), el)}
205
+ {...props}
206
+ />
106
207
  );
107
208
  };
108
209
 
109
- const DropdownMenuItem: Component<
110
- ComponentProps<typeof DropdownMenuPrimitive.Item> & {
111
- inset?: boolean;
112
- tone?: string;
113
- /** @deprecated pass `tone="var(--destructive)"` instead */
114
- variant?: "default" | "destructive";
115
- }
116
- > = (props) => <MenuItemPart slotName="dropdown-menu-item" {...props} />;
210
+ function DropdownMenuItem<T extends ValidComponent = "div">(props: MenuItemProps<T>): JSX.Element {
211
+ return (
212
+ <MenuItemPart slotName="dropdown-menu-item" {...(props as MenuItemProps<ValidComponent>)} />
213
+ );
214
+ }
117
215
 
118
- const DropdownMenuSubTrigger: ParentComponent<
119
- ComponentProps<typeof DropdownMenuPrimitive.SubTrigger> & { inset?: boolean }
120
- > = (props) => <MenuSubTriggerPart slotName="dropdown-menu-sub-trigger" {...props} />;
216
+ function DropdownMenuSubTrigger<T extends ValidComponent = "div">(
217
+ props: MenuSubTriggerProps<T>,
218
+ ): JSX.Element {
219
+ return (
220
+ <MenuSubTriggerPart
221
+ slotName="dropdown-menu-sub-trigger"
222
+ {...(props as MenuSubTriggerProps<ValidComponent>)}
223
+ />
224
+ );
225
+ }
121
226
 
122
- const DropdownMenuSubContent: ParentComponent<
123
- ComponentProps<typeof DropdownMenuPrimitive.SubContent>
124
- > = (props) => <MenuSubContentPart slotName="dropdown-menu-sub-content" {...props} />;
227
+ const DropdownMenuSubContent: Component<MenuContentProps> = (props) => (
228
+ <MenuSubContentPart slotName="dropdown-menu-sub-content" {...props} />
229
+ );
125
230
 
126
- const DropdownMenuCheckboxItem: ParentComponent<
127
- ComponentProps<typeof DropdownMenuPrimitive.CheckboxItem>
128
- > = (props) => <MenuCheckboxItemPart slotName="dropdown-menu-checkbox-item" {...props} />;
231
+ function DropdownMenuCheckboxItem<T extends ValidComponent = "div">(
232
+ props: MenuCheckboxItemProps<T>,
233
+ ): JSX.Element {
234
+ return (
235
+ <MenuCheckboxItemPart
236
+ slotName="dropdown-menu-checkbox-item"
237
+ {...(props as MenuCheckboxItemProps<ValidComponent>)}
238
+ />
239
+ );
240
+ }
129
241
 
130
- const DropdownMenuRadioItem: ParentComponent<
131
- ComponentProps<typeof DropdownMenuPrimitive.RadioItem>
132
- > = (props) => <MenuRadioItemPart slotName="dropdown-menu-radio-item" {...props} />;
242
+ function DropdownMenuRadioItem<T extends ValidComponent = "div">(
243
+ props: MenuRadioItemProps<T>,
244
+ ): JSX.Element {
245
+ return (
246
+ <MenuRadioItemPart
247
+ slotName="dropdown-menu-radio-item"
248
+ {...(props as MenuRadioItemProps<ValidComponent>)}
249
+ />
250
+ );
251
+ }
133
252
 
134
- const DropdownMenuSeparator: Component<ComponentProps<typeof DropdownMenuPrimitive.Separator>> = (
135
- props,
136
- ) => <MenuSeparatorPart slotName="dropdown-menu-separator" {...props} />;
253
+ const DropdownMenuSeparator: Component<ComponentProps<"hr">> = (props) => (
254
+ <MenuSeparatorPart slotName="dropdown-menu-separator" {...props} />
255
+ );
137
256
 
138
- const DropdownMenuLabel: Component<
139
- ComponentProps<typeof DropdownMenuPrimitive.GroupLabel> & { inset?: boolean }
140
- > = (props) => <MenuLabelPart slotName="dropdown-menu-label" {...props} />;
257
+ const DropdownMenuLabel: Component<MenuLabelProps> = (props) => (
258
+ <MenuLabelPart slotName="dropdown-menu-label" {...props} />
259
+ );
141
260
 
142
261
  const DropdownMenuShortcut: Component<ComponentProps<"span">> = (props) => {
143
262
  const [local, others] = splitProps(props, ["class"]);
@@ -150,6 +269,32 @@ const DropdownMenuShortcut: Component<ComponentProps<"span">> = (props) => {
150
269
  );
151
270
  };
152
271
 
272
+ const DropdownMenuGroup = MenuGroup;
273
+ const DropdownMenuSub = MenuSub;
274
+ const DropdownMenuRadioGroup = MenuRadioGroup;
275
+
276
+ const DropdownMenu = Object.assign(DropdownMenuRoot, {
277
+ Root: DropdownMenuRoot,
278
+ Trigger: DropdownMenuTrigger,
279
+ Portal: MenuPortal,
280
+ Content: DropdownMenuContent,
281
+ Arrow: MenuArrow,
282
+ Item: DropdownMenuItem,
283
+ CheckboxItem: DropdownMenuCheckboxItem,
284
+ RadioGroup: DropdownMenuRadioGroup,
285
+ RadioItem: DropdownMenuRadioItem,
286
+ Group: DropdownMenuGroup,
287
+ GroupLabel: DropdownMenuLabel,
288
+ Separator: DropdownMenuSeparator,
289
+ Sub: DropdownMenuSub,
290
+ SubTrigger: DropdownMenuSubTrigger,
291
+ SubContent: DropdownMenuSubContent,
292
+ ItemIndicator: MenuItemIndicator,
293
+ Icon: MenuTextPart,
294
+ ItemLabel: MenuTextPart,
295
+ ItemDescription: MenuTextPart,
296
+ });
297
+
153
298
  export {
154
299
  DropdownMenu,
155
300
  DropdownMenuCheckboxItem,