@tristan2828/ui-foundation 4.80.0 → 4.82.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.
@@ -67,13 +67,19 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
67
67
  // switch. With no `tab` on any section, nothing changes.
68
68
  //
69
69
  // `leading`, left of the title: the record's mark or avatar, with the
70
- // title, subtitle and badges beside it at every width. `railFields`
71
- // `beside` puts a rail field's label to the left of its value, as in an
72
- // issue tracker's sidebar, stacked again where the rail's list is too
73
- // narrow for both.
74
- import { useEffect, useId, useState, useSyncExternalStore } from 'react';
70
+ // title, subtitle and badges beside it at every width. A rail field's
71
+ // label sits to the left of its value (`railFields`, `beside` by default
72
+ // since 4.81), as in an issue tracker's sidebar, stacked again where the
73
+ // rail's list is too narrow for both.
74
+ //
75
+ // The record page standard (4.81): `badges` sit on the title's line,
76
+ // wrapping under a long title; Print notes is an icon (`IconAction`, its
77
+ // words the tooltip and name); an empty value reads as a dash
78
+ // (`emptyText`), and `emptyFields="fold"` folds each fields card's empty
79
+ // ones behind an "N empty" toggle, still edited in place once opened.
80
+ import { createContext, useContext, useEffect, useId, useState, useSyncExternalStore } from 'react';
75
81
  import { Link, useLocation, useNavigate } from 'react-router';
76
- import { ArrowLeftIcon, SearchXIcon } from 'lucide-react';
82
+ import { ArrowLeftIcon, ChevronDownIcon, SearchXIcon } from 'lucide-react';
77
83
  import { cn } from 'cn';
78
84
  import { ErrorState } from './error-state.js';
79
85
  import { Button } from '../ui/button.js';
@@ -87,22 +93,22 @@ import { EditInPlaceContext } from './edit-in-place.js';
87
93
  import { EditInPlaceStore } from './edit-in-place-store.js';
88
94
  import { EditableList } from './editable-list.js';
89
95
  import { EditableValue, LeaveGuard } from './editable-value.js';
96
+ import { EmptyText } from './empty-value.js';
97
+ import { EMPTY_DASH, isEmptyValue } from '../../lib/empty-value.js';
90
98
  import { useRecordDialog } from './record-dialog-context.js';
91
99
  import { entityViewTabSlug } from '../../lib/entity-view-tab.js';
92
- const DEFAULT_EMPTY_LABEL = 'Not set';
93
- function isEmptyValue(value) {
94
- return (value === null ||
95
- value === undefined ||
96
- value === false ||
97
- value === '' ||
98
- (Array.isArray(value) && value.length === 0));
99
- }
100
+ // The view's empty text and whether fields cards fold their empty ones,
101
+ // for every section without threading them through each layout.
102
+ const EmptyContext = createContext({ text: EMPTY_DASH, fold: false });
100
103
  function BackLink({ back }) {
101
104
  return (_jsxs(Link, { to: back.to, className: "inline-flex w-fit items-center gap-1 rounded-sm type-label text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50", children: [_jsx(ArrowLeftIcon, { "aria-hidden": "true", className: "size-4" }), back.label] }));
102
105
  }
103
- function orEmptyLabel(value, emptyLabel) {
104
- return isEmptyValue(value) ? (_jsx("span", { className: "text-muted-foreground", children: emptyLabel ?? DEFAULT_EMPTY_LABEL })) : (value);
106
+ function OrEmpty({ value, emptyLabel }) {
107
+ const empty = useContext(EmptyContext);
108
+ return isEmptyValue(value) ? _jsx(EmptyText, { text: emptyLabel ?? empty.text }) : value;
105
109
  }
110
+ // A preset shows its own value unless the screen gives one.
111
+ const fieldValue = (field) => (field.value !== undefined ? field.value : field.edit?.shown);
106
112
  // Label beside value from `sm` up; below it (a phone) the label sits
107
113
  // above its value, so nothing is squeezed into a narrow second column. In
108
114
  // the rail the list's own width decides instead (a container query on the
@@ -117,13 +123,26 @@ const FIELD_ROW = {
117
123
  railBeside: '@2xs/fields:grid @2xs/fields:grid-cols-[minmax(--spacing(24),--spacing(28))_minmax(0,1fr)] @2xs/fields:items-baseline @2xs/fields:gap-4 @2xs/fields:py-2.5 @lg/fields:grid-cols-[minmax(--spacing(32),--spacing(48))_1fr] @lg/fields:gap-6',
118
124
  };
119
125
  function FieldRow({ field, place }) {
120
- // A preset shows its own value unless the screen gives one.
121
- const value = field.value !== undefined ? field.value : field.edit?.shown;
122
- return (_jsxs("div", { className: cn('flex flex-col gap-1 py-3 first:pt-0 last:pb-0', FIELD_ROW[place]), children: [_jsx("dt", { className: "type-label text-muted-foreground", children: field.label }), _jsx("dd", { className: "min-w-0 type-body break-words text-foreground", children: field.edit ? (_jsx(EditableValue, { label: field.label, edit: field.edit, children: orEmptyLabel(value, field.emptyLabel) })) : (orEmptyLabel(value, field.emptyLabel)) })] }));
126
+ const value = fieldValue(field);
127
+ return (_jsxs("div", { className: cn('flex flex-col gap-1 py-3 first:pt-0 last:pb-0', FIELD_ROW[place]), children: [_jsx("dt", { className: "type-label text-muted-foreground", children: field.label }), _jsx("dd", { className: "min-w-0 type-body break-words text-foreground", children: field.edit ? (_jsx(EditableValue, { label: field.label, edit: field.edit, children: _jsx(OrEmpty, { value: value, emptyLabel: field.emptyLabel }) })) : (_jsx(OrEmpty, { value: value, emptyLabel: field.emptyLabel })) })] }));
128
+ }
129
+ // A fields card's rows. Folded (`emptyFields="fold"`), the filled fields
130
+ // show and the empty ones wait behind "N empty", which opens them in their
131
+ // places (the card's order kept) and then reads "Hide empty". Whether a
132
+ // field is empty is read on every render, so one filled elsewhere joins
133
+ // the card at once.
134
+ function FieldList({ fields, inRail, place }) {
135
+ const empty = useContext(EmptyContext);
136
+ const [open, setOpen] = useState(false);
137
+ const listId = useId();
138
+ const emptyCount = empty.fold ? fields.filter((field) => isEmptyValue(fieldValue(field))).length : 0;
139
+ const shown = emptyCount === 0 || open ? fields : fields.filter((field) => !isEmptyValue(fieldValue(field)));
140
+ return (_jsxs("div", { className: "flex flex-col gap-3", children: [shown.length > 0 && (_jsx("dl", { id: listId, className: cn('flex flex-col divide-y divide-border', inRail && '@container/fields'), children: shown.map((field) => (_jsx(FieldRow, { field: field, place: place }, field.label))) })), emptyCount > 0 && (_jsxs("button", { type: "button", "data-slot": "entity-view-empty-toggle", "aria-expanded": open, "aria-controls": shown.length > 0 ? listId : undefined, onClick: () => setOpen((was) => !was), className: "inline-flex w-fit items-center gap-1 rounded-sm type-label text-muted-foreground underline underline-offset-3 outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50 pointer-coarse:min-h-11", children: [open ? 'Hide empty' : `${emptyCount} empty`, _jsx(ChevronDownIcon, { "aria-hidden": "true", className: cn('size-3.5 transition-transform', open && 'rotate-180') })] }))] }));
123
141
  }
124
- function ViewSection({ section, inRail = false, railFields = 'stacked', }) {
142
+ function ViewSection({ section, inRail = false, railFields = 'beside', }) {
125
143
  const headingId = useId();
126
- return (_jsx("section", { "aria-labelledby": headingId, children: _jsxs(Card, { children: [_jsx(CardHeader, { children: _jsx("h2", { id: headingId, className: "type-section-title text-foreground", children: section.title }) }), _jsx(CardContent, { children: 'fields' in section ? (_jsx("dl", { className: cn('flex flex-col divide-y divide-border', inRail && '@container/fields'), children: section.fields.map((field) => (_jsx(FieldRow, { field: field, place: !inRail ? 'page' : railFields === 'beside' ? 'railBeside' : 'rail' }, field.label))) })) : section.edit?.kind === 'list' ? (_jsxs("div", { className: "flex min-w-0 flex-col gap-2 type-body break-words text-foreground", children: [!isEmptyValue(section.content) && section.content, _jsx(EditableList, { label: section.title, edit: section.edit, emptyLabel: section.emptyLabel ?? DEFAULT_EMPTY_LABEL })] })) : (_jsx("div", { className: "min-w-0 type-body break-words text-foreground", children: section.edit ? (_jsx(EditableValue, { label: section.title, edit: section.edit, children: orEmptyLabel(section.content, section.emptyLabel) })) : (orEmptyLabel(section.content, section.emptyLabel)) })) })] }) }));
144
+ const empty = useContext(EmptyContext);
145
+ return (_jsx("section", { "aria-labelledby": headingId, children: _jsxs(Card, { children: [_jsx(CardHeader, { children: _jsx("h2", { id: headingId, className: "type-section-title text-foreground", children: section.title }) }), _jsx(CardContent, { children: 'fields' in section ? (_jsx(FieldList, { fields: section.fields, inRail: inRail, place: !inRail ? 'page' : railFields === 'beside' ? 'railBeside' : 'rail' })) : section.edit?.kind === 'list' ? (_jsxs("div", { className: "flex min-w-0 flex-col gap-2 type-body break-words text-foreground", children: [!isEmptyValue(section.content) && section.content, _jsx(EditableList, { label: section.title, edit: section.edit, emptyLabel: section.emptyLabel ?? empty.text })] })) : (_jsx("div", { className: "min-w-0 type-body break-words text-foreground", children: section.edit ? (_jsx(EditableValue, { label: section.title, edit: section.edit, children: _jsx(OrEmpty, { value: section.content, emptyLabel: section.emptyLabel }) })) : (_jsx(OrEmpty, { value: section.content, emptyLabel: section.emptyLabel })) })) })] }) }));
127
146
  }
128
147
  // The rail layout's columns. The view is a container, so the switch to
129
148
  // two columns follows the width the page actually has (the sidebar open
@@ -220,7 +239,7 @@ function MainTabs({ tabs, label }) {
220
239
  };
221
240
  return (_jsxs(Tabs, { value: selected, onValueChange: (value) => select(String(value)), "data-slot": "entity-view-tabs", className: "min-w-0 gap-6", children: [_jsx(TabsList, { variant: "line", activateOnFocus: true, "aria-label": label, className: "h-auto w-full max-w-full justify-start gap-1 overflow-x-auto overflow-y-hidden border-b border-border p-0 [scrollbar-width:thin]", children: tabs.map((tab) => (_jsx(TabsTrigger, { value: tab.slug, className: "h-9 flex-none px-3 text-muted-foreground group-data-horizontal/tabs:after:bottom-0 pointer-coarse:h-11 dark:text-muted-foreground", children: tab.name }, tab.slug))) }), tabs.map((tab) => (_jsx(TabsContent, { value: tab.slug, keepMounted: true, className: "flex min-w-0 flex-col gap-6 [font-size:inherit]", children: tab.sections.map((section) => (_jsx(ViewSection, { section: section }, section.title))) }, tab.slug)))] }));
222
241
  }
223
- export function EntityView({ title, documentTitle, titleEdit, leading, subtitle, badges, actions, notice, strip, sections, footer, layout = 'column', mobileOrder = 'rail-first', railLabel, railFields = 'stacked', printLabel = 'Print notes', isLoading, error, onRetry, back, }) {
242
+ export function EntityView({ title, documentTitle, titleEdit, leading, subtitle, badges, actions, notice, strip, sections, footer, layout = 'column', mobileOrder = 'rail-first', railLabel, railFields = 'beside', emptyFields = 'show', emptyText = EMPTY_DASH, printLabel = 'Print notes', printLook = 'icon', isLoading, error, onRetry, back, }) {
224
243
  // Editing in place: one store per view, and the leave-page prompt only
225
244
  // while a field is open (or one is always editable).
226
245
  const [store] = useState(() => new EditInPlaceStore());
@@ -252,7 +271,7 @@ export function EntityView({ title, documentTitle, titleEdit, leading, subtitle,
252
271
  : { content: section.content, emptyLabel: section.emptyLabel }),
253
272
  answerSpace: typeof section.print === 'object' && section.print.answerSpace === true,
254
273
  }));
255
- const printButton = printSections.length > 0 && (_jsx(PrintNotes, { label: printLabel, title: title, subtitle: subtitle, sections: printSections }));
274
+ const printButton = printSections.length > 0 && (_jsx(PrintNotes, { label: printLabel, look: printLook, title: title, subtitle: subtitle, sections: printSections }));
256
275
  const railSections = sections.filter((section) => section.placement === 'rail');
257
276
  const mainSections = sections.filter((section) => section.placement !== 'rail');
258
277
  // Tabs: a main section's `tab` (a rail one's is ignored, in either layout).
@@ -262,7 +281,7 @@ export function EntityView({ title, documentTitle, titleEdit, leading, subtitle,
262
281
  const main = (_jsxs("div", { className: cn(RAIL_LAYOUT.main, railSections.length === 0 && RAIL_LAYOUT.mainAlone), children: [mainSections
263
282
  .filter((section) => !tabbed(section))
264
283
  .map((section) => (_jsx(ViewSection, { section: section }, section.title))), tabList] }));
265
- return (_jsx(EditInPlaceContext.Provider, { value: store, children: _jsxs("div", { className: layout === 'rail' ? RAIL_LAYOUT.page : 'flex max-w-4xl flex-col gap-6', "data-state": "success", "data-layout": layout, children: [editing && _jsx(LeaveGuard, { store: store }), !dialog && _jsx(BackLink, { back: back }), _jsxs("header", { className: "flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between", children: [_jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-3 sm:min-w-64 sm:gap-4", children: [leading && (_jsx("div", { "data-slot": "entity-view-leading", className: "flex shrink-0 items-center", children: leading })), _jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-2", children: [_jsx("h1", { id: dialog?.titleId, className: "type-page-title break-words text-foreground", children: titleEdit ? (_jsx(EditableValue, { label: titleEdit.label, edit: titleEdit, layout: "inline", className: "w-full", children: title })) : (title) }), subtitle && (_jsx("div", { "data-slot": "entity-view-subtitle", className: "type-body break-words text-muted-foreground", children: subtitle })), badges && _jsx("div", { className: "flex flex-wrap items-center gap-1.5", children: badges })] })] }), (actions || printButton) && (_jsxs("div", { className: "flex min-w-0 flex-wrap items-center gap-2 sm:justify-end", children: [printButton, actions] }))] }), _jsx("div", { role: "status", "data-slot": "entity-view-notice", className: "empty:hidden", children: notice }), strip && _jsx("div", { "data-slot": "entity-view-strip", children: strip }), layout === 'rail' ? (_jsxs("div", { className: RAIL_LAYOUT.columns, children: [mobileOrder === 'main-first' && main, railSections.length > 0 && (_jsx(Rail, { label: railLabel ?? `${title ?? 'Record'} details`, sections: railSections, fields: railFields })), mobileOrder === 'rail-first' && main] })) : (_jsxs(_Fragment, { children: [sections
266
- .filter((section) => !tabbed(section))
267
- .map((section) => (_jsx(ViewSection, { section: section }, section.title))), tabList] })), footer && (_jsx("footer", { "data-slot": "entity-view-footer", className: "type-caption text-muted-foreground", children: footer }))] }) }));
284
+ return (_jsx(EditInPlaceContext.Provider, { value: store, children: _jsx(EmptyContext.Provider, { value: { text: emptyText, fold: emptyFields === 'fold' }, children: _jsxs("div", { className: layout === 'rail' ? RAIL_LAYOUT.page : 'flex max-w-4xl flex-col gap-6', "data-state": "success", "data-layout": layout, children: [editing && _jsx(LeaveGuard, { store: store }), !dialog && _jsx(BackLink, { back: back }), _jsxs("header", { className: "flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between", children: [_jsxs("div", { className: "flex min-w-0 flex-1 items-start gap-3 sm:min-w-64 sm:gap-4", children: [leading && (_jsx("div", { "data-slot": "entity-view-leading", className: "flex shrink-0 items-center", children: leading })), _jsxs("div", { className: "flex min-w-0 flex-1 flex-col gap-2", children: [_jsxs("div", { "data-slot": "entity-view-title-line", className: "flex min-w-0 flex-wrap items-center gap-x-3 gap-y-2", children: [_jsx("h1", { id: dialog?.titleId, className: "min-w-0 max-w-full type-page-title break-words text-foreground", children: titleEdit ? (_jsx(EditableValue, { label: titleEdit.label, edit: titleEdit, layout: "inline", className: "w-full", children: title })) : (title) }), badges && (_jsx("div", { "data-slot": "entity-view-badges", className: "flex flex-wrap items-center gap-1.5", children: badges }))] }), subtitle && (_jsx("div", { "data-slot": "entity-view-subtitle", className: "type-body break-words text-muted-foreground", children: subtitle }))] })] }), (actions || printButton) && (_jsxs("div", { className: "flex min-w-0 flex-wrap items-center gap-2 sm:justify-end", children: [printButton, actions] }))] }), _jsx("div", { role: "status", "data-slot": "entity-view-notice", className: "empty:hidden", children: notice }), strip && _jsx("div", { "data-slot": "entity-view-strip", children: strip }), layout === 'rail' ? (_jsxs("div", { className: RAIL_LAYOUT.columns, children: [mobileOrder === 'main-first' && main, railSections.length > 0 && (_jsx(Rail, { label: railLabel ?? `${title ?? 'Record'} details`, sections: railSections, fields: railFields })), mobileOrder === 'rail-first' && main] })) : (_jsxs(_Fragment, { children: [sections
285
+ .filter((section) => !tabbed(section))
286
+ .map((section) => (_jsx(ViewSection, { section: section }, section.title))), tabList] })), footer && (_jsx("footer", { "data-slot": "entity-view-footer", className: "type-caption text-muted-foreground", children: footer }))] }) }) }));
268
287
  }
@@ -0,0 +1,29 @@
1
+ import { type FormEvent, type ReactNode } from 'react';
2
+ import type { AppError } from '../../api/contracts.js';
3
+ export type FormDialogProps = {
4
+ /** "New book": the dialog's name. */
5
+ title: string;
6
+ /** One short line under the title, only where the title can't say it. */
7
+ description?: ReactNode;
8
+ /**
9
+ * Whether it's open. Leave it out inside RecordDialogRoute's `newPath`
10
+ * route, which opens it; anywhere else it's closed until it's given.
11
+ */
12
+ open?: boolean;
13
+ /** Asked to close (Esc, the close button, a click outside, Cancel). */
14
+ onOpenChange?: (open: boolean) => void;
15
+ onSubmit: (event: FormEvent<HTMLFormElement>) => void;
16
+ isSubmitting: boolean;
17
+ /** "Add book": the word Add on every create form. */
18
+ submitLabel: string;
19
+ /** A save that failed: a Notice above the buttons, with Try again. */
20
+ submitError?: AppError | null;
21
+ /** Try again's action. Defaults to submitting the form again. */
22
+ onRetry?: () => void;
23
+ /** Something was typed: closing asks "Discard your changes?" first. */
24
+ dirty?: boolean;
25
+ /** The fields. */
26
+ children: ReactNode;
27
+ className?: string;
28
+ };
29
+ export declare function FormDialog({ title, description, open, onOpenChange, onSubmit, isSubmitting, submitLabel, submitError, onRetry, dirty, children, className, }: FormDialogProps): import("react").JSX.Element;
@@ -0,0 +1,42 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // A small form in a dialog (4.82): a new record of up to about eight
3
+ // fields over its list, or any short form a page opens (a weak spot, a
4
+ // quick add). The title and the buttons stay put and the fields scroll
5
+ // between them; 640px wide (DialogContent `size="form"`), the whole screen
6
+ // on a phone, with Cancel and the submit button as two wide buttons.
7
+ //
8
+ // Two ways to open one:
9
+ //
10
+ // - Anywhere, with `open` and `onOpenChange`, as any dialog.
11
+ // - As RecordDialogRoute's `newPath` route (`/widgets/new`), with neither:
12
+ // the route opens it over the list and closing goes back, as a record's
13
+ // dialog does. After the save, `useNewRecordDialog().openRecord(to)`
14
+ // opens the new record over the same list.
15
+ //
16
+ // `dirty` (react-hook-form's `formState.isDirty`) makes Esc, the close
17
+ // button, a click outside and Cancel ask "Discard your changes?" first.
18
+ // A failed save is a Notice above the buttons with Try again, which
19
+ // submits again unless `onRetry` says otherwise; what was typed stays.
20
+ //
21
+ // The form is the app's: its fields, its schema and its react-hook-form
22
+ // wiring, as with EntityForm. This draws the chrome around them.
23
+ import { useId, useRef } from 'react';
24
+ import { cn } from 'cn';
25
+ import { Button } from '../ui/button.js';
26
+ import { Dialog, DialogClose, DialogContent, DialogDescription, DialogTitle } from '../ui/dialog.js';
27
+ import { FieldGroup } from '../ui/field.js';
28
+ import { Spinner } from '../ui/spinner.js';
29
+ import { Notice } from './notice.js';
30
+ import { useNewRecordDialog } from './record-dialog-context.js';
31
+ export function FormDialog({ title, description, open, onOpenChange, onSubmit, isSubmitting, submitLabel, submitError, onRetry, dirty = false, children, className, }) {
32
+ const route = useNewRecordDialog();
33
+ const form = useRef(null);
34
+ const formId = useId();
35
+ const isOpen = open ?? route !== null;
36
+ return (_jsx(Dialog, { open: isOpen, onOpenChange: (next) => {
37
+ if (onOpenChange)
38
+ onOpenChange(next);
39
+ else if (!next)
40
+ route?.close();
41
+ }, children: _jsxs(DialogContent, { size: "form", dirty: dirty, "data-slot": "form-dialog", className: cn('gap-0 p-0', className), children: [_jsxs("div", { "data-slot": "form-dialog-header", className: "flex flex-col gap-1 px-4 pt-4 pb-2 pr-12 md:px-6 md:pt-5 md:pr-14", children: [_jsx(DialogTitle, { className: "type-section-title", children: title }), description && _jsx(DialogDescription, { children: description })] }), _jsxs("form", { ref: form, id: formId, onSubmit: onSubmit, noValidate: true, className: "flex min-h-0 flex-1 flex-col", children: [_jsx("div", { "data-slot": "form-dialog-body", className: "min-h-0 flex-1 overflow-y-auto px-4 py-4 md:px-6", children: _jsx(FieldGroup, { children: children }) }), _jsxs("div", { "data-slot": "form-dialog-bar", className: "flex flex-col gap-3 border-t px-4 py-3 pb-[calc(0.75rem+env(safe-area-inset-bottom))] md:px-6 md:pb-3", children: [_jsx("div", { role: "status", className: "empty:hidden", children: submitError && (_jsx(Notice, { tone: "destructive", action: _jsx(Button, { type: "button", size: "sm", variant: "outline", disabled: isSubmitting, onClick: () => (onRetry ? onRetry() : form.current?.requestSubmit()), children: "Try again" }), children: submitError.message })) }), _jsxs("div", { className: "flex justify-end gap-2 max-md:[&>*]:h-10 max-md:[&>*]:flex-1", children: [_jsx(DialogClose, { render: _jsx(Button, { type: "button", variant: "outline", disabled: isSubmitting }), children: "Cancel" }), _jsxs(Button, { type: "submit", disabled: isSubmitting, children: [isSubmitting && _jsx(Spinner, { className: "size-4" }), submitLabel] })] })] })] })] }) }));
42
+ }
@@ -0,0 +1,14 @@
1
+ import type { ComponentProps } from 'react';
2
+ import { Button } from '../ui/button.js';
3
+ import type { IconComponent } from './icon-component.js';
4
+ export type IconActionProps = Omit<ComponentProps<typeof Button>, 'children' | 'size' | 'aria-label'> & {
5
+ /** The action's icon, from the app's icon vocabulary (Lucide `Trash2Icon` for Delete). */
6
+ icon: IconComponent;
7
+ /** What the action does ("Delete", "Print notes"): its tooltip and accessible name. */
8
+ label: string;
9
+ /** `destructive` draws the icon in the destructive tone (Delete). Defaults to `default`. */
10
+ tone?: 'default' | 'destructive';
11
+ /** The button's size: `default` (36px, beside the header's other buttons) or `sm` (32px). */
12
+ size?: 'default' | 'sm';
13
+ };
14
+ export declare function IconAction({ icon: Icon, label, tone, size, variant, className, ...props }: IconActionProps): import("react").JSX.Element;
@@ -0,0 +1,7 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { cn } from 'cn';
3
+ import { Button } from '../ui/button.js';
4
+ import { Tooltip, TooltipContent, TooltipTrigger } from '../ui/tooltip.js';
5
+ export function IconAction({ icon: Icon, label, tone = 'default', size = 'default', variant = 'outline', className, ...props }) {
6
+ return (_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { render: _jsx(Button, { ...props, "data-slot": "icon-action", variant: variant, size: size === 'sm' ? 'icon-sm' : 'icon', "aria-label": label, className: className }), children: _jsx(Icon, { "aria-hidden": "true", className: cn('size-4', tone === 'destructive' && 'text-destructive-text') }) }), _jsx(TooltipContent, { children: label })] }));
7
+ }
@@ -1,13 +1,16 @@
1
1
  import type { ReactNode } from 'react';
2
+ import type { IconComponent } from './icon-component.js';
2
3
  export type KeyStat = {
3
4
  label: string;
4
5
  value: ReactNode;
5
6
  /** A few words under the value ("of 12", "since May"), in caption type. */
6
7
  hint?: ReactNode;
8
+ /** The concept's icon, drawn at 13px before the label (decorative: the label still says it). */
9
+ icon?: IconComponent;
7
10
  };
8
11
  export type KeyStatsProps = {
9
12
  stats: readonly KeyStat[];
10
- /** What an empty value reads as. Defaults to "Not set". */
13
+ /** What an empty value reads as. Defaults to a dash, read as "Not set". */
11
14
  emptyLabel?: string;
12
15
  className?: string;
13
16
  };
@@ -1,6 +1,11 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { cn } from 'cn';
3
+ import { EmptyText } from './empty-value.js';
4
+ import { EMPTY_DASH } from '../../lib/empty-value.js';
3
5
  const isEmpty = (value) => value === null || value === undefined || value === false || value === '';
4
- export function KeyStats({ stats, emptyLabel = 'Not set', className }) {
5
- return (_jsx("dl", { "data-slot": "key-stats", className: cn('flex flex-wrap gap-x-8 gap-y-3 rounded-lg border border-border bg-card px-4 py-3 text-card-foreground', className), children: stats.map((stat) => (_jsxs("div", { className: "flex min-w-0 flex-col gap-0.5", children: [_jsx("dt", { className: "type-caption text-muted-foreground", children: stat.label }), _jsx("dd", { className: "type-section-title tabular-nums break-words text-foreground", children: isEmpty(stat.value) ? _jsx("span", { className: "type-body text-muted-foreground", children: emptyLabel }) : stat.value }), stat.hint && _jsx("dd", { className: "type-caption text-muted-foreground", children: stat.hint })] }, stat.label))) }));
6
+ export function KeyStats({ stats, emptyLabel = EMPTY_DASH, className }) {
7
+ return (_jsx("dl", { "data-slot": "key-stats", className: cn('flex flex-wrap gap-x-8 gap-y-3 rounded-lg border border-border bg-card px-4 py-3 text-card-foreground', className), children: stats.map((stat) => {
8
+ const Icon = stat.icon;
9
+ return (_jsxs("div", { className: "flex min-w-0 flex-col gap-0.5", children: [_jsxs("dt", { className: cn('type-caption text-muted-foreground', Icon && 'inline-flex items-center gap-1'), children: [Icon && _jsx(Icon, { "aria-hidden": "true", className: "size-3.25 shrink-0" }), stat.label] }), _jsx("dd", { className: "type-section-title tabular-nums break-words text-foreground", children: isEmpty(stat.value) ? _jsx(EmptyText, { text: emptyLabel, className: "type-body text-muted-foreground" }) : stat.value }), stat.hint && _jsx("dd", { className: "type-caption text-muted-foreground", children: stat.hint })] }, stat.label));
10
+ }) }));
6
11
  }
@@ -37,3 +37,20 @@ export declare const RecordDialogContext: import("react").Context<RecordDialogCo
37
37
  * on the record's full page.
38
38
  */
39
39
  export declare function useRecordDialog(): RecordDialogContextValue | null;
40
+ export type NewRecordDialogContextValue = {
41
+ /** Closes the new record's form: back to the list, as Back does. */
42
+ close: () => void;
43
+ /**
44
+ * After the save: the new record, over the same list (this entry
45
+ * replaced, so Back from it is the list). `open('/widgets/12')`.
46
+ */
47
+ openRecord: (to: string) => void;
48
+ };
49
+ export declare const NewRecordDialogContext: import("react").Context<NewRecordDialogContextValue | null>;
50
+ /**
51
+ * Inside RecordDialogRoute's `newPath` route (`/widgets/new`): its `close`
52
+ * and `openRecord`. FormDialog reads it to open itself over the list with
53
+ * no `open` of its own; the route's form calls `openRecord` once it has
54
+ * saved. `null` anywhere else.
55
+ */
56
+ export declare function useNewRecordDialog(): NewRecordDialogContextValue | null;
@@ -46,3 +46,13 @@ export const RecordDialogContext = createContext(null);
46
46
  export function useRecordDialog() {
47
47
  return useContext(RecordDialogContext);
48
48
  }
49
+ export const NewRecordDialogContext = createContext(null);
50
+ /**
51
+ * Inside RecordDialogRoute's `newPath` route (`/widgets/new`): its `close`
52
+ * and `openRecord`. FormDialog reads it to open itself over the list with
53
+ * no `open` of its own; the route's form calls `openRecord` once it has
54
+ * saved. `null` anywhere else.
55
+ */
56
+ export function useNewRecordDialog() {
57
+ return useContext(NewRecordDialogContext);
58
+ }
@@ -8,7 +8,7 @@ export type RecordDialogRouteProps = {
8
8
  * loads, fails or isn't found. Once it loads, its title names the dialog.
9
9
  */
10
10
  label: string;
11
- /** Extra classes on the dialog's popup (its width, say: `sm:max-w-5xl`). */
11
+ /** Extra classes on the dialog's popup. It's 1,120px (`size="record"`) by default. */
12
12
  className?: string;
13
13
  /**
14
14
  * False: the record has no full page, only this dialog (DataTable's
@@ -26,8 +26,15 @@ export type RecordDialogRouteProps = {
26
26
  * records (the nested pattern).
27
27
  */
28
28
  listPath?: string;
29
+ /**
30
+ * The child route that creates a record (`'new'` for `/widgets/new`):
31
+ * drawn over the list with no record dialog round it, its element a
32
+ * FormDialog, which opens itself (useNewRecordDialog). Left out, every
33
+ * child is a record.
34
+ */
35
+ newPath?: string;
29
36
  };
30
- export declare function RecordDialogRoute({ list, label, className, fullPage, listPath: listAt }: RecordDialogRouteProps): import("react").JSX.Element | null;
37
+ export declare function RecordDialogRoute({ list, label, className, fullPage, listPath: listAt, newPath: newAt, }: RecordDialogRouteProps): import("react").JSX.Element | null;
31
38
  export type RecordLinkProps = Omit<ComponentProps<typeof Link>, 'state'>;
32
39
  /**
33
40
  * A link to a record that opens it over the current list (a board's card,
@@ -47,6 +47,22 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
47
47
  // { path: 'parents/:id', element: <RecordDialogRoute list={<ParentView />} label="Document" fullPage={false} />,
48
48
  // children: [{ path: 'children/:childId', element: <ChildView /> }] }
49
49
  //
50
+ // A new record over its list (4.82, `newPath`): the list's `new` child
51
+ // route (`/widgets/new`) is the create form, drawn as its own FormDialog
52
+ // over the list, always: opened from the list, from anywhere else, or a
53
+ // deep link (a form of up to about 8 fields has no page of its own). It
54
+ // has no record dialog round it; FormDialog opens itself from the
55
+ // route's context (useNewRecordDialog), closing goes back as a record's
56
+ // dialog does, and `openRecord` after the save swaps the entry for the new
57
+ // record over the same list:
58
+ //
59
+ // { path: 'widgets', element: <RecordDialogRoute list={<WidgetsTableRoute />} label="Widget" newPath="new" />,
60
+ // children: [{ path: 'new', element: <NewWidgetRoute /> }, { path: ':id', element: <WidgetViewRoute /> }] }
61
+ //
62
+ // The record dialog is 1,120px (DialogContent `size="record"`, 4.82), so
63
+ // an EntityView with `layout="rail"` keeps its rail beside the main
64
+ // column, as on its page; a narrower screen stacks them.
65
+ //
50
66
  // Inside, the record's route renders as it always does. EntityView sees
51
67
  // the dialog (useRecordDialog): its heading names the dialog, its back link
52
68
  // gives way to the dialog's own bar, and not found's way out closes the
@@ -58,16 +74,19 @@ import { cn } from 'cn';
58
74
  import { Button, buttonVariants } from '../ui/button.js';
59
75
  import { Dialog, DialogClose, DialogContent } from '../ui/dialog.js';
60
76
  import { escapePassedThrough } from './escape-passthrough.js';
61
- import { backgroundOf, recordDialogState, RecordDialogContext } from './record-dialog-context.js';
77
+ import { backgroundOf, NewRecordDialogContext, recordDialogState, RecordDialogContext, } from './record-dialog-context.js';
62
78
  // Two paths are the same address, give or take a trailing slash.
63
79
  const samePath = (a, b) => a.replace(/\/+$/, '') === b.replace(/\/+$/, '');
64
- export function RecordDialogRoute({ list, label, className, fullPage = true, listPath: listAt }) {
80
+ export function RecordDialogRoute({ list, label, className, fullPage = true, listPath: listAt, newPath: newAt, }) {
65
81
  const location = useLocation();
66
82
  const navigationType = useNavigationType();
67
83
  const navigate = useNavigate();
68
84
  const childOutlet = useOutlet();
69
85
  const titleId = useId();
70
86
  const listPath = useResolvedPath(listAt ?? '.').pathname;
87
+ // Resolved from the list's address: `/widgets/new` beside `/widgets/12`.
88
+ const newPathname = useResolvedPath(newAt === undefined ? '.' : `${listPath}/${newAt}`).pathname;
89
+ const atNew = newAt !== undefined && samePath(location.pathname, newPathname);
71
90
  // With `listPath`, its own (element-less) child is the list, not a record.
72
91
  const atList = listAt !== undefined && samePath(location.pathname, listPath);
73
92
  const outlet = atList ? null : childOutlet;
@@ -76,16 +95,19 @@ export function RecordDialogRoute({ list, label, className, fullPage = true, lis
76
95
  // would be a page the user wasn't on.
77
96
  // With no full page, a record opened any other way is the dialog over
78
97
  // the list's plain address, with nothing to go back over.
98
+ // The new record's form is always over the list.
99
+ const plainList = { pathname: listPath, search: '', hash: '', depth: 0 };
79
100
  const background = opened && samePath(opened.pathname, listPath)
80
101
  ? opened
81
- : !fullPage && outlet !== null
82
- ? { pathname: listPath, search: '', hash: '', depth: 0 }
102
+ : (!fullPage || atNew) && outlet !== null
103
+ ? plainList
83
104
  : null;
84
- const open = outlet !== null && background !== null;
105
+ const over = outlet !== null && background !== null;
106
+ const open = over && !atNew;
85
107
  // The list sees the address it was opened from, not the record's, so
86
108
  // its URL state (filters, sort, page) stays as it was under the dialog.
87
109
  const { pathname, search, hash } = background ?? location;
88
- const listLocation = useMemo(() => ({ pathname, search, hash, state: null, key: open ? 'record-dialog-list' : location.key }), [pathname, search, hash, open, location.key]);
110
+ const listLocation = useMemo(() => ({ pathname, search, hash, state: null, key: over ? 'record-dialog-list' : location.key }), [pathname, search, hash, over, location.key]);
89
111
  const listContext = useMemo(() => ({ location: listLocation, navigationType }), [listLocation, navigationType]);
90
112
  const depth = background?.depth ?? 0;
91
113
  const close = useCallback(() => {
@@ -98,14 +120,19 @@ export function RecordDialogRoute({ list, label, className, fullPage = true, lis
98
120
  void navigate(-depth);
99
121
  }, [navigate, location.key, depth, pathname, search, hash]);
100
122
  const dialog = useMemo(() => ({ titleId, close }), [titleId, close]);
123
+ // After the save: the new record over the same list, in this entry.
124
+ const created = useMemo(() => ({
125
+ close,
126
+ openRecord: (to) => void navigate(to, { replace: true, state: { recordDialog: { pathname, search, hash, depth } } }),
127
+ }), [close, navigate, pathname, search, hash, depth]);
101
128
  // The record's full page: a deep link, or "Open full page".
102
- if (outlet !== null && !open)
129
+ if (outlet !== null && !over)
103
130
  return outlet;
104
131
  // `listPath` given and this route's own address (no index route): there
105
132
  // is no list here to draw.
106
133
  if (listAt !== undefined && outlet === null && !atList)
107
134
  return null;
108
- return (_jsxs(_Fragment, { children: [_jsx(LocationContext.Provider, { value: listContext, children: list }), _jsx(Dialog, { open: open, onOpenChange: (next, details) => {
135
+ return (_jsxs(_Fragment, { children: [_jsx(LocationContext.Provider, { value: listContext, children: list }), atNew && _jsx(NewRecordDialogContext.Provider, { value: created, children: outlet }), _jsx(Dialog, { open: open, onOpenChange: (next, details) => {
109
136
  if (next)
110
137
  return;
111
138
  // An Esc something inside already used (a field edited in place
@@ -118,7 +145,7 @@ export function RecordDialogRoute({ list, label, className, fullPage = true, lis
118
145
  return;
119
146
  }
120
147
  close();
121
- }, children: _jsxs(DialogContent, { showCloseButton: false, "aria-labelledby": titleId, "aria-label": label, "data-slot": "record-dialog", className: cn('flex max-h-[calc(100dvh-2rem)] flex-col gap-0 overflow-hidden p-0 sm:max-w-4xl data-closed:hidden', className), children: [_jsxs("div", { className: cn('flex items-center gap-2 border-b px-2 py-2', fullPage ? 'justify-between' : 'justify-end'), children: [fullPage && (_jsxs(Link, { to: { pathname: location.pathname, search: location.search, hash: location.hash }, replace: true, className: buttonVariants({ variant: 'ghost', size: 'sm' }), children: [_jsx(Maximize2Icon, { "aria-hidden": "true" }), "Open full page"] })), _jsxs(DialogClose, { render: _jsx(Button, { variant: "ghost", size: "icon-sm" }), children: [_jsx(XIcon, { "aria-hidden": "true" }), _jsx("span", { className: "sr-only", children: "Close" })] })] }), _jsx("div", { "data-slot": "record-dialog-body", className: "min-h-0 overflow-y-auto p-4 sm:p-6", children: _jsx(RecordDialogContext.Provider, { value: dialog, children: open && outlet }) })] }) })] }));
148
+ }, children: _jsxs(DialogContent, { showCloseButton: false, "aria-labelledby": titleId, "aria-label": label, "data-slot": "record-dialog", size: "record", className: cn('gap-0 overflow-hidden p-0 data-closed:hidden', className), children: [_jsxs("div", { className: cn('flex items-center gap-2 border-b px-2 py-2', fullPage ? 'justify-between' : 'justify-end'), children: [fullPage && (_jsxs(Link, { to: { pathname: location.pathname, search: location.search, hash: location.hash }, replace: true, className: buttonVariants({ variant: 'ghost', size: 'sm' }), children: [_jsx(Maximize2Icon, { "aria-hidden": "true" }), "Open full page"] })), _jsxs(DialogClose, { render: _jsx(Button, { variant: "ghost", size: "icon-sm" }), children: [_jsx(XIcon, { "aria-hidden": "true" }), _jsx("span", { className: "sr-only", children: "Close" })] })] }), _jsx("div", { "data-slot": "record-dialog-body", className: "min-h-0 overflow-y-auto p-4 sm:p-6", children: _jsx(RecordDialogContext.Provider, { value: dialog, children: open && outlet }) })] }) })] }));
122
149
  }
123
150
  /**
124
151
  * A link to a record that opens it over the current list (a board's card,
@@ -1,7 +1,7 @@
1
1
  import { useRender } from "@base-ui/react/use-render";
2
2
  import { type VariantProps } from "class-variance-authority";
3
3
  declare const badgeVariants: (props?: ({
4
- variant?: "link" | "success" | "default" | "outline" | "secondary" | "ghost" | "destructive" | "solid" | "warning" | "info" | "outline-success" | "outline-warning" | "outline-info" | "outline-destructive" | "tinted-success" | "tinted-warning" | "tinted-info" | "tinted-destructive" | null | undefined;
4
+ variant?: "link" | "success" | "default" | "outline" | "secondary" | "ghost" | "destructive" | "solid" | "info" | "warning" | "outline-success" | "outline-warning" | "outline-info" | "outline-destructive" | "tinted-success" | "tinted-warning" | "tinted-info" | "tinted-destructive" | null | undefined;
5
5
  } & import("class-variance-authority/types").ClassProp) | undefined) => string;
6
6
  declare function Badge({ className, variant, render, ...props }: useRender.ComponentProps<"span"> & VariantProps<typeof badgeVariants>): import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
7
7
  export { Badge, badgeVariants };
@@ -1,12 +1,26 @@
1
1
  import * as React from "react";
2
2
  import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
3
- declare function Dialog({ ...props }: DialogPrimitive.Root.Props): React.JSX.Element;
3
+ declare function Dialog({ onOpenChange, actionsRef: actionsRefProp, children, ...props }: DialogPrimitive.Root.Props): React.JSX.Element;
4
4
  declare function DialogTrigger({ ...props }: DialogPrimitive.Trigger.Props): React.JSX.Element;
5
5
  declare function DialogPortal({ ...props }: DialogPrimitive.Portal.Props): React.JSX.Element;
6
6
  declare function DialogClose({ ...props }: DialogPrimitive.Close.Props): React.JSX.Element;
7
7
  declare function DialogOverlay({ className, ...props }: DialogPrimitive.Backdrop.Props): React.JSX.Element;
8
- declare function DialogContent({ className, children, showCloseButton, ...props }: DialogPrimitive.Popup.Props & {
8
+ /**
9
+ * The dialog's width: `confirm` 420px (a question with two buttons; a
10
+ * bottom sheet on a phone), `form` 640px (a small form; the whole screen
11
+ * on a phone), `record` 1,120px (a record over its list, as wide as its
12
+ * page). Left out, shadcn's 384px.
13
+ */
14
+ type DialogSize = "confirm" | "form" | "record";
15
+ declare function DialogContent({ className, children, showCloseButton, size, dirty, ...props }: DialogPrimitive.Popup.Props & {
9
16
  showCloseButton?: boolean;
17
+ /** One of the three widths (see DialogSize). Left out, 384px. */
18
+ size?: DialogSize;
19
+ /**
20
+ * Something was typed and not saved: closing (Esc, the close button, a
21
+ * click outside, a DialogClose) asks "Discard your changes?" first.
22
+ */
23
+ dirty?: boolean;
10
24
  }): React.JSX.Element;
11
25
  declare function DialogHeader({ className, ...props }: React.ComponentProps<"div">): React.JSX.Element;
12
26
  declare function DialogFooter({ className, showCloseButton, children, ...props }: React.ComponentProps<"div"> & {
@@ -14,4 +28,19 @@ declare function DialogFooter({ className, showCloseButton, children, ...props }
14
28
  }): React.JSX.Element;
15
29
  declare function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props): React.JSX.Element;
16
30
  declare function DialogDescription({ className, ...props }: DialogPrimitive.Description.Props): React.JSX.Element;
17
- export { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, };
31
+ /**
32
+ * "Discard your changes?": the one prompt for leaving with something typed
33
+ * and not saved (a dialog's `dirty`, a form's leave guard, a value edited
34
+ * in place). A confirm: 420px, a bottom sheet on a phone. Esc or a click
35
+ * outside keeps editing.
36
+ */
37
+ declare function DiscardChangesDialog({ open, onKeepEditing, onDiscard, description, ...props }: {
38
+ open: boolean;
39
+ /** Keep editing, Esc or a click outside: the prompt closes and nothing is lost. */
40
+ onKeepEditing: () => void;
41
+ /** Discard: throw away what was typed and leave. */
42
+ onDiscard: () => void;
43
+ /** The line under the question. Defaults to "What you typed will be lost." */
44
+ description?: React.ReactNode;
45
+ } & Omit<DialogPrimitive.Popup.Props, "children">): React.JSX.Element;
46
+ export { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, DiscardChangesDialog, type DialogSize, };
@@ -1,11 +1,35 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import * as React from "react";
3
3
  import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";
4
4
  import { cn } from "cn";
5
5
  import { Button } from "./button.js";
6
6
  import { XIcon } from "lucide-react";
7
- function Dialog({ ...props }) {
8
- return _jsx(DialogPrimitive.Root, { "data-slot": "dialog", ...props });
7
+ const DialogGuardContext = React.createContext(null);
8
+ const IMPERATIVE = "imperative-action";
9
+ function Dialog({ onOpenChange, actionsRef: actionsRefProp, children, ...props }) {
10
+ const ownActions = React.useRef(null);
11
+ const actionsRef = actionsRefProp ?? ownActions;
12
+ const dirty = React.useRef(false);
13
+ const [asking, setAsking] = React.useState(false);
14
+ const guard = React.useMemo(() => ({
15
+ setDirty: (next) => {
16
+ dirty.current = next;
17
+ },
18
+ }), []);
19
+ const discard = () => {
20
+ setAsking(false);
21
+ dirty.current = false;
22
+ actionsRef.current?.close();
23
+ };
24
+ const ask = (_jsx(DiscardChangesDialog, { open: asking, onKeepEditing: () => setAsking(false), onDiscard: discard }));
25
+ return (_jsx(DialogGuardContext.Provider, { value: guard, children: _jsx(DialogPrimitive.Root, { "data-slot": "dialog", actionsRef: actionsRef, onOpenChange: (open, details) => {
26
+ if (!open && dirty.current && details.reason !== IMPERATIVE) {
27
+ details.cancel();
28
+ setAsking(true);
29
+ return;
30
+ }
31
+ onOpenChange?.(open, details);
32
+ }, ...props, children: typeof children === "function" ? ((payload) => (_jsxs(_Fragment, { children: [children(payload), ask] }))) : (_jsxs(_Fragment, { children: [children, ask] })) }) }));
9
33
  }
10
34
  function DialogTrigger({ ...props }) {
11
35
  return _jsx(DialogPrimitive.Trigger, { "data-slot": "dialog-trigger", ...props });
@@ -19,8 +43,27 @@ function DialogClose({ ...props }) {
19
43
  function DialogOverlay({ className, ...props }) {
20
44
  return (_jsx(DialogPrimitive.Backdrop, { "data-slot": "dialog-overlay", className: cn("fixed inset-0 isolate z-50 bg-black/20 duration-100 supports-backdrop-filter:backdrop-blur-sm dark:bg-black/40 data-open:animate-in data-open:fade-in-0 data-closed:animate-out data-closed:fade-out-0", className), ...props }));
21
45
  }
22
- function DialogContent({ className, children, showCloseButton = true, ...props }) {
23
- return (_jsxs(DialogPortal, { children: [_jsx(DialogOverlay, {}), _jsxs(DialogPrimitive.Popup, { "data-slot": "dialog-content", className: cn("fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 gap-4 rounded-xl bg-popover p-4 text-sm text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95", className), ...props, children: [children, showCloseButton && (_jsxs(DialogPrimitive.Close, { "data-slot": "dialog-close", render: _jsx(Button, { variant: "ghost", className: "absolute top-2 right-2", size: "icon-sm" }), children: [_jsx(XIcon, {}), _jsx("span", { className: "sr-only", children: "Close" })] }))] })] }));
46
+ // Written phone first, the laptop's look from `md:` (48rem), so nothing
47
+ // in the base classes' `sm:` (40rem) leaks into a phone's layout between
48
+ // the two. cn() drops each base class these replace.
49
+ const DIALOG_SIZE = {
50
+ // A phone: a sheet across the bottom, its footer's buttons full width
51
+ // and 40px, stacked with the action (the footer's last child) on top,
52
+ // clear of the home indicator.
53
+ confirm: cn("top-auto bottom-0 left-0 max-w-none translate-x-0 translate-y-0 rounded-t-2xl rounded-b-none sm:max-w-none data-open:slide-in-from-bottom-8 data-closed:slide-out-to-bottom-8", "max-md:[&_[data-slot=dialog-footer]]:flex-col-reverse max-md:[&_[data-slot=dialog-footer]]:pb-[calc(1rem+env(safe-area-inset-bottom))] max-md:[&_[data-slot=dialog-footer]>*]:h-10 max-md:[&_[data-slot=dialog-footer]>*]:w-full", "md:top-1/2 md:bottom-auto md:left-1/2 md:max-w-[26.25rem] md:-translate-x-1/2 md:-translate-y-1/2 md:rounded-xl md:data-open:slide-in-from-bottom-0 md:data-closed:slide-out-to-bottom-0"),
54
+ // A column whose children decide what scrolls (FormDialog's body). A
55
+ // phone: the whole screen.
56
+ form: cn("inset-0 flex h-dvh max-h-none max-w-none translate-x-0 translate-y-0 flex-col rounded-none ring-0 sm:max-w-none", "md:inset-auto md:top-1/2 md:left-1/2 md:h-auto md:max-h-[calc(100dvh-2rem)] md:max-w-[40rem] md:-translate-x-1/2 md:-translate-y-1/2 md:rounded-xl md:ring-1"),
57
+ // A phone keeps the 1rem margin round it, the rail under the main column.
58
+ record: "flex max-h-[calc(100dvh-2rem)] flex-col sm:max-w-[70rem]",
59
+ };
60
+ function DialogContent({ className, children, showCloseButton = true, size, dirty = false, ...props }) {
61
+ const guard = React.useContext(DialogGuardContext);
62
+ React.useEffect(() => {
63
+ guard?.setDirty(dirty);
64
+ }, [guard, dirty]);
65
+ React.useEffect(() => () => guard?.setDirty(false), [guard]);
66
+ return (_jsxs(DialogPortal, { children: [_jsx(DialogOverlay, {}), _jsxs(DialogPrimitive.Popup, { "data-slot": "dialog-content", "data-size": size, className: cn("fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 gap-4 rounded-xl bg-popover p-4 text-sm text-popover-foreground ring-1 ring-foreground/10 duration-100 outline-none sm:max-w-sm data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95", size && DIALOG_SIZE[size], className), ...props, children: [children, showCloseButton && (_jsxs(DialogPrimitive.Close, { "data-slot": "dialog-close", render: _jsx(Button, { variant: "ghost", className: "absolute top-2 right-2", size: "icon-sm" }), children: [_jsx(XIcon, {}), _jsx("span", { className: "sr-only", children: "Close" })] }))] })] }));
24
67
  }
25
68
  function DialogHeader({ className, ...props }) {
26
69
  return (_jsx("div", { "data-slot": "dialog-header", className: cn("flex flex-col gap-2", className), ...props }));
@@ -34,4 +77,18 @@ function DialogTitle({ className, ...props }) {
34
77
  function DialogDescription({ className, ...props }) {
35
78
  return (_jsx(DialogPrimitive.Description, { "data-slot": "dialog-description", className: cn("text-sm text-muted-foreground *:[a]:underline *:[a]:underline-offset-3 *:[a]:hover:text-foreground", className), ...props }));
36
79
  }
37
- export { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, };
80
+ /**
81
+ * "Discard your changes?": the one prompt for leaving with something typed
82
+ * and not saved (a dialog's `dirty`, a form's leave guard, a value edited
83
+ * in place). A confirm: 420px, a bottom sheet on a phone. Esc or a click
84
+ * outside keeps editing.
85
+ */
86
+ function DiscardChangesDialog({ open, onKeepEditing, onDiscard, description = "What you typed will be lost.", ...props }) {
87
+ // Its own Root, outside any guard: rendered inside a dirty dialog, its
88
+ // content mustn't tell that dialog's guard it's clean.
89
+ return (_jsx(DialogGuardContext.Provider, { value: null, children: _jsx(DialogPrimitive.Root, { open: open, onOpenChange: (next) => {
90
+ if (!next)
91
+ onKeepEditing();
92
+ }, children: _jsxs(DialogContent, { size: "confirm", showCloseButton: false, "data-discard-changes": "", ...props, children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { children: "Discard your changes?" }), _jsx(DialogDescription, { children: description })] }), _jsxs(DialogFooter, { children: [_jsx(DialogClose, { render: _jsx(Button, { variant: "outline" }), children: "Keep editing" }), _jsx(Button, { variant: "destructive", onClick: onDiscard, children: "Discard" })] })] }) }) }));
93
+ }
94
+ export { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, DiscardChangesDialog, };