@omer-x/tailwind-ui-kit 1.0.0-alpha.21 → 1.0.0-alpha.23
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/MIGRATION.md +36 -0
- package/README.md +85 -21
- package/dist/components/dialog/BaseDialog.d.ts +58 -0
- package/dist/components/dialog/BaseDialog.js +24 -0
- package/dist/components/dialog/BaseDialogClose.d.ts +13 -0
- package/dist/components/dialog/BaseDialogClose.js +7 -0
- package/dist/components/dialog/BaseDialogDescription.d.ts +8 -0
- package/dist/components/dialog/BaseDialogDescription.js +7 -0
- package/dist/components/dialog/BaseDialogPopup.d.ts +41 -0
- package/dist/components/dialog/BaseDialogPopup.js +42 -0
- package/dist/components/dialog/BaseDialogTitle.d.ts +8 -0
- package/dist/components/dialog/BaseDialogTitle.js +11 -0
- package/dist/components/dialog/BaseDialogTrigger.d.ts +20 -0
- package/dist/components/dialog/BaseDialogTrigger.js +11 -0
- package/dist/components/dialog/createDialogHandle.d.ts +26 -0
- package/dist/components/dialog/createDialogHandle.js +28 -0
- package/dist/components/dialog/index.d.ts +6 -4
- package/dist/components/dialog/index.js +3 -4
- package/package.json +1 -1
- package/dist/components/dialog/AnimatedPopup.d.ts +0 -7
- package/dist/components/dialog/AnimatedPopup.js +0 -9
- package/dist/components/dialog/DialogBackdrop.d.ts +0 -7
- package/dist/components/dialog/DialogBackdrop.js +0 -9
- package/dist/components/dialog/DialogTitle.d.ts +0 -7
- package/dist/components/dialog/DialogTitle.js +0 -9
package/MIGRATION.md
CHANGED
|
@@ -102,6 +102,42 @@ Remove any `pending={isSubmitting}` you wired up by hand for submit buttons of `
|
|
|
102
102
|
|
|
103
103
|
**Why:** screen readers announced a plain group of buttons and links, with no current page and unnamed arrows.
|
|
104
104
|
|
|
105
|
+
## Dialog
|
|
106
|
+
|
|
107
|
+
### `AnimatedPopup`, `DialogBackdrop` and `DialogTitle` are replaced by `BaseDialog` (mechanical, #52)
|
|
108
|
+
|
|
109
|
+
The dialog entry point now wraps every part, so apps no longer import `@base-ui/react/dialog`. `BaseDialog.Popup` brings its portal and the backdrop that `DialogBackdrop` used to style, and `BaseDialog.Title` has `DialogTitle`'s typography:
|
|
110
|
+
|
|
111
|
+
```diff
|
|
112
|
+
- import { Dialog } from "@base-ui/react/dialog";
|
|
113
|
+
- import { AnimatedPopup, DialogBackdrop, DialogTitle } from "@omer-x/tailwind-ui-kit/dialog";
|
|
114
|
+
+ import { BaseDialog } from "@omer-x/tailwind-ui-kit/dialog";
|
|
115
|
+
|
|
116
|
+
- <Dialog.Root>
|
|
117
|
+
- <Dialog.Trigger render={<Button />}>Open</Dialog.Trigger>
|
|
118
|
+
- <Dialog.Portal>
|
|
119
|
+
- <Dialog.Backdrop render={<DialogBackdrop />} />
|
|
120
|
+
- <Dialog.Popup render={<AnimatedPopup className="rounded-lg bg-white p-6 shadow-lg" />}>
|
|
121
|
+
- <Dialog.Title render={<DialogTitle />}>Delete item?</Dialog.Title>
|
|
122
|
+
- <Dialog.Close render={<Button />}>Cancel</Dialog.Close>
|
|
123
|
+
- </Dialog.Popup>
|
|
124
|
+
- </Dialog.Portal>
|
|
125
|
+
- </Dialog.Root>
|
|
126
|
+
+ <BaseDialog>
|
|
127
|
+
+ <BaseDialog.Trigger render={<Button />}>Open</BaseDialog.Trigger>
|
|
128
|
+
+ <BaseDialog.Popup className="rounded-lg bg-white p-6 shadow-lg">
|
|
129
|
+
+ <BaseDialog.Title>Delete item?</BaseDialog.Title>
|
|
130
|
+
+ <BaseDialog.Close render={<Button />}>Cancel</BaseDialog.Close>
|
|
131
|
+
+ </BaseDialog.Popup>
|
|
132
|
+
+ </BaseDialog>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
- `Dialog.createHandle()` becomes `createDialogHandle()`. Its `open()` no longer needs an argument.
|
|
136
|
+
- `onOpenChange` gets the DOM event instead of Base UI's event details.
|
|
137
|
+
- Portal props (`container`, `keepMounted`) move to `BaseDialog.Popup`. Style the backdrop with its `renderBackdrop` prop, or turn it off with `backdrop={false}`.
|
|
138
|
+
|
|
139
|
+
**Why:** apps had to import Base UI to build a dialog, which tied them to it.
|
|
140
|
+
|
|
105
141
|
## Form
|
|
106
142
|
|
|
107
143
|
### `BaseSwitch`: `onCheckedChange` gets the DOM event (mechanical, #55)
|
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ Tailwind v4 does not scan `node_modules`, so the classes used inside the compone
|
|
|
44
44
|
@source "../../node_modules/@omer-x/tailwind-ui-kit";
|
|
45
45
|
```
|
|
46
46
|
|
|
47
|
-
Some components use the `dark:` variant (e.g. `
|
|
47
|
+
Some components use the `dark:` variant (e.g. the backdrop of `BaseDialog.Popup`). They follow whatever `dark` variant your app defines.
|
|
48
48
|
|
|
49
49
|
### 2. Your bundler must support CSS modules
|
|
50
50
|
|
|
@@ -299,32 +299,96 @@ Error texts come from `BaseForm`'s `formatMessage`. Render functions get `FieldS
|
|
|
299
299
|
|
|
300
300
|
### `@omer-x/tailwind-ui-kit/dialog`
|
|
301
301
|
|
|
302
|
-
|
|
302
|
+
#### `BaseDialog`
|
|
303
303
|
|
|
304
|
-
|
|
304
|
+
A Base UI [`Dialog`](https://base-ui.com/react/components/dialog) made of a few parts. `Popup` brings its portal and backdrop, so a simple dialog needs no more than this:
|
|
305
305
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
306
|
+
```tsx
|
|
307
|
+
import { BaseDialog } from "@omer-x/tailwind-ui-kit/dialog";
|
|
308
|
+
|
|
309
|
+
<BaseDialog>
|
|
310
|
+
<BaseDialog.Trigger render={<Button />}>Delete</BaseDialog.Trigger>
|
|
311
|
+
<BaseDialog.Popup className="rounded-lg bg-white p-6 shadow-lg">
|
|
312
|
+
<BaseDialog.Title>Delete item?</BaseDialog.Title>
|
|
313
|
+
<BaseDialog.Description>This can't be undone.</BaseDialog.Description>
|
|
314
|
+
<BaseDialog.Close render={<Button />}>Cancel</BaseDialog.Close>
|
|
315
|
+
</BaseDialog.Popup>
|
|
316
|
+
</BaseDialog>
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
The parts are reached with dot notation (`BaseDialog.Popup`), which only works in client components. Build dialogs in a `"use client"` file, e.g. your app's themed wrapper.
|
|
320
|
+
|
|
321
|
+
| Part | Renders | Description |
|
|
322
|
+
|------|---------|-------------|
|
|
323
|
+
| `BaseDialog` | nothing | Groups the parts and holds the open state |
|
|
324
|
+
| `BaseDialog.Trigger` | `<button>` | Opens the dialog. `data-popup-open` while its dialog is open |
|
|
325
|
+
| `BaseDialog.Popup` | `<div>` | The dialog's window, portaled with a backdrop. Centered, with scale and fade transitions. Nested dialogs stack with an offset and scale down |
|
|
326
|
+
| `BaseDialog.Title` | `<h2>` | The heading that names the dialog (`text-xl font-semibold`) |
|
|
327
|
+
| `BaseDialog.Description` | `<p>` | Describes the dialog to assistive technology |
|
|
328
|
+
| `BaseDialog.Close` | `<button>` | Closes the dialog |
|
|
329
|
+
|
|
330
|
+
Every part takes `className` and `render`. `Trigger` and `Close` also take `nativeButton` (set it to `false` when `render` doesn't render a `<button>`).
|
|
331
|
+
|
|
332
|
+
`BaseDialog` props:
|
|
333
|
+
|
|
334
|
+
| Prop | Type | Default | Description |
|
|
335
|
+
|------|------|---------|-------------|
|
|
336
|
+
| `open` / `defaultOpen` | `boolean` | `false` | Controlled / initial open state |
|
|
337
|
+
| `onOpenChange` | `(open, event) => void` | | Called with the new state and the DOM event that changed it (`undefined` when changed from code) |
|
|
338
|
+
| `onOpenChangeComplete` | `(open) => void` | | Called after the open or close transition ends |
|
|
339
|
+
| `modal` | `boolean \| "trap-focus"` | `true` | `true` traps focus, locks page scroll and blocks outside pointer input. `"trap-focus"` only traps focus. `false` leaves the page usable |
|
|
340
|
+
| `disablePointerDismissal` | `boolean` | `false` | Keeps the dialog open on outside presses |
|
|
341
|
+
| `handle` | `DialogHandle<Payload>` | | Connects triggers outside the dialog and opens it from code (see below) |
|
|
342
|
+
| `triggerId` / `defaultTriggerId` | `string \| null` | | The trigger a controlled / initially open dialog belongs to |
|
|
343
|
+
| `children` | `ReactNode \| ({ payload }) => ReactNode` | | The parts, or a function that gets the payload of the trigger that opened the dialog |
|
|
344
|
+
|
|
345
|
+
`BaseDialog.Popup` props:
|
|
346
|
+
|
|
347
|
+
| Prop | Type | Default | Description |
|
|
348
|
+
|------|------|---------|-------------|
|
|
349
|
+
| `size` | `"lg" \| "md" \| "sm" \| "xs"` | `"md"` | Width: `xs` 288px, `sm` 320px, `md` 384px, `lg` 512px. Never wider than the viewport. Other widths go through `className` |
|
|
350
|
+
| `initialFocus` | `DialogFocusTarget` | `true` | The element focused on open: `true` (first tabbable element), `false` (don't move focus), a ref, or a function of the interaction type (`"mouse"`, `"touch"`, `"pen"`, `"keyboard"`) |
|
|
351
|
+
| `finalFocus` | `DialogFocusTarget` | `true` | The element focused on close. `true` is the trigger |
|
|
352
|
+
| `container` | `HTMLElement \| ShadowRoot \| RefObject \| null` | `<body>` | Where the popup is portaled to |
|
|
353
|
+
| `keepMounted` | `boolean` | `false` | Keeps the popup in the DOM while closed |
|
|
354
|
+
| `backdrop` | `boolean` | `true` | Set to `false` for no backdrop. Nested dialogs never render one |
|
|
355
|
+
| `renderBackdrop` | `RenderProp<"div", DialogBackdropState>` | | Style or replace the backdrop: a fixed full-screen black overlay (`opacity-20`, `dark:opacity-70`) with a fade transition. A `className` on the element wins over the kit's conflicting classes (`<div className="opacity-50" />`). A function gets the kit's classes in `props.className` |
|
|
356
|
+
| `render` | `RenderProp<"div", DialogPopupState>` | | The state is `{ open, nested, nestedDialogOpen }` |
|
|
357
|
+
|
|
358
|
+
Data attributes: the popup and backdrop have `data-open` / `data-closed` and `data-starting-style` / `data-ending-style` during transitions. The popup also has `data-nested` (inside another dialog) and `data-nested-dialog-open` (a dialog inside it is open).
|
|
359
|
+
|
|
360
|
+
**Nested dialogs:** put a `BaseDialog` inside another one's `Popup`. It stacks on top, and the outer popup gets a light overlay while the inner one is open.
|
|
361
|
+
|
|
362
|
+
#### `createDialogHandle`
|
|
363
|
+
|
|
364
|
+
Opens one dialog from triggers anywhere on the page, or from code. Each trigger can pass a `payload`, which the dialog's function children receive:
|
|
311
365
|
|
|
312
366
|
```tsx
|
|
313
|
-
import {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
</
|
|
367
|
+
import { BaseDialog, createDialogHandle } from "@omer-x/tailwind-ui-kit/dialog";
|
|
368
|
+
|
|
369
|
+
const deleteDialog = createDialogHandle<{ id: string, name: string }>();
|
|
370
|
+
|
|
371
|
+
// triggers anywhere on the page
|
|
372
|
+
<BaseDialog.Trigger handle={deleteDialog} payload={{ id: p.id, name: p.name }} render={<Button />}>Delete</BaseDialog.Trigger>
|
|
373
|
+
|
|
374
|
+
// one dialog on the page
|
|
375
|
+
<BaseDialog handle={deleteDialog}>
|
|
376
|
+
{({ payload }) => payload && (
|
|
377
|
+
<BaseDialog.Popup size="sm">
|
|
378
|
+
<BaseDialog.Title>Delete {payload.name}?</BaseDialog.Title>
|
|
379
|
+
</BaseDialog.Popup>
|
|
380
|
+
)}
|
|
381
|
+
</BaseDialog>
|
|
382
|
+
|
|
383
|
+
// from code
|
|
384
|
+
deleteDialog.openWithPayload({ id, name });
|
|
326
385
|
```
|
|
327
386
|
|
|
387
|
+
A `DialogHandle<Payload>` has `open(triggerId?)`, `openWithPayload(payload)`, `close()` and `isOpen`.
|
|
388
|
+
|
|
389
|
+
- Calls are **ignored while the dialog isn't mounted**. Keep the `BaseDialog` with the handle rendered.
|
|
390
|
+
- Call them **from event handlers or effects, never during render**.
|
|
391
|
+
|
|
328
392
|
### `@omer-x/tailwind-ui-kit/layout`
|
|
329
393
|
|
|
330
394
|
#### `Container`
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { RenderProp } from "../../utils/render.js";
|
|
2
|
+
import { BaseDialogTriggerProps } from "./BaseDialogTrigger.js";
|
|
3
|
+
import { DialogBackdropState, DialogFocusTarget, DialogPopupState } from "./BaseDialogPopup.js";
|
|
4
|
+
import "../../utils/index.js";
|
|
5
|
+
import { DialogCloseState } from "./BaseDialogClose.js";
|
|
6
|
+
import { DialogHandle } from "./createDialogHandle.js";
|
|
7
|
+
import { ReactNode } from "react";
|
|
8
|
+
type BaseDialogProps<Payload = unknown> = {
|
|
9
|
+
open?: boolean;
|
|
10
|
+
defaultOpen?: boolean;
|
|
11
|
+
/** Called with the new state and the DOM event that changed it (none when it was changed from code). */
|
|
12
|
+
onOpenChange?: (open: boolean, event: Event | undefined) => void;
|
|
13
|
+
/** Called after the open or close transition ends. */
|
|
14
|
+
onOpenChangeComplete?: (open: boolean) => void;
|
|
15
|
+
/**
|
|
16
|
+
* - `true`: focus is trapped, page scroll is locked and the rest of the page ignores the pointer.
|
|
17
|
+
* - `false`: the rest of the page stays usable.
|
|
18
|
+
* - `"trap-focus"`: focus is trapped, but the page scrolls and takes pointer input.
|
|
19
|
+
* @default true
|
|
20
|
+
*/
|
|
21
|
+
modal?: boolean | "trap-focus";
|
|
22
|
+
/** Keeps the dialog open on outside presses. */
|
|
23
|
+
disablePointerDismissal?: boolean;
|
|
24
|
+
/** Connects triggers placed outside the dialog, and opens it from code. Create one with `createDialogHandle()`. */
|
|
25
|
+
handle?: DialogHandle<Payload>;
|
|
26
|
+
/** The `id` of the trigger the dialog belongs to, for a controlled dialog with several triggers. */
|
|
27
|
+
triggerId?: string | null;
|
|
28
|
+
/** The `id` of the trigger an initially open dialog belongs to. */
|
|
29
|
+
defaultTriggerId?: string | null;
|
|
30
|
+
/** The dialog's parts, or a function that gets the payload of the trigger that opened it. */
|
|
31
|
+
children?: ReactNode | ((context: {
|
|
32
|
+
payload: Payload | undefined;
|
|
33
|
+
}) => ReactNode);
|
|
34
|
+
};
|
|
35
|
+
declare const _default: (<Payload>({ handle, onOpenChange, ...otherProps }: BaseDialogProps<Payload>) => import("react").JSX.Element) & {
|
|
36
|
+
Trigger: <Payload>({ handle, ...otherProps }: BaseDialogTriggerProps<Payload>) => import("react").JSX.Element;
|
|
37
|
+
Popup: ({ size, container, keepMounted, backdrop, renderBackdrop, className, ...otherProps }: import("react").ClassAttributes<HTMLDivElement> & import("react").HTMLAttributes<HTMLDivElement> & {
|
|
38
|
+
size?: "lg" | "md" | "sm" | "xs";
|
|
39
|
+
render?: RenderProp<"div", DialogPopupState>;
|
|
40
|
+
initialFocus?: DialogFocusTarget;
|
|
41
|
+
finalFocus?: DialogFocusTarget;
|
|
42
|
+
container?: HTMLElement | ShadowRoot | import("react").RefObject<HTMLElement | ShadowRoot | null> | null;
|
|
43
|
+
keepMounted?: boolean;
|
|
44
|
+
backdrop?: boolean;
|
|
45
|
+
renderBackdrop?: RenderProp<"div", DialogBackdropState>;
|
|
46
|
+
}) => import("react").JSX.Element;
|
|
47
|
+
Title: ({ className, ...otherProps }: import("react").ClassAttributes<HTMLHeadingElement> & import("react").HTMLAttributes<HTMLHeadingElement> & {
|
|
48
|
+
render?: RenderProp<"h2">;
|
|
49
|
+
}) => import("react").JSX.Element;
|
|
50
|
+
Description: (props: import("react").ClassAttributes<HTMLParagraphElement> & import("react").HTMLAttributes<HTMLParagraphElement> & {
|
|
51
|
+
render?: RenderProp<"p">;
|
|
52
|
+
}) => import("react").JSX.Element;
|
|
53
|
+
Close: (props: import("react").ClassAttributes<HTMLButtonElement> & import("react").ButtonHTMLAttributes<HTMLButtonElement> & {
|
|
54
|
+
render?: RenderProp<"button", DialogCloseState>;
|
|
55
|
+
nativeButton?: boolean;
|
|
56
|
+
}) => import("react").JSX.Element;
|
|
57
|
+
};
|
|
58
|
+
export { _default as default };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Dialog } from "@base-ui/react/dialog";
|
|
3
|
+
import BaseDialogClose from "./BaseDialogClose.js";
|
|
4
|
+
import BaseDialogDescription from "./BaseDialogDescription.js";
|
|
5
|
+
import BaseDialogPopup from "./BaseDialogPopup.js";
|
|
6
|
+
import BaseDialogTitle from "./BaseDialogTitle.js";
|
|
7
|
+
import BaseDialogTrigger from "./BaseDialogTrigger.js";
|
|
8
|
+
import { toBaseUIHandle } from "./createDialogHandle.js";
|
|
9
|
+
import { jsx } from "react/jsx-runtime";
|
|
10
|
+
// src/components/dialog/BaseDialog.tsx
|
|
11
|
+
/** Groups the parts of a dialog. Renders no element. */
|
|
12
|
+
const BaseDialog = ({ handle, onOpenChange, ...otherProps }) => /* @__PURE__ */ jsx(Dialog.Root, {
|
|
13
|
+
...otherProps,
|
|
14
|
+
handle: handle && toBaseUIHandle(handle),
|
|
15
|
+
onOpenChange: onOpenChange && ((open, details) => onOpenChange(open, details.event))
|
|
16
|
+
});
|
|
17
|
+
var BaseDialog_default = Object.assign(BaseDialog, {
|
|
18
|
+
Trigger: BaseDialogTrigger,
|
|
19
|
+
Popup: BaseDialogPopup,
|
|
20
|
+
Title: BaseDialogTitle,
|
|
21
|
+
Description: BaseDialogDescription,
|
|
22
|
+
Close: BaseDialogClose
|
|
23
|
+
});
|
|
24
|
+
export { BaseDialog_default as default };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { RenderProp } from "../../utils/render.js";
|
|
2
|
+
import { ComponentProps } from "react";
|
|
3
|
+
export type DialogCloseState = {
|
|
4
|
+
disabled: boolean;
|
|
5
|
+
};
|
|
6
|
+
type BaseDialogCloseProps = ComponentProps<"button"> & {
|
|
7
|
+
render?: RenderProp<"button", DialogCloseState>;
|
|
8
|
+
/** Set to `false` when `render` doesn't render a `<button>`. */
|
|
9
|
+
nativeButton?: boolean;
|
|
10
|
+
};
|
|
11
|
+
/** A button that closes the dialog. Renders a `<button>`. */
|
|
12
|
+
declare const BaseDialogClose: (props: BaseDialogCloseProps) => import("react").JSX.Element;
|
|
13
|
+
export { BaseDialogClose as default };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Dialog } from "@base-ui/react/dialog";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
// src/components/dialog/BaseDialogClose.tsx
|
|
5
|
+
/** A button that closes the dialog. Renders a `<button>`. */
|
|
6
|
+
const BaseDialogClose = (props) => /* @__PURE__ */ jsx(Dialog.Close, { ...props });
|
|
7
|
+
export { BaseDialogClose as default };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RenderProp } from "../../utils/render.js";
|
|
2
|
+
import { ComponentProps } from "react";
|
|
3
|
+
type BaseDialogDescriptionProps = ComponentProps<"p"> & {
|
|
4
|
+
render?: RenderProp<"p">;
|
|
5
|
+
};
|
|
6
|
+
/** A paragraph that describes the dialog to assistive technology. Renders a `<p>`. */
|
|
7
|
+
declare const BaseDialogDescription: (props: BaseDialogDescriptionProps) => import("react").JSX.Element;
|
|
8
|
+
export { BaseDialogDescription as default };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Dialog } from "@base-ui/react/dialog";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
// src/components/dialog/BaseDialogDescription.tsx
|
|
5
|
+
/** A paragraph that describes the dialog to assistive technology. Renders a `<p>`. */
|
|
6
|
+
const BaseDialogDescription = (props) => /* @__PURE__ */ jsx(Dialog.Description, { ...props });
|
|
7
|
+
export { BaseDialogDescription as default };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ComponentProps, RefObject } from "react";
|
|
2
|
+
import { RenderProp } from "../../utils/render.js";
|
|
3
|
+
export type DialogPopupState = {
|
|
4
|
+
open: boolean;
|
|
5
|
+
/** Whether the dialog is inside another dialog. */
|
|
6
|
+
nested: boolean;
|
|
7
|
+
/** Whether a dialog inside this one is open. */
|
|
8
|
+
nestedDialogOpen: boolean;
|
|
9
|
+
};
|
|
10
|
+
export type DialogBackdropState = {
|
|
11
|
+
open: boolean;
|
|
12
|
+
};
|
|
13
|
+
/** How the dialog was opened or closed. Empty when it's unknown (e.g. opened from code). */
|
|
14
|
+
export type DialogInteractionType = "mouse" | "touch" | "pen" | "keyboard" | "";
|
|
15
|
+
/**
|
|
16
|
+
* - `true`: the default (the first tabbable element when opening, the trigger when closing).
|
|
17
|
+
* - `false`: don't move focus.
|
|
18
|
+
* - a ref: focus that element.
|
|
19
|
+
* - a function: return an element, `true` / `null` for the default, or `false` / `undefined` to not move focus.
|
|
20
|
+
*/
|
|
21
|
+
export type DialogFocusTarget = boolean | RefObject<HTMLElement | null> | ((interactionType: DialogInteractionType) => boolean | HTMLElement | null | void);
|
|
22
|
+
type BaseDialogPopupProps = ComponentProps<"div"> & {
|
|
23
|
+
/** The popup's width: `xs` 288px, `sm` 320px, `md` 384px, `lg` 512px. Never wider than the viewport. */
|
|
24
|
+
size?: "lg" | "md" | "sm" | "xs";
|
|
25
|
+
render?: RenderProp<"div", DialogPopupState>;
|
|
26
|
+
/** The element focused when the dialog opens. */
|
|
27
|
+
initialFocus?: DialogFocusTarget;
|
|
28
|
+
/** The element focused when the dialog closes. */
|
|
29
|
+
finalFocus?: DialogFocusTarget;
|
|
30
|
+
/** Where the popup is portaled to. Defaults to `<body>`. */
|
|
31
|
+
container?: HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | null;
|
|
32
|
+
/** Keeps the popup in the DOM while the dialog is closed. */
|
|
33
|
+
keepMounted?: boolean;
|
|
34
|
+
/** Set to `false` to render no backdrop. Nested dialogs render none either way. */
|
|
35
|
+
backdrop?: boolean;
|
|
36
|
+
/** A `className` on the element replaces the kit's conflicting classes (e.g. `opacity-50`). */
|
|
37
|
+
renderBackdrop?: RenderProp<"div", DialogBackdropState>;
|
|
38
|
+
};
|
|
39
|
+
/** The dialog's window, with its portal and backdrop. Renders a `<div>`. */
|
|
40
|
+
declare const BaseDialogPopup: ({ size, container, keepMounted, backdrop, renderBackdrop, className, ...otherProps }: BaseDialogPopupProps) => import("react").JSX.Element;
|
|
41
|
+
export { BaseDialogPopup as default };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Dialog } from "@base-ui/react/dialog";
|
|
3
|
+
import { cn } from "cn";
|
|
4
|
+
import { cloneElement, isValidElement } from "react";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
// src/components/dialog/BaseDialogPopup.tsx
|
|
7
|
+
const sizes = {
|
|
8
|
+
xs: "w-72",
|
|
9
|
+
sm: "w-80",
|
|
10
|
+
md: "w-96",
|
|
11
|
+
lg: "w-lg"
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Base UI only joins the `className` of a `render` element with the part's own, so conflicting
|
|
15
|
+
* Tailwind classes would win by their order in the CSS. Moves it into `cn` instead, last so it wins (#14).
|
|
16
|
+
*/
|
|
17
|
+
function splitClassName(render) {
|
|
18
|
+
if (!isValidElement(render) || !render.props.className) return {
|
|
19
|
+
render,
|
|
20
|
+
className: void 0
|
|
21
|
+
};
|
|
22
|
+
return {
|
|
23
|
+
render: cloneElement(render, { className: void 0 }),
|
|
24
|
+
className: render.props.className
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** The dialog's window, with its portal and backdrop. Renders a `<div>`. */
|
|
28
|
+
const BaseDialogPopup = ({ size = "md", container, keepMounted, backdrop = true, renderBackdrop, className, ...otherProps }) => {
|
|
29
|
+
const backdropElement = splitClassName(renderBackdrop);
|
|
30
|
+
return /* @__PURE__ */ jsxs(Dialog.Portal, {
|
|
31
|
+
container,
|
|
32
|
+
keepMounted,
|
|
33
|
+
children: [backdrop && /* @__PURE__ */ jsx(Dialog.Backdrop, {
|
|
34
|
+
render: backdropElement.render,
|
|
35
|
+
className: cn("fixed", "inset-0", "min-h-dvh", "bg-black", "opacity-20", "transition-all", "duration-150", "data-ending-style:opacity-0", "data-starting-style:opacity-0", "dark:opacity-70", "supports-[-webkit-touch-callout:none]:absolute", backdropElement.className)
|
|
36
|
+
}), /* @__PURE__ */ jsx(Dialog.Popup, {
|
|
37
|
+
...otherProps,
|
|
38
|
+
className: cn("fixed", "top-[calc(50%+1.25rem*var(--nested-dialogs))]", "left-1/2", "-mt-8", sizes[size], "max-w-[calc(100vw-3rem)]", "-translate-x-1/2", "-translate-y-1/2", "scale-[calc(1-0.1*var(--nested-dialogs))]", "transition-all", "duration-150", "data-ending-style:scale-90", "data-ending-style:opacity-0", "data-nested-dialog-open:after:absolute", "data-nested-dialog-open:after:inset-0", "data-nested-dialog-open:after:rounded-[inherit]", "data-nested-dialog-open:after:bg-black/5", "data-starting-style:scale-90", "data-starting-style:opacity-0", className)
|
|
39
|
+
})]
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
export { BaseDialogPopup as default };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RenderProp } from "../../utils/render.js";
|
|
2
|
+
import { ComponentProps } from "react";
|
|
3
|
+
type BaseDialogTitleProps = ComponentProps<"h2"> & {
|
|
4
|
+
render?: RenderProp<"h2">;
|
|
5
|
+
};
|
|
6
|
+
/** The dialog's heading, which also names the dialog. Renders an `<h2>`. */
|
|
7
|
+
declare const BaseDialogTitle: ({ className, ...otherProps }: BaseDialogTitleProps) => import("react").JSX.Element;
|
|
8
|
+
export { BaseDialogTitle as default };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Dialog } from "@base-ui/react/dialog";
|
|
3
|
+
import { cn } from "cn";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
// src/components/dialog/BaseDialogTitle.tsx
|
|
6
|
+
/** The dialog's heading, which also names the dialog. Renders an `<h2>`. */
|
|
7
|
+
const BaseDialogTitle = ({ className, ...otherProps }) => /* @__PURE__ */ jsx(Dialog.Title, {
|
|
8
|
+
...otherProps,
|
|
9
|
+
className: cn("-mt-1.5", "mb-1", "text-xl", "font-semibold", className)
|
|
10
|
+
});
|
|
11
|
+
export { BaseDialogTitle as default };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { DialogHandle } from "./createDialogHandle.js";
|
|
2
|
+
import { RenderProp } from "../../utils/render.js";
|
|
3
|
+
import { ComponentProps } from "react";
|
|
4
|
+
export type DialogTriggerState = {
|
|
5
|
+
disabled: boolean;
|
|
6
|
+
/** Whether the dialog is open and was opened by this trigger. */
|
|
7
|
+
open: boolean;
|
|
8
|
+
};
|
|
9
|
+
export type BaseDialogTriggerProps<Payload = unknown> = ComponentProps<"button"> & {
|
|
10
|
+
render?: RenderProp<"button", DialogTriggerState>;
|
|
11
|
+
/** Connects a trigger placed outside its `BaseDialog`. Create one with `createDialogHandle()`. */
|
|
12
|
+
handle?: DialogHandle<Payload>;
|
|
13
|
+
/** Passed to the dialog's function children when this trigger opens it. */
|
|
14
|
+
payload?: Payload;
|
|
15
|
+
/** Set to `false` when `render` doesn't render a `<button>`. */
|
|
16
|
+
nativeButton?: boolean;
|
|
17
|
+
};
|
|
18
|
+
/** A button that opens the dialog. Renders a `<button>`, with `data-popup-open` while its dialog is open. */
|
|
19
|
+
declare const BaseDialogTrigger: <Payload>({ handle, ...otherProps }: BaseDialogTriggerProps<Payload>) => import("react").JSX.Element;
|
|
20
|
+
export { BaseDialogTrigger as default };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { Dialog } from "@base-ui/react/dialog";
|
|
3
|
+
import { toBaseUIHandle } from "./createDialogHandle.js";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
// src/components/dialog/BaseDialogTrigger.tsx
|
|
6
|
+
/** A button that opens the dialog. Renders a `<button>`, with `data-popup-open` while its dialog is open. */
|
|
7
|
+
const BaseDialogTrigger = ({ handle, ...otherProps }) => /* @__PURE__ */ jsx(Dialog.Trigger, {
|
|
8
|
+
...otherProps,
|
|
9
|
+
handle: handle && toBaseUIHandle(handle)
|
|
10
|
+
});
|
|
11
|
+
export { BaseDialogTrigger as default };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opens and closes a dialog from anywhere: from triggers outside the dialog and from code.
|
|
3
|
+
* Create one with `createDialogHandle()` and pass it to `BaseDialog` and its triggers.
|
|
4
|
+
*
|
|
5
|
+
* Calls are ignored while no `BaseDialog` with this handle is mounted.
|
|
6
|
+
* Call the methods from event handlers or effects, never during render.
|
|
7
|
+
*/
|
|
8
|
+
export type DialogHandle<Payload = unknown> = {
|
|
9
|
+
/** Opens the dialog. Give a trigger's `id` to open it as if that trigger was pressed. */
|
|
10
|
+
open: (triggerId?: string | null) => void;
|
|
11
|
+
/** Opens the dialog with a payload, which `BaseDialog`'s function children receive. */
|
|
12
|
+
openWithPayload: (payload: Payload) => void;
|
|
13
|
+
close: () => void;
|
|
14
|
+
/** Whether the dialog is open. `false` while no `BaseDialog` with this handle is mounted. */
|
|
15
|
+
readonly isOpen: boolean;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Returns the Base UI handle behind a kit handle. Internal: not exported to apps.
|
|
19
|
+
* Its return type doesn't name Base UI so the declaration files don't either. Callers cast it.
|
|
20
|
+
*/
|
|
21
|
+
export declare function toBaseUIHandle(handle: DialogHandle<never>): object | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a handle to open a dialog from triggers outside it, or from code.
|
|
24
|
+
* Pass the payload type to type the payload of triggers and `openWithPayload`.
|
|
25
|
+
*/
|
|
26
|
+
export default function createDialogHandle<Payload = unknown>(): DialogHandle<Payload>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Dialog } from "@base-ui/react/dialog";
|
|
2
|
+
// src/components/dialog/createDialogHandle.ts
|
|
3
|
+
const baseUIHandles = /* @__PURE__ */ new WeakMap();
|
|
4
|
+
/**
|
|
5
|
+
* Returns the Base UI handle behind a kit handle. Internal: not exported to apps.
|
|
6
|
+
* Its return type doesn't name Base UI so the declaration files don't either. Callers cast it.
|
|
7
|
+
*/
|
|
8
|
+
function toBaseUIHandle(handle) {
|
|
9
|
+
return baseUIHandles.get(handle);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Creates a handle to open a dialog from triggers outside it, or from code.
|
|
13
|
+
* Pass the payload type to type the payload of triggers and `openWithPayload`.
|
|
14
|
+
*/
|
|
15
|
+
function createDialogHandle() {
|
|
16
|
+
const baseUIHandle = Dialog.createHandle();
|
|
17
|
+
const handle = {
|
|
18
|
+
open: (triggerId = null) => baseUIHandle.open(triggerId),
|
|
19
|
+
openWithPayload: (payload) => baseUIHandle.openWithPayload(payload),
|
|
20
|
+
close: () => baseUIHandle.close(),
|
|
21
|
+
get isOpen() {
|
|
22
|
+
return baseUIHandle.isOpen;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
baseUIHandles.set(handle, baseUIHandle);
|
|
26
|
+
return handle;
|
|
27
|
+
}
|
|
28
|
+
export { createDialogHandle as default, toBaseUIHandle };
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
1
|
+
import _default from "./BaseDialog.js";
|
|
2
|
+
import createDialogHandle, { DialogHandle } from "./createDialogHandle.js";
|
|
3
|
+
import { DialogBackdropState, DialogFocusTarget, DialogInteractionType, DialogPopupState } from "./BaseDialogPopup.js";
|
|
4
|
+
import { DialogCloseState } from "./BaseDialogClose.js";
|
|
5
|
+
import { DialogTriggerState } from "./BaseDialogTrigger.js";
|
|
6
|
+
export { _default as BaseDialog, type DialogBackdropState, type DialogCloseState, type DialogFocusTarget, type DialogHandle, type DialogInteractionType, type DialogPopupState, type DialogTriggerState, createDialogHandle };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
export { AnimatedPopup, DialogBackdrop, DialogTitle };
|
|
1
|
+
import BaseDialog_default from "./BaseDialog.js";
|
|
2
|
+
import createDialogHandle from "./createDialogHandle.js";
|
|
3
|
+
export { BaseDialog_default as BaseDialog, createDialogHandle };
|
package/package.json
CHANGED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { RenderProp } from "../../utils/render.js";
|
|
2
|
-
import { ComponentProps } from "react";
|
|
3
|
-
type AnimatedPopupProps = ComponentProps<"div"> & {
|
|
4
|
-
render?: RenderProp<"div">;
|
|
5
|
-
};
|
|
6
|
-
declare const AnimatedPopup: ({ render, className, ...otherProps }: AnimatedPopupProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
7
|
-
export { AnimatedPopup as default };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { cn } from "cn";
|
|
2
|
-
import { mergeProps, useRender } from "../../utils/render.js";
|
|
3
|
-
// src/components/dialog/AnimatedPopup.tsx
|
|
4
|
-
const AnimatedPopup = ({ render, className, ...otherProps }) => useRender({
|
|
5
|
-
defaultTagName: "div",
|
|
6
|
-
render,
|
|
7
|
-
props: mergeProps({ className: cn("fixed", "top-[calc(50%+1.25rem*var(--nested-dialogs))]", "left-1/2", "-mt-8", "w-96", "max-w-[calc(100vw-3rem)]", "-translate-x-1/2", "-translate-y-1/2", "scale-[calc(1-0.1*var(--nested-dialogs))]", "transition-all", "duration-150", "data-[ending-style]:scale-90", "data-[ending-style]:opacity-0", "data-[nested-dialog-open]:after:absolute", "data-[nested-dialog-open]:after:inset-0", "data-[nested-dialog-open]:after:rounded-[inherit]", "data-[nested-dialog-open]:after:bg-black/5", "data-[starting-style]:scale-90", "data-[starting-style]:opacity-0", className) }, otherProps)
|
|
8
|
-
});
|
|
9
|
-
export { AnimatedPopup as default };
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { RenderProp } from "../../utils/render.js";
|
|
2
|
-
import { ComponentProps } from "react";
|
|
3
|
-
type DialogBackdropProps = ComponentProps<"div"> & {
|
|
4
|
-
render?: RenderProp<"div">;
|
|
5
|
-
};
|
|
6
|
-
declare const DialogBackdrop: ({ render, ...otherProps }: DialogBackdropProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
7
|
-
export { DialogBackdrop as default };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { cn } from "cn";
|
|
2
|
-
import { mergeProps, useRender } from "../../utils/render.js";
|
|
3
|
-
// src/components/dialog/DialogBackdrop.tsx
|
|
4
|
-
const DialogBackdrop = ({ render, ...otherProps }) => useRender({
|
|
5
|
-
defaultTagName: "div",
|
|
6
|
-
render,
|
|
7
|
-
props: mergeProps({ className: cn("fixed", "inset-0", "min-h-dvh", "bg-black", "opacity-20", "transition-all", "duration-150", "data-[ending-style]:opacity-0", "data-[starting-style]:opacity-0", "dark:opacity-70", "supports-[-webkit-touch-callout:none]:absolute") }, otherProps)
|
|
8
|
-
});
|
|
9
|
-
export { DialogBackdrop as default };
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { RenderProp } from "../../utils/render.js";
|
|
2
|
-
import { ComponentProps } from "react";
|
|
3
|
-
type DialogTitleProps = ComponentProps<"h2"> & {
|
|
4
|
-
render?: RenderProp<"h2">;
|
|
5
|
-
};
|
|
6
|
-
declare const DialogTitle: ({ render, ...otherProps }: DialogTitleProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
7
|
-
export { DialogTitle as default };
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { cn } from "cn";
|
|
2
|
-
import { mergeProps, useRender } from "../../utils/render.js";
|
|
3
|
-
// src/components/dialog/DialogTitle.tsx
|
|
4
|
-
const DialogTitle = ({ render, ...otherProps }) => useRender({
|
|
5
|
-
defaultTagName: "h2",
|
|
6
|
-
render,
|
|
7
|
-
props: mergeProps({ className: cn("-mt-1.5", "mb-1", "text-xl", "font-semibold") }, otherProps)
|
|
8
|
-
});
|
|
9
|
-
export { DialogTitle as default };
|