@vendure-io/ui 2.2.0 → 3.0.0-next.1

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 (120) hide show
  1. package/package.json +19 -5
  2. package/src/components/atoms/accordion.tsx +21 -6
  3. package/src/components/atoms/alert-dialog.tsx +31 -6
  4. package/src/components/atoms/alert.tsx +72 -18
  5. package/src/components/atoms/avatar.tsx +12 -4
  6. package/src/components/atoms/badge.tsx +7 -2
  7. package/src/components/atoms/breadcrumb.tsx +493 -4
  8. package/src/components/atoms/button-group.tsx +30 -6
  9. package/src/components/atoms/button.tsx +128 -18
  10. package/src/components/atoms/calendar.tsx +6 -6
  11. package/src/components/atoms/card.tsx +130 -16
  12. package/src/components/atoms/carousel.tsx +16 -9
  13. package/src/components/atoms/chart.tsx +87 -1
  14. package/src/components/atoms/checkbox.tsx +85 -7
  15. package/src/components/atoms/collapsible.tsx +2 -1
  16. package/src/components/atoms/combobox.tsx +358 -29
  17. package/src/components/atoms/command.tsx +336 -13
  18. package/src/components/atoms/context-menu.tsx +28 -23
  19. package/src/components/atoms/dialog.tsx +173 -29
  20. package/src/components/atoms/direction.tsx +23 -2
  21. package/src/components/atoms/drawer.tsx +15 -10
  22. package/src/components/atoms/dropdown-menu.tsx +43 -28
  23. package/src/components/atoms/empty.tsx +5 -1
  24. package/src/components/atoms/field.tsx +41 -15
  25. package/src/components/atoms/hover-card.tsx +61 -8
  26. package/src/components/atoms/input-group.tsx +19 -11
  27. package/src/components/atoms/input-otp.tsx +6 -6
  28. package/src/components/atoms/input.tsx +7 -1
  29. package/src/components/atoms/item.tsx +3 -3
  30. package/src/components/atoms/kbd.tsx +32 -3
  31. package/src/components/atoms/label-swap.tsx +113 -0
  32. package/src/components/atoms/label.tsx +1 -1
  33. package/src/components/atoms/menubar.tsx +28 -26
  34. package/src/components/atoms/native-select.tsx +3 -2
  35. package/src/components/atoms/navigation-menu.tsx +13 -8
  36. package/src/components/atoms/pagination.tsx +2 -2
  37. package/src/components/atoms/popover.tsx +29 -6
  38. package/src/components/atoms/progress.tsx +46 -33
  39. package/src/components/atoms/radio-group.tsx +9 -4
  40. package/src/components/atoms/resizable.tsx +1 -1
  41. package/src/components/atoms/scroll-area.tsx +2 -2
  42. package/src/components/atoms/select.tsx +258 -19
  43. package/src/components/atoms/sheet.tsx +76 -23
  44. package/src/components/atoms/sidebar.tsx +405 -23
  45. package/src/components/atoms/skeleton.tsx +1 -1
  46. package/src/components/atoms/slider.tsx +1 -1
  47. package/src/components/atoms/sonner.tsx +128 -38
  48. package/src/components/atoms/spinner.tsx +1 -1
  49. package/src/components/atoms/switch.tsx +10 -5
  50. package/src/components/atoms/table.tsx +147 -7
  51. package/src/components/atoms/tabs.tsx +3 -3
  52. package/src/components/atoms/textarea.tsx +3 -1
  53. package/src/components/atoms/toggle-group.tsx +4 -2
  54. package/src/components/atoms/toggle.tsx +28 -6
  55. package/src/components/atoms/tooltip.tsx +85 -15
  56. package/src/components/molecules/anonymized-token.tsx +24 -8
  57. package/src/components/molecules/app-shell.tsx +53 -20
  58. package/src/components/molecules/arrow-link.tsx +40 -0
  59. package/src/components/molecules/chip.tsx +43 -4
  60. package/src/components/molecules/code-block.tsx +54 -31
  61. package/src/components/molecules/collapsible-section.tsx +129 -0
  62. package/src/components/molecules/confirm-dialog.tsx +122 -9
  63. package/src/components/molecules/copyable-text.tsx +75 -11
  64. package/src/components/molecules/count-badge.tsx +115 -0
  65. package/src/components/molecules/data-table/data-table-bulk-actions.tsx +32 -4
  66. package/src/components/molecules/data-table/data-table-column-header.tsx +20 -3
  67. package/src/components/molecules/data-table/data-table-filters.tsx +62 -22
  68. package/src/components/molecules/data-table/data-table-helpers.tsx +88 -1
  69. package/src/components/molecules/data-table/data-table-selection-storage.tsx +115 -0
  70. package/src/components/molecules/data-table/data-table-types.tsx +160 -4
  71. package/src/components/molecules/data-table/data-table-view-options.tsx +2 -2
  72. package/src/components/molecules/data-table/data-table.tsx +869 -81
  73. package/src/components/molecules/data-table/table-pagination.tsx +2 -2
  74. package/src/components/molecules/date-picker.tsx +13 -7
  75. package/src/components/molecules/date-range-picker.tsx +13 -6
  76. package/src/components/molecules/date-time-picker.tsx +13 -7
  77. package/src/components/molecules/date-time.tsx +8 -1
  78. package/src/components/molecules/direction-link.tsx +130 -0
  79. package/src/components/molecules/elapsed-time.tsx +223 -0
  80. package/src/components/molecules/file-dropzone.tsx +2 -2
  81. package/src/components/molecules/id-chip.tsx +169 -32
  82. package/src/components/molecules/inline-code.tsx +4 -0
  83. package/src/components/molecules/issued-secret.tsx +318 -0
  84. package/src/components/molecules/list-row.tsx +349 -0
  85. package/src/components/molecules/live-indicator.tsx +83 -0
  86. package/src/components/molecules/page-header.tsx +117 -5
  87. package/src/components/molecules/password-input.tsx +2 -2
  88. package/src/components/molecules/relative-time.tsx +85 -56
  89. package/src/components/molecules/search-trigger.tsx +111 -0
  90. package/src/components/molecules/secret-value.tsx +29 -9
  91. package/src/components/molecules/skip-link.tsx +1 -1
  92. package/src/components/molecules/stat-card.tsx +4 -4
  93. package/src/components/molecules/state-views/empty-state.tsx +35 -17
  94. package/src/components/molecules/state-views/error-state.tsx +32 -14
  95. package/src/components/molecules/status-badge.tsx +415 -49
  96. package/src/components/molecules/time-tooltip.tsx +140 -0
  97. package/src/components/molecules/timeline.tsx +246 -0
  98. package/src/components/molecules/visibility-icon.tsx +89 -0
  99. package/src/hooks/use-active-label-size.ts +66 -0
  100. package/src/hooks/use-apple-platform.ts +26 -0
  101. package/src/hooks/use-pinned-height.ts +55 -0
  102. package/src/hooks/use-scrolled.ts +27 -0
  103. package/src/lib/content-inset.ts +17 -0
  104. package/src/lib/control-name.ts +55 -0
  105. package/src/lib/dialog-padding.ts +48 -0
  106. package/src/lib/field-surface.ts +31 -0
  107. package/src/lib/format-elapsed.ts +67 -0
  108. package/src/lib/format-relative.ts +29 -1
  109. package/src/lib/highlight.ts +3 -3
  110. package/src/lib/menu-item.tsx +72 -0
  111. package/src/lib/outline-edge.ts +21 -0
  112. package/src/lib/popup-align.ts +67 -0
  113. package/src/lib/popup-direction.ts +22 -0
  114. package/src/lib/popup-motion.ts +19 -0
  115. package/src/lib/state-dictionary.ts +42 -6
  116. package/src/lib/syntax-colors.ts +8 -0
  117. package/src/lib/tint-recipes.ts +68 -0
  118. package/src/lib/tone-icons.ts +24 -0
  119. package/src/lib/trigger-slot.ts +16 -0
  120. package/src/lib/utils.ts +97 -1
@@ -4,16 +4,42 @@ import * as React from "react"
4
4
  import { Select as SelectPrimitive } from "@base-ui/react/select"
5
5
 
6
6
  import { cn } from "@vendure-io/ui/lib/utils"
7
+ import { MenuCheck, menuCheckItem, menuItemDisabled } from "../../lib/menu-item.tsx"
8
+ import { fieldSurface } from "@vendure-io/ui/lib/field-surface"
9
+ import { popupMotion } from "../../lib/popup-motion.ts"
7
10
  import { useFieldControlProps } from "@vendure-io/ui/components/atoms/field"
8
- import { ChevronDownIcon, CheckIcon, ChevronUpIcon } from "lucide-react"
11
+ import { ChevronDownIcon, ChevronUpIcon } from "lucide-react"
12
+ import { usePopupDir } from "../../lib/popup-direction.ts"
13
+ import { usePopupAlign } from "../../lib/popup-align.ts"
14
+ import { nameFrom, useRegisteredRef } from "../../lib/control-name.ts"
9
15
 
10
- const Select = SelectPrimitive.Root
16
+ /**
17
+ * The trigger of the nearest Select. Base UI names the trigger but not the
18
+ * list, so SelectContent reads the trigger's name from here and gives it to
19
+ * the list.
20
+ */
21
+ const SelectTriggerContext = React.createContext<{
22
+ trigger: HTMLElement | null
23
+ setTrigger: (trigger: HTMLElement | null) => void
24
+ } | null>(null)
25
+
26
+ function Select<Value, Multiple extends boolean | undefined = false>(
27
+ props: SelectPrimitive.Root.Props<Value, Multiple>
28
+ ) {
29
+ const [trigger, setTrigger] = React.useState<HTMLElement | null>(null)
30
+ const context = React.useMemo(() => ({ trigger, setTrigger }), [trigger])
31
+ return (
32
+ <SelectTriggerContext.Provider value={context}>
33
+ <SelectPrimitive.Root {...props} />
34
+ </SelectTriggerContext.Provider>
35
+ )
36
+ }
11
37
 
12
38
  function SelectGroup({ className, ...props }: SelectPrimitive.Group.Props) {
13
39
  return (
14
40
  <SelectPrimitive.Group
15
41
  data-slot="select-group"
16
- className={cn("scroll-my-1 p-1", className)}
42
+ className={cn("scroll-my-1.25", className)}
17
43
  {...props}
18
44
  />
19
45
  )
@@ -23,7 +49,7 @@ function SelectValue({ className, ...props }: SelectPrimitive.Value.Props) {
23
49
  return (
24
50
  <SelectPrimitive.Value
25
51
  data-slot="select-value"
26
- className={cn("flex flex-1 text-left", className)}
52
+ className={cn("flex flex-1 text-start", className)}
27
53
  {...props}
28
54
  />
29
55
  )
@@ -33,16 +59,20 @@ function SelectTrigger({
33
59
  className,
34
60
  size = "default",
35
61
  children,
62
+ ref,
36
63
  ...props
37
64
  }: SelectPrimitive.Trigger.Props & {
38
65
  size?: "sm" | "default"
39
66
  }) {
67
+ const triggerRef = useRegisteredRef(ref, React.useContext(SelectTriggerContext)?.setTrigger)
40
68
  return (
41
69
  <SelectPrimitive.Trigger
70
+ ref={triggerRef}
42
71
  data-slot="select-trigger"
43
72
  data-size={size}
44
73
  className={cn(
45
- "border-input data-placeholder:text-muted-foreground dark:bg-input/30 dark:hover:bg-input/50 focus-visible:border-ring focus-visible:ring-ring/50 aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive flex w-fit items-center justify-between gap-1.5 rounded-md border bg-transparent py-2 pr-2 pl-2.5 text-sm whitespace-nowrap transition-[color,box-shadow] outline-none focus-visible:ring-3 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:ring-3 data-[size=default]:h-9 data-[size=sm]:h-8 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-1.5 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
74
+ fieldSurface,
75
+ "data-placeholder:text-faint-foreground flex w-fit items-center justify-between gap-1.5 px-2.5 py-2 text-sm whitespace-nowrap disabled:cursor-not-allowed disabled:opacity-50 data-[size=default]:h-8.5 data-[size=sm]:h-7 *:data-[slot=select-value]:line-clamp-1 *:data-[slot=select-value]:flex *:data-[slot=select-value]:items-center *:data-[slot=select-value]:gap-1.5 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
46
76
  className
47
77
  )}
48
78
  {...useFieldControlProps(props)}
@@ -57,15 +87,193 @@ function SelectTrigger({
57
87
  )
58
88
  }
59
89
 
90
+ /**
91
+ * Turns a `box-shadow` value into `drop-shadow()` filters. A 1px ring (no
92
+ * offset, no blur, a spread) becomes four 1px drop shadows, one per side.
93
+ * A drop shadow's blur is a standard deviation, half a box-shadow blur, and it
94
+ * has no spread: a soft shadow with a negative spread takes 0.4 of its blur
95
+ * and 60% of its colour instead, which matches `shadow-overlay` in light and
96
+ * dark within a few levels of grey.
97
+ */
98
+ function shadowToFilter(shadow: string) {
99
+ const outside = /[,\s]+(?![^(]*\))/
100
+ return shadow
101
+ .split(/,(?![^(]*\))/)
102
+ .flatMap((layer) => {
103
+ const parts = layer.trim().split(outside).filter((part) => part && part !== "inset")
104
+ const isLength = (part: string) => /^-?[\d.]+(px)?$/.test(part)
105
+ const [x = 0, y = 0, blur = 0, spread = 0] = parts.filter(isLength).map(parseFloat)
106
+ const color = parts.filter((part) => !isLength(part)).join(" ")
107
+ if (!color) return []
108
+ if (!x && !y && !blur && spread > 0) {
109
+ return [`${spread}px 0`, `-${spread}px 0`, `0 ${spread}px`, `0 -${spread}px`].map(
110
+ (offset) => `drop-shadow(${offset} 0 ${color})`
111
+ )
112
+ }
113
+ if (spread < 0) {
114
+ const lighter = `color-mix(in oklab, ${color} 60%, transparent)`
115
+ return [`drop-shadow(${x}px ${y}px ${blur * 0.4}px ${lighter})`]
116
+ }
117
+ return [`drop-shadow(${x}px ${y}px ${blur / 2}px ${color})`]
118
+ })
119
+ .join(" ")
120
+ }
121
+
122
+ /**
123
+ * A list that Base UI places over its trigger (`data-side="none"`) covers the
124
+ * trigger and grows out of it.
125
+ *
126
+ * Base UI moves the list so the selected item's text sits on the trigger's
127
+ * text. The item text is further in than the trigger text, so the list starts
128
+ * left of the trigger by the difference; at the trigger's width it then ends
129
+ * inside the trigger, and the trigger's edge and chevron show beside it. The
130
+ * list is made wider by that difference on both sides, so it overhangs the
131
+ * trigger evenly.
132
+ *
133
+ * `--select-trigger-box` is the trigger's box as a `clip-path` inset of the
134
+ * list. The list starts and ends clipped to it, so it opens out of the
135
+ * trigger and closes back into it. A `clip-path` also clips the list's own
136
+ * shadow, so the positioner draws the edge and the shadow as `drop-shadow`
137
+ * filters, which follow the clip.
138
+ *
139
+ * Base UI positions the list on each open, so this runs on every style
140
+ * change of the popup.
141
+ */
142
+ /**
143
+ * Base UI puts the selected item on the trigger and cuts the list at the edge
144
+ * of the screen, so a short list near an edge shows a few items and scrolls
145
+ * ("10" selected at the bottom of the screen hides 20 to 100 below the edge).
146
+ * When the whole list fits on the screen, this moves it up or down just far
147
+ * enough to show every item, inside Base UI's 10px screen margin, and drops
148
+ * the scroll. It still covers the trigger: Base UI places a list over its
149
+ * trigger only when the trigger is at least 20px from the edges, and the list
150
+ * moves by no more than it ran past the edge. A list taller than the screen
151
+ * keeps Base UI's placement and scrolls.
152
+ */
153
+ function fitOnScreen(positioner: HTMLElement, list: HTMLElement) {
154
+ const hidden = list.scrollHeight - list.clientHeight
155
+ if (hidden <= 1) return
156
+ const style = getComputedStyle(positioner)
157
+ const marginTop = parseFloat(style.marginTop) || 0
158
+ const marginBottom = parseFloat(style.marginBottom) || 0
159
+ const rect = positioner.getBoundingClientRect()
160
+ const height = rect.height + hidden
161
+ const screen = document.documentElement.clientHeight
162
+ if (height > screen - marginTop - marginBottom) return
163
+ // Where the list's first item would be if the list did not scroll: the
164
+ // selected item stays on the trigger as far as the screen allows.
165
+ const top = Math.min(
166
+ Math.max(rect.top - list.scrollTop, marginTop),
167
+ screen - marginBottom - height
168
+ )
169
+ // `top` positions the margin box: move by the difference from now.
170
+ const currentTop = parseFloat(style.top) || 0
171
+ positioner.style.top = `${currentTop + top - rect.top}px`
172
+ positioner.style.bottom = ""
173
+ positioner.style.height = `${height}px`
174
+ list.scrollTop = 0
175
+ // Base UI shows its scroll arrows from scroll events: a list that no
176
+ // longer scrolls hides them.
177
+ list.dispatchEvent(new Event("scroll"))
178
+ }
179
+
180
+ function growFromTrigger(popup: HTMLElement | null) {
181
+ if (!popup) return () => {}
182
+ const positioner = popup.parentElement
183
+ let placed = ""
184
+ const place = () => {
185
+ if (popup.dataset.side !== "none") {
186
+ placed = ""
187
+ popup.style.removeProperty("min-width")
188
+ popup.style.removeProperty("--select-trigger-box")
189
+ positioner?.style.removeProperty("filter")
190
+ return
191
+ }
192
+ // A closing list keeps the box it opened from: a new box would turn the
193
+ // closing transition towards it.
194
+ if (popup.hasAttribute("data-ending-style") || popup.hasAttribute("data-closed")) return
195
+ const list = popup.querySelector<HTMLElement>("[role=listbox]")
196
+ const trigger = list?.id
197
+ ? document.querySelector<HTMLElement>(`[aria-controls="${CSS.escape(list.id)}"]`)
198
+ : null
199
+ const value = trigger?.querySelector<HTMLElement>("[data-slot=select-value]")
200
+ const item =
201
+ popup.querySelector<HTMLElement>("[data-slot=select-item][data-selected]") ??
202
+ popup.querySelector<HTMLElement>("[data-slot=select-item]")
203
+ const text = item?.firstElementChild
204
+ if (!trigger || !value || !text || !popup.offsetWidth) return
205
+ if (positioner && list) fitOnScreen(positioner, list)
206
+ const rtl = getComputedStyle(popup).direction === "rtl"
207
+ const box = trigger.getBoundingClientRect()
208
+ const valueBox = value.getBoundingClientRect()
209
+ const textBox = text.getBoundingClientRect()
210
+ let popupBox = popup.getBoundingClientRect()
211
+ const overhang = Math.max(
212
+ 0,
213
+ rtl
214
+ ? popupBox.right - textBox.right - (box.right - valueBox.right)
215
+ : textBox.left - popupBox.left - (valueBox.left - box.left)
216
+ )
217
+ const minWidth = `calc(var(--anchor-width) + ${2 * overhang}px)`
218
+ if (popup.style.minWidth !== minWidth) popup.style.minWidth = minWidth
219
+ popupBox = popup.getBoundingClientRect()
220
+ const radius = getComputedStyle(trigger).borderTopLeftRadius
221
+ const inset = [
222
+ box.top - popupBox.top,
223
+ popupBox.right - box.right,
224
+ popupBox.bottom - box.bottom,
225
+ box.left - popupBox.left,
226
+ ]
227
+ .map((side) => `${Math.round(side * 100) / 100}px`)
228
+ .join(" ")
229
+ const clip = `inset(${inset} round ${radius})`
230
+ // Writing a style is a style change, which runs this again: skip a write
231
+ // that would only repeat the last one.
232
+ if (clip === placed) return
233
+ placed = clip
234
+ popup.style.setProperty("--select-trigger-box", clip)
235
+ if (positioner) {
236
+ positioner.style.filter = shadowToFilter(
237
+ getComputedStyle(popup).getPropertyValue("--shadow-overlay")
238
+ )
239
+ }
240
+ }
241
+ const observer = new MutationObserver(place)
242
+ observer.observe(popup, { attributes: true, attributeFilter: ["style", "data-side", "data-closed"] })
243
+ place()
244
+ return () => observer.disconnect()
245
+ }
246
+
247
+ /**
248
+ * The motion of a list over its trigger. It has no side, so it drops the
249
+ * popup's 2px slide and its scale, and keeps its opacity at 1: from the first
250
+ * frame it covers the trigger. Its `clip-path` grows from the trigger's box
251
+ * (`--select-trigger-box`) to its own, with the popup's timings: `normal` on
252
+ * the strong ease-out to open, `quick` on `ease-soft` to close. A transition
253
+ * starts from where the last one stopped, so a list closed while it opens
254
+ * turns back from there. Reduced motion keeps only the popup's fade.
255
+ */
256
+ const growFromTriggerMotion =
257
+ "data-[side=none]:shadow-none data-[side=none]:transition-[opacity,clip-path] data-[side=none]:[transition-duration:var(--transition-duration-popup-fade),var(--transition-duration-normal)] data-[side=none]:data-ending-style:duration-quick motion-safe:data-[side=none]:[clip-path:inset(0_round_var(--radius-xl))] motion-safe:data-[side=none]:data-starting-style:[clip-path:var(--select-trigger-box)] motion-safe:data-[side=none]:data-ending-style:[clip-path:var(--select-trigger-box)] motion-safe:data-[side=none]:data-starting-style:opacity-100 motion-safe:data-[side=none]:data-ending-style:opacity-100 motion-safe:data-[side=none]:data-starting-style:scale-100 motion-safe:data-[side=none]:data-ending-style:scale-100 motion-safe:data-[side=none]:data-starting-style:translate-y-0 motion-safe:data-[side=none]:data-ending-style:translate-y-0"
258
+
60
259
  // min-w-(--anchor-width) rather than w-(--anchor-width) so the popup can expand beyond the trigger to fit content
260
+ /**
261
+ * By default the list opens over its trigger and grows out of it. A list
262
+ * that opens below or above its trigger (`alignItemWithTrigger={false}`, or
263
+ * when Base UI falls back for touch or a lack of room) opens toward the
264
+ * centre of the screen, as a DropdownMenu does: a trigger in the right half
265
+ * lines the list up with its right edge, one in the left half with its left
266
+ * edge. An explicit `align` always wins.
267
+ */
61
268
  function SelectContent({
62
269
  className,
63
270
  children,
64
271
  side = "bottom",
65
272
  sideOffset = 4,
66
- align = "center",
273
+ align,
67
274
  alignOffset = 0,
68
275
  alignItemWithTrigger = true,
276
+ ref,
69
277
  ...props
70
278
  }: SelectPrimitive.Popup.Props &
71
279
  Pick<
@@ -91,24 +299,57 @@ function SelectContent({
91
299
  [children]
92
300
  )
93
301
 
302
+ const trigger = React.useContext(SelectTriggerContext)?.trigger ?? null
303
+ const getTrigger = React.useCallback(() => trigger, [trigger])
304
+ const { popupRef: alignRef, align: positionAlign } = usePopupAlign(align, side, undefined, getTrigger)
305
+
306
+ const popupRef = React.useCallback(
307
+ (node: HTMLDivElement | null) => {
308
+ alignRef(node)
309
+ const stop = growFromTrigger(node)
310
+ if (typeof ref === "function") ref(node)
311
+ else if (ref) ref.current = node
312
+ return () => {
313
+ stop()
314
+ if (typeof ref === "function") ref(null)
315
+ else if (ref) ref.current = null
316
+ }
317
+ },
318
+ [ref, alignRef]
319
+ )
320
+
321
+ const dir = usePopupDir()
94
322
  return (
95
323
  <SelectPrimitive.Portal key={childrenKey}>
96
324
  <SelectPrimitive.Positioner
325
+ dir={dir}
97
326
  side={side}
98
327
  sideOffset={sideOffset}
99
- align={align}
328
+ align={positionAlign}
100
329
  alignOffset={alignOffset}
101
330
  alignItemWithTrigger={alignItemWithTrigger}
102
- className="isolate z-50"
331
+ // Base UI gives up on placing the list over its trigger when less
332
+ // than its min-height (100px by default) fits between the trigger
333
+ // and the edge, and opens it beside the trigger instead. 1px keeps
334
+ // the list over the trigger; `fitOnScreen` then moves it into view.
335
+ className="isolate z-50 min-h-px"
103
336
  >
104
337
  <SelectPrimitive.Popup
105
338
  data-slot="select-content"
106
339
  data-align-trigger={alignItemWithTrigger}
107
- className={cn("bg-popover text-popover-foreground data-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 data-closed:zoom-out-95 data-open:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 ring-foreground/10 data-[side=inline-start]:slide-in-from-right-2 data-[side=inline-end]:slide-in-from-left-2 relative isolate z-50 max-h-(--available-height) min-w-(--anchor-width) origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-md shadow-md ring-1 motion-reduce:animate-none data-open:duration-(--transition-duration-normal) data-open:ease-(--ease-out) data-closed:duration-(--transition-duration-fast) data-closed:ease-(--ease-in) data-[align-trigger=true]:animate-none", className )}
340
+ className={cn("bg-popover text-popover-foreground shadow-overlay relative isolate z-50 max-h-(--available-height) min-w-(--anchor-width) overflow-x-hidden overflow-y-auto rounded-xl", popupMotion, growFromTriggerMotion, className)}
341
+ ref={popupRef}
342
+ // A closing list is gone for assistive technology: its trigger no
343
+ // longer points at it, and it takes no input while it fades out.
344
+ render={(popupProps, state) => (
345
+ <div {...popupProps} aria-hidden={state.open ? popupProps["aria-hidden"] : true} />
346
+ )}
108
347
  {...props}
109
348
  >
110
349
  <SelectScrollUpButton />
111
- <SelectPrimitive.List>{children}</SelectPrimitive.List>
350
+ <SelectPrimitive.List className="p-1.25" {...nameFrom(trigger, { orSelf: true })}>
351
+ {children}
352
+ </SelectPrimitive.List>
112
353
  <SelectScrollDownButton />
113
354
  </SelectPrimitive.Popup>
114
355
  </SelectPrimitive.Positioner>
@@ -138,7 +379,9 @@ function SelectItem({
138
379
  <SelectPrimitive.Item
139
380
  data-slot="select-item"
140
381
  className={cn(
141
- "focus:bg-accent focus:text-accent-foreground not-data-[variant=destructive]:focus:**:text-accent-foreground relative flex w-full cursor-default items-center gap-2 rounded-sm py-1.5 pr-8 pl-2 text-sm outline-hidden select-none data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
382
+ menuItemDisabled,
383
+ menuCheckItem,
384
+ "not-data-disabled:focus:bg-muted not-data-disabled:focus:text-accent-foreground not-data-[variant=destructive]:not-data-disabled:focus:**:text-accent-foreground relative flex min-h-8.5 w-full cursor-default items-center gap-2 rounded-lg py-1.5 pe-8 ps-2 text-sm text-foreground-secondary data-selected:text-foreground outline-hidden select-none focus-visible:outline-solid focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 [&_svg:not([class*='stroke-'])]:stroke-[1.75] [&>:first-child>svg:not([class*='opacity-'])]:opacity-55 [&>:first-child>svg:not([class*='opacity-'])]:transition-opacity [&>:first-child>svg:not([class*='opacity-'])]:duration-moderate not-data-disabled:focus:[&>:first-child>svg:not([class*='opacity-'])]:opacity-85 *:[span]:last:flex *:[span]:last:items-center *:[span]:last:gap-2",
142
385
  className
143
386
  )}
144
387
  {...props}
@@ -146,13 +389,9 @@ function SelectItem({
146
389
  <SelectPrimitive.ItemText className="flex flex-1 shrink-0 gap-2 whitespace-nowrap">
147
390
  {children}
148
391
  </SelectPrimitive.ItemText>
149
- <SelectPrimitive.ItemIndicator
150
- render={
151
- <span className="pointer-events-none absolute right-2 flex size-4 items-center justify-center" />
152
- }
153
- >
154
- <CheckIcon className="pointer-events-none" />
155
- </SelectPrimitive.ItemIndicator>
392
+ <span className="pointer-events-none absolute end-2 flex size-4 items-center justify-center">
393
+ <MenuCheck />
394
+ </span>
156
395
  </SelectPrimitive.Item>
157
396
  )
158
397
  }
@@ -164,7 +403,7 @@ function SelectSeparator({
164
403
  return (
165
404
  <SelectPrimitive.Separator
166
405
  data-slot="select-separator"
167
- className={cn("bg-border pointer-events-none -mx-1 my-1 h-px", className)}
406
+ className={cn("bg-border pointer-events-none -mx-1.25 my-1.25 h-px", className)}
168
407
  {...props}
169
408
  />
170
409
  )
@@ -6,13 +6,16 @@ import { Dialog as SheetPrimitive } from "@base-ui/react/dialog"
6
6
  import { cn } from "@vendure-io/ui/lib/utils"
7
7
  import { Button } from "@vendure-io/ui/components/atoms/button"
8
8
  import { XIcon } from "lucide-react"
9
+ import { usePopupDir } from "../../lib/popup-direction.ts"
10
+ import { usePinnedHeight } from "../../hooks/use-pinned-height.ts"
11
+ import { triggerSlot } from "../../lib/trigger-slot.ts"
9
12
 
10
13
  function Sheet({ ...props }: SheetPrimitive.Root.Props) {
11
14
  return <SheetPrimitive.Root data-slot="sheet" {...props} />
12
15
  }
13
16
 
14
17
  function SheetTrigger({ ...props }: SheetPrimitive.Trigger.Props) {
15
- return <SheetPrimitive.Trigger data-slot="sheet-trigger" {...props} />
18
+ return <SheetPrimitive.Trigger {...triggerSlot("sheet-trigger", props.render)} {...props} />
16
19
  }
17
20
 
18
21
  function SheetClose({ ...props }: SheetPrimitive.Close.Props) {
@@ -28,7 +31,7 @@ function SheetOverlay({ className, ...props }: SheetPrimitive.Backdrop.Props) {
28
31
  <SheetPrimitive.Backdrop
29
32
  data-slot="sheet-overlay"
30
33
  className={cn(
31
- "data-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 fixed inset-0 z-50 bg-scrim motion-reduce:animate-none data-open:duration-(--transition-duration-normal) data-open:ease-(--ease-out) data-closed:duration-(--transition-duration-fast) data-closed:ease-(--ease-in) data-ending-style:opacity-0 data-starting-style:opacity-0 supports-backdrop-filter:backdrop-blur-xs",
34
+ "fixed inset-0 z-50 bg-scrim transition-opacity duration-normal ease-drawer data-starting-style:opacity-0 data-ending-style:opacity-0 data-ending-style:duration-fast supports-backdrop-filter:backdrop-blur-xs",
32
35
  className
33
36
  )}
34
37
  {...props}
@@ -36,65 +39,115 @@ function SheetOverlay({ className, ...props }: SheetPrimitive.Backdrop.Props) {
36
39
  )
37
40
  }
38
41
 
42
+ // Tells SheetHeader to host the close button, so the ✕ stays pinned with the
43
+ // header while the body scrolls.
44
+ const SheetCloseButtonContext = React.createContext(false)
45
+
46
+ function SheetCloseButton({ className }: { className?: string }) {
47
+ return (
48
+ <SheetPrimitive.Close
49
+ data-slot="sheet-close"
50
+ render={
51
+ <Button
52
+ variant="ghost"
53
+ className={cn("absolute top-4 end-4", className)}
54
+ size="icon-sm"
55
+ />
56
+ }
57
+ >
58
+ <XIcon
59
+ />
60
+ <span className="sr-only">Close</span>
61
+ </SheetPrimitive.Close>
62
+ )
63
+ }
64
+
65
+ // The width of a left or right sheet per size, as whole class strings so
66
+ // Tailwind finds them in this source. A top or bottom sheet spans the viewport
67
+ // and ignores the size. Below `sm` every side sheet is 3/4 of the viewport.
68
+ const sheetSizes = {
69
+ default: "data-[side=left]:sm:max-w-lg data-[side=right]:sm:max-w-lg",
70
+ xl: "data-[side=left]:sm:max-w-200 data-[side=right]:sm:max-w-200",
71
+ } as const
72
+
39
73
  function SheetContent({
40
74
  className,
41
75
  children,
42
76
  side = "right",
77
+ size = "default",
43
78
  showCloseButton = true,
44
79
  ...props
45
80
  }: SheetPrimitive.Popup.Props & {
46
81
  side?: "top" | "right" | "bottom" | "left"
82
+ /**
83
+ * The width of a left or right sheet. `default` (512px): a short form or a
84
+ * settings list. `xl` (800px): a table or a wide form. Both stay at 3/4 of
85
+ * the viewport below 640px. Top and bottom sheets ignore it.
86
+ */
87
+ size?: keyof typeof sheetSizes
47
88
  showCloseButton?: boolean
48
89
  }) {
90
+ const dir = usePopupDir()
49
91
  return (
50
92
  <SheetPortal>
51
93
  <SheetOverlay />
94
+ {/* A sheet taller than the viewport scrolls inside; SheetHeader and SheetFooter stick to its edges, and the scroll padding keeps a focused field clear of both. */}
52
95
  <SheetPrimitive.Popup
96
+ dir={dir}
53
97
  data-slot="sheet-content"
54
98
  data-side={side}
99
+ data-size={size}
55
100
  className={cn(
56
- "bg-overlay data-open:animate-in data-closed:animate-out data-[side=right]:data-closed:slide-out-to-right-10 data-[side=right]:data-open:slide-in-from-right-10 data-[side=left]:data-closed:slide-out-to-left-10 data-[side=left]:data-open:slide-in-from-left-10 data-[side=top]:data-closed:slide-out-to-top-10 data-[side=top]:data-open:slide-in-from-top-10 data-closed:fade-out-0 data-open:fade-in-0 data-[side=bottom]:data-closed:slide-out-to-bottom-10 data-[side=bottom]:data-open:slide-in-from-bottom-10 fixed z-50 flex flex-col gap-4 bg-clip-padding text-sm shadow-lg transition motion-reduce:animate-none motion-reduce:transition-none data-open:duration-(--transition-duration-normal) data-open:ease-(--ease-out) data-closed:duration-(--transition-duration-fast) data-closed:ease-(--ease-in) data-[side=bottom]:inset-x-0 data-[side=bottom]:bottom-0 data-[side=bottom]:h-auto data-[side=bottom]:border-t data-[side=left]:inset-y-0 data-[side=left]:left-0 data-[side=left]:h-full data-[side=left]:w-3/4 data-[side=left]:border-r data-[side=right]:inset-y-0 data-[side=right]:right-0 data-[side=right]:h-full data-[side=right]:w-3/4 data-[side=right]:border-l data-[side=top]:inset-x-0 data-[side=top]:top-0 data-[side=top]:h-auto data-[side=top]:border-b data-[side=left]:sm:max-w-lg data-[side=right]:sm:max-w-lg",
101
+ "group/sheet-content bg-overlay fixed z-50 flex flex-col gap-4 overflow-y-auto overscroll-contain [scroll-padding-block:calc(var(--pinned-header-height,0px)+--spacing(1))_calc(var(--pinned-footer-height,0px)+--spacing(1))] bg-clip-padding text-sm shadow-overlay transition-[opacity,translate] duration-normal ease-drawer data-starting-style:opacity-0 data-ending-style:opacity-0 data-ending-style:duration-fast motion-safe:data-[side=right]:data-starting-style:translate-x-10 motion-safe:data-[side=right]:data-ending-style:translate-x-10 motion-safe:data-[side=left]:data-starting-style:-translate-x-10 motion-safe:data-[side=left]:data-ending-style:-translate-x-10 motion-safe:data-[side=top]:data-starting-style:-translate-y-10 motion-safe:data-[side=top]:data-ending-style:-translate-y-10 motion-safe:data-[side=bottom]:data-starting-style:translate-y-10 motion-safe:data-[side=bottom]:data-ending-style:translate-y-10 data-[side=bottom]:inset-x-0 data-[side=bottom]:bottom-0 data-[side=bottom]:h-auto data-[side=bottom]:max-h-dvh data-[side=left]:inset-y-0 data-[side=left]:left-0 data-[side=left]:h-full data-[side=left]:w-3/4 data-[side=right]:inset-y-0 data-[side=right]:right-0 data-[side=right]:h-full data-[side=right]:w-3/4 data-[side=top]:inset-x-0 data-[side=top]:top-0 data-[side=top]:h-auto data-[side=top]:max-h-dvh",
102
+ sheetSizes[size],
57
103
  className
58
104
  )}
59
105
  {...props}
60
106
  >
61
- {children}
107
+ <SheetCloseButtonContext.Provider value={showCloseButton}>
108
+ {children}
109
+ </SheetCloseButtonContext.Provider>
110
+ {/* A sheet without a SheetHeader keeps the ✕ here, in the corner. */}
62
111
  {showCloseButton && (
63
- <SheetPrimitive.Close
64
- data-slot="sheet-close"
65
- render={
66
- <Button
67
- variant="ghost"
68
- className="absolute top-4 right-4"
69
- size="icon-sm"
70
- />
71
- }
72
- >
73
- <XIcon
74
- />
75
- <span className="sr-only">Close</span>
76
- </SheetPrimitive.Close>
112
+ <SheetCloseButton className="group-has-data-[slot=sheet-header]/sheet-content:hidden" />
77
113
  )}
78
114
  </SheetPrimitive.Popup>
79
115
  </SheetPortal>
80
116
  )
81
117
  }
82
118
 
83
- function SheetHeader({ className, ...props }: React.ComponentProps<"div">) {
119
+ // The header and the footer stick to the sheet's edges with its surface behind,
120
+ // so a scrolled body passes under them. Their own 16px padding keeps the body
121
+ // clear of their text. Each measures its height for the sheet's scroll padding.
122
+ function SheetHeader({ className, children, ref, ...props }: React.ComponentProps<"div">) {
123
+ const showCloseButton = React.useContext(SheetCloseButtonContext)
124
+ const pinnedRef = usePinnedHeight("--pinned-header-height", "sheet-content", ref)
84
125
  return (
85
126
  <div
127
+ ref={pinnedRef}
86
128
  data-slot="sheet-header"
87
- className={cn("flex flex-col gap-1.5 p-4", className)}
129
+ className={cn(
130
+ "bg-overlay sticky top-0 z-10 flex shrink-0 flex-col gap-1.5 p-4",
131
+ className
132
+ )}
88
133
  {...props}
89
- />
134
+ >
135
+ {children}
136
+ {showCloseButton && <SheetCloseButton />}
137
+ </div>
90
138
  )
91
139
  }
92
140
 
93
- function SheetFooter({ className, ...props }: React.ComponentProps<"div">) {
141
+ function SheetFooter({ className, ref, ...props }: React.ComponentProps<"div">) {
142
+ const pinnedRef = usePinnedHeight("--pinned-footer-height", "sheet-content", ref)
94
143
  return (
95
144
  <div
145
+ ref={pinnedRef}
96
146
  data-slot="sheet-footer"
97
- className={cn("mt-auto flex flex-col gap-2 p-4", className)}
147
+ className={cn(
148
+ "bg-overlay sticky bottom-0 z-10 mt-auto flex shrink-0 flex-col gap-2 p-4",
149
+ className
150
+ )}
98
151
  {...props}
99
152
  />
100
153
  )