@omer-x/tailwind-ui-kit 1.0.0-alpha.4 → 1.0.0-alpha.40
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 +283 -0
- package/README.md +1440 -0
- package/dist/components/button/BaseButton.d.ts +40 -12
- package/dist/components/button/BaseButton.js +35 -51
- package/dist/components/button/BaseToggle.d.ts +35 -0
- package/dist/components/button/BaseToggle.js +23 -0
- package/dist/components/button/BaseToggleGroup.d.ts +26 -0
- package/dist/components/button/BaseToggleGroup.js +12 -0
- package/dist/components/button/buttonStyles.d.ts +3 -0
- package/dist/components/button/buttonStyles.js +29 -0
- package/dist/components/button/index.d.ts +4 -4
- package/dist/components/button/index.js +4 -4
- package/dist/components/dialog/BaseAlertDialog.d.ts +49 -0
- package/dist/components/dialog/BaseAlertDialog.js +27 -0
- package/dist/components/dialog/BaseAlertDialogTrigger.d.ts +16 -0
- package/dist/components/dialog/BaseAlertDialogTrigger.js +11 -0
- 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 +29 -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/createAlertDialogHandle.d.ts +14 -0
- package/dist/components/dialog/createAlertDialogHandle.js +11 -0
- package/dist/components/dialog/createDialogHandle.d.ts +36 -0
- package/dist/components/dialog/createDialogHandle.js +31 -0
- package/dist/components/dialog/index.d.ts +8 -5
- package/dist/components/dialog/index.js +5 -8
- package/dist/components/disclosure/BaseAccordion.d.ts +48 -0
- package/dist/components/disclosure/BaseAccordion.js +18 -0
- package/dist/components/disclosure/BaseAccordionItem.d.ts +20 -0
- package/dist/components/disclosure/BaseAccordionItem.js +10 -0
- package/dist/components/disclosure/BaseAccordionPanel.d.ts +20 -0
- package/dist/components/disclosure/BaseAccordionPanel.js +15 -0
- package/dist/components/disclosure/BaseAccordionTrigger.d.ts +22 -0
- package/dist/components/disclosure/BaseAccordionTrigger.js +23 -0
- package/dist/components/disclosure/BaseCollapsible.d.ts +33 -0
- package/dist/components/disclosure/BaseCollapsible.js +16 -0
- package/dist/components/disclosure/BaseCollapsiblePanel.d.ts +20 -0
- package/dist/components/disclosure/BaseCollapsiblePanel.js +15 -0
- package/dist/components/disclosure/BaseCollapsibleTrigger.d.ts +15 -0
- package/dist/components/disclosure/BaseCollapsibleTrigger.js +16 -0
- package/dist/components/disclosure/index.d.ts +6 -0
- package/dist/components/disclosure/index.js +3 -0
- package/dist/components/disclosure/panelTransition.d.ts +7 -0
- package/dist/components/disclosure/panelTransition.js +9 -0
- package/dist/components/display/BaseAvatar.d.ts +24 -0
- package/dist/components/display/BaseAvatar.js +35 -0
- package/dist/components/display/BaseAvatarFallback.d.ts +14 -0
- package/dist/components/display/BaseAvatarFallback.js +14 -0
- package/dist/components/display/BaseAvatarImage.d.ts +23 -0
- package/dist/components/display/BaseAvatarImage.js +14 -0
- package/dist/components/display/BaseMeter.d.ts +60 -0
- package/dist/components/display/BaseMeter.js +45 -0
- package/dist/components/display/BaseMeterIndicator.d.ts +9 -0
- package/dist/components/display/BaseMeterIndicator.js +12 -0
- package/dist/components/display/BaseMeterLabel.d.ts +9 -0
- package/dist/components/display/BaseMeterLabel.js +7 -0
- package/dist/components/display/BaseMeterTrack.d.ts +12 -0
- package/dist/components/display/BaseMeterTrack.js +12 -0
- package/dist/components/display/BaseMeterValue.d.ts +14 -0
- package/dist/components/display/BaseMeterValue.js +10 -0
- package/dist/components/display/barStyles.d.ts +5 -0
- package/dist/components/display/barStyles.js +20 -0
- package/dist/components/display/index.d.ts +4 -0
- package/dist/components/display/index.js +3 -0
- package/dist/components/elements/JustAnchor.d.ts +7 -0
- package/dist/components/elements/JustAnchor.js +11 -0
- package/dist/components/elements/JustButton.d.ts +7 -0
- package/dist/components/elements/JustButton.js +11 -0
- package/dist/components/elements/JustDiv.d.ts +7 -0
- package/dist/components/elements/JustDiv.js +11 -0
- package/dist/components/elements/JustInput.d.ts +7 -0
- package/dist/components/elements/JustInput.js +11 -0
- package/dist/components/elements/JustSpan.d.ts +7 -0
- package/dist/components/elements/JustSpan.js +11 -0
- package/dist/components/elements/index.d.ts +6 -0
- package/dist/components/elements/index.js +6 -0
- package/dist/components/form/BaseAutocomplete.d.ts +198 -0
- package/dist/components/form/BaseAutocomplete.js +49 -0
- package/dist/components/form/BaseAutocompleteClear.d.ts +18 -0
- package/dist/components/form/BaseAutocompleteClear.js +7 -0
- package/dist/components/form/BaseAutocompleteCollection.d.ts +8 -0
- package/dist/components/form/BaseAutocompleteCollection.js +7 -0
- package/dist/components/form/BaseAutocompleteEmpty.d.ts +11 -0
- package/dist/components/form/BaseAutocompleteEmpty.js +10 -0
- package/dist/components/form/BaseAutocompleteGroup.d.ts +10 -0
- package/dist/components/form/BaseAutocompleteGroup.js +7 -0
- package/dist/components/form/BaseAutocompleteGroupLabel.d.ts +8 -0
- package/dist/components/form/BaseAutocompleteGroupLabel.js +7 -0
- package/dist/components/form/BaseAutocompleteIcon.d.ts +8 -0
- package/dist/components/form/BaseAutocompleteIcon.js +7 -0
- package/dist/components/form/BaseAutocompleteInput.d.ts +12 -0
- package/dist/components/form/BaseAutocompleteInput.js +12 -0
- package/dist/components/form/BaseAutocompleteInputGroup.d.ts +9 -0
- package/dist/components/form/BaseAutocompleteInputGroup.js +11 -0
- package/dist/components/form/BaseAutocompleteItem.d.ts +20 -0
- package/dist/components/form/BaseAutocompleteItem.js +11 -0
- package/dist/components/form/BaseAutocompleteList.d.ts +14 -0
- package/dist/components/form/BaseAutocompleteList.js +11 -0
- package/dist/components/form/BaseAutocompletePopup.d.ts +111 -0
- package/dist/components/form/BaseAutocompletePopup.js +45 -0
- package/dist/components/form/BaseAutocompleteRow.d.ts +8 -0
- package/dist/components/form/BaseAutocompleteRow.js +7 -0
- package/dist/components/form/BaseAutocompleteSeparator.d.ts +13 -0
- package/dist/components/form/BaseAutocompleteSeparator.js +7 -0
- package/dist/components/form/BaseAutocompleteStatus.d.ts +11 -0
- package/dist/components/form/BaseAutocompleteStatus.js +10 -0
- package/dist/components/form/BaseAutocompleteTrigger.d.ts +11 -0
- package/dist/components/form/BaseAutocompleteTrigger.js +7 -0
- package/dist/components/form/BaseAutocompleteValue.d.ts +8 -0
- package/dist/components/form/BaseAutocompleteValue.js +7 -0
- package/dist/components/form/BaseCheckbox.d.ts +49 -0
- package/dist/components/form/BaseCheckbox.js +31 -0
- package/dist/components/form/BaseCheckboxGroup.d.ts +24 -0
- package/dist/components/form/BaseCheckboxGroup.js +20 -0
- package/dist/components/form/BaseFieldset.d.ts +21 -0
- package/dist/components/form/BaseFieldset.js +18 -0
- package/dist/components/form/BaseForm.d.ts +13 -0
- package/dist/components/form/BaseForm.js +78 -0
- package/dist/components/form/BaseFormField.d.ts +33 -0
- package/dist/components/form/BaseFormField.js +33 -0
- package/dist/components/form/BaseInput.d.ts +8 -0
- package/dist/components/form/BaseInput.js +12 -0
- package/dist/components/form/BaseRadio.d.ts +30 -0
- package/dist/components/form/BaseRadio.js +22 -0
- package/dist/components/form/BaseRadioGroup.d.ts +28 -0
- package/dist/components/form/BaseRadioGroup.js +21 -0
- package/dist/components/form/BaseSwitch.d.ts +35 -0
- package/dist/components/form/BaseSwitch.js +21 -0
- package/dist/components/form/ClientSideError.d.ts +9 -0
- package/dist/components/form/ClientSideError.js +39 -0
- package/dist/components/form/FieldRoot.d.ts +8 -0
- package/dist/components/form/FieldRoot.js +25 -0
- package/dist/components/form/FormContext.d.ts +28 -0
- package/dist/components/form/FormContext.js +12 -0
- package/dist/components/form/InsideFieldContext.d.ts +2 -0
- package/dist/components/form/InsideFieldContext.js +5 -0
- package/dist/components/form/ServerSideError.d.ts +8 -0
- package/dist/components/form/ServerSideError.js +15 -0
- package/dist/components/form/autocompleteTypes.d.ts +16 -0
- package/dist/components/form/autocompleteTypes.js +1 -0
- package/dist/components/form/index.d.ts +21 -0
- package/dist/components/form/index.js +13 -0
- package/dist/components/form/inputSizes.d.ts +11 -0
- package/dist/components/form/inputSizes.js +19 -0
- package/dist/components/form/useAutocompleteFilter.d.ts +19 -0
- package/dist/components/form/useAutocompleteFilter.js +12 -0
- package/dist/components/form/useFormMessages.d.ts +10 -0
- package/dist/components/form/useFormMessages.js +16 -0
- package/dist/components/layout/Container.d.ts +4 -10
- package/dist/components/layout/Container.js +9 -10
- package/dist/components/layout/index.d.ts +2 -3
- package/dist/components/layout/index.js +1 -4
- package/dist/components/menu/BaseContextMenu.d.ts +150 -0
- package/dist/components/menu/BaseContextMenu.js +38 -0
- package/dist/components/menu/BaseContextMenuTrigger.d.ts +15 -0
- package/dist/components/menu/BaseContextMenuTrigger.js +10 -0
- package/dist/components/menu/BaseMenu.d.ts +171 -0
- package/dist/components/menu/BaseMenu.js +62 -0
- package/dist/components/menu/BaseMenuArrow.d.ts +19 -0
- package/dist/components/menu/BaseMenuArrow.js +10 -0
- package/dist/components/menu/BaseMenuCheckboxItem.d.ts +37 -0
- package/dist/components/menu/BaseMenuCheckboxItem.js +25 -0
- package/dist/components/menu/BaseMenuClear.d.ts +16 -0
- package/dist/components/menu/BaseMenuClear.js +10 -0
- package/dist/components/menu/BaseMenuEmpty.d.ts +8 -0
- package/dist/components/menu/BaseMenuEmpty.js +7 -0
- package/dist/components/menu/BaseMenuGroup.d.ts +8 -0
- package/dist/components/menu/BaseMenuGroup.js +7 -0
- package/dist/components/menu/BaseMenuGroupLabel.d.ts +8 -0
- package/dist/components/menu/BaseMenuGroupLabel.js +11 -0
- package/dist/components/menu/BaseMenuInput.d.ts +15 -0
- package/dist/components/menu/BaseMenuInput.js +10 -0
- package/dist/components/menu/BaseMenuItem.d.ts +24 -0
- package/dist/components/menu/BaseMenuItem.js +12 -0
- package/dist/components/menu/BaseMenuLinkItem.d.ts +20 -0
- package/dist/components/menu/BaseMenuLinkItem.js +12 -0
- package/dist/components/menu/BaseMenuList.d.ts +11 -0
- package/dist/components/menu/BaseMenuList.js +14 -0
- package/dist/components/menu/BaseMenuPopup.d.ts +116 -0
- package/dist/components/menu/BaseMenuPopup.js +42 -0
- package/dist/components/menu/BaseMenuRadioGroup.d.ts +19 -0
- package/dist/components/menu/BaseMenuRadioGroup.js +10 -0
- package/dist/components/menu/BaseMenuRadioItem.d.ts +36 -0
- package/dist/components/menu/BaseMenuRadioItem.js +24 -0
- package/dist/components/menu/BaseMenuSeparator.d.ts +11 -0
- package/dist/components/menu/BaseMenuSeparator.js +11 -0
- package/dist/components/menu/BaseMenuSubmenu.d.ts +13 -0
- package/dist/components/menu/BaseMenuSubmenu.js +22 -0
- package/dist/components/menu/BaseMenuSubmenuTrigger.d.ts +43 -0
- package/dist/components/menu/BaseMenuSubmenuTrigger.js +20 -0
- package/dist/components/menu/BaseMenuTrigger.d.ts +26 -0
- package/dist/components/menu/BaseMenuTrigger.js +11 -0
- package/dist/components/menu/BaseMenuViewport.d.ts +18 -0
- package/dist/components/menu/BaseMenuViewport.js +11 -0
- package/dist/components/menu/BaseMenubar.d.ts +187 -0
- package/dist/components/menu/BaseMenubar.js +18 -0
- package/dist/components/menu/createMenuHandle.d.ts +30 -0
- package/dist/components/menu/createMenuHandle.js +27 -0
- package/dist/components/menu/index.d.ts +20 -0
- package/dist/components/menu/index.js +5 -0
- package/dist/components/menu/menuItemStyles.d.ts +6 -0
- package/dist/components/menu/menuItemStyles.js +27 -0
- package/dist/components/menu/menuTypes.d.ts +69 -0
- package/dist/components/menu/menuTypes.js +16 -0
- package/dist/components/page/Pagination.d.ts +27 -0
- package/dist/components/page/Pagination.js +68 -0
- package/dist/components/page/index.d.ts +3 -0
- package/dist/components/page/index.js +3 -0
- package/dist/components/page/usePagination.d.ts +30 -0
- package/dist/components/page/usePagination.js +41 -0
- package/dist/components/table/TableCell.d.ts +13 -0
- package/dist/components/table/TableCell.js +17 -0
- package/dist/components/table/Tdx.d.ts +6 -10
- package/dist/components/table/Tdx.js +11 -9
- package/dist/components/table/index.d.ts +3 -3
- package/dist/components/table/index.js +3 -4
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/useRipples.d.ts +2 -5
- package/dist/hooks/useRipples.js +44 -32
- package/dist/utils/cn.d.ts +6 -5
- package/dist/utils/cn.js +8 -6
- package/dist/utils/dirty-form.d.ts +7 -0
- package/dist/utils/dirty-form.js +40 -0
- package/dist/utils/index.d.ts +3 -2
- package/dist/utils/index.js +2 -3
- package/dist/utils/render.d.ts +31 -0
- package/dist/utils/render.js +24 -0
- package/dist/utils/split-class-name.d.ts +15 -0
- package/dist/utils/split-class-name.js +18 -0
- package/package.json +64 -26
- package/typescript-badge.d.ts +4 -0
- package/dist/components/button/PulseButton.d.ts +0 -10
- package/dist/components/button/PulseButton.js +0 -24
- package/dist/components/dialog/AnimatedPopup.d.ts +0 -6
- package/dist/components/dialog/AnimatedPopup.js +0 -34
- package/dist/components/dialog/DialogBackdrop.d.ts +0 -6
- package/dist/components/dialog/DialogBackdrop.js +0 -26
- package/dist/components/dialog/DialogTitle.d.ts +0 -6
- package/dist/components/dialog/DialogTitle.js +0 -19
- package/dist/styles/ripple.module.css +0 -10
- package/dist/styles/ripple.module.d.ts +0 -2
package/README.md
ADDED
|
@@ -0,0 +1,1440 @@
|
|
|
1
|
+
# @omer-x/tailwind-ui-kit
|
|
2
|
+
|
|
3
|
+
Colorless React components built on [Base UI](https://base-ui.com) and [Tailwind CSS v4](https://tailwindcss.com). They handle structure and behavior: sizing, spacing, focus, transitions, loading states and form wiring. **Your app decides how they look.**
|
|
4
|
+
|
|
5
|
+
> **Status:** under active development. The API may change between versions.
|
|
6
|
+
|
|
7
|
+
## Principles
|
|
8
|
+
|
|
9
|
+
Read these before using or changing any component.
|
|
10
|
+
|
|
11
|
+
1. **Colorless bases.** Components ship without colors, background colors, border colors or any theme. You give them your look by wrapping them once in your app and using the wrapper everywhere.
|
|
12
|
+
2. **Everything is styleable.** Each component accepts `className`, and sub-parts are exposed through `render*` props (`renderLabel`, `renderErrors`, `renderThumb`, …). A `render` prop accepts an element, or a function that gets the props to spread and the component's state: `(props, state) => <a {...props} />` (the kit's [`RenderProp`](#renderprop) type).
|
|
13
|
+
3. **Style states with data attributes.** Components set attributes such as `data-disabled`, `data-checked` and `data-starting-style`. Target them with Tailwind variants (`data-disabled:opacity-50`). They are part of the kit's API and stay the same if Base UI is replaced.
|
|
14
|
+
4. **Naming.** `Base*` components are meant to be wrapped and themed. `Just*` components are bare elements that only add a `render` prop. Unprefixed components are layout and composition helpers.
|
|
15
|
+
5. **Client components.** All components are client components.
|
|
16
|
+
6. **Short for the common case, without losing power.** Each component is designed on its own: parts that always go together are combined, and Base UI's full capabilities stay available through props or optional parts.
|
|
17
|
+
7. **Never import Base UI in apps.** Base UI is an implementation detail and may be replaced someday. Apps use the kit's components only, and the kit's types never name Base UI types. To build your own component with a `render` prop, use the kit's [`useRender`](#userender) and [`mergeProps`](#mergeprops). If something you need isn't covered yet, add it to the kit instead of importing `@base-ui/react` (progress is tracked in #56).
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
> Upgrading from `1.0.0-alpha.18`? See [`MIGRATION.md`](./MIGRATION.md) (also in `node_modules/@omer-x/tailwind-ui-kit/MIGRATION.md`).
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
npm install @omer-x/tailwind-ui-kit
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Peer dependencies:
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
npm install react@19 react-dom@19 tailwindcss@4 @base-ui/react@1 zod@4
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`BaseForm` needs [`@omer-x/next-form-actions`](https://github.com/omermecitoglu/next-form-actions) v2 for its action and result types, and `BaseFormField` needs [`zod`](https://zod.dev) v4 for its `schema` prop.
|
|
34
|
+
|
|
35
|
+
## Setup
|
|
36
|
+
|
|
37
|
+
Tailwind v4 does not scan `node_modules`, so the classes used inside the components won't be generated unless you add an `@source` line to your global CSS. The path is relative to the CSS file:
|
|
38
|
+
|
|
39
|
+
```css
|
|
40
|
+
/* src/app/globals.css */
|
|
41
|
+
@import "tailwindcss";
|
|
42
|
+
@source "../../node_modules/@omer-x/tailwind-ui-kit/dist";
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
That's the only setup. The kit ships no CSS files: everything, including the ripple animation, is Tailwind utilities.
|
|
46
|
+
|
|
47
|
+
Some components use the `dark:` variant (e.g. the backdrop of `BaseDialog.Popup`). They follow whatever `dark` variant your app defines.
|
|
48
|
+
|
|
49
|
+
## Theming: wrap once, use everywhere
|
|
50
|
+
|
|
51
|
+
Create your themed versions in your app and import those instead of the base components:
|
|
52
|
+
|
|
53
|
+
```tsx
|
|
54
|
+
// src/components/Button.tsx
|
|
55
|
+
"use client";
|
|
56
|
+
import { BaseButton } from "@omer-x/tailwind-ui-kit/button";
|
|
57
|
+
import { cn } from "cn";
|
|
58
|
+
import Spinner from "./Spinner";
|
|
59
|
+
import type { ComponentProps } from "react";
|
|
60
|
+
|
|
61
|
+
type ButtonProps = ComponentProps<typeof BaseButton> & {
|
|
62
|
+
variant?: "primary" | "secondary",
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const Button = ({ variant = "primary", className, ...props }: ButtonProps) => (
|
|
66
|
+
<BaseButton
|
|
67
|
+
{...props}
|
|
68
|
+
spinner={<Spinner />} // your app's icon; the kit bundles no icons
|
|
69
|
+
className={cn({
|
|
70
|
+
"bg-blue-600 text-white hover:bg-blue-700 focus-visible:outline-blue-600": variant === "primary",
|
|
71
|
+
"bg-neutral-200 text-neutral-900 hover:bg-neutral-300 focus-visible:outline-neutral-500": variant === "secondary",
|
|
72
|
+
}, "data-disabled:opacity-50", className)}
|
|
73
|
+
/>
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
export default Button;
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Components
|
|
80
|
+
|
|
81
|
+
There is no root entry point. Import from the subpaths below.
|
|
82
|
+
|
|
83
|
+
### `@omer-x/tailwind-ui-kit/button`
|
|
84
|
+
|
|
85
|
+
#### `BaseButton`
|
|
86
|
+
|
|
87
|
+
A Base UI [`Button`](https://base-ui.com/react/components/button) with sizes, a pending state and a ripple effect on click.
|
|
88
|
+
|
|
89
|
+
| Prop | Type | Default | Description |
|
|
90
|
+
|------|------|---------|-------------|
|
|
91
|
+
| `size` | `"lg" \| "md" \| "sm" \| "xs"` | `"md"` | Height, padding, font size and gap |
|
|
92
|
+
| `pending` | `boolean` | `false` | Shows the spinner, blocks clicks and keeps the button focusable |
|
|
93
|
+
| `spinner` | `ReactNode` | | Shown while pending. Without it nothing is shown, and `data-pending` still marks the state |
|
|
94
|
+
| `iconOnly` | `boolean` | `false` | A square button for a single icon. Requires `aria-label`. While pending, the spinner replaces the icon |
|
|
95
|
+
| `render` | `RenderProp<"button", ButtonState>` | | Render another element, e.g. your app's link. The state is `{ disabled }` |
|
|
96
|
+
| `nativeButton` | `boolean` | `true` | Set to `false` when `render` doesn't render a `<button>` |
|
|
97
|
+
| `focusableWhenDisabled` | `boolean` | `false` | Keeps a disabled button in the tab order |
|
|
98
|
+
| …rest | `<button>` props | | `disabled`, `type`, `onClick`, … |
|
|
99
|
+
|
|
100
|
+
- **Pending** means the `pending` prop is set, **or** the button has `type="submit"` and its form is submitting (a `BaseForm` or a form with a native action). Either way the button shows `spinner`, ignores clicks, keeps keyboard focus, and sets `aria-busy="true"` and `data-pending`.
|
|
101
|
+
- Children are laid out with flex and a size-based gap, so icons and text line up without extra markup.
|
|
102
|
+
- The ripple uses `bg-current`, so it follows the button's text color. It is skipped when `onClick` calls `preventDefault()`, and hidden when the user prefers reduced motion.
|
|
103
|
+
- The kit bundles no icons. Pass your own `spinner` (and icons as children), usually once in your themed wrapper.
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
<Button size="sm" pending={isSaving}>Save</Button>
|
|
107
|
+
<Button iconOnly aria-label="Delete"><TrashIcon /></Button>
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Links that look like buttons:** render your app's own link (so typed routes keep working) and set `nativeButton={false}`:
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
<Button render={<Link href="/products/new" />} nativeButton={false}>New product</Button>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
#### `BaseToggle`
|
|
117
|
+
|
|
118
|
+
A Base UI [`Toggle`](https://base-ui.com/react/components/toggle): a two-state button that is pressed or not. It shares `BaseButton`'s sizes and ripple effect.
|
|
119
|
+
|
|
120
|
+
| Prop | Type | Default | Description |
|
|
121
|
+
|------|------|---------|-------------|
|
|
122
|
+
| `pressed` | `boolean` | | Whether it's pressed, when controlled |
|
|
123
|
+
| `defaultPressed` | `boolean` | `false` | Whether it's pressed at first |
|
|
124
|
+
| `onPressedChange` | `(pressed: boolean, event: Event) => void` | | Called with the new state and the DOM event that changed it |
|
|
125
|
+
| `value` | `string` | | Identifies the toggle inside a `BaseToggleGroup` |
|
|
126
|
+
| `size` | `"lg" \| "md" \| "sm" \| "xs"` | `"md"` | Same heights, padding, font sizes and gaps as `BaseButton` |
|
|
127
|
+
| `iconOnly` | `boolean` | `false` | A square toggle for a single icon. Requires `aria-label` |
|
|
128
|
+
| `render` | `RenderProp<"button", ToggleState>` | | Render another element. The state is `{ pressed, disabled }` |
|
|
129
|
+
| `nativeButton` | `boolean` | `true` | Set to `false` when `render` doesn't render a `<button>` |
|
|
130
|
+
| …rest | `<button>` props | | `disabled`, `onClick`, `aria-label`, … (`type` is always `"button"`) |
|
|
131
|
+
|
|
132
|
+
It sets `aria-pressed`, plus `data-pressed` while pressed, `data-disabled` and `data-icon-only`. Style the pressed look with `data-pressed:`. The ripple is skipped when `onClick` calls `preventDefault()`.
|
|
133
|
+
|
|
134
|
+
```tsx
|
|
135
|
+
<BaseToggle
|
|
136
|
+
iconOnly
|
|
137
|
+
aria-label="Favorite"
|
|
138
|
+
className="border border-neutral-400 data-pressed:bg-neutral-700 data-pressed:text-white"
|
|
139
|
+
>
|
|
140
|
+
<StarIcon />
|
|
141
|
+
</BaseToggle>
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### `BaseToggleGroup`
|
|
145
|
+
|
|
146
|
+
A Base UI [`Toggle Group`](https://base-ui.com/react/components/toggle-group): shared state for the `BaseToggle`s inside it. Give every toggle a `value`. By default pressing one releases the others; arrow keys move focus between them.
|
|
147
|
+
|
|
148
|
+
| Prop | Type | Default | Description |
|
|
149
|
+
|------|------|---------|-------------|
|
|
150
|
+
| `value` | `readonly string[]` | | The `value`s of the pressed toggles, when controlled |
|
|
151
|
+
| `defaultValue` | `readonly string[]` | | The `value`s of the toggles pressed at first |
|
|
152
|
+
| `onValueChange` | `(value: string[], event: Event) => void` | | Called with the pressed `value`s and the DOM event that changed them |
|
|
153
|
+
| `multiple` | `boolean` | `false` | Lets several toggles be pressed at once |
|
|
154
|
+
| `disabled` | `boolean` | `false` | Disables every toggle in the group |
|
|
155
|
+
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | Arrow-key direction, and the layout direction |
|
|
156
|
+
| `loopFocus` | `boolean` | `true` | Moves focus from the last toggle back to the first with the arrow keys |
|
|
157
|
+
| `className` | `string` | | Classes for the group `<div>` (default `flex`, `flex-col` when vertical) |
|
|
158
|
+
| `render` | `RenderProp<"div", ToggleGroupState>` | | Render another element for the group |
|
|
159
|
+
| …rest | `<div>` props | | Name the group with `aria-label` or `aria-labelledby` |
|
|
160
|
+
|
|
161
|
+
Render functions get `ToggleGroupState`: `{ disabled, multiple, orientation }`. The group has `role="group"` and sets `data-orientation`, `data-disabled` and `data-multiple`. Each toggle keeps its own `data-pressed` / `data-disabled`.
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
<BaseToggleGroup aria-label="Text alignment" defaultValue={["left"]} className="gap-1">
|
|
165
|
+
<Toggle value="left">Left</Toggle>
|
|
166
|
+
<Toggle value="center">Center</Toggle>
|
|
167
|
+
<Toggle value="right">Right</Toggle>
|
|
168
|
+
</BaseToggleGroup>
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### `@omer-x/tailwind-ui-kit/form`
|
|
172
|
+
|
|
173
|
+
#### `BaseInput`
|
|
174
|
+
|
|
175
|
+
A plain `<input>` with sizes, rounded corners and a transparent outline ready for focus styles. It accepts all input props plus `render`.
|
|
176
|
+
|
|
177
|
+
| Prop | Type | Default |
|
|
178
|
+
|------|------|---------|
|
|
179
|
+
| `size` | `"lg" \| "md" \| "sm" \| "xs"` | `"md"` |
|
|
180
|
+
|
|
181
|
+
```tsx
|
|
182
|
+
<BaseInput name="email" className="border border-neutral-300 focus:outline-blue-500" />
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
#### `BaseSwitch`
|
|
186
|
+
|
|
187
|
+
A Base UI [`Switch`](https://base-ui.com/react/components/switch) with its label. It submits `"true"` / `"false"` by default, so its value always appears in form data.
|
|
188
|
+
|
|
189
|
+
| Prop | Type | Default | Description |
|
|
190
|
+
|------|------|---------|-------------|
|
|
191
|
+
| `label` | `ReactNode` | | Text next to the switch (required) |
|
|
192
|
+
| `labelClassName` | `string` | | Classes for the `<label>` around the switch and its text (default `flex items-center gap-2 text-sm font-normal`) |
|
|
193
|
+
| `renderThumb` | `RenderProp<"span", SwitchState>` | | Style the thumb |
|
|
194
|
+
| `value` | `string` | `"true"` | Submitted when checked |
|
|
195
|
+
| `uncheckedValue` | `string` | `"false"` | Submitted when unchecked |
|
|
196
|
+
| `onCheckedChange` | `(checked: boolean, event: Event) => void` | | Called with the new state and the DOM event that changed it |
|
|
197
|
+
| `render` | `RenderProp<"span", SwitchState>` | | Render another element for the track |
|
|
198
|
+
| …rest | | | `name`, `checked`, `defaultChecked`, `disabled`, `readOnly`, `required`, `form`, `inputRef`, `nativeButton`, and `<span>` props |
|
|
199
|
+
|
|
200
|
+
Render functions get `SwitchState`: `{ checked, disabled, readOnly, required }`.
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
<BaseSwitch
|
|
204
|
+
name="active"
|
|
205
|
+
label="Active"
|
|
206
|
+
className="rounded-full bg-neutral-300 data-checked:bg-blue-600"
|
|
207
|
+
renderThumb={<span className="rounded-full bg-white" />}
|
|
208
|
+
/>
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### `BaseCheckbox`
|
|
212
|
+
|
|
213
|
+
A Base UI [`Checkbox`](https://base-ui.com/react/components/checkbox) with its label. Like `BaseSwitch`, it submits `"true"` / `"false"` by default, so its value always appears in form data, also inside `BaseForm`.
|
|
214
|
+
|
|
215
|
+
| Prop | Type | Default | Description |
|
|
216
|
+
|------|------|---------|-------------|
|
|
217
|
+
| `label` | `ReactNode` | | Text next to the checkbox (required) |
|
|
218
|
+
| `labelClassName` | `string` | | Classes for the `<label>` around the checkbox and its text (default `flex items-center gap-2 text-sm font-normal`) |
|
|
219
|
+
| `icon` | `ReactNode` | | Shown inside the box when checked. The kit bundles none |
|
|
220
|
+
| `indeterminateIcon` | `ReactNode` | `icon` | Shown inside the box when indeterminate |
|
|
221
|
+
| `renderIndicator` | `RenderProp<"span", CheckboxState>` | | Style the indicator that holds the icon |
|
|
222
|
+
| `keepIndicatorMounted` | `boolean` | `false` | Keep the indicator in the DOM when unchecked (e.g. to animate it out) |
|
|
223
|
+
| `indeterminate` | `boolean` | `false` | Show the mixed state (`aria-checked="mixed"`). It doesn't change `checked` |
|
|
224
|
+
| `parent` | `boolean` | `false` | Make it the "select all" checkbox of a `BaseCheckboxGroup` with `allValues`. It checks or clears the whole group, is indeterminate when only some are checked, and isn't submitted |
|
|
225
|
+
| `value` | `string` | `"true"` | Submitted when checked |
|
|
226
|
+
| `uncheckedValue` | `string` | `"false"` | Submitted when unchecked |
|
|
227
|
+
| `onCheckedChange` | `(checked: boolean, event: Event) => void` | | Called with the new state and the DOM event that changed it |
|
|
228
|
+
| `render` | `RenderProp<"span", CheckboxState>` | | Render another element for the box |
|
|
229
|
+
| …rest | | | `name`, `checked`, `defaultChecked`, `disabled`, `readOnly`, `required`, `form`, `inputRef`, `nativeButton`, and `<span>` props |
|
|
230
|
+
|
|
231
|
+
Render functions get `CheckboxState`: `{ checked, disabled, readOnly, required, indeterminate }`. The box and the indicator set `data-checked` / `data-unchecked`, `data-indeterminate`, `data-disabled`, `data-readonly` and `data-required`. The indicator also sets `data-starting-style` / `data-ending-style` while it enters and leaves.
|
|
232
|
+
|
|
233
|
+
```tsx
|
|
234
|
+
<BaseCheckbox
|
|
235
|
+
name="terms"
|
|
236
|
+
label="I accept the terms"
|
|
237
|
+
className="rounded border border-neutral-400 data-checked:border-blue-600 data-checked:bg-blue-600"
|
|
238
|
+
renderIndicator={<span className="text-white" />}
|
|
239
|
+
icon={<CheckIcon />}
|
|
240
|
+
/>
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
#### `BaseCheckboxGroup`
|
|
244
|
+
|
|
245
|
+
A Base UI [`Checkbox Group`](https://base-ui.com/react/components/checkbox-group): shared state for the checkboxes inside it. Its `name` is passed to every checkbox in the group, and each checked box submits its own `value` under that name, so the server reads them all with `formData.getAll(name)`. Checkboxes in a group never submit an unchecked value.
|
|
246
|
+
|
|
247
|
+
| Prop | Type | Default | Description |
|
|
248
|
+
|------|------|---------|-------------|
|
|
249
|
+
| `name` | `string` | | Submitted name of every checkbox in the group |
|
|
250
|
+
| `value` | `string[]` | | The `value`s of the checked boxes, when controlled |
|
|
251
|
+
| `defaultValue` | `string[]` | | The `value`s of the boxes checked at first |
|
|
252
|
+
| `onValueChange` | `(value: string[], event: Event) => void` | | Called with the new checked `value`s and the DOM event that changed it |
|
|
253
|
+
| `allValues` | `string[]` | | Every child `value`. Needed by a `BaseCheckbox` with `parent` ("select all") |
|
|
254
|
+
| `disabled` | `boolean` | | Disables every checkbox in the group |
|
|
255
|
+
| `className` | `string` | | Classes for the group `<div>` (default `flex flex-col gap-2`) |
|
|
256
|
+
| `render` | `RenderProp<"div", CheckboxGroupState>` | | Render another element for the group |
|
|
257
|
+
| …rest | | | `<div>` props. Give the group an accessible name with `aria-label` or `aria-labelledby` |
|
|
258
|
+
|
|
259
|
+
Render functions get `CheckboxGroupState`: `{ disabled }`. The group sets `data-disabled` when disabled.
|
|
260
|
+
|
|
261
|
+
With `BaseForm`'s `onlyDirty`, give the group an array default (`defaultValues={{ channels: ["email"] }}`). It is sent only when the set of checked values changed, and as an empty value when everything was unchecked.
|
|
262
|
+
|
|
263
|
+
```tsx
|
|
264
|
+
<BaseCheckboxGroup name="channels" aria-label="Channels" defaultValue={["email"]}>
|
|
265
|
+
<BaseCheckbox value="email" label="Email" />
|
|
266
|
+
<BaseCheckbox value="sms" label="SMS" />
|
|
267
|
+
</BaseCheckboxGroup>
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
#### `BaseRadioGroup`
|
|
271
|
+
|
|
272
|
+
A Base UI [`Radio Group`](https://base-ui.com/react/components/radio-group): shared state for the `BaseRadio`s inside it. Only one radio is selected at a time, and its `value` is submitted under the group's `name`. Arrow keys move the selection.
|
|
273
|
+
|
|
274
|
+
| Prop | Type | Default | Description |
|
|
275
|
+
|------|------|---------|-------------|
|
|
276
|
+
| `name` | `string` | | Submitted name of the group |
|
|
277
|
+
| `value` | `string` | | The `value` of the selected radio, when controlled |
|
|
278
|
+
| `defaultValue` | `string` | | The `value` of the radio selected at first |
|
|
279
|
+
| `onValueChange` | `(value: string, event: Event) => void` | | Called with the selected `value` and the DOM event that changed it |
|
|
280
|
+
| `disabled` | `boolean` | | Disables every radio in the group |
|
|
281
|
+
| `readOnly` | `boolean` | | Keeps the selection from changing |
|
|
282
|
+
| `required` | `boolean` | | A radio must be selected before the form submits |
|
|
283
|
+
| `form` | `string` | | The `id` of a `<form>` to submit with, when the group isn't inside it |
|
|
284
|
+
| `inputRef` | `Ref<HTMLInputElement>` | | The hidden `<input>` of the selected (or first enabled) radio |
|
|
285
|
+
| `className` | `string` | | Classes for the group `<div>` (default `flex flex-col gap-2`) |
|
|
286
|
+
| `render` | `RenderProp<"div", RadioGroupState>` | | Render another element for the group |
|
|
287
|
+
| …rest | | | `<div>` props. Give the group an accessible name with `aria-label` or `aria-labelledby` (inside `BaseFormField`, its `label` names the group) |
|
|
288
|
+
|
|
289
|
+
Render functions get `RadioGroupState`: `{ disabled, readOnly, required }`. The group sets `data-disabled`, `data-readonly` and `data-required`.
|
|
290
|
+
|
|
291
|
+
Inside `BaseForm`, use it alone with a `name`, or in `BaseFormField`'s `children` to get the field's name, label, `required` from the schema and server errors. With `onlyDirty`, give it a string default (`defaultValues={{ plan: "free" }}`). It is sent only when the selection differs from it.
|
|
292
|
+
|
|
293
|
+
```tsx
|
|
294
|
+
<BaseFormField name="plan" label="Plan" schema={Schema.shape.plan}>
|
|
295
|
+
{constraints => (
|
|
296
|
+
<BaseRadioGroup required={constraints.required} defaultValue="free">
|
|
297
|
+
<BaseRadio value="free" label="Free" />
|
|
298
|
+
<BaseRadio value="pro" label="Pro" />
|
|
299
|
+
</BaseRadioGroup>
|
|
300
|
+
)}
|
|
301
|
+
</BaseFormField>
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
#### `BaseRadio`
|
|
305
|
+
|
|
306
|
+
A Base UI [`Radio`](https://base-ui.com/react/components/radio-group) with its label, for use inside `BaseRadioGroup`.
|
|
307
|
+
|
|
308
|
+
| Prop | Type | Default | Description |
|
|
309
|
+
|------|------|---------|-------------|
|
|
310
|
+
| `label` | `ReactNode` | | Text next to the radio (required) |
|
|
311
|
+
| `value` | `string` | | Submitted under the group's `name` when selected (required) |
|
|
312
|
+
| `labelClassName` | `string` | | Classes for the `<label>` around the radio and its text (default `flex items-center gap-2 text-sm font-normal`) |
|
|
313
|
+
| `icon` | `ReactNode` | | Shown inside the radio when selected, e.g. a dot (`<span className="size-1.5 rounded-full bg-white" />`). The kit bundles none |
|
|
314
|
+
| `renderIndicator` | `RenderProp<"span", RadioState>` | | Style the indicator that holds the icon |
|
|
315
|
+
| `keepIndicatorMounted` | `boolean` | `false` | Keep the indicator in the DOM when not selected (e.g. to animate it out) |
|
|
316
|
+
| `className` | `string` | | Classes for the radio (default is a `size-4` `rounded-full` box with a focus ring offset) |
|
|
317
|
+
| `render` | `RenderProp<"span", RadioState>` | | Render another element for the radio |
|
|
318
|
+
| …rest | | | `disabled`, `readOnly`, `required`, `inputRef`, `nativeButton`, and `<span>` props |
|
|
319
|
+
|
|
320
|
+
Render functions get `RadioState`: `{ checked, disabled, readOnly, required }`. The radio and the indicator set `data-checked` / `data-unchecked`, `data-disabled`, `data-readonly` and `data-required`. The indicator also sets `data-starting-style` / `data-ending-style` while it enters and leaves. The `<label>` sets `data-disabled` when the radio itself is `disabled`.
|
|
321
|
+
|
|
322
|
+
```tsx
|
|
323
|
+
<BaseRadio
|
|
324
|
+
value="pro"
|
|
325
|
+
label="Pro"
|
|
326
|
+
className="border border-neutral-400 data-checked:border-blue-600 data-checked:bg-blue-600"
|
|
327
|
+
icon={<span className="size-1.5 rounded-full bg-white" />}
|
|
328
|
+
/>
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
#### `BaseFieldset`
|
|
332
|
+
|
|
333
|
+
A Base UI [`Fieldset`](https://base-ui.com/react/components/fieldset): a native `<fieldset>` that groups related controls under a stylable legend. The legend is linked to the fieldset (`aria-labelledby`), so screen readers announce it as the group's name.
|
|
334
|
+
|
|
335
|
+
| Prop | Type | Default | Description |
|
|
336
|
+
|------|------|---------|-------------|
|
|
337
|
+
| `legend` | `ReactNode` | | The caption, rendered before the children. Without it, no legend is rendered |
|
|
338
|
+
| `legendClassName` | `string` | | Classes for the legend `<div>` (default `font-semibold`) |
|
|
339
|
+
| `renderLegend` | `RenderProp<"div", FieldsetState>` | | Render another element for the legend (e.g. a heading) |
|
|
340
|
+
| `disabled` | `boolean` | | Disables every control inside, `BaseFormField`s included |
|
|
341
|
+
| `className` | `string` | | Classes for the `<fieldset>` (default `flex min-w-0 flex-col gap-3`) |
|
|
342
|
+
| `render` | `RenderProp<"fieldset", FieldsetState>` | | Render another element for the fieldset |
|
|
343
|
+
| …rest | | | `<fieldset>` props |
|
|
344
|
+
|
|
345
|
+
Render functions get `FieldsetState`: `{ disabled }`. The fieldset and the legend set `data-disabled` when disabled.
|
|
346
|
+
|
|
347
|
+
```tsx
|
|
348
|
+
<BaseFieldset legend="Billing address" className="rounded border p-4" legendClassName="border-b pb-2">
|
|
349
|
+
<BaseFormField name="street" label="Street" schema={Schema.shape.street} />
|
|
350
|
+
<BaseFormField name="city" label="City" schema={Schema.shape.city} />
|
|
351
|
+
</BaseFieldset>
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
#### `BaseForm`
|
|
355
|
+
|
|
356
|
+
A Base UI [`Form`](https://base-ui.com/react/components/form) connected to an action through React's `useActionState`. Actions are made with [`@omer-x/next-form-actions`](https://github.com/omermecitoglu/next-form-actions) (v2), which defines the result shape. Install it next to the kit:
|
|
357
|
+
|
|
358
|
+
```sh
|
|
359
|
+
npm install @omer-x/next-form-actions@^2.0.0-alpha.1
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
| Prop | Type | Description |
|
|
363
|
+
|------|------|-------------|
|
|
364
|
+
| `action` | `FormAction<Data>` | The action, e.g. `saveProduct` or `completeSale.bind(null, saleId)`. `Data` is what it returns on success |
|
|
365
|
+
| `formatMessage` | `(code: string, params?: Record<string, unknown>) => string` | Turns error codes and message texts into display text (see below). Without it, codes are shown as they are |
|
|
366
|
+
| `onResult` | `(result: FormActionState<Data>) => void` | Called once per completed submission, when the action returns. Never on mount |
|
|
367
|
+
| `resetOnSuccess` | `boolean` | After a successful result, reset every field inside the form to its default (see below) |
|
|
368
|
+
| `defaultValues` | `Record<string, unknown>` | Initial values, used by `onlyDirty` |
|
|
369
|
+
| `onlyDirty` | `boolean` | Submit only the fields whose value differs from `defaultValues`. Fields are compared by name, so multi-selects and checkbox groups take an array default. A changed field with no value (an unchecked checkbox, a cleared multi-select) is sent as an empty value |
|
|
370
|
+
| …rest | `<form>` props | Except `action` |
|
|
371
|
+
|
|
372
|
+
**How the result is used.** The action resolves to `{ status: "idle" }`, `{ status: "success", data }` or `{ status: "failure", fieldErrors, message? }`:
|
|
373
|
+
|
|
374
|
+
- `fieldErrors[name]` are shown under the `BaseFormField` with that `name`. A field can have several errors.
|
|
375
|
+
- `message` (`{ type: "error" | "warning", text }`) is a **form-level message**.
|
|
376
|
+
- Field errors with no matching field in the form (a schema field you didn't render, or an object-level refine without a `path`, under the `""` key) also become form-level messages, so they are never silently lost.
|
|
377
|
+
- `BaseForm` renders no form-level messages itself. Show them wherever you like with `useFormMessages()`.
|
|
378
|
+
|
|
379
|
+
The form never resets after a submission, so after a failure the user keeps what they typed. That includes controlled checkboxes and selects.
|
|
380
|
+
|
|
381
|
+
**`formatMessage`** receives every text the form shows:
|
|
382
|
+
|
|
383
|
+
| What | `code` | `params` |
|
|
384
|
+
|------|--------|----------|
|
|
385
|
+
| Server field error | Zod issue code or your custom code: `required`, `too_small`, `invalid_email`, `taken`, … | From the error, e.g. `{ minimum: 3, origin: "string" }` |
|
|
386
|
+
| Browser validity state | `valueMissing`, `typeMismatch`, `patternMismatch`, `tooShort`, `tooLong`, `rangeUnderflow`, `rangeOverflow`, `stepMismatch`, `badInput` | `{ minimum }` for `tooShort` / `rangeUnderflow`, `{ maximum }` for `tooLong` / `rangeOverflow` (taken from the field's schema) |
|
|
387
|
+
| Form message | The message's `text`, e.g. `SALE_ALREADY_COMPLETED` | none |
|
|
388
|
+
|
|
389
|
+
It is a function, so it can't be passed from a server component. Set it once in your themed form wrapper, and every form in the app gets translated messages:
|
|
390
|
+
|
|
391
|
+
```tsx
|
|
392
|
+
// src/components/Form.tsx
|
|
393
|
+
"use client";
|
|
394
|
+
import { BaseForm } from "@omer-x/tailwind-ui-kit/form";
|
|
395
|
+
import { useTranslations } from "next-intl";
|
|
396
|
+
import type { ComponentProps } from "react";
|
|
397
|
+
|
|
398
|
+
const Form = <Data,>(props: ComponentProps<typeof BaseForm<Data>>) => {
|
|
399
|
+
const t = useTranslations("Form");
|
|
400
|
+
return <BaseForm {...props} formatMessage={(code, params) => t(code, params as Record<string, string>)} />;
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
export default Form;
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
```json
|
|
407
|
+
{
|
|
408
|
+
"Form": {
|
|
409
|
+
"required": "This field is required",
|
|
410
|
+
"valueMissing": "This field is required",
|
|
411
|
+
"too_small": "{origin, select, string {At least {minimum, plural, one {# character} other {# characters}}} other {At least {minimum}}}",
|
|
412
|
+
"tooShort": "At least {minimum, plural, one {# character} other {# characters}}",
|
|
413
|
+
"SALE_ALREADY_COMPLETED": "This sale is already completed"
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
**`resetOnSuccess`** remounts the form's children after a successful result. Uncontrolled inputs go back to their `defaultValue`, and components inside the form lose their state, including controlled ones. State kept *outside* the form (e.g. in the page that renders it) is not reset.
|
|
419
|
+
|
|
420
|
+
```tsx
|
|
421
|
+
// actions.ts
|
|
422
|
+
"use server";
|
|
423
|
+
export const saveProfile = defineFormAction({
|
|
424
|
+
schema: z.object({ name: z.string().min(3) }),
|
|
425
|
+
action: input => async () => {
|
|
426
|
+
if (await isDown()) displayMessage("error", "SERVER_DOWN");
|
|
427
|
+
return await save(input); // becomes state.data
|
|
428
|
+
},
|
|
429
|
+
});
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
```tsx
|
|
433
|
+
<Form action={saveProfile} onResult={result => result.status === "success" && dialog.close()} className="flex flex-col gap-4">
|
|
434
|
+
<FormAlerts />
|
|
435
|
+
<FormField name="name" label="Name" schema={z.string().min(3)} />
|
|
436
|
+
<Button type="submit">Save</Button>
|
|
437
|
+
</Form>
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
Forms can be nested. A submit inside an inner form does not submit the outer one.
|
|
441
|
+
|
|
442
|
+
#### `useFormMessages`
|
|
443
|
+
|
|
444
|
+
Returns the form-level messages of the latest result of the surrounding `BaseForm`, already passed through `formatMessage`. Call it anywhere inside the form.
|
|
445
|
+
|
|
446
|
+
```ts
|
|
447
|
+
const { messages, submissionId } = useFormMessages();
|
|
448
|
+
// messages: { key: string, type: "error" | "warning", text: string }[]
|
|
449
|
+
// submissionId: changes once per completed submission
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
```tsx
|
|
453
|
+
// Inline alerts, placed anywhere in the form
|
|
454
|
+
const FormAlerts = () => {
|
|
455
|
+
const { messages } = useFormMessages();
|
|
456
|
+
return messages.map(m => <Alert key={m.key} type={m.type}>{m.text}</Alert>);
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
// Toasts: fire once per submission, not on every re-render
|
|
460
|
+
const FormToasts = () => {
|
|
461
|
+
const { messages, submissionId } = useFormMessages();
|
|
462
|
+
useEffect(() => {
|
|
463
|
+
messages.forEach(m => toast(m.type, m.text));
|
|
464
|
+
}, [submissionId]);
|
|
465
|
+
return null;
|
|
466
|
+
};
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
#### `BaseFormField`
|
|
470
|
+
|
|
471
|
+
A Base UI [`Field`](https://base-ui.com/react/components/field) made of a label, a control and error messages. It reads native validation constraints (`required`, `minLength`, `max`, …) from a Zod schema, and the form checks them in the browser before the action runs: an invalid field blocks the submit and shows its error (`valueMissing`, `tooShort`, …) through `formatMessage`.
|
|
472
|
+
|
|
473
|
+
| Prop | Type | Description |
|
|
474
|
+
|------|------|-------------|
|
|
475
|
+
| `name` | `string` | Field name. It also links the server errors in `fieldErrors[name]` |
|
|
476
|
+
| `schema` | `ZodType` | Source of the HTML constraints applied to the control |
|
|
477
|
+
| `label` | `string` | Label text. No label is rendered when omitted |
|
|
478
|
+
| `control` | `RenderProp<"input", FieldState>` | The input to render, as Base UI's `Field.Control` (so it gets `aria-invalid`, `data-invalid`, … and is validated). Defaults to a plain `<input>`. Pass your themed input here |
|
|
479
|
+
| `children` | `(constraints: FieldConstraints) => ReactNode` | Renders a control that isn't a single `<input>` (e.g. [`BaseAutocomplete`](#baseautocomplete)) instead of `control`. Gets the constraints read from `schema` (`required`, `minLength`, …); pass on the ones the control supports. The control takes the field's `name` by itself |
|
|
480
|
+
| `renderLabel` | `RenderProp<"label", FieldState>` | Style the label |
|
|
481
|
+
| `renderErrors` | `RenderProp<"div", FieldState>` | Style client-side and server-side error messages. Each error is its own element |
|
|
482
|
+
| `className` | `string` | Classes for the field root (a flex column by default) |
|
|
483
|
+
|
|
484
|
+
Error texts come from `BaseForm`'s `formatMessage`. Render functions get `FieldState`: `{ disabled, touched, dirty, valid, filled, focused }` (`valid` is `null` until the field is validated).
|
|
485
|
+
|
|
486
|
+
```tsx
|
|
487
|
+
<BaseFormField
|
|
488
|
+
name="username"
|
|
489
|
+
label="Username"
|
|
490
|
+
schema={z.string().min(3).max(20)}
|
|
491
|
+
control={<Input />}
|
|
492
|
+
renderErrors={<p className="text-sm text-red-600" />}
|
|
493
|
+
/>
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
**One schema for the server and the client.** Define one Zod object schema per form. Use it to validate in the server action, and pass its fields to `BaseFormField`. The browser checks then always match the server rules:
|
|
497
|
+
|
|
498
|
+
```ts
|
|
499
|
+
// src/schemas/user.ts
|
|
500
|
+
import { z } from "zod";
|
|
501
|
+
|
|
502
|
+
export const UserSchema = z.object({
|
|
503
|
+
username: z.string().min(3).max(20),
|
|
504
|
+
email: z.email(),
|
|
505
|
+
});
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
```tsx
|
|
509
|
+
<BaseFormField name="username" label="Username" schema={UserSchema.shape.username} control={<Input />} />
|
|
510
|
+
<BaseFormField name="email" label="Email" schema={UserSchema.shape.email} control={<Input type="email" />} />
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
The browser can check only the rules that map to HTML attributes (`required`, `minLength`, `maxLength`, `min`, `max`, …). Refinements, transforms, cross-field rules and async checks such as "username taken" only run on the server, and come back as `fieldErrors`. Users can also bypass browser checks, so **the server action must always validate the full schema**. The client constraints only give earlier feedback.
|
|
514
|
+
|
|
515
|
+
#### `BaseAutocomplete`
|
|
516
|
+
|
|
517
|
+
A Base UI [`Autocomplete`](https://base-ui.com/react/components/autocomplete): a text input that suggests matching items as you type. The input is free text; choosing an item fills it in. A simple one needs no more than this:
|
|
518
|
+
|
|
519
|
+
```tsx
|
|
520
|
+
import { BaseAutocomplete } from "@omer-x/tailwind-ui-kit/form";
|
|
521
|
+
|
|
522
|
+
<BaseAutocomplete items={fruits}>
|
|
523
|
+
<BaseAutocomplete.Input aria-label="Fruit" className="border border-neutral-300" />
|
|
524
|
+
<BaseAutocomplete.Popup sideOffset={4} className="rounded-md bg-white py-1 shadow-lg">
|
|
525
|
+
<BaseAutocomplete.Empty className="px-3 not-empty:py-1.5">No fruits found</BaseAutocomplete.Empty>
|
|
526
|
+
<BaseAutocomplete.List>
|
|
527
|
+
{(fruit: string) => (
|
|
528
|
+
<BaseAutocomplete.Item key={fruit} value={fruit} className="px-3 py-1.5 data-highlighted:bg-blue-600">
|
|
529
|
+
{fruit}
|
|
530
|
+
</BaseAutocomplete.Item>
|
|
531
|
+
)}
|
|
532
|
+
</BaseAutocomplete.List>
|
|
533
|
+
</BaseAutocomplete.Popup>
|
|
534
|
+
</BaseAutocomplete>
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
The parts are reached with dot notation, which only works in client components. Build autocompletes in a `"use client"` file, e.g. your app's themed wrapper.
|
|
538
|
+
|
|
539
|
+
| Part | Renders | Description |
|
|
540
|
+
|------|---------|-------------|
|
|
541
|
+
| `BaseAutocomplete` | nothing | Groups the parts, holds the input's value and filters `items` |
|
|
542
|
+
| `.Input` | `<input>` | The text input. Takes `size` (`"lg" \| "md" \| "sm" \| "xs"`, default `"md"`), the same sizes as `BaseInput` |
|
|
543
|
+
| `.InputGroup` | `<div>` | Optional. Wraps the input with a trigger and a clear button (`relative flex items-center`). The popup is placed against it |
|
|
544
|
+
| `.Trigger` | `<button>` | Opens the popup. `data-popup-open` while it's open |
|
|
545
|
+
| `.Icon` | `<span>` | A decorative icon, hidden from assistive technology. Bring your own icon as children |
|
|
546
|
+
| `.Clear` | `<button>` | Clears the input. Only rendered while the input has a value (`keepMounted` keeps it) |
|
|
547
|
+
| `.Value` | nothing | The input's value, or a function of it, anywhere inside the root |
|
|
548
|
+
| `.Popup` | `<div>` | The floating container, portaled and positioned. As wide as the input, never taller than the space left (max `24rem`), with scale and fade transitions |
|
|
549
|
+
| `.Status` | `<div>` | A live region for screen readers ("Loading…", "12 results") |
|
|
550
|
+
| `.Empty` | `<div>` | Its children show only when no item matches. Always rendered (it's a live region): style it with `not-empty:` / `empty:` |
|
|
551
|
+
| `.List` | `<div>` | The scrollable list. Children can be a function that renders each filtered item (or group). `data-empty` when nothing matches |
|
|
552
|
+
| `.Item` | `<div>` | An option (`flex items-center gap-2`). `value` is the item. `data-highlighted`, `data-disabled` |
|
|
553
|
+
| `.Group` | `<div>` | A group of items. Pass the group's `items` to use `.Collection` inside |
|
|
554
|
+
| `.GroupLabel` | `<div>` | Names a group |
|
|
555
|
+
| `.Collection` | nothing | Renders the filtered items of the surrounding group with a function |
|
|
556
|
+
| `.Separator` | `<div>` | A `role="separator"` line, with `data-orientation` |
|
|
557
|
+
| `.Row` | `<div>` | A row of items when the root has `grid` |
|
|
558
|
+
|
|
559
|
+
Every part takes `className` and a `render` prop. Render functions get the part's state (`AutocompleteInputState`, `AutocompleteItemState`, `AutocompletePopupState`, …), which is also set as data attributes: `data-open`, `data-popup-side`, `data-list-empty`, `data-highlighted`, `data-side`, `data-align`, `data-starting-style`, … The input, input group and trigger also carry the field's state (`data-invalid`, `data-dirty`, `data-touched`, `data-filled`, `data-focused`).
|
|
560
|
+
|
|
561
|
+
`BaseAutocomplete` props:
|
|
562
|
+
|
|
563
|
+
| Prop | Type | Description |
|
|
564
|
+
|------|------|-------------|
|
|
565
|
+
| `items` | `Item[] \| { items: Item[] }[]` | The items, flat or in groups. Filtered by the input's value and passed to `List`'s function children |
|
|
566
|
+
| `filteredItems` | same as `items` | Items you filtered yourself, shown instead |
|
|
567
|
+
| `filter` | `(item, query, itemToString?) => boolean \| null` | Decides what matches. `null` turns filtering off. Defaults to a case-insensitive "contains" |
|
|
568
|
+
| `limit` | `number` | Shows at most this many matches |
|
|
569
|
+
| `mode` | `"list" \| "both" \| "inline" \| "none"` | `list` (default) filters; `both` also fills the input with the highlighted item; `inline` only fills; `none` does neither |
|
|
570
|
+
| `itemToStringValue` | `(item) => string` | Text of an object item. `{ value, label }` objects use `label` without it |
|
|
571
|
+
| `value` / `defaultValue` | `string` | The input's value, controlled / uncontrolled |
|
|
572
|
+
| `onValueChange` | `(value, event) => void` | Called with the new value and the DOM event (none when changed from code) |
|
|
573
|
+
| `open` / `defaultOpen` | `boolean` | The popup's state |
|
|
574
|
+
| `onOpenChange` | `(open, event) => void` | Called with the new state and the DOM event |
|
|
575
|
+
| `onOpenChangeComplete` | `(open) => void` | Called after the transition ends |
|
|
576
|
+
| `openOnInputClick` | `boolean` | Opens the popup on click |
|
|
577
|
+
| `autoHighlight` | `boolean \| "always"` | Highlights the first match while typing (`"always"`: also on open) |
|
|
578
|
+
| `keepHighlight` | `boolean` | Keeps the highlight when the pointer leaves the list |
|
|
579
|
+
| `highlightItemOnHover` | `boolean` | Default `true`. `false` lets `:hover` and `data-highlighted` differ |
|
|
580
|
+
| `onItemHighlighted` | `(item, { reason, index }) => void` | `reason` is `"keyboard"`, `"pointer"` or `"none"` |
|
|
581
|
+
| `loopFocus` | `boolean` | Default `true`. Arrow keys wrap around the list |
|
|
582
|
+
| `submitOnItemClick` | `boolean` | Submits the form when an item is chosen (e.g. a search box) |
|
|
583
|
+
| `grid` | `boolean` | Items in `Row`s, navigated with all four arrow keys |
|
|
584
|
+
| `inline` | `boolean` | Renders the list in place, without a popup. Use with `open` |
|
|
585
|
+
| `modal` | `boolean` | Locks page scroll and outside pointer input while open |
|
|
586
|
+
| `virtualized` | `boolean` | For lists rendered by a virtualizer (give each item its `index`) |
|
|
587
|
+
| `locale` | `Intl.LocalesArgument` | Locale of the default filter |
|
|
588
|
+
| `name`, `form`, `id`, `required`, `disabled`, `readOnly` | | Form props. Inside `BaseFormField`, the field's `name` is used |
|
|
589
|
+
| `inputRef` | `Ref<HTMLInputElement>` | The hidden input used for validation |
|
|
590
|
+
| `actionsRef` | `RefObject<AutocompleteActions>` | `actionsRef.current?.close()` |
|
|
591
|
+
|
|
592
|
+
`BaseAutocomplete.Popup` props, besides `<div>` props:
|
|
593
|
+
|
|
594
|
+
| Prop | Description |
|
|
595
|
+
|------|-------------|
|
|
596
|
+
| `side`, `align` | Placement against the input (group). Default `"bottom"`, `"start"` |
|
|
597
|
+
| `sideOffset`, `alignOffset` | Offsets in pixels, or a function of the placement and sizes |
|
|
598
|
+
| `anchor`, `positionMethod`, `collisionBoundary`, `collisionPadding`, `collisionAvoidance`, `sticky`, `disableAnchorTracking` | Positioning control |
|
|
599
|
+
| `renderPositioner` | Replaces the element that positions the popup, e.g. `<div className="z-50" />` |
|
|
600
|
+
| `container`, `keepMounted` | Portal target (default `<body>`), and keeping the popup in the DOM while closed |
|
|
601
|
+
| `backdrop`, `renderBackdrop` | Renders a `fixed inset-0` backdrop (off by default, use it with `modal`) |
|
|
602
|
+
| `arrow`, `renderArrow`, `arrowPadding` | An arrow pointing at the input. Rendered when `arrow` (its content, e.g. an `<svg>`) or `renderArrow` is given |
|
|
603
|
+
| `finalFocus` | The element focused when the popup closes |
|
|
604
|
+
|
|
605
|
+
**Your own filtering.** `useAutocompleteFilter(options?)` returns locale-aware matchers (`contains`, `startsWith`, `endsWith`, accent- and case-insensitive by default). Pass one as `filter`, or filter items yourself and pass `filteredItems`:
|
|
606
|
+
|
|
607
|
+
```tsx
|
|
608
|
+
const filter = useAutocompleteFilter();
|
|
609
|
+
<BaseAutocomplete items={fruits} filter={filter.startsWith}>…</BaseAutocomplete>
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
**In a form.** Put it in `BaseFormField`'s `children`. The field gives it its `name`, label, validation, server errors and `data-invalid`, and `onlyDirty` sees its value. Only `required` is checked in the browser; other schema rules are checked by the server:
|
|
613
|
+
|
|
614
|
+
```tsx
|
|
615
|
+
<BaseFormField name="fruit" label="Fruit" schema={Schema.shape.fruit}>
|
|
616
|
+
{constraints => (
|
|
617
|
+
<BaseAutocomplete items={fruits} required={constraints.required}>
|
|
618
|
+
…
|
|
619
|
+
</BaseAutocomplete>
|
|
620
|
+
)}
|
|
621
|
+
</BaseFormField>
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
### `@omer-x/tailwind-ui-kit/dialog`
|
|
625
|
+
|
|
626
|
+
#### `BaseDialog`
|
|
627
|
+
|
|
628
|
+
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:
|
|
629
|
+
|
|
630
|
+
```tsx
|
|
631
|
+
import { BaseDialog } from "@omer-x/tailwind-ui-kit/dialog";
|
|
632
|
+
|
|
633
|
+
<BaseDialog>
|
|
634
|
+
<BaseDialog.Trigger render={<Button />}>Delete</BaseDialog.Trigger>
|
|
635
|
+
<BaseDialog.Popup className="rounded-lg bg-white p-6 shadow-lg">
|
|
636
|
+
<BaseDialog.Title>Delete item?</BaseDialog.Title>
|
|
637
|
+
<BaseDialog.Description>This can't be undone.</BaseDialog.Description>
|
|
638
|
+
<BaseDialog.Close render={<Button />}>Cancel</BaseDialog.Close>
|
|
639
|
+
</BaseDialog.Popup>
|
|
640
|
+
</BaseDialog>
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
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.
|
|
644
|
+
|
|
645
|
+
| Part | Renders | Description |
|
|
646
|
+
|------|---------|-------------|
|
|
647
|
+
| `BaseDialog` | nothing | Groups the parts and holds the open state |
|
|
648
|
+
| `BaseDialog.Trigger` | `<button>` | Opens the dialog. `data-popup-open` while its dialog is open |
|
|
649
|
+
| `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 |
|
|
650
|
+
| `BaseDialog.Title` | `<h2>` | The heading that names the dialog (`text-xl font-semibold`) |
|
|
651
|
+
| `BaseDialog.Description` | `<p>` | Describes the dialog to assistive technology |
|
|
652
|
+
| `BaseDialog.Close` | `<button>` | Closes the dialog |
|
|
653
|
+
|
|
654
|
+
Every part takes `className` and `render`. `Trigger` and `Close` also take `nativeButton` (set it to `false` when `render` doesn't render a `<button>`).
|
|
655
|
+
|
|
656
|
+
`BaseDialog` props:
|
|
657
|
+
|
|
658
|
+
| Prop | Type | Default | Description |
|
|
659
|
+
|------|------|---------|-------------|
|
|
660
|
+
| `open` / `defaultOpen` | `boolean` | `false` | Controlled / initial open state |
|
|
661
|
+
| `onOpenChange` | `(open, event) => void` | | Called with the new state and the DOM event that changed it (`undefined` when changed from code) |
|
|
662
|
+
| `onOpenChangeComplete` | `(open) => void` | | Called after the open or close transition ends |
|
|
663
|
+
| `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 |
|
|
664
|
+
| `disablePointerDismissal` | `boolean` | `false` | Keeps the dialog open on outside presses |
|
|
665
|
+
| `handle` | `DialogHandle<Payload>` | | Connects triggers outside the dialog and opens it from code (see below) |
|
|
666
|
+
| `triggerId` / `defaultTriggerId` | `string \| null` | | The trigger a controlled / initially open dialog belongs to |
|
|
667
|
+
| `children` | `ReactNode \| ({ payload }) => ReactNode` | | The parts, or a function that gets the payload of the trigger that opened the dialog |
|
|
668
|
+
|
|
669
|
+
`BaseDialog.Popup` props:
|
|
670
|
+
|
|
671
|
+
| Prop | Type | Default | Description |
|
|
672
|
+
|------|------|---------|-------------|
|
|
673
|
+
| `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` |
|
|
674
|
+
| `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"`) |
|
|
675
|
+
| `finalFocus` | `DialogFocusTarget` | `true` | The element focused on close. `true` is the trigger |
|
|
676
|
+
| `container` | `HTMLElement \| ShadowRoot \| RefObject \| null` | `<body>` | Where the popup is portaled to |
|
|
677
|
+
| `keepMounted` | `boolean` | `false` | Keeps the popup in the DOM while closed |
|
|
678
|
+
| `backdrop` | `boolean` | `true` | Set to `false` for no backdrop. Nested dialogs never render one |
|
|
679
|
+
| `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` |
|
|
680
|
+
| `render` | `RenderProp<"div", DialogPopupState>` | | The state is `{ open, nested, nestedDialogOpen }` |
|
|
681
|
+
|
|
682
|
+
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).
|
|
683
|
+
|
|
684
|
+
**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.
|
|
685
|
+
|
|
686
|
+
#### `createDialogHandle`
|
|
687
|
+
|
|
688
|
+
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:
|
|
689
|
+
|
|
690
|
+
```tsx
|
|
691
|
+
import { BaseDialog, createDialogHandle } from "@omer-x/tailwind-ui-kit/dialog";
|
|
692
|
+
|
|
693
|
+
const deleteDialog = createDialogHandle<{ id: string, name: string }>();
|
|
694
|
+
|
|
695
|
+
// triggers anywhere on the page
|
|
696
|
+
<BaseDialog.Trigger handle={deleteDialog} payload={{ id: p.id, name: p.name }} render={<Button />}>Delete</BaseDialog.Trigger>
|
|
697
|
+
|
|
698
|
+
// one dialog on the page
|
|
699
|
+
<BaseDialog handle={deleteDialog}>
|
|
700
|
+
{({ payload }) => payload && (
|
|
701
|
+
<BaseDialog.Popup size="sm">
|
|
702
|
+
<BaseDialog.Title>Delete {payload.name}?</BaseDialog.Title>
|
|
703
|
+
</BaseDialog.Popup>
|
|
704
|
+
)}
|
|
705
|
+
</BaseDialog>
|
|
706
|
+
|
|
707
|
+
// from code
|
|
708
|
+
deleteDialog.openWithPayload({ id, name });
|
|
709
|
+
```
|
|
710
|
+
|
|
711
|
+
A `DialogHandle<Payload>` has `open(triggerId?)`, `openWithPayload(payload)`, `close()` and `isOpen`.
|
|
712
|
+
|
|
713
|
+
- Calls are **ignored while the dialog isn't mounted**. Keep the `BaseDialog` with the handle rendered.
|
|
714
|
+
- Call them **from event handlers or effects, never during render**.
|
|
715
|
+
|
|
716
|
+
#### `BaseAlertDialog`
|
|
717
|
+
|
|
718
|
+
A Base UI [`Alert Dialog`](https://base-ui.com/react/components/alert-dialog): a modal dialog the user has to respond to. It works like `BaseDialog`, with these differences:
|
|
719
|
+
|
|
720
|
+
- The popup has `role="alertdialog"`.
|
|
721
|
+
- It is always modal, and outside presses don't close it. Escape and `Close` buttons still do. There are no `modal` or `disablePointerDismissal` props.
|
|
722
|
+
- `Popup`, `Title`, `Description` and `Close` are `BaseDialog`'s parts, so they look the same and take the same props (`size`, `initialFocus`, `renderBackdrop`, …).
|
|
723
|
+
|
|
724
|
+
```tsx
|
|
725
|
+
import { BaseAlertDialog } from "@omer-x/tailwind-ui-kit/dialog";
|
|
726
|
+
|
|
727
|
+
<BaseAlertDialog>
|
|
728
|
+
<BaseAlertDialog.Trigger render={<Button />}>Discard draft</BaseAlertDialog.Trigger>
|
|
729
|
+
<BaseAlertDialog.Popup size="sm" className="rounded-lg bg-white p-6 shadow-lg">
|
|
730
|
+
<BaseAlertDialog.Title>Discard draft?</BaseAlertDialog.Title>
|
|
731
|
+
<BaseAlertDialog.Description>You can't undo this action.</BaseAlertDialog.Description>
|
|
732
|
+
<BaseAlertDialog.Close render={<Button />}>Cancel</BaseAlertDialog.Close>
|
|
733
|
+
<BaseAlertDialog.Close render={<Button />} onClick={discard}>Discard</BaseAlertDialog.Close>
|
|
734
|
+
</BaseAlertDialog.Popup>
|
|
735
|
+
</BaseAlertDialog>
|
|
736
|
+
```
|
|
737
|
+
|
|
738
|
+
| Part | Renders | Description |
|
|
739
|
+
|------|---------|-------------|
|
|
740
|
+
| `BaseAlertDialog` | nothing | Groups the parts and holds the open state |
|
|
741
|
+
| `BaseAlertDialog.Trigger` | `<button>` | Opens the alert dialog. `data-popup-open` while its alert dialog is open |
|
|
742
|
+
| `BaseAlertDialog.Popup` | `<div>` | Same as `BaseDialog.Popup` |
|
|
743
|
+
| `BaseAlertDialog.Title` | `<h2>` | Same as `BaseDialog.Title` |
|
|
744
|
+
| `BaseAlertDialog.Description` | `<p>` | Same as `BaseDialog.Description` |
|
|
745
|
+
| `BaseAlertDialog.Close` | `<button>` | Same as `BaseDialog.Close` |
|
|
746
|
+
|
|
747
|
+
`BaseAlertDialog` takes `BaseDialog`'s props except `modal` and `disablePointerDismissal`: `open`, `defaultOpen`, `onOpenChange`, `onOpenChangeComplete`, `handle` (an `AlertDialogHandle<Payload>`), `triggerId`, `defaultTriggerId` and `children` (parts, or a function of `{ payload }`).
|
|
748
|
+
|
|
749
|
+
Put it inside a `BaseDialog.Popup` to confirm something from a dialog: it stacks on top like a nested dialog.
|
|
750
|
+
|
|
751
|
+
#### `createAlertDialogHandle`
|
|
752
|
+
|
|
753
|
+
`createDialogHandle` for alert dialogs. It returns an `AlertDialogHandle<Payload>` with the same methods (`open`, `openWithPayload`, `close`, `isOpen`) and the same rules. Pass it to `BaseAlertDialog` and `BaseAlertDialog.Trigger`:
|
|
754
|
+
|
|
755
|
+
```tsx
|
|
756
|
+
import { BaseAlertDialog, createAlertDialogHandle } from "@omer-x/tailwind-ui-kit/dialog";
|
|
757
|
+
|
|
758
|
+
const removeAlert = createAlertDialogHandle<{ id: string, name: string }>();
|
|
759
|
+
|
|
760
|
+
<BaseAlertDialog.Trigger handle={removeAlert} payload={member} render={<Button />}>Remove</BaseAlertDialog.Trigger>
|
|
761
|
+
|
|
762
|
+
<BaseAlertDialog handle={removeAlert}>
|
|
763
|
+
{({ payload }) => payload && (
|
|
764
|
+
<BaseAlertDialog.Popup size="sm">
|
|
765
|
+
<BaseAlertDialog.Title>Remove {payload.name}?</BaseAlertDialog.Title>
|
|
766
|
+
</BaseAlertDialog.Popup>
|
|
767
|
+
)}
|
|
768
|
+
</BaseAlertDialog>
|
|
769
|
+
```
|
|
770
|
+
|
|
771
|
+
### `@omer-x/tailwind-ui-kit/menu`
|
|
772
|
+
|
|
773
|
+
#### `BaseContextMenu`
|
|
774
|
+
|
|
775
|
+
A Base UI [`Context Menu`](https://base-ui.com/react/components/context-menu): a menu that opens at the pointer on right click or long press. `Popup` brings its portal and positioner, so a simple context menu needs no more than this:
|
|
776
|
+
|
|
777
|
+
```tsx
|
|
778
|
+
import { BaseContextMenu } from "@omer-x/tailwind-ui-kit/menu";
|
|
779
|
+
|
|
780
|
+
<BaseContextMenu>
|
|
781
|
+
<BaseContextMenu.Trigger className="h-40 rounded border border-dashed">Right click here</BaseContextMenu.Trigger>
|
|
782
|
+
<BaseContextMenu.Popup className="rounded-md border bg-white shadow-lg">
|
|
783
|
+
<BaseContextMenu.Item className="data-highlighted:bg-neutral-100" onClick={reload}>Reload</BaseContextMenu.Item>
|
|
784
|
+
<BaseContextMenu.Separator className="bg-neutral-200" />
|
|
785
|
+
<BaseContextMenu.LinkItem className="data-highlighted:bg-neutral-100" href="/help">Help</BaseContextMenu.LinkItem>
|
|
786
|
+
</BaseContextMenu.Popup>
|
|
787
|
+
</BaseContextMenu>
|
|
788
|
+
```
|
|
789
|
+
|
|
790
|
+
Like `BaseDialog`, the parts use dot notation, so build menus in a `"use client"` file. Every part except the root and `Trigger` is shared with `BaseMenu`, so one set of popup and item styles themes both.
|
|
791
|
+
|
|
792
|
+
| Part | Renders | Description |
|
|
793
|
+
|------|---------|-------------|
|
|
794
|
+
| `BaseContextMenu` | nothing | Groups the parts and holds the open state |
|
|
795
|
+
| `BaseContextMenu.Trigger` | `<div>` | The area that opens the menu on right click or long press. `data-popup-open` while the menu is open |
|
|
796
|
+
| `BaseContextMenu.Popup` | `<div>` | The menu, portaled and placed at the pointer (or next to its submenu trigger), with scale and fade transitions. Scrolls when taller than the viewport. Optional backdrop |
|
|
797
|
+
| `BaseContextMenu.Arrow` | `<div>` | An arrow pointing at the anchor, inside `Popup`. Draw it yourself |
|
|
798
|
+
| `BaseContextMenu.Item` | `<div>` | An action. Closes the menu when clicked (`closeOnClick`, default `true`) |
|
|
799
|
+
| `BaseContextMenu.LinkItem` | `<a>` | A link. Use `render` for your router's link. Doesn't close the menu (`closeOnClick`, default `false`) |
|
|
800
|
+
| `BaseContextMenu.Separator` | `<div>` | A `1px` line between items (`my-1 h-px`). Give it a color with `className` |
|
|
801
|
+
| `BaseContextMenu.Group` | `<div>` | Groups related items, labelled by the `GroupLabel` inside it |
|
|
802
|
+
| `BaseContextMenu.GroupLabel` | `<div>` | The group's name (`text-xs font-medium`) |
|
|
803
|
+
| `BaseContextMenu.RadioGroup` | `<div>` | Holds `RadioItem`s: `value` / `defaultValue`, `onValueChange(value, event)`, `disabled` |
|
|
804
|
+
| `BaseContextMenu.RadioItem` | `<div>` | An option of a `RadioGroup` (`value`) |
|
|
805
|
+
| `BaseContextMenu.CheckboxItem` | `<div>` | A toggle: `checked` / `defaultChecked`, `onCheckedChange(checked, event)` |
|
|
806
|
+
| `BaseContextMenu.Submenu` | nothing | Holds a `SubmenuTrigger` and a nested `Popup`. Takes `open` / `defaultOpen`, `onOpenChange`, `onOpenChangeComplete`, `disabled`, `loopFocus`, `highlightItemOnHover` and `closeParentOnEsc` |
|
|
807
|
+
| `BaseContextMenu.SubmenuTrigger` | `<div>` | The item that opens its submenu, on click or hover (`openOnHover`, `delay`, `closeDelay`). `icon` is shown at the end (e.g. a chevron), with `iconClassName` for its `<span>` |
|
|
808
|
+
|
|
809
|
+
Every part that renders an element takes `className` and `render`.
|
|
810
|
+
|
|
811
|
+
Items (`Item`, `LinkItem`, `SubmenuTrigger`, `CheckboxItem`, `RadioItem`) share one layout: `flex items-center gap-2 px-3 py-1.5 text-sm`, no outline, faded and inert while disabled. They take `label` (the text for keyboard text navigation), and the `<div>` ones take `disabled` and `nativeButton` (set it to `true` when `render` renders a `<button>`). Style the highlighted item with `data-highlighted:`.
|
|
812
|
+
|
|
813
|
+
`CheckboxItem` and `RadioItem` put their indicator in a first `1rem` column, so labels line up whether it shows or not. Indicator props:
|
|
814
|
+
|
|
815
|
+
| Prop | Type | Description |
|
|
816
|
+
|------|------|-------------|
|
|
817
|
+
| `indicator` | `ReactNode` | Shown when checked, e.g. a check mark or a dot. Nothing by default |
|
|
818
|
+
| `renderIndicator` | `RenderProp<"span", State>` | Style or replace the indicator's `<span>` |
|
|
819
|
+
| `keepIndicatorMounted` | `boolean` | Keeps the indicator in the DOM when unchecked, e.g. to animate it out |
|
|
820
|
+
| `closeOnClick` | `boolean` | Closes the menu when clicked. Default `false` |
|
|
821
|
+
|
|
822
|
+
`BaseContextMenu` props:
|
|
823
|
+
|
|
824
|
+
| Prop | Type | Default | Description |
|
|
825
|
+
|------|------|---------|-------------|
|
|
826
|
+
| `open` / `defaultOpen` | `boolean` | `false` | Controlled / initial open state |
|
|
827
|
+
| `onOpenChange` | `(open, event) => void` | | Called with the new state and the DOM event that changed it (`undefined` when changed from code) |
|
|
828
|
+
| `onOpenChangeComplete` | `(open) => void` | | Called after the open or close transition ends |
|
|
829
|
+
| `disabled` | `boolean` | `false` | Doesn't open, so the browser's own context menu shows |
|
|
830
|
+
| `loopFocus` | `boolean` | `true` | Arrow keys wrap from the last item to the first |
|
|
831
|
+
| `highlightItemOnHover` | `boolean` | `true` | Highlights items under the pointer. Turn it off to style `:hover` apart from `data-highlighted` |
|
|
832
|
+
| `orientation` | `"vertical" \| "horizontal"` | `"vertical"` | Which arrow keys move between items |
|
|
833
|
+
|
|
834
|
+
`BaseContextMenu.Popup` props:
|
|
835
|
+
|
|
836
|
+
| Prop | Type | Default | Description |
|
|
837
|
+
|------|------|---------|-------------|
|
|
838
|
+
| `side` / `align` | `MenuSide` / `MenuAlign` | at the pointer | Where the popup sits against its anchor (`"top"`, `"bottom"`, `"left"`, `"right"`, `"inline-start"`, `"inline-end"` / `"start"`, `"center"`, `"end"`). Submenus default to `"inline-end"` / `"start"` |
|
|
839
|
+
| `sideOffset` / `alignOffset` | `number \| MenuOffsetFunction` | | Offsets from the anchor, in pixels, or a function of the placement and sizes |
|
|
840
|
+
| `collisionPadding` | `number \| { top, right, bottom, left }` | `5` | Space kept from the viewport edges |
|
|
841
|
+
| `collisionBoundary` / `collisionAvoidance` | | `"clipping-ancestors"` | The area the popup stays inside, and how it avoids its edges (flip or shift) |
|
|
842
|
+
| `anchor` | `Element \| RefObject \| () => Element \| null` | the trigger | The element the popup is placed against |
|
|
843
|
+
| `disableAnchorTracking` | `boolean` | `false` | Stops following the anchor when it moves |
|
|
844
|
+
| `arrowPadding` | `number` | `5` | Minimum distance between an `Arrow` and the popup's corners |
|
|
845
|
+
| `sticky` | `boolean` | `false` | Stays in view while the anchor scrolls away |
|
|
846
|
+
| `positionMethod` | `"absolute" \| "fixed"` | `"absolute"` | Use `"fixed"` when the anchor is inside a fixed container |
|
|
847
|
+
| `positionerClassName` / `renderPositioner` | `string` / `RenderProp<"div", MenuPositionerState>` | | Style or replace the positioner, the `<div>` around the popup that places it (`z-50`) |
|
|
848
|
+
| `finalFocus` | `MenuFocusTarget` | `true` | The element focused on close: `true` (default), `false` (don't move focus), a ref, or a function of the interaction type |
|
|
849
|
+
| `container` | `HTMLElement \| ShadowRoot \| RefObject \| null` | `<body>` | Where the popup is portaled to |
|
|
850
|
+
| `keepMounted` | `boolean` | `false` | Keeps the popup in the DOM while closed |
|
|
851
|
+
| `backdrop` | `boolean` | `false` | Renders a transparent full-screen backdrop (`fixed inset-0`) behind the popup |
|
|
852
|
+
| `renderBackdrop` | `RenderProp<"div", MenuBackdropState>` | | Style or replace the backdrop. A `className` on the element wins over the kit's conflicting classes |
|
|
853
|
+
| `render` | `RenderProp<"div", MenuPopupState>` | | The state is `{ open, side, align, nested }` |
|
|
854
|
+
|
|
855
|
+
Data attributes: the popup has `data-open` / `data-closed`, `data-side`, `data-align`, `data-nested` (in a submenu) and `data-starting-style` / `data-ending-style` during transitions. Items have `data-highlighted` and `data-disabled`; checkbox and radio items and their indicators also have `data-checked` / `data-unchecked`.
|
|
856
|
+
|
|
857
|
+
**Submenus:** put a `Submenu` among the items, with a `SubmenuTrigger` and another `Popup` inside:
|
|
858
|
+
|
|
859
|
+
```tsx
|
|
860
|
+
<BaseContextMenu.Submenu>
|
|
861
|
+
<BaseContextMenu.SubmenuTrigger icon={<ChevronRight />}>Share</BaseContextMenu.SubmenuTrigger>
|
|
862
|
+
<BaseContextMenu.Popup className="rounded-md border bg-white shadow-lg">
|
|
863
|
+
<BaseContextMenu.Item>Email</BaseContextMenu.Item>
|
|
864
|
+
</BaseContextMenu.Popup>
|
|
865
|
+
</BaseContextMenu.Submenu>
|
|
866
|
+
```
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
#### `BaseMenu`
|
|
870
|
+
|
|
871
|
+
A Base UI [`Menu`](https://base-ui.com/react/components/menu): a list of actions opened from a button, with keyboard navigation, submenus, checkbox and radio items, and optional filtering. It uses the same `Popup`, `Arrow`, items, groups and submenus as `BaseContextMenu` (see above), so one set of styles themes both. A simple menu needs no more than this:
|
|
872
|
+
|
|
873
|
+
```tsx
|
|
874
|
+
import { BaseMenu } from "@omer-x/tailwind-ui-kit/menu";
|
|
875
|
+
|
|
876
|
+
<BaseMenu>
|
|
877
|
+
<BaseMenu.Trigger render={<Button />}>Actions</BaseMenu.Trigger>
|
|
878
|
+
<BaseMenu.Popup sideOffset={4} className="rounded-md border bg-white shadow-lg">
|
|
879
|
+
<BaseMenu.Item className="data-highlighted:bg-neutral-100" onClick={edit}>Edit</BaseMenu.Item>
|
|
880
|
+
<BaseMenu.Separator className="bg-neutral-200" />
|
|
881
|
+
<BaseMenu.Item className="data-highlighted:bg-neutral-100" onClick={remove}>Delete</BaseMenu.Item>
|
|
882
|
+
</BaseMenu.Popup>
|
|
883
|
+
</BaseMenu>
|
|
884
|
+
```
|
|
885
|
+
|
|
886
|
+
Parts besides the shared ones (`Popup`, `Arrow`, `Item`, `LinkItem`, `Separator`, `Group`, `GroupLabel`, `RadioGroup`, `RadioItem`, `CheckboxItem`, `Submenu`, `SubmenuTrigger`):
|
|
887
|
+
|
|
888
|
+
| Part | Renders | Description |
|
|
889
|
+
|------|---------|-------------|
|
|
890
|
+
| `BaseMenu` | nothing | Groups the parts and holds the open state |
|
|
891
|
+
| `BaseMenu.Trigger` | `<button>` | Opens the menu. `data-popup-open` while its menu is open. Takes `handle`, `payload`, `openOnHover` (also open on hover), `delay` / `closeDelay` (hover delays in ms, default `100` / `0`) and `nativeButton` |
|
|
892
|
+
| `BaseMenu.Input` | `<input>` | The search box of a `filterable` menu (`role="searchbox"`) |
|
|
893
|
+
| `BaseMenu.Clear` | `<button>` | Clears the search box. Only rendered while there's a query. A pointer shortcut: out of the tab order and `aria-hidden` |
|
|
894
|
+
| `BaseMenu.Empty` | `<div>` | Shown only when no item matches (`role="status"`). Not rendered otherwise |
|
|
895
|
+
| `BaseMenu.List` | `<div>` | Holds the items when the popup has other content too (the search box). Scrolls when the popup runs out of space (`min-h-0 flex-1 overflow-y-auto`) |
|
|
896
|
+
| `BaseMenu.Viewport` | `<div>` | Animates the content when one menu is opened by several triggers and its content depends on the trigger. `data-activation-direction`, `data-transitioning` |
|
|
897
|
+
|
|
898
|
+
The popup is placed below the trigger and centered by default (`side="bottom"`, `align="center"`).
|
|
899
|
+
|
|
900
|
+
`BaseMenu` props:
|
|
901
|
+
|
|
902
|
+
| Prop | Type | Default | Description |
|
|
903
|
+
|------|------|---------|-------------|
|
|
904
|
+
| `open` / `defaultOpen` | `boolean` | `false` | Controlled / initial open state |
|
|
905
|
+
| `onOpenChange` | `(open, event) => void` | | Called with the new state and the DOM event that changed it (`undefined` when changed from code) |
|
|
906
|
+
| `onOpenChangeComplete` | `(open) => void` | | Called after the open or close transition ends |
|
|
907
|
+
| `modal` | `boolean` | `true` | Locks page scroll and blocks outside pointer input while open. Menus opened by hover never are |
|
|
908
|
+
| `disabled` | `boolean` | `false` | |
|
|
909
|
+
| `loopFocus` | `boolean` | `true` | Arrow keys wrap from the last item to the first |
|
|
910
|
+
| `highlightItemOnHover` | `boolean` | `true` | Turn it off to style `:hover` apart from `data-highlighted` |
|
|
911
|
+
| `orientation` | `"vertical" \| "horizontal"` | `"vertical"` | Which arrow keys move between items |
|
|
912
|
+
| `onItemHighlighted` | `(item, { reason, label }) => void` | | The highlighted element (or `undefined`). `reason` is `"keyboard"`, `"pointer"` or `"none"` |
|
|
913
|
+
| `actionsRef` | `RefObject<MenuActions>` | | `actionsRef.current?.close()` |
|
|
914
|
+
| `handle` | `MenuHandle<Payload>` | | Connects triggers outside the menu and opens it from code (see below) |
|
|
915
|
+
| `triggerId` / `defaultTriggerId` | `string \| null` | | The trigger a controlled / initially open menu belongs to |
|
|
916
|
+
| `children` | `ReactNode \| ({ payload }) => ReactNode` | | The parts, or a function that gets the payload of the trigger that opened the menu |
|
|
917
|
+
| `filterable` | `boolean` | `false` | Adds filtering (see below) |
|
|
918
|
+
|
|
919
|
+
`Submenu` also takes `orientation`, `onItemHighlighted`, `actionsRef` and the filtering props.
|
|
920
|
+
|
|
921
|
+
**Router links.** `LinkItem` renders an `<a>`. Pass your router's link as `render`, and it gets the item's props and behavior:
|
|
922
|
+
|
|
923
|
+
```tsx
|
|
924
|
+
import Link from "next/link";
|
|
925
|
+
|
|
926
|
+
<BaseMenu.LinkItem render={<Link href="/settings" />}>Settings</BaseMenu.LinkItem>
|
|
927
|
+
```
|
|
928
|
+
|
|
929
|
+
**Filtering.** `filterable` (on `BaseMenu` or a `Submenu`) lets the user filter the items by typing in an `Input`. Put the items in a `List`; items that don't match are removed. The popup becomes a `role="dialog"` holding the search box and the `role="menu"` list. Make the popup a flex column (`flex flex-col`) so only the list scrolls:
|
|
930
|
+
|
|
931
|
+
```tsx
|
|
932
|
+
<BaseMenu filterable>
|
|
933
|
+
<BaseMenu.Trigger render={<Button />}>Labels</BaseMenu.Trigger>
|
|
934
|
+
<BaseMenu.Popup className="flex flex-col">
|
|
935
|
+
<BaseMenu.Input aria-label="Search labels" placeholder="Search labels" />
|
|
936
|
+
<BaseMenu.Empty className="py-1.5">No labels found</BaseMenu.Empty>
|
|
937
|
+
<BaseMenu.List>
|
|
938
|
+
{labels.map(label => <BaseMenu.CheckboxItem key={label}>{label}</BaseMenu.CheckboxItem>)}
|
|
939
|
+
</BaseMenu.List>
|
|
940
|
+
</BaseMenu.Popup>
|
|
941
|
+
</BaseMenu>
|
|
942
|
+
```
|
|
943
|
+
|
|
944
|
+
| Prop | Type | Description |
|
|
945
|
+
|------|------|-------------|
|
|
946
|
+
| `filter` | `(text, query) => boolean \| null` | Decides what matches, from the item's `label` (or text) and the trimmed query. Defaults to "contains", ignoring case, accents and punctuation. `null` shows every item (filter them yourself) |
|
|
947
|
+
| `autoHighlight` | `boolean \| "always"` | Highlights the first match while there's a query (`"always"`: also without one) |
|
|
948
|
+
| `locale` | `Intl.LocalesArgument` | Locale of the default filter |
|
|
949
|
+
| `query` / `defaultQuery` | `string` | The query, controlled / uncontrolled. It's cleared when the menu closes |
|
|
950
|
+
| `onQueryChange` | `(query, event) => void` | Called with the new query and the DOM event |
|
|
951
|
+
|
|
952
|
+
#### `createMenuHandle`
|
|
953
|
+
|
|
954
|
+
Opens one menu from triggers anywhere on the page, or from code. Each trigger can pass a `payload`, which the menu's function children receive:
|
|
955
|
+
|
|
956
|
+
```tsx
|
|
957
|
+
import { BaseMenu, createMenuHandle } from "@omer-x/tailwind-ui-kit/menu";
|
|
958
|
+
|
|
959
|
+
const rowMenu = createMenuHandle<{ id: string, name: string }>();
|
|
960
|
+
|
|
961
|
+
// triggers anywhere on the page
|
|
962
|
+
<BaseMenu.Trigger id={`row-${row.id}`} handle={rowMenu} payload={row} render={<Button />}>Options</BaseMenu.Trigger>
|
|
963
|
+
|
|
964
|
+
// one menu on the page
|
|
965
|
+
<BaseMenu handle={rowMenu}>
|
|
966
|
+
{({ payload }) => payload && (
|
|
967
|
+
<BaseMenu.Popup>
|
|
968
|
+
<BaseMenu.Item onClick={() => rename(payload.id)}>Rename {payload.name}</BaseMenu.Item>
|
|
969
|
+
</BaseMenu.Popup>
|
|
970
|
+
)}
|
|
971
|
+
</BaseMenu>
|
|
972
|
+
|
|
973
|
+
// from code
|
|
974
|
+
rowMenu.open(`row-${id}`);
|
|
975
|
+
```
|
|
976
|
+
|
|
977
|
+
A `MenuHandle<Payload>` has `open(triggerId)`, `close()` and `isOpen`. Unlike the dialog handles, it has no `openWithPayload`: a menu is placed against its trigger, so `open` needs the `id` of a trigger with the same handle (it throws when there's none).
|
|
978
|
+
|
|
979
|
+
- Calls are **ignored while the menu isn't mounted**. Keep the `BaseMenu` with the handle rendered.
|
|
980
|
+
- Call them **from event handlers or effects, never during render**.
|
|
981
|
+
|
|
982
|
+
#### `BaseMenubar`
|
|
983
|
+
|
|
984
|
+
A Base UI [`Menubar`](https://base-ui.com/react/components/menubar): an application menu bar holding several `BaseMenu`s. Once one menu is open, hovering another trigger switches to its menu, and the arrow keys move between the triggers. `BaseMenu` is attached as `BaseMenubar.Menu`, so one import is enough:
|
|
985
|
+
|
|
986
|
+
```tsx
|
|
987
|
+
import { BaseMenubar } from "@omer-x/tailwind-ui-kit/menu";
|
|
988
|
+
|
|
989
|
+
<BaseMenubar className="gap-1 rounded-md border p-1">
|
|
990
|
+
<BaseMenubar.Menu>
|
|
991
|
+
<BaseMenubar.Menu.Trigger className="rounded px-3 py-1 data-popup-open:bg-neutral-200">File</BaseMenubar.Menu.Trigger>
|
|
992
|
+
<BaseMenubar.Menu.Popup sideOffset={6} className="rounded-md border bg-white shadow-lg">
|
|
993
|
+
<BaseMenubar.Menu.Item className="data-highlighted:bg-neutral-100" onClick={save}>Save</BaseMenubar.Menu.Item>
|
|
994
|
+
</BaseMenubar.Menu.Popup>
|
|
995
|
+
</BaseMenubar.Menu>
|
|
996
|
+
<BaseMenubar.Menu>
|
|
997
|
+
<BaseMenubar.Menu.Trigger className="rounded px-3 py-1 data-popup-open:bg-neutral-200">Edit</BaseMenubar.Menu.Trigger>
|
|
998
|
+
<BaseMenubar.Menu.Popup sideOffset={6} className="rounded-md border bg-white shadow-lg">
|
|
999
|
+
<BaseMenubar.Menu.Item className="data-highlighted:bg-neutral-100" onClick={undo}>Undo</BaseMenubar.Menu.Item>
|
|
1000
|
+
</BaseMenubar.Menu.Popup>
|
|
1001
|
+
</BaseMenubar.Menu>
|
|
1002
|
+
</BaseMenubar>
|
|
1003
|
+
```
|
|
1004
|
+
|
|
1005
|
+
`<BaseMenu>` inside a `<BaseMenubar>` works the same. Every menu part (`Popup`, items, submenus, …) behaves as described for `BaseMenu`, with these differences:
|
|
1006
|
+
|
|
1007
|
+
- Triggers are items of the menubar (`role="menuitem"`), with one tab stop for the whole bar.
|
|
1008
|
+
- Popups open below a horizontal menubar's triggers and start-aligned (`side="bottom"`, `align="start"`), or beside a vertical one's.
|
|
1009
|
+
- The menubar's `modal` and `disabled` apply to every menu in it. `BaseMenu`'s own `modal` is ignored there.
|
|
1010
|
+
|
|
1011
|
+
| Part | Renders | Description |
|
|
1012
|
+
|------|---------|-------------|
|
|
1013
|
+
| `BaseMenubar` | `<div>` | The bar (`role="menubar"`, `flex`, `flex-col` when vertical). `data-orientation`, `data-modal`, `data-has-submenu-open` (one of its menus is open) |
|
|
1014
|
+
| `BaseMenubar.Menu` | nothing | `BaseMenu`, with all its parts (`BaseMenubar.Menu.Trigger`, `BaseMenubar.Menu.Popup`, …) |
|
|
1015
|
+
|
|
1016
|
+
`BaseMenubar` props:
|
|
1017
|
+
|
|
1018
|
+
| Prop | Type | Default | Description |
|
|
1019
|
+
|------|------|---------|-------------|
|
|
1020
|
+
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | The direction of the bar, and the arrow keys that move between its triggers |
|
|
1021
|
+
| `modal` | `boolean` | `true` | Locks page scroll and blocks outside pointer input while a menu is open |
|
|
1022
|
+
| `disabled` | `boolean` | `false` | Disables every menu (`data-disabled` on the triggers) |
|
|
1023
|
+
| `loopFocus` | `boolean` | `true` | Arrow keys wrap from the last trigger to the first |
|
|
1024
|
+
| `render` | `RenderProp<"div", MenubarState>` | | The state is `{ orientation, modal, hasSubmenuOpen }` |
|
|
1025
|
+
|
|
1026
|
+
### `@omer-x/tailwind-ui-kit/disclosure`
|
|
1027
|
+
|
|
1028
|
+
#### `BaseAccordion`
|
|
1029
|
+
|
|
1030
|
+
A Base UI [`Accordion`](https://base-ui.com/react/components/accordion): collapsible panels with headings. `Trigger` brings the heading around it, so each item needs three parts:
|
|
1031
|
+
|
|
1032
|
+
```tsx
|
|
1033
|
+
import { BaseAccordion } from "@omer-x/tailwind-ui-kit/disclosure";
|
|
1034
|
+
|
|
1035
|
+
<BaseAccordion className="rounded-md border">
|
|
1036
|
+
<BaseAccordion.Item value="shipping" className="border-b last:border-b-0">
|
|
1037
|
+
<BaseAccordion.Trigger className="px-5 py-4 data-panel-open:bg-blue-50" icon={<ChevronDown />} iconClassName="in-data-panel-open:rotate-180">
|
|
1038
|
+
Shipping
|
|
1039
|
+
</BaseAccordion.Trigger>
|
|
1040
|
+
<BaseAccordion.Panel>
|
|
1041
|
+
<div className="px-5 py-4">Orders ship within two days.</div>
|
|
1042
|
+
</BaseAccordion.Panel>
|
|
1043
|
+
</BaseAccordion.Item>
|
|
1044
|
+
</BaseAccordion>
|
|
1045
|
+
```
|
|
1046
|
+
|
|
1047
|
+
The parts are reached with dot notation (`BaseAccordion.Item`), which only works in client components. Build accordions in a `"use client"` file, e.g. your app's themed wrapper.
|
|
1048
|
+
|
|
1049
|
+
| Part | Renders | Description |
|
|
1050
|
+
|------|---------|-------------|
|
|
1051
|
+
| `BaseAccordion` | `<div>` | Groups the items and holds which ones are open |
|
|
1052
|
+
| `BaseAccordion.Item` | `<div>` | Groups a trigger with its panel |
|
|
1053
|
+
| `BaseAccordion.Trigger` | `<h3>` around a `<button>` | The heading that labels the panel, and the button that opens and closes it. A full-width flex row (`justify-between`) with a focus outline |
|
|
1054
|
+
| `BaseAccordion.Panel` | `<div>` | The item's content, with a height transition (shared with Collapsible). **Put padding on an element inside it**, so it can shrink to zero |
|
|
1055
|
+
|
|
1056
|
+
Every part takes `className` and `render`.
|
|
1057
|
+
|
|
1058
|
+
`BaseAccordion` props:
|
|
1059
|
+
|
|
1060
|
+
| Prop | Type | Default | Description |
|
|
1061
|
+
|------|------|---------|-------------|
|
|
1062
|
+
| `value` / `defaultValue` | `Value[]` | `[]` | Controlled / initial values of the open items |
|
|
1063
|
+
| `onValueChange` | `(value, event) => void` | | Called with the open items' values and the DOM event that changed them (`undefined` when changed from code) |
|
|
1064
|
+
| `multiple` | `boolean` | `false` | Lets several items be open at once |
|
|
1065
|
+
| `disabled` | `boolean` | `false` | Ignores user interaction on every item |
|
|
1066
|
+
| `keepMounted` | `boolean` | `false` | Keeps closed panels in the DOM |
|
|
1067
|
+
| `hiddenUntilFound` | `boolean` | `false` | Keeps closed panels hidden with `hidden="until-found"`, so the browser's page search finds and opens them. Overrides `keepMounted` |
|
|
1068
|
+
| `render` | `RenderProp<"div", AccordionState>` | | The state is `{ value, disabled }` |
|
|
1069
|
+
|
|
1070
|
+
`BaseAccordion.Item` props:
|
|
1071
|
+
|
|
1072
|
+
| Prop | Type | Default | Description |
|
|
1073
|
+
|------|------|---------|-------------|
|
|
1074
|
+
| `value` | `unknown` | generated | Identifies the item in the accordion's `value` |
|
|
1075
|
+
| `disabled` | `boolean` | `false` | Ignores user interaction on this item |
|
|
1076
|
+
| `onOpenChange` | `(open, event) => void` | | Called with the item's new state and the DOM event |
|
|
1077
|
+
| `render` | `RenderProp<"div", AccordionItemState>` | | The state is `{ open, disabled, index }` |
|
|
1078
|
+
|
|
1079
|
+
`BaseAccordion.Trigger` props:
|
|
1080
|
+
|
|
1081
|
+
| Prop | Type | Default | Description |
|
|
1082
|
+
|------|------|---------|-------------|
|
|
1083
|
+
| `icon` | `ReactNode` | | Shown after the text, in a `<span>` with a transform transition. Nothing is rendered without it |
|
|
1084
|
+
| `iconClassName` | `string` | | Classes for the icon's `<span>`, e.g. `in-data-panel-open:rotate-180` |
|
|
1085
|
+
| `headerClassName` | `string` | | Classes for the heading |
|
|
1086
|
+
| `renderHeader` | `RenderProp<"h3", AccordionItemState>` | | Replaces the heading, e.g. `<h2 />` for another level |
|
|
1087
|
+
| `render` | `RenderProp<"button", AccordionItemState>` | | Replaces the button |
|
|
1088
|
+
| `nativeButton` | `boolean` | `true` | Set to `false` when `render` doesn't render a `<button>` |
|
|
1089
|
+
|
|
1090
|
+
`BaseAccordion.Panel` takes `keepMounted` and `hiddenUntilFound` (overriding the accordion's) and `render` (`RenderProp<"div", AccordionPanelState>`, the item's state plus `transitionStatus`).
|
|
1091
|
+
|
|
1092
|
+
Data attributes: the item, heading and panel have `data-open` and `data-index`. The trigger has `data-panel-open`. Disabled parts have `data-disabled`. The panel has `data-starting-style` / `data-ending-style` during its transition.
|
|
1093
|
+
|
|
1094
|
+
#### `BaseCollapsible`
|
|
1095
|
+
|
|
1096
|
+
A Base UI [`Collapsible`](https://base-ui.com/react/components/collapsible): a panel shown or hidden by a button.
|
|
1097
|
+
|
|
1098
|
+
```tsx
|
|
1099
|
+
import { BaseCollapsible } from "@omer-x/tailwind-ui-kit/disclosure";
|
|
1100
|
+
|
|
1101
|
+
<BaseCollapsible>
|
|
1102
|
+
<BaseCollapsible.Trigger className="rounded-md px-3 py-1.5" icon={<ChevronDown />} iconClassName="in-data-panel-open:rotate-180">
|
|
1103
|
+
Recovery keys
|
|
1104
|
+
</BaseCollapsible.Trigger>
|
|
1105
|
+
<BaseCollapsible.Panel>
|
|
1106
|
+
<div className="p-4">alien-bean-pasta</div>
|
|
1107
|
+
</BaseCollapsible.Panel>
|
|
1108
|
+
</BaseCollapsible>
|
|
1109
|
+
```
|
|
1110
|
+
|
|
1111
|
+
Like `BaseAccordion`, the parts use dot notation, so build collapsibles in a `"use client"` file.
|
|
1112
|
+
|
|
1113
|
+
| Part | Renders | Description |
|
|
1114
|
+
|------|---------|-------------|
|
|
1115
|
+
| `BaseCollapsible` | `<div>` | Groups the trigger with the panel and holds whether it is open |
|
|
1116
|
+
| `BaseCollapsible.Trigger` | `<button>` | Opens and closes the panel. An inline flex row with a focus outline |
|
|
1117
|
+
| `BaseCollapsible.Panel` | `<div>` | The content, with a height transition (shared with Accordion). **Put padding on an element inside it**, so it can shrink to zero |
|
|
1118
|
+
|
|
1119
|
+
Every part takes `className` and `render`.
|
|
1120
|
+
|
|
1121
|
+
`BaseCollapsible` props:
|
|
1122
|
+
|
|
1123
|
+
| Prop | Type | Default | Description |
|
|
1124
|
+
|------|------|---------|-------------|
|
|
1125
|
+
| `open` / `defaultOpen` | `boolean` | `false` | Controlled / initial state of the panel |
|
|
1126
|
+
| `onOpenChange` | `(open, event) => void` | | Called with the new state and the DOM event that changed it (`undefined` when changed from code) |
|
|
1127
|
+
| `disabled` | `boolean` | `false` | Ignores user interaction |
|
|
1128
|
+
| `render` | `RenderProp<"div", CollapsibleState>` | | The state is `{ open, disabled }` |
|
|
1129
|
+
|
|
1130
|
+
`BaseCollapsible.Trigger` props:
|
|
1131
|
+
|
|
1132
|
+
| Prop | Type | Default | Description |
|
|
1133
|
+
|------|------|---------|-------------|
|
|
1134
|
+
| `icon` | `ReactNode` | | Shown after the text, in a `<span>` with a transform transition. Nothing is rendered without it |
|
|
1135
|
+
| `iconClassName` | `string` | | Classes for the icon's `<span>`, e.g. `in-data-panel-open:rotate-180` |
|
|
1136
|
+
| `render` | `RenderProp<"button", CollapsibleState>` | | Replaces the button |
|
|
1137
|
+
| `nativeButton` | `boolean` | `true` | Set to `false` when `render` doesn't render a `<button>` |
|
|
1138
|
+
|
|
1139
|
+
`BaseCollapsible.Panel` props:
|
|
1140
|
+
|
|
1141
|
+
| Prop | Type | Default | Description |
|
|
1142
|
+
|------|------|---------|-------------|
|
|
1143
|
+
| `keepMounted` | `boolean` | `false` | Keeps the closed panel in the DOM |
|
|
1144
|
+
| `hiddenUntilFound` | `boolean` | `false` | Keeps the closed panel hidden with `hidden="until-found"`, so the browser's page search finds and opens it. Overrides `keepMounted` |
|
|
1145
|
+
| `render` | `RenderProp<"div", CollapsiblePanelState>` | | The state is `{ open, disabled, transitionStatus }` |
|
|
1146
|
+
|
|
1147
|
+
Data attributes: the root and panel have `data-open` / `data-closed`. The trigger has `data-panel-open`. Disabled parts have `data-disabled`. The panel has `data-starting-style` / `data-ending-style` during its transition.
|
|
1148
|
+
|
|
1149
|
+
### `@omer-x/tailwind-ui-kit/display`
|
|
1150
|
+
|
|
1151
|
+
#### `BaseAvatar`
|
|
1152
|
+
|
|
1153
|
+
A Base UI [`Avatar`](https://base-ui.com/react/components/avatar): a profile picture with a fallback (initials or an icon) shown while it is missing, loading or broken.
|
|
1154
|
+
|
|
1155
|
+
```tsx
|
|
1156
|
+
import { BaseAvatar } from "@omer-x/tailwind-ui-kit/display";
|
|
1157
|
+
|
|
1158
|
+
<BaseAvatar size="sm" className="bg-neutral-200 font-medium">
|
|
1159
|
+
<BaseAvatar.Image src={user.photoUrl} alt={user.name} />
|
|
1160
|
+
<BaseAvatar.Fallback>{initials}</BaseAvatar.Fallback>
|
|
1161
|
+
</BaseAvatar>
|
|
1162
|
+
```
|
|
1163
|
+
|
|
1164
|
+
The parts are reached with dot notation (`BaseAvatar.Image`), which only works in client components. Build avatars in a `"use client"` file, e.g. your app's themed wrapper.
|
|
1165
|
+
|
|
1166
|
+
| Part | Renders | Description |
|
|
1167
|
+
|------|---------|-------------|
|
|
1168
|
+
| `BaseAvatar` | `<span>` | A round, centered, `overflow-hidden` box of the given size. Make it square with `className="rounded-md"` |
|
|
1169
|
+
| `BaseAvatar.Image` | `<img>` | The picture, covering the box (`size-full object-cover`). Rendered only once it has loaded |
|
|
1170
|
+
| `BaseAvatar.Fallback` | `<span>` | Fills the box and centers its content. Rendered until the image has loaded |
|
|
1171
|
+
|
|
1172
|
+
Every part takes `className` and `render`.
|
|
1173
|
+
|
|
1174
|
+
`BaseAvatar` props:
|
|
1175
|
+
|
|
1176
|
+
| Prop | Type | Default | Description |
|
|
1177
|
+
|------|------|---------|-------------|
|
|
1178
|
+
| `size` | `"lg" \| "md" \| "sm" \| "xs"` | `"md"` | `size-12 text-lg` / `size-10 text-base` / `size-8 text-sm` / `size-6 text-xs` |
|
|
1179
|
+
| `render` | `RenderProp<"span", AvatarState>` | | The state is `{ imageLoadingStatus }`: `"idle"`, `"loading"`, `"loaded"` or `"error"` |
|
|
1180
|
+
|
|
1181
|
+
`BaseAvatar.Image` props:
|
|
1182
|
+
|
|
1183
|
+
| Prop | Type | Default | Description |
|
|
1184
|
+
|------|------|---------|-------------|
|
|
1185
|
+
| `onLoadingStatusChange` | `(status) => void` | | Called when the image starts loading, loads or fails |
|
|
1186
|
+
| `keepMounted` | `boolean` | `false` | Keeps the `<img>` in the DOM and loads it in place instead of preloading it. Use it for `loading="lazy"` or an optimized image component (`render={<NextImage … />}`). Hide it until it loads with `data-loading:invisible data-error:hidden` |
|
|
1187
|
+
| `render` | `RenderProp<"img", AvatarImageState>` | | The avatar's state plus `transitionStatus` |
|
|
1188
|
+
|
|
1189
|
+
`BaseAvatar.Fallback` props:
|
|
1190
|
+
|
|
1191
|
+
| Prop | Type | Default | Description |
|
|
1192
|
+
|------|------|---------|-------------|
|
|
1193
|
+
| `delay` | `number` | `0` | Milliseconds to wait before showing, so a fast image doesn't flash it |
|
|
1194
|
+
| `render` | `RenderProp<"span", AvatarState>` | | |
|
|
1195
|
+
|
|
1196
|
+
Data attributes: the image has `data-starting-style` / `data-ending-style` while it appears and disappears, so it can fade in (`transition-opacity data-starting-style:opacity-0`). With `keepMounted`, it has `data-loading` while loading and `data-error` when it failed.
|
|
1197
|
+
|
|
1198
|
+
#### `BaseMeter`
|
|
1199
|
+
|
|
1200
|
+
A Base UI [`Meter`](https://base-ui.com/react/components/meter): a graphical display of a numeric value within a known range, like disk usage or battery level. It renders a `<div role="meter">` with the label and the value on top and the track below (`flex flex-wrap`). For a task's completion, use a progress bar instead.
|
|
1201
|
+
|
|
1202
|
+
```tsx
|
|
1203
|
+
import { BaseMeter } from "@omer-x/tailwind-ui-kit/display";
|
|
1204
|
+
|
|
1205
|
+
<BaseMeter
|
|
1206
|
+
value={usedGb}
|
|
1207
|
+
max={quotaGb}
|
|
1208
|
+
label="Storage used"
|
|
1209
|
+
showValue
|
|
1210
|
+
trackClassName="bg-neutral-200"
|
|
1211
|
+
indicatorClassName="bg-blue-600"
|
|
1212
|
+
/>
|
|
1213
|
+
```
|
|
1214
|
+
|
|
1215
|
+
| Prop | Type | Default | Description |
|
|
1216
|
+
|------|------|---------|-------------|
|
|
1217
|
+
| `value` | `number` | | The current value, clamped to `min`…`max` |
|
|
1218
|
+
| `min` / `max` | `number` | `0` / `100` | The range |
|
|
1219
|
+
| `format` | `Intl.NumberFormatOptions` | | How the value is formatted. Without it, it is shown as a percentage of the range |
|
|
1220
|
+
| `locale` | `Intl.LocalesArgument` | runtime locale | The locale used to format the value |
|
|
1221
|
+
| `getAriaValueText` | `(formattedValue, value) => string` | | What screen readers announce for the value |
|
|
1222
|
+
| `label` | `ReactNode` | | The visible name. It labels the meter. Without it, nothing is rendered for it: use `aria-label` |
|
|
1223
|
+
| `labelClassName` | `string` | | Classes for the label `<span>` (default `grow`) |
|
|
1224
|
+
| `renderLabel` | `RenderProp<"span", MeterState>` | | Render another element for the label |
|
|
1225
|
+
| `showValue` | `boolean \| (formattedValue, value) => ReactNode` | `false` | Shows the value as text next to the label. A function shows something else, e.g. `` (f) => `${f} of 5 kWh` `` |
|
|
1226
|
+
| `valueClassName` | `string` | | Classes for the value `<span>` (default `ms-auto tabular-nums`). It is `aria-hidden`: screen readers read the meter's value |
|
|
1227
|
+
| `renderValue` | `RenderProp<"span", MeterState>` | | Render another element for the value |
|
|
1228
|
+
| `size` | `"lg" \| "md" \| "sm" \| "xs"` | `"md"` | The track's height: `h-3` / `h-2` / `h-1.5` / `h-1` |
|
|
1229
|
+
| `trackClassName` | `string` | | Classes for the track `<div>` (default `relative block w-full overflow-hidden rounded-full`) |
|
|
1230
|
+
| `renderTrack` | `RenderProp<"div", MeterState>` | | Render another element for the track |
|
|
1231
|
+
| `indicatorClassName` | `string` | | Classes for the indicator `<div>`, the filled part. Its width is set inline and transitions (`transition-[width] duration-500`) |
|
|
1232
|
+
| `renderIndicator` | `RenderProp<"div", MeterState>` | | Render another element for the indicator |
|
|
1233
|
+
| `children` | `ReactNode` | | Lays out the parts yourself (see below). The built-in label, value and track aren't rendered then |
|
|
1234
|
+
| `className` | `string` | | Classes for the root `<div>` |
|
|
1235
|
+
| `render` | `RenderProp<"div", MeterState>` | | Render another element for the root |
|
|
1236
|
+
| …rest | | | `<div>` props |
|
|
1237
|
+
|
|
1238
|
+
To place the parts yourself, e.g. the value inside the track, pass them as children. Dot notation only works in client components.
|
|
1239
|
+
|
|
1240
|
+
```tsx
|
|
1241
|
+
<BaseMeter value={24}>
|
|
1242
|
+
<BaseMeter.Label className="sr-only">Upload quota</BaseMeter.Label>
|
|
1243
|
+
<BaseMeter.Track className="h-5 bg-neutral-200">
|
|
1244
|
+
<BaseMeter.Indicator className="bg-blue-600" />
|
|
1245
|
+
<BaseMeter.Value className="absolute inset-0 flex items-center justify-center" />
|
|
1246
|
+
</BaseMeter.Track>
|
|
1247
|
+
</BaseMeter>
|
|
1248
|
+
```
|
|
1249
|
+
|
|
1250
|
+
| Part | Renders | Props |
|
|
1251
|
+
|------|---------|-------|
|
|
1252
|
+
| `BaseMeter.Label` | `<span>` | `className`, `render` |
|
|
1253
|
+
| `BaseMeter.Track` | `<div>` | `size`, `className`, `render` |
|
|
1254
|
+
| `BaseMeter.Indicator` | `<div>` | `className`, `render` |
|
|
1255
|
+
| `BaseMeter.Value` | `<span>` | `className`, `render`, `children?: (formattedValue, value) => ReactNode` |
|
|
1256
|
+
|
|
1257
|
+
Render functions get `MeterState`, which is empty for now. The meter sets no data attributes: style value ranges in your wrapper (e.g. pick the indicator color from `value`).
|
|
1258
|
+
|
|
1259
|
+
### `@omer-x/tailwind-ui-kit/layout`
|
|
1260
|
+
|
|
1261
|
+
#### `Container`
|
|
1262
|
+
|
|
1263
|
+
Tailwind's `container`, centered with horizontal padding (`container mx-auto px-3`). Accepts all `<div>` props.
|
|
1264
|
+
|
|
1265
|
+
### `@omer-x/tailwind-ui-kit/page`
|
|
1266
|
+
|
|
1267
|
+
#### `Pagination`
|
|
1268
|
+
|
|
1269
|
+
Pagination inside a `<nav>`. It shows previous/next arrows, the first and last pages, `siblings` pages on each side of the current one, and ellipses for gaps. It only decides which pages to show: **your app builds each link**, so typed routes stay type-safe.
|
|
1270
|
+
|
|
1271
|
+
| Prop | Type | Description |
|
|
1272
|
+
|------|------|-------------|
|
|
1273
|
+
| `totalItems` | `number` | Total number of items |
|
|
1274
|
+
| `itemsPerPage` | `number` | Items per page |
|
|
1275
|
+
| `currentPage` | `number` | Current page, starting at 1 |
|
|
1276
|
+
| `siblings` | `number` | Pages shown on each side of the current one. Default `2`. `0` gives `« 1 … [50] … 100 »` |
|
|
1277
|
+
| `renderLink` | `(page: number, props: PaginationLinkProps) => ReactElement` | Builds the link to `page`. Spread `props` (`children`, `aria-current="page"` on the current page, `aria-label` on the arrows) onto it. It can also be a `<button onClick>` for pagination without URLs |
|
|
1278
|
+
| `renderButton` | render prop | Renders a disabled arrow (a disabled `<button>`) |
|
|
1279
|
+
| `renderEllipsis` | render prop | Renders an ellipsis (a `<span aria-hidden>`) |
|
|
1280
|
+
| `aria-label` | `string` | The navigation's label. Default `"Pagination"` |
|
|
1281
|
+
| `previousLabel` / `nextLabel` | `string` | The arrows' accessible names. Default `"Previous page"` / `"Next page"` |
|
|
1282
|
+
| `previousContent` / `nextContent` | `ReactNode` | The arrows' content, e.g. icons. Default `«` / `»` |
|
|
1283
|
+
| `className` | `string` | Classes for the `<nav>`. Default `flex gap-3` |
|
|
1284
|
+
| …rest | `<nav>` props | |
|
|
1285
|
+
|
|
1286
|
+
Style the current page with `aria-[current=page]:`.
|
|
1287
|
+
|
|
1288
|
+
```tsx
|
|
1289
|
+
import { Link } from "@/i18n/navigation";
|
|
1290
|
+
|
|
1291
|
+
<Pagination
|
|
1292
|
+
totalItems={total}
|
|
1293
|
+
itemsPerPage={20}
|
|
1294
|
+
currentPage={page}
|
|
1295
|
+
renderLink={(page, props) => (
|
|
1296
|
+
<Link {...props} href={{ pathname: "/products", query: { ...searchParams, page } }} className="px-3 py-1 aria-[current=page]:font-bold" />
|
|
1297
|
+
)}
|
|
1298
|
+
renderButton={<button className="px-3 py-1 disabled:opacity-50" />}
|
|
1299
|
+
renderEllipsis={<span className="px-3 py-1" />}
|
|
1300
|
+
/>
|
|
1301
|
+
```
|
|
1302
|
+
|
|
1303
|
+
#### `usePagination`
|
|
1304
|
+
|
|
1305
|
+
The logic behind `Pagination`, for a completely custom layout. It holds no state, so it also works in server components.
|
|
1306
|
+
|
|
1307
|
+
```tsx
|
|
1308
|
+
const items = usePagination({ totalItems, itemsPerPage, currentPage, siblings: 1 });
|
|
1309
|
+
// [
|
|
1310
|
+
// { type: "previous", page: 9, disabled: false },
|
|
1311
|
+
// { type: "page", page: 1, current: false },
|
|
1312
|
+
// { type: "ellipsis" },
|
|
1313
|
+
// { type: "page", page: 10, current: true },
|
|
1314
|
+
// …
|
|
1315
|
+
// { type: "next", page: 11, disabled: false },
|
|
1316
|
+
// ]
|
|
1317
|
+
```
|
|
1318
|
+
|
|
1319
|
+
### `@omer-x/tailwind-ui-kit/table`
|
|
1320
|
+
|
|
1321
|
+
#### `TableCell`
|
|
1322
|
+
|
|
1323
|
+
A `<td>` that accepts all `<td>` props (`colSpan`, `rowSpan`, `title`, event handlers, …).
|
|
1324
|
+
|
|
1325
|
+
| Prop | Type | Description |
|
|
1326
|
+
|------|------|-------------|
|
|
1327
|
+
| `overflow` | `"wrap" \| "truncate"` | `"wrap"` (default) is a normal `<td>`. `"truncate"` keeps the content on one line and cuts it with an ellipsis instead of widening the column. Exposed as `data-overflow`. |
|
|
1328
|
+
|
|
1329
|
+
```tsx
|
|
1330
|
+
<tr>
|
|
1331
|
+
<TableCell overflow="truncate" className="max-w-48">{product.description}</TableCell>
|
|
1332
|
+
<TableCell overflow={expanded ? "wrap" : "truncate"}>{product.notes}</TableCell>
|
|
1333
|
+
</tr>
|
|
1334
|
+
```
|
|
1335
|
+
|
|
1336
|
+
#### `Tdx` (deprecated)
|
|
1337
|
+
|
|
1338
|
+
Use `<TableCell overflow="truncate">` instead. `Tdx` is kept as an alias for it and accepts the same `<td>` props.
|
|
1339
|
+
|
|
1340
|
+
### `@omer-x/tailwind-ui-kit/elements`
|
|
1341
|
+
|
|
1342
|
+
Bare HTML elements whose only addition is a `render` prop (`RenderProp<Tag>`): `JustAnchor` (`<a>`), `JustButton` (`<button>`), `JustDiv` (`<div>`), `JustInput` (`<input>`) and `JustSpan` (`<span>`). They take all props of their element and add no styles.
|
|
1343
|
+
|
|
1344
|
+
```tsx
|
|
1345
|
+
import { JustDiv } from "@omer-x/tailwind-ui-kit/elements";
|
|
1346
|
+
|
|
1347
|
+
<JustDiv className="p-4" render={<section />}>…</JustDiv>
|
|
1348
|
+
```
|
|
1349
|
+
|
|
1350
|
+
### `@omer-x/tailwind-ui-kit/hooks`
|
|
1351
|
+
|
|
1352
|
+
#### `useRipples`
|
|
1353
|
+
|
|
1354
|
+
The ripple effect of `BaseButton` and `BaseToggle`, for your own clickable elements. Returns `[handleClick, ripples]`: call `handleClick` from `onClick` and render `ripples` inside the element.
|
|
1355
|
+
|
|
1356
|
+
- The element needs `relative` (the ripples are positioned absolutely and clipped to its rounded corners).
|
|
1357
|
+
- Ripples use `bg-current`, so they follow the text color. They start from the center on keyboard clicks and are hidden when the user prefers reduced motion.
|
|
1358
|
+
- It imports a CSS module (see [Setup](#2-your-bundler-must-support-css-modules)).
|
|
1359
|
+
|
|
1360
|
+
```tsx
|
|
1361
|
+
import { useRipples } from "@omer-x/tailwind-ui-kit/hooks";
|
|
1362
|
+
|
|
1363
|
+
const Chip = ({ children, onClick, ...props }: ComponentProps<"button">) => {
|
|
1364
|
+
const [handleRipple, ripples] = useRipples();
|
|
1365
|
+
return (
|
|
1366
|
+
<button
|
|
1367
|
+
{...props}
|
|
1368
|
+
className="relative rounded-full px-3 py-1"
|
|
1369
|
+
onClick={event => { handleRipple(event); onClick?.(event); }}
|
|
1370
|
+
>
|
|
1371
|
+
{children}
|
|
1372
|
+
{ripples}
|
|
1373
|
+
</button>
|
|
1374
|
+
);
|
|
1375
|
+
};
|
|
1376
|
+
```
|
|
1377
|
+
|
|
1378
|
+
### `@omer-x/tailwind-ui-kit/utils`
|
|
1379
|
+
|
|
1380
|
+
Helpers for building your own components on the kit, so you never import Base UI.
|
|
1381
|
+
|
|
1382
|
+
#### `RenderProp`
|
|
1383
|
+
|
|
1384
|
+
The type of every `render` and `render*` prop: `RenderProp<Tag, State>` is an element, or a function `(props, state) => ReactElement`. `props` are the props of `Tag` (spread them onto your element) and `state` is the component's state (`object` when it has none).
|
|
1385
|
+
|
|
1386
|
+
#### `useRender`
|
|
1387
|
+
|
|
1388
|
+
Renders an element that can be replaced through a `render` prop, with your props merged into it.
|
|
1389
|
+
|
|
1390
|
+
| Option | Type | Description |
|
|
1391
|
+
|--------|------|-------------|
|
|
1392
|
+
| `defaultTagName` | `keyof JSX.IntrinsicElements` | The element rendered when `render` is missing |
|
|
1393
|
+
| `render` | `RenderProp<Tag, State>` | The `render` prop of your component |
|
|
1394
|
+
| `props` | props of `Tag` | Merged into the rendered element |
|
|
1395
|
+
| `state` | `State` | Passed to a `render` function, and set as data attributes (`{ active: true }` becomes `data-active`) |
|
|
1396
|
+
|
|
1397
|
+
```tsx
|
|
1398
|
+
import { type RenderProp, useRender } from "@omer-x/tailwind-ui-kit/utils";
|
|
1399
|
+
|
|
1400
|
+
type BadgeProps = ComponentProps<"span"> & {
|
|
1401
|
+
render?: RenderProp<"span">,
|
|
1402
|
+
};
|
|
1403
|
+
|
|
1404
|
+
const Badge = ({ render, className, ...props }: BadgeProps) => useRender({
|
|
1405
|
+
defaultTagName: "span",
|
|
1406
|
+
render,
|
|
1407
|
+
props: { ...props, className: cn("rounded px-2 text-xs", className) },
|
|
1408
|
+
});
|
|
1409
|
+
```
|
|
1410
|
+
|
|
1411
|
+
#### `mergeProps`
|
|
1412
|
+
|
|
1413
|
+
Merges props for one element, later ones winning. Event handlers are all called (later ones first), `className`s are joined and `style`s are merged. `ref` is not merged.
|
|
1414
|
+
|
|
1415
|
+
```tsx
|
|
1416
|
+
useRender({ defaultTagName: "div", render, props: mergeProps<"div">({ className: "p-4", onClick: track }, props) });
|
|
1417
|
+
```
|
|
1418
|
+
|
|
1419
|
+
#### `cn` (deprecated)
|
|
1420
|
+
|
|
1421
|
+
Kept for backward compatibility. Import from the [`cn`](https://www.npmjs.com/package/cn) package directly instead:
|
|
1422
|
+
|
|
1423
|
+
```ts
|
|
1424
|
+
import { cn } from "cn";
|
|
1425
|
+
```
|
|
1426
|
+
|
|
1427
|
+
## Development
|
|
1428
|
+
|
|
1429
|
+
```sh
|
|
1430
|
+
npm run dev # Storybook on http://localhost:3000
|
|
1431
|
+
npm test # Vitest with coverage: unit tests and every story (headless Chromium)
|
|
1432
|
+
npm run lint
|
|
1433
|
+
npm run build # tsdown → dist/
|
|
1434
|
+
```
|
|
1435
|
+
|
|
1436
|
+
Contributor and agent guidelines live in [`AGENTS.md`](./AGENTS.md) and [`ai/`](./ai).
|
|
1437
|
+
|
|
1438
|
+
## License
|
|
1439
|
+
|
|
1440
|
+
MIT
|