@dinachi/cli 1.0.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +69 -0
- package/dist/index.js +244 -72
- package/package.json +5 -1
- package/templates/accordion/accordion.tsx +5 -1
- package/templates/agent/agent.tsx +35 -18
- package/templates/alert/alert.tsx +345 -0
- package/templates/alert/index.ts +16 -0
- package/templates/alert-dialog/alert-dialog.tsx +14 -3
- package/templates/animated-icon/animated-icon.tsx +1 -0
- package/templates/animated-list/animated-list.tsx +2 -1
- package/templates/animated-tabs/animated-tabs.tsx +4 -0
- package/templates/ask-bar/ask-bar.tsx +1061 -0
- package/templates/ask-bar/index.ts +1 -0
- package/templates/autocomplete/autocomplete.tsx +14 -4
- package/templates/avatar/avatar.tsx +3 -0
- package/templates/badge/badge.tsx +1 -0
- package/templates/breadcrumb/breadcrumb.tsx +606 -0
- package/templates/breadcrumb/index.ts +9 -0
- package/templates/button/button.tsx +1 -0
- package/templates/calendar/calendar.tsx +1155 -0
- package/templates/calendar/index.ts +15 -0
- package/templates/card/card.tsx +6 -1
- package/templates/carousel/carousel.tsx +7 -0
- package/templates/checkbox/checkbox.tsx +1 -0
- package/templates/checkbox-group/checkbox-group.tsx +1 -0
- package/templates/collapsible/collapsible.tsx +3 -1
- package/templates/combobox/combobox.tsx +15 -4
- package/templates/command-items/command-items.tsx +69 -0
- package/templates/command-items/index.ts +1 -0
- package/templates/command-palette/command-palette.tsx +954 -0
- package/templates/command-palette/index.ts +1 -0
- package/templates/compare-slider/compare-slider.tsx +1 -0
- package/templates/comparison-table/comparison-table.tsx +540 -0
- package/templates/comparison-table/index.ts +15 -0
- package/templates/context-menu/context-menu.tsx +61 -14
- package/templates/data-table/data-table.tsx +2077 -0
- package/templates/data-table/index.ts +14 -0
- package/templates/date-picker/date-picker.tsx +527 -0
- package/templates/date-picker/index.ts +2 -0
- package/templates/date-range-picker/date-range-picker.tsx +504 -0
- package/templates/date-range-picker/index.ts +2 -0
- package/templates/detail-list/detail-list.tsx +419 -0
- package/templates/detail-list/index.ts +12 -0
- package/templates/dialog/dialog.tsx +13 -3
- package/templates/drawer/drawer.tsx +19 -6
- package/templates/expandable-card/expandable-card.tsx +5 -1
- package/templates/field/field.tsx +5 -1
- package/templates/fieldset/fieldset.tsx +2 -0
- package/templates/form/form.tsx +1 -0
- package/templates/heat-table/heat-table.tsx +613 -0
- package/templates/heat-table/index.ts +9 -0
- package/templates/hold-to-confirm/hold-to-confirm.tsx +1 -0
- package/templates/input/input.tsx +1 -0
- package/templates/label/label.tsx +1 -0
- package/templates/link/link.tsx +1 -0
- package/templates/load-transition/load-transition.tsx +1 -0
- package/templates/marquee/marquee.tsx +1 -0
- package/templates/menu/menu.tsx +27 -6
- package/templates/menubar/menubar.tsx +15 -3
- package/templates/meter/meter.tsx +5 -1
- package/templates/month-picker/index.ts +1 -0
- package/templates/month-picker/month-picker.tsx +351 -0
- package/templates/navigation-menu/navigation-menu.tsx +13 -2
- package/templates/number-field/number-field.tsx +7 -1
- package/templates/number-ticker/number-ticker.tsx +3 -0
- package/templates/omnibar/index.ts +1 -0
- package/templates/omnibar/omnibar.tsx +1009 -0
- package/templates/otp-field/otp-field.tsx +4 -0
- package/templates/pagination/index.ts +8 -0
- package/templates/pagination/pagination.tsx +389 -0
- package/templates/popover/popover.tsx +19 -5
- package/templates/preview-card/preview-card.tsx +11 -3
- package/templates/progress/progress.tsx +5 -1
- package/templates/progress-ring/progress-ring.tsx +1 -0
- package/templates/radio/radio.tsx +3 -0
- package/templates/scroll-area/scroll-area.tsx +14 -4
- package/templates/scroll-progress/scroll-progress.tsx +1 -0
- package/templates/scroll-reveal/scroll-reveal.tsx +1 -0
- package/templates/select/select.tsx +18 -2
- package/templates/separator/separator.tsx +1 -0
- package/templates/skeleton/skeleton.tsx +1 -0
- package/templates/slider/slider.tsx +7 -0
- package/templates/sortable/sortable.tsx +3 -0
- package/templates/stagger-list/stagger-list.tsx +2 -0
- package/templates/streaming-text/streaming-text.tsx +1 -0
- package/templates/swipeable-row/swipeable-row.tsx +1 -0
- package/templates/switch/switch.tsx +2 -0
- package/templates/table/index.ts +13 -0
- package/templates/table/table.tsx +265 -0
- package/templates/tabs/tabs.tsx +5 -0
- package/templates/text/text.tsx +1 -0
- package/templates/text-morph/text-morph.tsx +2 -0
- package/templates/text-shimmer/text-shimmer.tsx +1 -0
- package/templates/textarea/textarea.tsx +1 -0
- package/templates/time-slots/index.ts +1 -0
- package/templates/time-slots/time-slots.tsx +589 -0
- package/templates/toast/toast.tsx +10 -1
- package/templates/toggle/toggle.tsx +1 -0
- package/templates/toggle-group/toggle-group.tsx +2 -0
- package/templates/toolbar/toolbar.tsx +6 -0
- package/templates/tooltip/tooltip.tsx +9 -3
- package/templates/week-strip/index.ts +1 -0
- package/templates/week-strip/week-strip.tsx +329 -0
- package/templates/year-view/index.ts +1 -0
- package/templates/year-view/year-view.tsx +362 -0
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import * as React from "react"
|
|
4
|
+
import type { useRender } from "@base-ui/react/use-render"
|
|
5
|
+
import { ChevronLeft, ChevronRight, ListTree } from "lucide-react"
|
|
6
|
+
import { cn } from "@/lib/utils"
|
|
7
|
+
import {
|
|
8
|
+
AgentProvider,
|
|
9
|
+
changeAsAgent,
|
|
10
|
+
matchAgentChoice,
|
|
11
|
+
useAgentControl,
|
|
12
|
+
useHasAgent,
|
|
13
|
+
useMergedRef,
|
|
14
|
+
waitForAgent,
|
|
15
|
+
type Agent,
|
|
16
|
+
} from "@/components/ui/agent"
|
|
17
|
+
import { Drawer, DrawerContent, DrawerTitle, DrawerTrigger } from "@/components/ui/drawer"
|
|
18
|
+
import { Link } from "@/components/ui/link"
|
|
19
|
+
import { Menu, MenuContent, MenuLinkItem, MenuTrigger } from "@/components/ui/menu"
|
|
20
|
+
|
|
21
|
+
export interface BreadcrumbItem {
|
|
22
|
+
label: string
|
|
23
|
+
/** Where the level lives. Without one the level is plain text. */
|
|
24
|
+
href?: string
|
|
25
|
+
/** Renders the link as your router's, e.g. `<NextLink href="/rooms" />`. */
|
|
26
|
+
render?: useRender.RenderProp
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Where a move came from. `imperative-action` is an agent's `goTo`. */
|
|
30
|
+
export type BreadcrumbNavigateReason = "link-press" | "menu-press" | "back-press" | "sheet-press" | "imperative-action"
|
|
31
|
+
|
|
32
|
+
export interface BreadcrumbNavigateDetails {
|
|
33
|
+
reason: BreadcrumbNavigateReason | (string & {})
|
|
34
|
+
/** The click, an agent's included. `event.preventDefault()` stops the link for a move your router makes. */
|
|
35
|
+
event?: Event
|
|
36
|
+
isCanceled: boolean
|
|
37
|
+
cancel(): void
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface BreadcrumbProps extends Omit<React.HTMLAttributes<HTMLElement>, "children"> {
|
|
41
|
+
/** From the root to the current page. The last item is the current page. */
|
|
42
|
+
items: BreadcrumbItem[]
|
|
43
|
+
/** Leaves the current page out of the trail, for pages whose heading already names it. */
|
|
44
|
+
hideCurrent?: boolean
|
|
45
|
+
/** `trail` lists the levels. `back` is one link to the parent, with the trail in a sheet. `auto` is `back` below 768px. */
|
|
46
|
+
presentation?: "auto" | "trail" | "back"
|
|
47
|
+
/** Draws `back` as frosted pills that read over a photo. */
|
|
48
|
+
floating?: boolean
|
|
49
|
+
/**
|
|
50
|
+
* Runs before every move, for people and agents. `details.reason` says where it came from,
|
|
51
|
+
* `imperative-action` for an agent. `details.cancel()` refuses the move. To make it through
|
|
52
|
+
* your router, call it and `details.event.preventDefault()`: the move still counts as done.
|
|
53
|
+
*/
|
|
54
|
+
onNavigate?: (item: BreadcrumbItem, details: BreadcrumbNavigateDetails) => void
|
|
55
|
+
disabled?: boolean
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function navigateDetails(reason: BreadcrumbNavigateReason, event?: Event): BreadcrumbNavigateDetails {
|
|
59
|
+
const details: BreadcrumbNavigateDetails = {
|
|
60
|
+
reason,
|
|
61
|
+
event,
|
|
62
|
+
isCanceled: false,
|
|
63
|
+
cancel() {
|
|
64
|
+
details.isCanceled = true
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
return details
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface BreadcrumbFitInput {
|
|
71
|
+
/** Each shown level's width, root first. */
|
|
72
|
+
widths: number[]
|
|
73
|
+
separator: number
|
|
74
|
+
ellipsis: number
|
|
75
|
+
/** The flex gap between every two parts. */
|
|
76
|
+
gap?: number
|
|
77
|
+
available: number
|
|
78
|
+
/** The fewest levels kept after the collapsed ones. */
|
|
79
|
+
minKeep: number
|
|
80
|
+
/** The narrowest a level may truncate to. When even the fewest levels cannot keep this, returns 0. */
|
|
81
|
+
readable?: number
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* How many levels to keep after the root, collapsing the ones between into a menu. Keeps
|
|
86
|
+
* as many as fit without truncating any; at `widths.length` nothing collapses.
|
|
87
|
+
*/
|
|
88
|
+
export function breadcrumbFit({ widths, separator, ellipsis, gap = 0, available, minKeep, readable }: BreadcrumbFitInput): number {
|
|
89
|
+
const n = widths.length
|
|
90
|
+
if (n < 2) return n
|
|
91
|
+
// Whole pixels, rounded up, so a level that is kept is never a fraction short.
|
|
92
|
+
const up = Math.ceil
|
|
93
|
+
// The width of the root, then `keep` levels from the end, folded between when they don't meet.
|
|
94
|
+
const row = (keep: number, w: (x: number) => number = up) => {
|
|
95
|
+
const folded = keep < n - 1
|
|
96
|
+
const levels = [0, ...widths.slice(n - keep).map((_, i) => n - keep + i)]
|
|
97
|
+
const parts = levels.length * 2 - 1 + (folded ? 2 : 0)
|
|
98
|
+
return (
|
|
99
|
+
levels.reduce((a, i) => a + w(widths[i]), 0) +
|
|
100
|
+
(levels.length - 1 + (folded ? 1 : 0)) * up(separator) +
|
|
101
|
+
(folded ? up(ellipsis) : 0) +
|
|
102
|
+
Math.max(0, parts - 1) * gap
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
if (row(n - 1) <= available) return n
|
|
106
|
+
for (let keep = n - 2; keep >= Math.max(1, minKeep); keep--) {
|
|
107
|
+
if (row(keep) <= available) return keep
|
|
108
|
+
}
|
|
109
|
+
const keep = n - 1 <= minKeep ? n : Math.max(1, minKeep)
|
|
110
|
+
if (readable && row(Math.min(keep, n - 1), (x) => Math.min(up(x), readable)) > available) return 0
|
|
111
|
+
return keep
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const WIDE = "(min-width: 768px)"
|
|
115
|
+
// Below this a truncated level stops reading as a name; `auto` turns to `back` instead.
|
|
116
|
+
const READABLE = 64
|
|
117
|
+
|
|
118
|
+
/** `both` until the client knows the width: CSS shows the right one, so phones never flash the trail. */
|
|
119
|
+
function usePresentation(presentation: "auto" | "trail" | "back"): "trail" | "back" | "both" {
|
|
120
|
+
const wide = React.useSyncExternalStore<boolean | null>(
|
|
121
|
+
(onChange) => {
|
|
122
|
+
const query = window.matchMedia?.(WIDE)
|
|
123
|
+
query?.addEventListener("change", onChange)
|
|
124
|
+
return () => query?.removeEventListener("change", onChange)
|
|
125
|
+
},
|
|
126
|
+
() => window.matchMedia?.(WIDE).matches ?? true,
|
|
127
|
+
() => null
|
|
128
|
+
)
|
|
129
|
+
if (presentation !== "auto") return presentation
|
|
130
|
+
return wide === null ? "both" : wide ? "trail" : "back"
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The menu and sheet are part of the breadcrumb to an agent, so they don't register their own. */
|
|
134
|
+
const Unlisted = ({ children }: { children: React.ReactNode }) => (
|
|
135
|
+
<AgentProvider agent={null as unknown as Agent}>{children}</AgentProvider>
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
const crumb = "block truncate rounded-md px-1.5 py-[3px]"
|
|
139
|
+
// Inset, so the list's overflow and the sheet's scroll area cannot clip it.
|
|
140
|
+
const ring = "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring focus-visible:ring-offset-0"
|
|
141
|
+
const inert = "aria-disabled:pointer-events-none aria-disabled:opacity-50"
|
|
142
|
+
// Levels before the last are capped, so one long name cannot push the rest out.
|
|
143
|
+
const capped = "max-w-[12rem] focus-visible:max-w-none"
|
|
144
|
+
const linkClass = cn(crumb, ring, inert, "text-muted-foreground hover:bg-muted hover:text-foreground")
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Where a page sits, as a trail of links up to it. Middle levels fold into a menu as the
|
|
148
|
+
* container narrows; on phones it is one back link with the whole trail in a sheet.
|
|
149
|
+
*/
|
|
150
|
+
const Breadcrumb = React.forwardRef<HTMLElement, BreadcrumbProps>((props, ref) => {
|
|
151
|
+
const {
|
|
152
|
+
items,
|
|
153
|
+
hideCurrent = false,
|
|
154
|
+
presentation: presentationProp = "auto",
|
|
155
|
+
floating = false,
|
|
156
|
+
onNavigate,
|
|
157
|
+
disabled,
|
|
158
|
+
className,
|
|
159
|
+
"aria-label": ariaLabel = "Breadcrumb",
|
|
160
|
+
...rest
|
|
161
|
+
} = props
|
|
162
|
+
const presentation = usePresentation(presentationProp)
|
|
163
|
+
const hasAgent = useHasAgent()
|
|
164
|
+
const part = hasAgent ? "" : undefined
|
|
165
|
+
const nav = React.useRef<HTMLElement | null>(null)
|
|
166
|
+
const navRef = useMergedRef(ref, nav)
|
|
167
|
+
const menuPopup = React.useRef<HTMLDivElement | null>(null)
|
|
168
|
+
const sheetPopup = React.useRef<HTMLDivElement | null>(null)
|
|
169
|
+
const [menuOpen, setMenuOpen] = React.useState(false)
|
|
170
|
+
const [sheetOpen, setSheetOpen] = React.useState(false)
|
|
171
|
+
// The details an agent's press carries into the click it makes.
|
|
172
|
+
const pending = React.useRef<BreadcrumbNavigateDetails | null>(null)
|
|
173
|
+
|
|
174
|
+
const last = items.length - 1
|
|
175
|
+
const shown = hideCurrent ? items.slice(0, -1) : items
|
|
176
|
+
const n = shown.length
|
|
177
|
+
const minKeep = hideCurrent ? 1 : 2
|
|
178
|
+
const auto = presentationProp === "auto"
|
|
179
|
+
|
|
180
|
+
/* ---- fitting ---- */
|
|
181
|
+
const measure = React.useRef<HTMLSpanElement | null>(null)
|
|
182
|
+
// 0 when even the fewest levels would be unreadable: `auto` then shows `back`.
|
|
183
|
+
const [keep, setKeep] = React.useState(n)
|
|
184
|
+
// The full trail's width, so a shrink-to-fit container has room to grow back into it.
|
|
185
|
+
const [need, setNeed] = React.useState(0)
|
|
186
|
+
const compact = auto && keep === 0
|
|
187
|
+
const view = compact && presentation !== "back" ? "back" : presentation
|
|
188
|
+
const labels = shown.map((i) => i.label).join("\u0000")
|
|
189
|
+
const fit = React.useCallback(() => {
|
|
190
|
+
const box = nav.current
|
|
191
|
+
const m = measure.current
|
|
192
|
+
if (!box || !m) return
|
|
193
|
+
const width = (el: Element | null) => el?.getBoundingClientRect().width ?? 0
|
|
194
|
+
// The nav's content box, the same in either view. Zero while hidden by CSS.
|
|
195
|
+
const style = getComputedStyle(box)
|
|
196
|
+
const edges = ["paddingLeft", "paddingRight", "borderLeftWidth", "borderRightWidth"] as const
|
|
197
|
+
const available = width(box) - edges.reduce((a, k) => a + (parseFloat(style[k]) || 0), 0)
|
|
198
|
+
if (available <= 0) return
|
|
199
|
+
const widths = Array.from(m.querySelectorAll("[data-measure-item]"), width)
|
|
200
|
+
const gap = parseFloat(getComputedStyle(m).columnGap) || 0
|
|
201
|
+
const separator = width(m.querySelector("[data-measure-separator]"))
|
|
202
|
+
const next = breadcrumbFit({
|
|
203
|
+
widths,
|
|
204
|
+
separator,
|
|
205
|
+
ellipsis: width(m.querySelector("[data-measure-ellipsis]")),
|
|
206
|
+
gap,
|
|
207
|
+
available,
|
|
208
|
+
minKeep,
|
|
209
|
+
readable: auto ? READABLE : undefined,
|
|
210
|
+
})
|
|
211
|
+
setKeep((k) => (k === next ? k : next))
|
|
212
|
+
const full = widths.reduce((a, w) => a + Math.ceil(w), 0) + Math.max(0, widths.length - 1) * (Math.ceil(separator) + 2 * gap)
|
|
213
|
+
setNeed((w) => (w === full ? w : full))
|
|
214
|
+
}, [minKeep, auto])
|
|
215
|
+
|
|
216
|
+
// Either view is as wide as the full trail could be, so switching cannot change what is measured.
|
|
217
|
+
React.useLayoutEffect(() => {
|
|
218
|
+
if (presentation === "back") return
|
|
219
|
+
fit()
|
|
220
|
+
if (typeof ResizeObserver === "undefined") return
|
|
221
|
+
const observer = new ResizeObserver(() => fit())
|
|
222
|
+
if (nav.current) observer.observe(nav.current)
|
|
223
|
+
if (measure.current) observer.observe(measure.current)
|
|
224
|
+
return () => observer.disconnect()
|
|
225
|
+
}, [fit, labels, presentation, compact])
|
|
226
|
+
|
|
227
|
+
const collapsed = keep < n - 1
|
|
228
|
+
const hiddenFrom = 1
|
|
229
|
+
const hiddenTo = collapsed ? n - keep : 1
|
|
230
|
+
const visible = collapsed ? [0, ...shown.slice(n - keep).map((_, i) => n - keep + i)] : shown.map((_, i) => i)
|
|
231
|
+
|
|
232
|
+
/* ---- navigating ---- */
|
|
233
|
+
const press = (index: number, reason: BreadcrumbNavigateReason) => (e: React.MouseEvent) => {
|
|
234
|
+
if (disabled || !items[index]?.href) return e.preventDefault()
|
|
235
|
+
const details = pending.current ?? navigateDetails(reason, e.nativeEvent)
|
|
236
|
+
pending.current = null
|
|
237
|
+
// An agent's details too, so `event.preventDefault()` stops the link for either.
|
|
238
|
+
details.event = e.nativeEvent
|
|
239
|
+
onNavigate?.(items[index], details)
|
|
240
|
+
if (details.isCanceled) e.preventDefault()
|
|
241
|
+
if (reason === "sheet-press") setSheetOpen(false)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/* ---- agent ---- */
|
|
245
|
+
const choices = items.map((item, i) => ({ value: i + 1, label: item.label, disabled: i !== last && !item.href }))
|
|
246
|
+
const elementFor = (index: number, scope: ParentNode | null | undefined) =>
|
|
247
|
+
scope?.querySelector<HTMLElement>(`[data-breadcrumb-index="${index}"]`) ?? null
|
|
248
|
+
|
|
249
|
+
useAgentControl(() => ({
|
|
250
|
+
kind: "list",
|
|
251
|
+
element: nav.current,
|
|
252
|
+
value: items[last]?.label,
|
|
253
|
+
items: choices.map((c) => c.label),
|
|
254
|
+
disabled,
|
|
255
|
+
actions: {
|
|
256
|
+
goTo: async (wanted: unknown) => {
|
|
257
|
+
const match = matchAgentChoice(choices, wanted)
|
|
258
|
+
if (typeof match === "string") return match
|
|
259
|
+
const index = match.value - 1
|
|
260
|
+
if (index === last) return undefined
|
|
261
|
+
let el = elementFor(index, nav.current)
|
|
262
|
+
if (!el) {
|
|
263
|
+
const sheet = view === "back"
|
|
264
|
+
if (sheet) setSheetOpen(true)
|
|
265
|
+
else setMenuOpen(true)
|
|
266
|
+
el = await waitForAgent(() => elementFor(index, (sheet ? sheetPopup : menuPopup).current))
|
|
267
|
+
}
|
|
268
|
+
if (!el) return `"${match.label}" did not appear.`
|
|
269
|
+
return changeAsAgent<number, BreadcrumbNavigateDetails>((_, details) => {
|
|
270
|
+
pending.current = details
|
|
271
|
+
el!.click()
|
|
272
|
+
pending.current = null
|
|
273
|
+
}, index)
|
|
274
|
+
},
|
|
275
|
+
},
|
|
276
|
+
}))
|
|
277
|
+
|
|
278
|
+
const hold = useHold(() => setSheetOpen(true), disabled)
|
|
279
|
+
|
|
280
|
+
/* ---- parts ---- */
|
|
281
|
+
const link = (
|
|
282
|
+
index: number,
|
|
283
|
+
reason: BreadcrumbNavigateReason,
|
|
284
|
+
className: string,
|
|
285
|
+
extra: React.ComponentProps<typeof Link> = {}
|
|
286
|
+
) => {
|
|
287
|
+
const item = items[index]
|
|
288
|
+
const onClick = press(index, reason)
|
|
289
|
+
return (
|
|
290
|
+
<Link
|
|
291
|
+
variant="unstyled"
|
|
292
|
+
href={item.href}
|
|
293
|
+
render={item.render}
|
|
294
|
+
title={item.label}
|
|
295
|
+
aria-disabled={disabled || undefined}
|
|
296
|
+
data-slot="breadcrumb-link"
|
|
297
|
+
data-breadcrumb-index={index}
|
|
298
|
+
data-agent-part={part}
|
|
299
|
+
{...extra}
|
|
300
|
+
onClick={(e: React.MouseEvent<HTMLAnchorElement>) => {
|
|
301
|
+
extra.onClick?.(e)
|
|
302
|
+
if (!e.defaultPrevented) onClick(e)
|
|
303
|
+
}}
|
|
304
|
+
className={className}
|
|
305
|
+
>
|
|
306
|
+
{extra.children ?? item.label}
|
|
307
|
+
</Link>
|
|
308
|
+
)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Every level at its natural width, read to decide what folds.
|
|
312
|
+
const measurer = (
|
|
313
|
+
<span
|
|
314
|
+
ref={measure}
|
|
315
|
+
aria-hidden="true"
|
|
316
|
+
data-agent-hidden={part}
|
|
317
|
+
className="pointer-events-none invisible absolute left-0 top-0 flex h-0 gap-0.5 overflow-hidden whitespace-nowrap text-[13px]"
|
|
318
|
+
>
|
|
319
|
+
{shown.map((item, i) => (
|
|
320
|
+
<span
|
|
321
|
+
key={i}
|
|
322
|
+
data-measure-item=""
|
|
323
|
+
className={cn(crumb, "shrink-0 overflow-visible", i < n - 1 && "max-w-[12rem]", !hideCurrent && i === last && "font-medium")}
|
|
324
|
+
>
|
|
325
|
+
{item.label}
|
|
326
|
+
</span>
|
|
327
|
+
))}
|
|
328
|
+
<span data-measure-separator="" className="flex shrink-0">
|
|
329
|
+
<ChevronRight className="size-3.5" />
|
|
330
|
+
</span>
|
|
331
|
+
<span data-measure-ellipsis="" className={cn(crumb, "shrink-0 tracking-[1px]")}>
|
|
332
|
+
…
|
|
333
|
+
</span>
|
|
334
|
+
</span>
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
// Up to the full trail wide but never wider than the container: a zero-height grid
|
|
338
|
+
// track whose min-content is 0 and max-content is `need`.
|
|
339
|
+
const sizer = need > 0 && (
|
|
340
|
+
<span aria-hidden="true" className="invisible col-span-2 grid h-0" style={{ gridTemplateColumns: `minmax(0,${need}px)` }} />
|
|
341
|
+
)
|
|
342
|
+
|
|
343
|
+
const backView = (viewRef: React.Ref<HTMLElement> | undefined, only?: string) => {
|
|
344
|
+
if (last < 1) return null
|
|
345
|
+
// The nearest level up that can be gone to.
|
|
346
|
+
let parentIndex = last - 1
|
|
347
|
+
while (parentIndex > 0 && !items[parentIndex].href) parentIndex--
|
|
348
|
+
const parent = items[parentIndex]
|
|
349
|
+
return (
|
|
350
|
+
<nav
|
|
351
|
+
{...rest}
|
|
352
|
+
ref={viewRef}
|
|
353
|
+
aria-label={ariaLabel}
|
|
354
|
+
data-slot="breadcrumb"
|
|
355
|
+
data-presentation="back"
|
|
356
|
+
data-floating={floating || undefined}
|
|
357
|
+
className={cn("relative grid w-full min-w-0 grid-cols-[minmax(0,1fr)_auto] items-center gap-x-1", className, only)}
|
|
358
|
+
>
|
|
359
|
+
{parent.href ? (
|
|
360
|
+
link(parentIndex, "back-press", backClass(floating), {
|
|
361
|
+
...hold,
|
|
362
|
+
"data-slot": "breadcrumb-back",
|
|
363
|
+
children: backLabel(parent.label),
|
|
364
|
+
} as React.ComponentProps<typeof Link>)
|
|
365
|
+
) : (
|
|
366
|
+
<span data-slot="breadcrumb-back" className={cn(backClass(floating), "text-muted-foreground")}>
|
|
367
|
+
<span className="truncate">{parent.label}</span>
|
|
368
|
+
</span>
|
|
369
|
+
)}
|
|
370
|
+
<Unlisted>
|
|
371
|
+
<Drawer open={sheetOpen} onOpenChange={setSheetOpen}>
|
|
372
|
+
<DrawerTrigger
|
|
373
|
+
disabled={disabled}
|
|
374
|
+
aria-label="Show full path"
|
|
375
|
+
data-slot="breadcrumb-path"
|
|
376
|
+
data-agent-part={part}
|
|
377
|
+
className={cn(
|
|
378
|
+
"grid size-11 shrink-0 cursor-pointer place-items-center rounded-[10px] text-muted-foreground",
|
|
379
|
+
ring,
|
|
380
|
+
"disabled:pointer-events-none disabled:opacity-50",
|
|
381
|
+
floating && cn("size-10 rounded-full text-foreground", frosted)
|
|
382
|
+
)}
|
|
383
|
+
>
|
|
384
|
+
<ListTree aria-hidden className="size-5" />
|
|
385
|
+
</DrawerTrigger>
|
|
386
|
+
<DrawerContent
|
|
387
|
+
ref={sheetPopup}
|
|
388
|
+
side="bottom"
|
|
389
|
+
data-slot="breadcrumb-sheet"
|
|
390
|
+
className="max-h-[85dvh] gap-0 rounded-t-[20px] border-0 bg-popover px-2 pb-[calc(26px+env(safe-area-inset-bottom))] pt-2 text-popover-foreground"
|
|
391
|
+
>
|
|
392
|
+
<div aria-hidden className="mx-auto mb-2.5 mt-0.5 h-[5px] w-9 shrink-0 rounded-full bg-[color:color-mix(in_oklab,var(--muted-foreground)_30%,transparent)]" />
|
|
393
|
+
<DrawerTitle className="sr-only">{ariaLabel}</DrawerTitle>
|
|
394
|
+
<nav aria-label={ariaLabel}>
|
|
395
|
+
{/* Root first, so the nearest levels sit lowest, by the thumb. */}
|
|
396
|
+
<ol className="m-0 flex list-none flex-col p-0">
|
|
397
|
+
{items.map((item, i) => (
|
|
398
|
+
<li key={i} className="min-w-0">
|
|
399
|
+
{i === last ? (
|
|
400
|
+
<span
|
|
401
|
+
aria-current="page"
|
|
402
|
+
data-slot="breadcrumb-page"
|
|
403
|
+
className="block break-words rounded-xl px-3.5 py-3 text-[15px] font-semibold text-foreground"
|
|
404
|
+
>
|
|
405
|
+
{item.label}
|
|
406
|
+
</span>
|
|
407
|
+
) : item.href ? (
|
|
408
|
+
link(i, "sheet-press", cn("block break-words rounded-xl px-3.5 py-3 text-[15px] text-muted-foreground active:bg-muted", ring, inert))
|
|
409
|
+
) : (
|
|
410
|
+
<span className="block break-words px-3.5 py-3 text-[15px] text-muted-foreground">{item.label}</span>
|
|
411
|
+
)}
|
|
412
|
+
</li>
|
|
413
|
+
))}
|
|
414
|
+
</ol>
|
|
415
|
+
</nav>
|
|
416
|
+
</DrawerContent>
|
|
417
|
+
</Drawer>
|
|
418
|
+
</Unlisted>
|
|
419
|
+
{compact && measurer}
|
|
420
|
+
{compact && sizer}
|
|
421
|
+
</nav>
|
|
422
|
+
)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const trailView = (viewRef: React.Ref<HTMLElement> | undefined, only?: string) => {
|
|
426
|
+
const hidden = shown.slice(hiddenFrom, hiddenTo)
|
|
427
|
+
const separator = (key: string) => (
|
|
428
|
+
<li key={key} role="presentation" aria-hidden="true" data-slot="breadcrumb-separator" className="flex shrink-0 text-muted-foreground opacity-60">
|
|
429
|
+
<ChevronRight className="size-3.5 rtl:rotate-180" />
|
|
430
|
+
</li>
|
|
431
|
+
)
|
|
432
|
+
|
|
433
|
+
const parts: React.ReactNode[] = []
|
|
434
|
+
visible.forEach((index, k) => {
|
|
435
|
+
const tail = k === visible.length - 1
|
|
436
|
+
const item = shown[index]
|
|
437
|
+
if (k > 0) parts.push(separator(`s${index}`))
|
|
438
|
+
if (k === 1 && collapsed) {
|
|
439
|
+
parts.push(
|
|
440
|
+
<li key="more" data-slot="breadcrumb-ellipsis" className="flex shrink-0">
|
|
441
|
+
<Unlisted>
|
|
442
|
+
<Menu open={menuOpen} onOpenChange={setMenuOpen}>
|
|
443
|
+
<MenuTrigger
|
|
444
|
+
disabled={disabled}
|
|
445
|
+
aria-label={`Show ${hidden.length} more ${hidden.length === 1 ? "level" : "levels"}`}
|
|
446
|
+
data-agent-part={part}
|
|
447
|
+
className={cn(
|
|
448
|
+
crumb,
|
|
449
|
+
ring,
|
|
450
|
+
"h-auto border-0 bg-transparent text-[13px] font-normal leading-[inherit] tracking-[1px] text-muted-foreground",
|
|
451
|
+
"hover:bg-muted hover:text-foreground data-[popup-open]:bg-muted data-[popup-open]:text-foreground"
|
|
452
|
+
)}
|
|
453
|
+
>
|
|
454
|
+
…
|
|
455
|
+
</MenuTrigger>
|
|
456
|
+
<MenuContent ref={menuPopup} align="start" sideOffset={4} className="min-w-[200px] max-w-[min(20rem,calc(100vw-2rem))]">
|
|
457
|
+
{hidden.map((h, i) => {
|
|
458
|
+
const at = hiddenFrom + i
|
|
459
|
+
return (
|
|
460
|
+
<MenuLinkItem
|
|
461
|
+
key={at}
|
|
462
|
+
href={h.href}
|
|
463
|
+
render={h.render as React.ComponentProps<typeof MenuLinkItem>["render"]}
|
|
464
|
+
closeOnClick
|
|
465
|
+
aria-disabled={disabled || !h.href || undefined}
|
|
466
|
+
data-breadcrumb-index={at}
|
|
467
|
+
data-slot="breadcrumb-menu-item"
|
|
468
|
+
onClick={press(at, "menu-press")}
|
|
469
|
+
className={cn(inert, "max-sm:min-h-11")}
|
|
470
|
+
>
|
|
471
|
+
<span className="truncate">{h.label}</span>
|
|
472
|
+
</MenuLinkItem>
|
|
473
|
+
)
|
|
474
|
+
})}
|
|
475
|
+
</MenuContent>
|
|
476
|
+
</Menu>
|
|
477
|
+
</Unlisted>
|
|
478
|
+
</li>
|
|
479
|
+
)
|
|
480
|
+
parts.push(separator("s-more"))
|
|
481
|
+
}
|
|
482
|
+
const isCurrent = !hideCurrent && index === last
|
|
483
|
+
parts.push(
|
|
484
|
+
<li
|
|
485
|
+
key={index}
|
|
486
|
+
data-slot="breadcrumb-item"
|
|
487
|
+
className={cn("flex", tail ? "min-w-[3rem] shrink-[1000]" : "min-w-0 shrink")}
|
|
488
|
+
>
|
|
489
|
+
{isCurrent ? (
|
|
490
|
+
<span aria-current="page" title={item.label} data-slot="breadcrumb-page" className={cn(crumb, "font-medium text-foreground")}>
|
|
491
|
+
{item.label}
|
|
492
|
+
</span>
|
|
493
|
+
) : item.href ? (
|
|
494
|
+
link(index, "link-press", cn(linkClass, !tail && capped))
|
|
495
|
+
) : (
|
|
496
|
+
<span title={item.label} className={cn(crumb, "text-muted-foreground", !tail && capped)}>
|
|
497
|
+
{item.label}
|
|
498
|
+
</span>
|
|
499
|
+
)}
|
|
500
|
+
</li>
|
|
501
|
+
)
|
|
502
|
+
})
|
|
503
|
+
|
|
504
|
+
return (
|
|
505
|
+
<nav
|
|
506
|
+
{...rest}
|
|
507
|
+
ref={viewRef}
|
|
508
|
+
aria-label={ariaLabel}
|
|
509
|
+
data-slot="breadcrumb"
|
|
510
|
+
data-presentation="trail"
|
|
511
|
+
data-collapsed={collapsed || undefined}
|
|
512
|
+
className={cn("relative block w-full min-w-0 text-[13px]", className, only)}
|
|
513
|
+
>
|
|
514
|
+
<ol data-slot="breadcrumb-list" className="m-0 flex min-w-0 list-none flex-nowrap items-center gap-0.5 overflow-hidden whitespace-nowrap p-0">
|
|
515
|
+
{parts}
|
|
516
|
+
</ol>
|
|
517
|
+
{measurer}
|
|
518
|
+
{sizer}
|
|
519
|
+
</nav>
|
|
520
|
+
)
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
if (view === "back") return backView(navRef)
|
|
524
|
+
if (presentation === "trail") return trailView(navRef)
|
|
525
|
+
// Before the client knows the width; the next render drops the hidden one.
|
|
526
|
+
return (
|
|
527
|
+
<>
|
|
528
|
+
{backView(undefined, "md:hidden")}
|
|
529
|
+
{trailView(navRef, "max-md:hidden")}
|
|
530
|
+
</>
|
|
531
|
+
)
|
|
532
|
+
})
|
|
533
|
+
Breadcrumb.displayName = "Breadcrumb"
|
|
534
|
+
|
|
535
|
+
// Arbitrary values, so they compile on Tailwind 3, where the tokens take no alpha modifier.
|
|
536
|
+
const frosted =
|
|
537
|
+
"bg-[color:color-mix(in_oklab,var(--background)_80%,transparent)] shadow-[0_2px_12px_rgb(0_0_0/0.12)] backdrop-blur-md"
|
|
538
|
+
|
|
539
|
+
const backClass = (floating: boolean) =>
|
|
540
|
+
cn(
|
|
541
|
+
"flex min-w-0 select-none items-center gap-0.5 justify-self-start [-webkit-touch-callout:none]",
|
|
542
|
+
ring,
|
|
543
|
+
inert,
|
|
544
|
+
floating
|
|
545
|
+
? cn("h-10 rounded-full pl-2 pr-3.5 text-[15px] font-medium text-foreground", frosted)
|
|
546
|
+
: // Toward the foreground, so it passes 4.5:1 in either theme.
|
|
547
|
+
"rounded-md px-1 py-1.5 text-base text-[color:color-mix(in_oklab,var(--info)_80%,var(--foreground))]",
|
|
548
|
+
// A 44px target on phones, the look unchanged.
|
|
549
|
+
"relative max-sm:after:absolute max-sm:after:inset-x-0 max-sm:after:top-1/2 max-sm:after:h-11 max-sm:after:-translate-y-1/2 max-sm:after:content-['']"
|
|
550
|
+
)
|
|
551
|
+
|
|
552
|
+
const backLabel = (label: string) => (
|
|
553
|
+
<>
|
|
554
|
+
<ChevronLeft aria-hidden className="size-[22px] shrink-0 rtl:rotate-180" />
|
|
555
|
+
<span className="sr-only">Back to {label}</span>
|
|
556
|
+
<span aria-hidden="true" className="truncate">
|
|
557
|
+
{label}
|
|
558
|
+
</span>
|
|
559
|
+
</>
|
|
560
|
+
)
|
|
561
|
+
|
|
562
|
+
const HOLD_MS = 450
|
|
563
|
+
|
|
564
|
+
/** Holding the back link opens the trail, as a platform back button does. */
|
|
565
|
+
function useHold(onHold: () => void, disabled: boolean | undefined) {
|
|
566
|
+
const timer = React.useRef<number | undefined>(undefined)
|
|
567
|
+
const held = React.useRef(false)
|
|
568
|
+
const pointer = React.useRef("")
|
|
569
|
+
const end = () => window.clearTimeout(timer.current)
|
|
570
|
+
// After the click that a hold ends in, so a later Enter is not swallowed.
|
|
571
|
+
const release = () => {
|
|
572
|
+
end()
|
|
573
|
+
if (held.current) window.setTimeout(() => (held.current = false))
|
|
574
|
+
}
|
|
575
|
+
React.useEffect(() => end, [])
|
|
576
|
+
return {
|
|
577
|
+
onPointerDown: (e: React.PointerEvent) => {
|
|
578
|
+
held.current = false
|
|
579
|
+
pointer.current = e.pointerType
|
|
580
|
+
if (disabled || e.button !== 0) return
|
|
581
|
+
end()
|
|
582
|
+
timer.current = window.setTimeout(() => {
|
|
583
|
+
held.current = true
|
|
584
|
+
onHold()
|
|
585
|
+
}, HOLD_MS)
|
|
586
|
+
},
|
|
587
|
+
onPointerUp: release,
|
|
588
|
+
onPointerLeave: end,
|
|
589
|
+
onPointerCancel: release,
|
|
590
|
+
onKeyDown: () => {
|
|
591
|
+
held.current = false
|
|
592
|
+
},
|
|
593
|
+
// A touch hold opens the browser's link menu otherwise.
|
|
594
|
+
onContextMenu: (e: React.MouseEvent) => {
|
|
595
|
+
if (pointer.current && pointer.current !== "mouse") e.preventDefault()
|
|
596
|
+
},
|
|
597
|
+
// The click that ends a hold does not also go back.
|
|
598
|
+
onClick: (e: React.MouseEvent) => {
|
|
599
|
+
if (!held.current) return
|
|
600
|
+
held.current = false
|
|
601
|
+
e.preventDefault()
|
|
602
|
+
},
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
export { Breadcrumb }
|
|
@@ -59,6 +59,7 @@ const Button = React.forwardRef<HTMLElement, ButtonProps>(
|
|
|
59
59
|
({ className, variant, size, render, nativeButton, ...props }, ref) => {
|
|
60
60
|
return (
|
|
61
61
|
<BaseButton
|
|
62
|
+
data-slot="button"
|
|
62
63
|
className={cn(buttonVariants({ variant, size, className }))}
|
|
63
64
|
ref={ref}
|
|
64
65
|
render={render}
|