@bearmenu/ui 0.8.33 → 0.8.35

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 (86) hide show
  1. package/dist/{chunk-GESB7GCF.js → chunk-6W25S5CF.js} +1 -1
  2. package/dist/chunk-LY67SOFC.js +142 -0
  3. package/dist/{chunk-ZU4NAYN7.js → chunk-NIAL4ZU2.js} +13 -1
  4. package/dist/{chunk-JWTN7MNK.js → chunk-TVEIUTBQ.js} +2 -2
  5. package/dist/{chunk-ZJMCU7MI.js → chunk-VCRXL7H5.js} +1 -1
  6. package/dist/chunk-XLHMF6F5.js +127 -0
  7. package/dist/{chunk-VSRGEB36.js → chunk-XNBDACL7.js} +1 -1
  8. package/dist/components/action-bar.d.ts +67 -0
  9. package/dist/components/action-bar.js +43 -0
  10. package/dist/components/activity-row.d.ts +38 -0
  11. package/dist/components/activity-row.js +43 -0
  12. package/dist/components/alert.d.ts +2 -2
  13. package/dist/components/avatar.d.ts +4 -1
  14. package/dist/components/avatar.js +39 -5
  15. package/dist/components/badge.d.ts +1 -1
  16. package/dist/components/badge.js +1 -1
  17. package/dist/components/booking-row.d.ts +11 -1
  18. package/dist/components/booking-row.js +4 -1
  19. package/dist/components/button.d.ts +2 -2
  20. package/dist/components/combobox.js +2 -2
  21. package/dist/components/command.d.ts +15 -15
  22. package/dist/components/command.js +2 -2
  23. package/dist/components/concept-convergence.js +2 -2
  24. package/dist/components/currency-input.js +4 -142
  25. package/dist/components/dialog.d.ts +6 -0
  26. package/dist/components/dialog.js +1 -1
  27. package/dist/components/drawer.d.ts +6 -0
  28. package/dist/components/drawer.js +3 -127
  29. package/dist/components/empty-state.d.ts +2 -2
  30. package/dist/components/filter-category-row.js +1 -1
  31. package/dist/components/menu-item.js +3 -3
  32. package/dist/components/menu-pick-row.d.ts +47 -0
  33. package/dist/components/menu-pick-row.js +49 -0
  34. package/dist/components/multi-select-combobox.js +3 -3
  35. package/dist/components/place-card.js +1 -1
  36. package/dist/components/place-details-mobile.js +1 -1
  37. package/dist/components/qr-panel.d.ts +31 -0
  38. package/dist/components/qr-panel.js +33 -0
  39. package/dist/components/reconcile-bar.d.ts +47 -0
  40. package/dist/components/reconcile-bar.js +60 -0
  41. package/dist/components/responsive-overlay.d.ts +59 -0
  42. package/dist/components/responsive-overlay.js +132 -0
  43. package/dist/components/searchable-select.js +2 -2
  44. package/dist/components/signature-chip.js +2 -2
  45. package/dist/components/spinner.d.ts +2 -2
  46. package/dist/components/split-share-row.d.ts +77 -0
  47. package/dist/components/split-share-row.js +101 -0
  48. package/dist/components/table-ledger-card.d.ts +80 -0
  49. package/dist/components/table-ledger-card.js +84 -0
  50. package/dist/components/table-trigger-band.d.ts +56 -0
  51. package/dist/components/table-trigger-band.js +43 -0
  52. package/dist/components/tag.d.ts +2 -2
  53. package/dist/components/text.d.ts +2 -2
  54. package/dist/components/toggle.d.ts +1 -1
  55. package/dist/components/top-ranking-entry.js +3 -3
  56. package/dist/components/venue-events-block.js +1 -1
  57. package/package.json +23 -207
  58. package/src/components/action-bar.stories.tsx +78 -0
  59. package/src/components/action-bar.tsx +80 -0
  60. package/src/components/activity-row.stories.tsx +78 -0
  61. package/src/components/activity-row.tsx +65 -0
  62. package/src/components/avatar.stories.tsx +55 -0
  63. package/src/components/avatar.tsx +63 -19
  64. package/src/components/badge.stories.tsx +41 -0
  65. package/src/components/badge.tsx +13 -0
  66. package/src/components/booking-row.tsx +13 -0
  67. package/src/components/dialog.tsx +8 -2
  68. package/src/components/drawer.tsx +8 -2
  69. package/src/components/menu-pick-row.stories.tsx +71 -0
  70. package/src/components/menu-pick-row.tsx +76 -0
  71. package/src/components/qr-panel.stories.tsx +39 -0
  72. package/src/components/qr-panel.tsx +52 -0
  73. package/src/components/reconcile-bar.stories.tsx +57 -0
  74. package/src/components/reconcile-bar.tsx +80 -0
  75. package/src/components/responsive-overlay.stories.tsx +91 -0
  76. package/src/components/responsive-overlay.test.tsx +162 -0
  77. package/src/components/responsive-overlay.tsx +232 -0
  78. package/src/components/shared-table-primitives.test.tsx +482 -0
  79. package/src/components/split-share-row.stories.tsx +114 -0
  80. package/src/components/split-share-row.tsx +150 -0
  81. package/src/components/table-ledger-card.stories.tsx +120 -0
  82. package/src/components/table-ledger-card.tsx +138 -0
  83. package/src/components/table-trigger-band.stories.tsx +76 -0
  84. package/src/components/table-trigger-band.tsx +75 -0
  85. package/src/globals.css +44 -0
  86. package/src/test/setup.ts +11 -0
@@ -0,0 +1,71 @@
1
+ import * as React from "react";
2
+ import type { Meta, StoryObj } from "@storybook/react";
3
+ import { MenuPickRow } from "./menu-pick-row";
4
+
5
+ const meta: Meta<typeof MenuPickRow> = {
6
+ title: "Shared table/MenuPickRow",
7
+ component: MenuPickRow,
8
+ tags: ["autodocs"],
9
+ parameters: {
10
+ docs: {
11
+ description: {
12
+ component:
13
+ "A dish in the add-an-item overlay. The disc is the control, not the row: a whole-row target would make reading the menu and adding to the table the same gesture.",
14
+ },
15
+ },
16
+ },
17
+ };
18
+
19
+ export default meta;
20
+ type Story = StoryObj<typeof MenuPickRow>;
21
+
22
+ export const Unselected: Story = {
23
+ args: { label: "Mușchi de vită la grătar", price: "96 lei", addLabel: "Add Mușchi de vită la grătar" },
24
+ };
25
+
26
+ export const Selected: Story = {
27
+ args: {
28
+ label: "Tochitură ardelenească",
29
+ price: "54 lei",
30
+ selected: true,
31
+ removeLabel: "Remove Tochitură ardelenească",
32
+ },
33
+ };
34
+
35
+ /** T3 — a category of the venue's menu. */
36
+ export const Category: Story = {
37
+ render: () => {
38
+ const [picked, setPicked] = React.useState<string[]>(["Tochitură ardelenească"]);
39
+ const dishes = [
40
+ { label: "Mușchi de vită la grătar", price: "96 lei" },
41
+ { label: "Tochitură ardelenească", price: "54 lei" },
42
+ { label: "Păstrăv la cuptor", price: "62 lei" },
43
+ ];
44
+ return (
45
+ <div>
46
+ <p className="mb-1 font-mono text-[11px] font-bold uppercase tracking-wider text-muted-foreground">
47
+ Main courses
48
+ </p>
49
+ <div className="divide-y divide-border">
50
+ {dishes.map((dish) => (
51
+ <MenuPickRow
52
+ key={dish.label}
53
+ label={dish.label}
54
+ price={dish.price}
55
+ selected={picked.includes(dish.label)}
56
+ onToggle={() =>
57
+ setPicked((prev) =>
58
+ prev.includes(dish.label)
59
+ ? prev.filter((d) => d !== dish.label)
60
+ : [...prev, dish.label]
61
+ )
62
+ }
63
+ addLabel={`Add ${dish.label}`}
64
+ removeLabel={`Remove ${dish.label}`}
65
+ />
66
+ ))}
67
+ </div>
68
+ </div>
69
+ );
70
+ },
71
+ };
@@ -0,0 +1,76 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+ import { Plus } from "lucide-react";
5
+ import { cn } from "../lib/utils";
6
+
7
+ /**
8
+ * MenuPickRow — a dish in the add-an-item overlay (16-shared-table.html T3):
9
+ * its name, its price, and a disc that adds it.
10
+ *
11
+ * The disc is the control and the row is not: a whole-row target would make
12
+ * "read the menu" and "add this" the same gesture, and a reader scrolling a
13
+ * long menu would add things by accident. It is 44×44 as the accessibility
14
+ * floor asks, with the drawn 28px disc inside it.
15
+ *
16
+ * A tap adds one unit. There is no stepper and no quantity anywhere in the
17
+ * shared table — one row per unit, so removing one is removing one thing and
18
+ * never an arithmetic edit (the board's ruling 3). `selected` is therefore
19
+ * "this dish is on the table", not a count; a caller showing a second unit
20
+ * shows a second row.
21
+ */
22
+ export type MenuPickRowProps = Omit<React.HTMLAttributes<HTMLDivElement>, "onToggle"> & {
23
+ /** The dish. */
24
+ label: React.ReactNode;
25
+ /** Its price, formatted by the caller. */
26
+ price: React.ReactNode;
27
+ /** Is it on the table already? Fills the disc. */
28
+ selected?: boolean;
29
+ onToggle?: () => void;
30
+ /** The disc's accessible name when it would add — "Add Papanași". Injected. */
31
+ addLabel?: string;
32
+ /** Its name when it would remove — "Remove Papanași". Injected. */
33
+ removeLabel?: string;
34
+ disabled?: boolean;
35
+ };
36
+
37
+ const MenuPickRow = React.forwardRef<HTMLDivElement, MenuPickRowProps>(
38
+ (
39
+ { className, label, price, selected = false, onToggle, addLabel, removeLabel, disabled = false, ...props },
40
+ ref
41
+ ) => (
42
+ <div
43
+ ref={ref}
44
+ data-slot="menu-pick-row"
45
+ data-selected={selected ? "" : undefined}
46
+ className={cn("flex items-center gap-3 py-2.5", className)}
47
+ {...props}
48
+ >
49
+ <span className="min-w-0 flex-1 truncate text-[14px] font-bold">{label}</span>
50
+ {/* The numeral is the bold sans; only the caller's currency suffix is mono. */}
51
+ <span className="shrink-0 text-[14px] font-bold tabular-nums">{price}</span>
52
+ <button
53
+ type="button"
54
+ onClick={onToggle}
55
+ disabled={disabled}
56
+ aria-pressed={selected}
57
+ aria-label={selected ? removeLabel : addLabel}
58
+ className="grid size-11 shrink-0 place-items-center rounded-full focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
59
+ >
60
+ <span
61
+ className={cn(
62
+ "grid size-7 place-items-center rounded-full border transition-colors",
63
+ selected
64
+ ? "border-transparent bg-primary text-primary-foreground"
65
+ : "border-border bg-background text-muted-foreground"
66
+ )}
67
+ >
68
+ <Plus className="size-4" aria-hidden strokeWidth={2.5} />
69
+ </span>
70
+ </button>
71
+ </div>
72
+ )
73
+ );
74
+ MenuPickRow.displayName = "MenuPickRow";
75
+
76
+ export { MenuPickRow };
@@ -0,0 +1,39 @@
1
+ import type { Meta, StoryObj } from "@storybook/react";
2
+ import { QrPanel } from "./qr-panel";
3
+
4
+ const meta: Meta<typeof QrPanel> = {
5
+ title: "Shared table/QrPanel",
6
+ component: QrPanel,
7
+ tags: ["autodocs"],
8
+ parameters: {
9
+ docs: {
10
+ description: {
11
+ component:
12
+ "The frame around the code that gets a phone onto a table. The code arrives as children — the design system imports no QR library. The plate stays white in dark mode because a reader needs the light module ground and the quiet zone to decode.",
13
+ },
14
+ },
15
+ },
16
+ };
17
+
18
+ export default meta;
19
+ type Story = StoryObj<typeof QrPanel>;
20
+
21
+ /** Stand-in for whatever the app's QR library renders. */
22
+ const code = (
23
+ <svg viewBox="0 0 21 21" shapeRendering="crispEdges" aria-hidden>
24
+ <rect width="21" height="21" fill="#fff" />
25
+ {Array.from({ length: 21 }, (_, y) =>
26
+ Array.from({ length: 21 }, (_, x) =>
27
+ (x * 7 + y * 3) % 5 < 2 ? <rect key={`${x}-${y}`} x={x} y={y} width="1" height="1" /> : null
28
+ )
29
+ )}
30
+ </svg>
31
+ );
32
+
33
+ export const Default: Story = {
34
+ args: { children: code, caption: "Scan with a phone camera to join" },
35
+ };
36
+
37
+ export const WithoutCaption: Story = {
38
+ args: { children: code },
39
+ };
@@ -0,0 +1,52 @@
1
+ import * as React from "react";
2
+ import { cn } from "../lib/utils";
3
+
4
+ /**
5
+ * QrPanel — the frame around the code that gets a phone onto a shared table
6
+ * (16-shared-table.html T8).
7
+ *
8
+ * The code arrives as `children`: an `img` with a data URL, an inline `svg`,
9
+ * whatever the caller's QR library returns. The design system imports no QR
10
+ * library — a dependency that ships in every consumer's bundle to serve one
11
+ * sheet is the wrong trade, and the app already has `qrcode`.
12
+ *
13
+ * The white plate is not decoration. A QR reader needs the quiet zone and the
14
+ * light module ground to decode, so the plate stays white in dark mode too and
15
+ * the padding is the quiet zone, not padding taste. Its hairline is a neutral
16
+ * alpha rather than `--border`: the plate is always white, so a themed border
17
+ * would put a dark ring on it in dark mode.
18
+ *
19
+ * The caption sits OUTSIDE the plate, on the sheet's own ground (T8), which is
20
+ * also what lets it use `--muted-foreground` instead of an ink picked to work
21
+ * on white.
22
+ */
23
+ export type QrPanelProps = React.HTMLAttributes<HTMLDivElement> & {
24
+ /** The line under the code — "Scan with a phone camera to join". */
25
+ caption?: React.ReactNode;
26
+ };
27
+
28
+ const QrPanel = React.forwardRef<HTMLDivElement, QrPanelProps>(
29
+ ({ className, caption, children, ...props }, ref) => (
30
+ <div
31
+ ref={ref}
32
+ data-slot="qr-panel"
33
+ className={cn("flex flex-col items-center gap-3", className)}
34
+ {...props}
35
+ >
36
+ <div
37
+ data-slot="qr-panel-plate"
38
+ className="rounded-2xl border border-black/10 bg-white p-5"
39
+ >
40
+ <div data-slot="qr-panel-code" className="[&>*]:block [&>*]:size-full max-w-[220px]">
41
+ {children}
42
+ </div>
43
+ </div>
44
+ {caption ? (
45
+ <p className="text-center text-[12.5px] leading-normal text-muted-foreground">{caption}</p>
46
+ ) : null}
47
+ </div>
48
+ )
49
+ );
50
+ QrPanel.displayName = "QrPanel";
51
+
52
+ export { QrPanel };
@@ -0,0 +1,57 @@
1
+ import type { Meta, StoryObj } from "@storybook/react";
2
+ import { ReconcileBar } from "./reconcile-bar";
3
+
4
+ const meta: Meta<typeof ReconcileBar> = {
5
+ title: "Shared table/ReconcileBar",
6
+ component: ReconcileBar,
7
+ tags: ["autodocs"],
8
+ parameters: {
9
+ docs: {
10
+ description: {
11
+ component:
12
+ "Does the split add up? The running figure and the target side by side, so the reader never does the subtraction. Figures are tabular so the bar does not twitch while someone types.",
13
+ },
14
+ },
15
+ },
16
+ };
17
+
18
+ export default meta;
19
+ type Story = StoryObj<typeof ReconcileBar>;
20
+
21
+ /** T6 — the shares come to the line total exactly. */
22
+ export const Ok: Story = {
23
+ args: {
24
+ tone: "ok",
25
+ message: <b>The amounts add up</b>,
26
+ current: "120",
27
+ target: "120",
28
+ },
29
+ };
30
+
31
+ /** X10 — the shortfall is named in money, and Save is blocked until it balances. */
32
+ export const Short: Story = {
33
+ args: {
34
+ tone: "short",
35
+ message: (
36
+ <>
37
+ <b>10 lei short</b> of the item total
38
+ </>
39
+ ),
40
+ current: "110",
41
+ target: "120",
42
+ },
43
+ };
44
+
45
+ /** An overshoot takes the same treatment — the bar says the figure, not the sign. */
46
+ export const Over: Story = {
47
+ args: {
48
+ tone: "short",
49
+ message: (
50
+ <>
51
+ <b>15 lei over</b> the item total
52
+ </>
53
+ ),
54
+ current: "135",
55
+ target: "120",
56
+ },
57
+ };
@@ -0,0 +1,80 @@
1
+ import * as React from "react";
2
+ import { cva, type VariantProps } from "class-variance-authority";
3
+ import { Check, TriangleAlert } from "lucide-react";
4
+ import { cn } from "../lib/utils";
5
+
6
+ /**
7
+ * ReconcileBar — does the split add up? The shared-table board puts it under
8
+ * every split, in both states (16-shared-table.html T6, X10): "The amounts add
9
+ * up · 120 / 120" on a green ground, "10 lei short of the item total ·
10
+ * 110 / 120" on the warning one.
11
+ *
12
+ * It is the whole answer to "can I save this", so it says the running figure
13
+ * and the target side by side and never makes the reader do the subtraction.
14
+ * The numbers are `tabular-nums`: they change on every keystroke in the custom
15
+ * mode, and proportional digits make the bar twitch while someone types.
16
+ *
17
+ * `message` is injected — the shortfall is money and money is formatted in the
18
+ * caller's locale, which the design system does not know.
19
+ */
20
+ const reconcileBarVariants = cva(
21
+ "flex items-center gap-2.5 rounded-xl px-3.5 py-3 text-[12.5px] leading-normal [&>svg]:size-4 [&>svg]:shrink-0",
22
+ {
23
+ variants: {
24
+ tone: {
25
+ ok: "bg-success/15 text-success-ink",
26
+ // `--warning-deep` and `--warning-ink` are the tokens for exactly this:
27
+ // the solid `--warning` is tuned as a fill and falls short of AA as ink
28
+ // on its own wash. Both have dark twins, so there is no `dark:` branch.
29
+ short: "bg-warning-deep/15 text-warning-ink",
30
+ },
31
+ },
32
+ defaultVariants: { tone: "ok" },
33
+ }
34
+ );
35
+
36
+ export type ReconcileBarProps = Omit<React.HTMLAttributes<HTMLDivElement>, "children"> &
37
+ VariantProps<typeof reconcileBarVariants> & {
38
+ /**
39
+ * "The amounts add up", "**10 lei short** of the item total". The caller
40
+ * carries the weight — the shortfall is bold, the rest is not.
41
+ */
42
+ message: React.ReactNode;
43
+ /** What the shares come to now, formatted by the caller. */
44
+ current: React.ReactNode;
45
+ /** What they have to come to — the item's line total. */
46
+ target: React.ReactNode;
47
+ };
48
+
49
+ const TONE_ICON = {
50
+ ok: <Check aria-hidden strokeWidth={3} />,
51
+ short: <TriangleAlert aria-hidden />,
52
+ } as const;
53
+
54
+ const ReconcileBar = React.forwardRef<HTMLDivElement, ReconcileBarProps>(
55
+ ({ className, tone, message, current, target, ...props }, ref) => (
56
+ <div
57
+ ref={ref}
58
+ data-slot="reconcile-bar"
59
+ data-tone={tone ?? "ok"}
60
+ className={cn(reconcileBarVariants({ tone }), className)}
61
+ {...props}
62
+ >
63
+ {TONE_ICON[tone ?? "ok"]}
64
+ {/* Weight is the caller's: X10 draws "10 lei short" bold and "of the item
65
+ total" regular, which a blanket `font-semibold` here would flatten. */}
66
+ <span className="min-w-0 flex-1">{message}</span>
67
+ <span
68
+ data-slot="reconcile-bar-figures"
69
+ className="shrink-0 text-[12px] font-bold tabular-nums"
70
+ >
71
+ {current}
72
+ <span className="px-1.5 opacity-60">/</span>
73
+ {target}
74
+ </span>
75
+ </div>
76
+ )
77
+ );
78
+ ReconcileBar.displayName = "ReconcileBar";
79
+
80
+ export { ReconcileBar, reconcileBarVariants };
@@ -0,0 +1,91 @@
1
+ import * as React from "react";
2
+ import type { Meta, StoryObj } from "@storybook/react";
3
+ import { ActionBar } from "./action-bar";
4
+ import { Button } from "./button";
5
+ import { MenuPickRow } from "./menu-pick-row";
6
+ import { ResponsiveOverlay } from "./responsive-overlay";
7
+
8
+ const meta: Meta<typeof ResponsiveOverlay> = {
9
+ title: "Shared table/ResponsiveOverlay",
10
+ component: ResponsiveOverlay,
11
+ tags: ["autodocs"],
12
+ parameters: {
13
+ docs: {
14
+ description: {
15
+ component:
16
+ "One overlay authored once: a Drawer under md, a Dialog from md. The chrome is the same at both widths — title, one-line subtitle, round close, body, footer band. Resize the preview across 768px to see it change container. The scrim is `--scrim-overlay`, never `--scrim`.",
17
+ },
18
+ },
19
+ },
20
+ };
21
+
22
+ export default meta;
23
+ type Story = StoryObj<typeof ResponsiveOverlay>;
24
+
25
+ /** T3 — add an item. */
26
+ export const AddAnItem: Story = {
27
+ render: () => {
28
+ const [open, setOpen] = React.useState(false);
29
+ return (
30
+ <>
31
+ <Button variant="cta" onClick={() => setOpen(true)}>
32
+ Add item
33
+ </Button>
34
+ <ResponsiveOverlay
35
+ open={open}
36
+ onOpenChange={setOpen}
37
+ title="Add an item"
38
+ subtitle="Table 18 · from the Sălcii Bistro menu"
39
+ closeLabel="Close"
40
+ footer={
41
+ <ActionBar
42
+ note="Added items can be assigned afterwards."
43
+ primary={<Button variant="cta">Add 1 item</Button>}
44
+ />
45
+ }
46
+ >
47
+ <p className="mb-1 font-mono text-[11px] font-bold uppercase tracking-wider text-muted-foreground">
48
+ Main courses
49
+ </p>
50
+ <div className="divide-y divide-border">
51
+ <MenuPickRow label="Mușchi de vită la grătar" price="96 lei" addLabel="Add it" />
52
+ <MenuPickRow label="Tochitură ardelenească" price="54 lei" selected removeLabel="Remove it" />
53
+ <MenuPickRow label="Păstrăv la cuptor" price="62 lei" addLabel="Add it" />
54
+ </div>
55
+ </ResponsiveOverlay>
56
+ </>
57
+ );
58
+ },
59
+ };
60
+
61
+ /** T1 — an overlay with no footer note: the two actions divide the band. */
62
+ export const JoinTheTable: Story = {
63
+ render: () => {
64
+ const [open, setOpen] = React.useState(false);
65
+ return (
66
+ <>
67
+ <Button variant="cta" onClick={() => setOpen(true)}>
68
+ Open the join overlay
69
+ </Button>
70
+ <ResponsiveOverlay
71
+ open={open}
72
+ onOpenChange={setOpen}
73
+ title="Join this table"
74
+ subtitle="Table 18 · Sălcii Bistro"
75
+ size="sm"
76
+ closeLabel="Close"
77
+ footer={
78
+ <ActionBar
79
+ secondary={<Button variant="outline">Not now</Button>}
80
+ primary={<Button variant="cta">Join the table</Button>}
81
+ />
82
+ }
83
+ >
84
+ <p className="text-[14px] text-muted-foreground">
85
+ Only the people at this table see it. No account needed.
86
+ </p>
87
+ </ResponsiveOverlay>
88
+ </>
89
+ );
90
+ },
91
+ };
@@ -0,0 +1,162 @@
1
+ import * as React from "react";
2
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
3
+ import { render, screen, waitFor } from "@testing-library/react";
4
+ import userEvent from "@testing-library/user-event";
5
+ import { ActionBar } from "./action-bar";
6
+ import { ResponsiveOverlay } from "./responsive-overlay";
7
+
8
+ /**
9
+ * jsdom has no matchMedia, so each test states the width it is testing. The
10
+ * component's own fallback (innerWidth) is covered by its own case below.
11
+ */
12
+ function setWidth(wide: boolean) {
13
+ window.matchMedia = ((query: string) =>
14
+ ({
15
+ matches: wide,
16
+ media: query,
17
+ onchange: null,
18
+ addEventListener: () => {},
19
+ removeEventListener: () => {},
20
+ addListener: () => {},
21
+ removeListener: () => {},
22
+ dispatchEvent: () => false,
23
+ }) as unknown as MediaQueryList) as typeof window.matchMedia;
24
+ }
25
+
26
+ const original = window.matchMedia;
27
+ beforeEach(() => setWidth(false));
28
+ afterEach(() => {
29
+ window.matchMedia = original;
30
+ vi.restoreAllMocks();
31
+ });
32
+
33
+ function open(props: Partial<React.ComponentProps<typeof ResponsiveOverlay>> = {}) {
34
+ return render(
35
+ <ResponsiveOverlay
36
+ open
37
+ onOpenChange={() => {}}
38
+ title="Add an item"
39
+ subtitle="Table 18 · from the menu"
40
+ closeLabel="Close"
41
+ {...props}
42
+ >
43
+ <p>body</p>
44
+ </ResponsiveOverlay>
45
+ );
46
+ }
47
+
48
+ describe("ResponsiveOverlay", () => {
49
+ it("is a drawer under md", () => {
50
+ open();
51
+ expect(document.querySelector('[data-slot="drawer-content"]')).toBeInTheDocument();
52
+ expect(document.querySelector('[data-slot="dialog-content"]')).toBeNull();
53
+ });
54
+
55
+ it("is a dialog from md", () => {
56
+ setWidth(true);
57
+ open();
58
+ expect(document.querySelector('[data-slot="dialog-content"]')).toBeInTheDocument();
59
+ expect(document.querySelector('[data-slot="drawer-content"]')).toBeNull();
60
+ });
61
+
62
+ it("carries the same chrome at both widths", () => {
63
+ for (const wide of [false, true]) {
64
+ setWidth(wide);
65
+ const view = open();
66
+ expect(screen.getByText("Add an item")).toBeInTheDocument();
67
+ expect(screen.getByText("Table 18 · from the menu")).toBeInTheDocument();
68
+ expect(screen.getByRole("button", { name: "Close" })).toBeInTheDocument();
69
+ view.unmount();
70
+ }
71
+ });
72
+
73
+ it("names the overlay by its title and describes it by its subtitle", () => {
74
+ setWidth(true);
75
+ open();
76
+ const dialog = screen.getByRole("dialog");
77
+ expect(dialog).toHaveAccessibleName("Add an item");
78
+ expect(dialog).toHaveAccessibleDescription("Table 18 · from the menu");
79
+ });
80
+
81
+ it("closes through the round button", async () => {
82
+ const onOpenChange = vi.fn();
83
+ open({ onOpenChange });
84
+ await userEvent.click(screen.getByRole("button", { name: "Close" }));
85
+ expect(onOpenChange).toHaveBeenCalledWith(false);
86
+ });
87
+
88
+ it("dims with the overlay token, never the app-shadowed --scrim", () => {
89
+ setWidth(true);
90
+ open();
91
+ const scrim = document.querySelector('[data-slot="dialog-overlay"]');
92
+ const classes = (scrim?.className ?? "").split(" ");
93
+ expect(classes).toContain("bg-scrim-overlay");
94
+ // `--scrim` is an opaque photo hue in bearmenu-fe's globals.css, which
95
+ // loads after ours. A bare `bg-scrim` here would paint it over the page.
96
+ expect(classes).not.toContain("bg-scrim");
97
+ expect(classes).not.toContain("bg-black/50");
98
+ });
99
+
100
+ it("renders the footer band when one is given", () => {
101
+ open({
102
+ footer: <ActionBar primary={<button type="button">Add 1 item</button>} />,
103
+ });
104
+ expect(screen.getByRole("button", { name: "Add 1 item" })).toBeInTheDocument();
105
+ expect(document.querySelector('[data-slot="responsive-overlay-footer"]')).toBeInTheDocument();
106
+ });
107
+
108
+ it("falls back to the phone variant when there is no matchMedia at all", () => {
109
+ // `useMediaQuery` is mobile-first: the server, the first client frame and
110
+ // an environment without matchMedia (jsdom) all get the drawer. Deterministic
111
+ // beats clever — a test that wants the dialog says so by mocking the query.
112
+ delete (window as { matchMedia?: unknown }).matchMedia;
113
+ open();
114
+ expect(document.querySelector('[data-slot="drawer-content"]')).toBeInTheDocument();
115
+ });
116
+
117
+ it("closes on Escape", async () => {
118
+ setWidth(true);
119
+ const onOpenChange = vi.fn();
120
+ open({ onOpenChange });
121
+ await userEvent.keyboard("{Escape}");
122
+ expect(onOpenChange).toHaveBeenCalledWith(false);
123
+ });
124
+
125
+ it("returns focus to the control that opened it", async () => {
126
+ setWidth(true);
127
+
128
+ function Harness() {
129
+ const [isOpen, setIsOpen] = React.useState(false);
130
+ return (
131
+ <>
132
+ <button type="button" onClick={() => setIsOpen(true)}>
133
+ Add item
134
+ </button>
135
+ <ResponsiveOverlay
136
+ open={isOpen}
137
+ onOpenChange={setIsOpen}
138
+ title="Add an item"
139
+ closeLabel="Close"
140
+ >
141
+ <p>body</p>
142
+ </ResponsiveOverlay>
143
+ </>
144
+ );
145
+ }
146
+
147
+ render(<Harness />);
148
+ const opener = screen.getByRole("button", { name: "Add item" });
149
+ await userEvent.click(opener);
150
+ await userEvent.click(screen.getByRole("button", { name: "Close" }));
151
+ await waitFor(() => expect(opener).toHaveFocus());
152
+ });
153
+
154
+ it("stays out of the DOM while closed", () => {
155
+ render(
156
+ <ResponsiveOverlay open={false} onOpenChange={() => {}} title="Add an item">
157
+ <p>body</p>
158
+ </ResponsiveOverlay>
159
+ );
160
+ expect(screen.queryByText("body")).toBeNull();
161
+ });
162
+ });