@omer-x/tailwind-ui-kit 1.0.0-alpha.20 → 1.0.0-alpha.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/MIGRATION.md +37 -0
  2. package/README.md +57 -11
  3. package/dist/components/button/BaseButton.d.ts +13 -2
  4. package/dist/components/button/index.d.ts +2 -2
  5. package/dist/components/dialog/AnimatedPopup.d.ts +6 -2
  6. package/dist/components/dialog/AnimatedPopup.js +1 -2
  7. package/dist/components/dialog/DialogBackdrop.d.ts +6 -2
  8. package/dist/components/dialog/DialogBackdrop.js +1 -2
  9. package/dist/components/dialog/DialogTitle.d.ts +6 -2
  10. package/dist/components/dialog/DialogTitle.js +1 -2
  11. package/dist/components/elements/JustAnchor.d.ts +6 -2
  12. package/dist/components/elements/JustAnchor.js +1 -1
  13. package/dist/components/elements/JustButton.d.ts +6 -2
  14. package/dist/components/elements/JustButton.js +1 -1
  15. package/dist/components/elements/JustDiv.d.ts +6 -2
  16. package/dist/components/elements/JustDiv.js +1 -1
  17. package/dist/components/elements/JustInput.d.ts +6 -2
  18. package/dist/components/elements/JustInput.js +1 -1
  19. package/dist/components/elements/JustSpan.d.ts +6 -2
  20. package/dist/components/elements/JustSpan.js +1 -1
  21. package/dist/components/form/BaseFormField.d.ts +15 -5
  22. package/dist/components/form/BaseSwitch.d.ts +30 -5
  23. package/dist/components/form/BaseSwitch.js +2 -1
  24. package/dist/components/form/ClientSideError.d.ts +3 -3
  25. package/dist/components/form/ServerSideError.d.ts +3 -3
  26. package/dist/components/form/index.d.ts +3 -3
  27. package/dist/utils/index.d.ts +2 -1
  28. package/dist/utils/index.js +3 -1
  29. package/dist/utils/render.d.ts +31 -0
  30. package/dist/utils/render.js +24 -0
  31. package/package.json +2 -1
package/MIGRATION.md CHANGED
@@ -5,6 +5,32 @@ Each breaking change has its own section with before / after code and the reason
5
5
  - **Mechanical:** a rename or a type change. The compiler points you to every place to fix.
6
6
  - **Check manually:** behavior changed. Code still compiles, but may act differently.
7
7
 
8
+ ## Types
9
+
10
+ ### Props no longer name Base UI types (mechanical, #55)
11
+
12
+ Component props are now native element props plus the kit's own props, and `render` / `render*` props use the kit's `RenderProp` type. This changes three things:
13
+
14
+ - `className` and `style` on `BaseButton` and `BaseSwitch` no longer take a function of the state. Use data attributes instead:
15
+
16
+ ```diff
17
+ - className={state => (state.disabled ? "opacity-50" : "")}
18
+ + className="data-disabled:opacity-50"
19
+ ```
20
+
21
+ - Event handlers get React's event types, so `event.preventBaseUIHandler()` is no longer typed.
22
+ - Types like `ComponentProps<typeof Button>` or `useRender.ComponentProps<"div">` from `@base-ui/react` are replaced by the kit's types: `RenderProp`, `ButtonState`, `SwitchState` and `FieldState`. To build your own component with a `render` prop, use `useRender` and `mergeProps` from `@omer-x/tailwind-ui-kit/utils`:
23
+
24
+ ```diff
25
+ - import { useRender } from "@base-ui/react/use-render";
26
+ - import { mergeProps } from "@base-ui/react/merge-props";
27
+ + import { mergeProps, type RenderProp, useRender } from "@omer-x/tailwind-ui-kit/utils";
28
+ ```
29
+
30
+ The kit's `useRender` takes `defaultTagName`, `render`, `props` and `state`.
31
+
32
+ **Why:** Base UI is an implementation detail. Types that named it tied apps to it, and replacing it would have broken them.
33
+
8
34
  ## Button
9
35
 
10
36
  ### Font Awesome is removed: pass a `spinner` (mechanical, #5, #8)
@@ -78,6 +104,17 @@ Remove any `pending={isSubmitting}` you wired up by hand for submit buttons of `
78
104
 
79
105
  ## Form
80
106
 
107
+ ### `BaseSwitch`: `onCheckedChange` gets the DOM event (mechanical, #55)
108
+
109
+ The second argument of `onCheckedChange` is now the DOM `Event`, not Base UI's event details. `eventDetails.cancel()` is gone: to refuse a change, control the switch with `checked`.
110
+
111
+ ```diff
112
+ - onCheckedChange={(checked, details) => track(details.event)}
113
+ + onCheckedChange={(checked, event) => track(event)}
114
+ ```
115
+
116
+ **Why:** the event details were a Base UI type (#55).
117
+
81
118
  ### Fields are validated in the browser (check manually, #65)
82
119
 
83
120
  `BaseFormField`'s constraints (`required`, `minLength`, `max`, … from the schema) used to block nothing: every submit reached the action. Now an invalid field blocks the submit and shows its browser error (`valueMissing`, `tooShort`, …) through `formatMessage`, so add those codes to your translations.
package/README.md CHANGED
@@ -9,12 +9,12 @@ Colorless React components built on [Base UI](https://base-ui.com) and [Tailwind
9
9
  Read these before using or changing any component.
10
10
 
11
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, exactly like [Base UI's `render`](https://base-ui.com/react/handbook/composition).
13
- 3. **Style states with data attributes.** Base UI sets attributes such as `data-disabled`, `data-checked` and `data-starting-style`. Target them with Tailwind variants (`data-disabled:opacity-50`).
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
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
15
  5. **Client components.** All components are client components.
16
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. If something you need isn't covered yet, add it to the kit instead of importing `@base-ui/react` (progress is tracked in #56).
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
18
 
19
19
  ## Installation
20
20
 
@@ -96,7 +96,10 @@ A Base UI [`Button`](https://base-ui.com/react/components/button) with sizes, a
96
96
  | `pending` | `boolean` | `false` | Shows the spinner, blocks clicks and keeps the button focusable |
97
97
  | `spinner` | `ReactNode` | | Shown while pending. Without it nothing is shown, and `data-pending` still marks the state |
98
98
  | `iconOnly` | `boolean` | `false` | A square button for a single icon. Requires `aria-label`. While pending, the spinner replaces the icon |
99
- | …rest | Base UI `Button` props | | `disabled`, `focusableWhenDisabled`, `type`, `onClick`, `render`, `nativeButton`, … |
99
+ | `render` | `RenderProp<"button", ButtonState>` | | Render another element, e.g. your app's link. The state is `{ disabled }` |
100
+ | `nativeButton` | `boolean` | `true` | Set to `false` when `render` doesn't render a `<button>` |
101
+ | `focusableWhenDisabled` | `boolean` | `false` | Keeps a disabled button in the tab order |
102
+ | …rest | `<button>` props | | `disabled`, `type`, `onClick`, … |
100
103
 
101
104
  - **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`.
102
105
  - Children are laid out with flex and a size-based gap, so icons and text line up without extra markup.
@@ -108,7 +111,7 @@ A Base UI [`Button`](https://base-ui.com/react/components/button) with sizes, a
108
111
  <Button iconOnly aria-label="Delete"><TrashIcon /></Button>
109
112
  ```
110
113
 
111
- **Links that look like buttons:** render your app's own link (so typed routes keep working) and tell Base UI it isn't a native button:
114
+ **Links that look like buttons:** render your app's own link (so typed routes keep working) and set `nativeButton={false}`:
112
115
 
113
116
  ```tsx
114
117
  <Button render={<Link href="/products/new" />} nativeButton={false}>New product</Button>
@@ -135,10 +138,14 @@ A Base UI [`Switch`](https://base-ui.com/react/components/switch) with its label
135
138
  | Prop | Type | Default | Description |
136
139
  |------|------|---------|-------------|
137
140
  | `label` | `ReactNode` | | Text next to the switch (required) |
138
- | `renderThumb` | render prop | | Style the thumb |
141
+ | `renderThumb` | `RenderProp<"span", SwitchState>` | | Style the thumb |
139
142
  | `value` | `string` | `"true"` | Submitted when checked |
140
143
  | `uncheckedValue` | `string` | `"false"` | Submitted when unchecked |
141
- | …rest | Base UI `Switch.Root` props | | `name`, `checked`, `defaultChecked`, `onCheckedChange`, … |
144
+ | `onCheckedChange` | `(checked: boolean, event: Event) => void` | | Called with the new state and the DOM event that changed it |
145
+ | `render` | `RenderProp<"span", SwitchState>` | | Render another element for the track |
146
+ | …rest | | | `name`, `checked`, `defaultChecked`, `disabled`, `readOnly`, `required`, `form`, `inputRef`, `nativeButton`, and `<span>` props |
147
+
148
+ Render functions get `SwitchState`: `{ checked, disabled, readOnly, required }`.
142
149
 
143
150
  ```tsx
144
151
  <BaseSwitch
@@ -273,12 +280,12 @@ A Base UI [`Field`](https://base-ui.com/react/components/field) made of a label,
273
280
  | `name` | `string` | Field name. It also links the server errors in `fieldErrors[name]` |
274
281
  | `schema` | `ZodType` | Source of the HTML constraints applied to the control |
275
282
  | `label` | `string` | Label text. No label is rendered when omitted |
276
- | `control` | render prop | 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 |
277
- | `renderLabel` | render prop | Style the label |
278
- | `renderErrors` | render prop | Style client-side and server-side error messages. Each error is its own element |
283
+ | `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 |
284
+ | `renderLabel` | `RenderProp<"label", FieldState>` | Style the label |
285
+ | `renderErrors` | `RenderProp<"div", FieldState>` | Style client-side and server-side error messages. Each error is its own element |
279
286
  | `className` | `string` | Classes for the field root (a flex column by default) |
280
287
 
281
- Error texts come from `BaseForm`'s `formatMessage`.
288
+ 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).
282
289
 
283
290
  ```tsx
284
291
  <BaseFormField
@@ -390,6 +397,45 @@ A `<td>` whose content is cut to a single line with an ellipsis instead of widen
390
397
 
391
398
  ### `@omer-x/tailwind-ui-kit/utils`
392
399
 
400
+ Helpers for building your own components on the kit, so you never import Base UI.
401
+
402
+ #### `RenderProp`
403
+
404
+ 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).
405
+
406
+ #### `useRender`
407
+
408
+ Renders an element that can be replaced through a `render` prop, with your props merged into it.
409
+
410
+ | Option | Type | Description |
411
+ |--------|------|-------------|
412
+ | `defaultTagName` | `keyof JSX.IntrinsicElements` | The element rendered when `render` is missing |
413
+ | `render` | `RenderProp<Tag, State>` | The `render` prop of your component |
414
+ | `props` | props of `Tag` | Merged into the rendered element |
415
+ | `state` | `State` | Passed to a `render` function, and set as data attributes (`{ active: true }` becomes `data-active`) |
416
+
417
+ ```tsx
418
+ import { type RenderProp, useRender } from "@omer-x/tailwind-ui-kit/utils";
419
+
420
+ type BadgeProps = ComponentProps<"span"> & {
421
+ render?: RenderProp<"span">,
422
+ };
423
+
424
+ const Badge = ({ render, className, ...props }: BadgeProps) => useRender({
425
+ defaultTagName: "span",
426
+ render,
427
+ props: { ...props, className: cn("rounded px-2 text-xs", className) },
428
+ });
429
+ ```
430
+
431
+ #### `mergeProps`
432
+
433
+ 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.
434
+
435
+ ```tsx
436
+ useRender({ defaultTagName: "div", render, props: mergeProps<"div">({ className: "p-4", onClick: track }, props) });
437
+ ```
438
+
393
439
  #### `cn` (deprecated)
394
440
 
395
441
  Kept for backward compatibility. Import from the [`cn`](https://www.npmjs.com/package/cn) package directly instead:
@@ -1,6 +1,17 @@
1
- import { Button } from "@base-ui/react/button";
2
1
  import { ComponentProps, ReactNode } from "react";
3
- type CommonProps = ComponentProps<typeof Button> & {
2
+ import { RenderProp } from "../../utils/render.js";
3
+ export type ButtonState = {
4
+ disabled: boolean;
5
+ };
6
+ type CommonProps = ComponentProps<"button"> & {
7
+ render?: RenderProp<"button", ButtonState>;
8
+ /**
9
+ * Set to `false` when `render` doesn't render a `<button>` (e.g. a link),
10
+ * so the button gets the attributes and keyboard handling a non-button element needs.
11
+ */
12
+ nativeButton?: boolean;
13
+ /** Keeps a disabled button focusable, so it stays in the tab order. */
14
+ focusableWhenDisabled?: boolean;
4
15
  size?: "lg" | "md" | "sm" | "xs";
5
16
  /**
6
17
  * Shows the spinner, blocks clicks and keeps the button focusable.
@@ -1,2 +1,2 @@
1
- import BaseButton from "./BaseButton.js";
2
- export { BaseButton };
1
+ import BaseButton, { ButtonState } from "./BaseButton.js";
2
+ export { BaseButton, type ButtonState };
@@ -1,3 +1,7 @@
1
- import { useRender } from "@base-ui/react/use-render";
2
- declare const AnimatedPopup: ({ render, className, ...otherProps }: useRender.ComponentProps<"div">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
1
+ import { RenderProp } from "../../utils/render.js";
2
+ import { ComponentProps } from "react";
3
+ type AnimatedPopupProps = ComponentProps<"div"> & {
4
+ render?: RenderProp<"div">;
5
+ };
6
+ declare const AnimatedPopup: ({ render, className, ...otherProps }: AnimatedPopupProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
7
  export { AnimatedPopup as default };
@@ -1,6 +1,5 @@
1
- import { mergeProps } from "@base-ui/react/merge-props";
2
- import { useRender } from "@base-ui/react/use-render";
3
1
  import { cn } from "cn";
2
+ import { mergeProps, useRender } from "../../utils/render.js";
4
3
  // src/components/dialog/AnimatedPopup.tsx
5
4
  const AnimatedPopup = ({ render, className, ...otherProps }) => useRender({
6
5
  defaultTagName: "div",
@@ -1,3 +1,7 @@
1
- import { useRender } from "@base-ui/react/use-render";
2
- declare const DialogBackdrop: ({ render, ...otherProps }: useRender.ComponentProps<"div">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
1
+ import { RenderProp } from "../../utils/render.js";
2
+ import { ComponentProps } from "react";
3
+ type DialogBackdropProps = ComponentProps<"div"> & {
4
+ render?: RenderProp<"div">;
5
+ };
6
+ declare const DialogBackdrop: ({ render, ...otherProps }: DialogBackdropProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
7
  export { DialogBackdrop as default };
@@ -1,6 +1,5 @@
1
- import { mergeProps } from "@base-ui/react/merge-props";
2
- import { useRender } from "@base-ui/react/use-render";
3
1
  import { cn } from "cn";
2
+ import { mergeProps, useRender } from "../../utils/render.js";
4
3
  // src/components/dialog/DialogBackdrop.tsx
5
4
  const DialogBackdrop = ({ render, ...otherProps }) => useRender({
6
5
  defaultTagName: "div",
@@ -1,3 +1,7 @@
1
- import { useRender } from "@base-ui/react/use-render";
2
- declare const DialogTitle: ({ render, ...otherProps }: useRender.ComponentProps<"h2">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
1
+ import { RenderProp } from "../../utils/render.js";
2
+ import { ComponentProps } from "react";
3
+ type DialogTitleProps = ComponentProps<"h2"> & {
4
+ render?: RenderProp<"h2">;
5
+ };
6
+ declare const DialogTitle: ({ render, ...otherProps }: DialogTitleProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
7
  export { DialogTitle as default };
@@ -1,6 +1,5 @@
1
- import { mergeProps } from "@base-ui/react/merge-props";
2
- import { useRender } from "@base-ui/react/use-render";
3
1
  import { cn } from "cn";
2
+ import { mergeProps, useRender } from "../../utils/render.js";
4
3
  // src/components/dialog/DialogTitle.tsx
5
4
  const DialogTitle = ({ render, ...otherProps }) => useRender({
6
5
  defaultTagName: "h2",
@@ -1,3 +1,7 @@
1
- import { useRender } from "@base-ui/react/use-render";
2
- declare const JustAnchor: ({ render, ...props }: useRender.ComponentProps<"a">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
1
+ import { RenderProp } from "../../utils/render.js";
2
+ import { ComponentProps } from "react";
3
+ type JustAnchorProps = ComponentProps<"a"> & {
4
+ render?: RenderProp<"a">;
5
+ };
6
+ declare const JustAnchor: ({ render, ...props }: JustAnchorProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
7
  export { JustAnchor as default };
@@ -1,4 +1,4 @@
1
- import { useRender } from "@base-ui/react/use-render";
1
+ import { useRender } from "../../utils/render.js";
2
2
  // src/components/elements/JustAnchor.tsx
3
3
  const JustAnchor = ({ render, ...props }) => {
4
4
  return useRender({
@@ -1,3 +1,7 @@
1
- import { useRender } from "@base-ui/react/use-render";
2
- declare const JustButton: ({ render, ...props }: useRender.ComponentProps<"button">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
1
+ import { RenderProp } from "../../utils/render.js";
2
+ import { ComponentProps } from "react";
3
+ type JustButtonProps = ComponentProps<"button"> & {
4
+ render?: RenderProp<"button">;
5
+ };
6
+ declare const JustButton: ({ render, ...props }: JustButtonProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
7
  export { JustButton as default };
@@ -1,4 +1,4 @@
1
- import { useRender } from "@base-ui/react/use-render";
1
+ import { useRender } from "../../utils/render.js";
2
2
  // src/components/elements/JustButton.tsx
3
3
  const JustButton = ({ render, ...props }) => {
4
4
  return useRender({
@@ -1,3 +1,7 @@
1
- import { useRender } from "@base-ui/react/use-render";
2
- declare const JustDiv: ({ render, ...props }: useRender.ComponentProps<"div">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
1
+ import { RenderProp } from "../../utils/render.js";
2
+ import { ComponentProps } from "react";
3
+ type JustDivProps = ComponentProps<"div"> & {
4
+ render?: RenderProp<"div">;
5
+ };
6
+ declare const JustDiv: ({ render, ...props }: JustDivProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
7
  export { JustDiv as default };
@@ -1,4 +1,4 @@
1
- import { useRender } from "@base-ui/react/use-render";
1
+ import { useRender } from "../../utils/render.js";
2
2
  // src/components/elements/JustDiv.tsx
3
3
  const JustDiv = ({ render, ...props }) => {
4
4
  return useRender({
@@ -1,3 +1,7 @@
1
- import { useRender } from "@base-ui/react/use-render";
2
- declare const JustInput: ({ render, ...props }: useRender.ComponentProps<"input">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
1
+ import { RenderProp } from "../../utils/render.js";
2
+ import { ComponentProps } from "react";
3
+ type JustInputProps = ComponentProps<"input"> & {
4
+ render?: RenderProp<"input">;
5
+ };
6
+ declare const JustInput: ({ render, ...props }: JustInputProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
7
  export { JustInput as default };
@@ -1,4 +1,4 @@
1
- import { useRender } from "@base-ui/react/use-render";
1
+ import { useRender } from "../../utils/render.js";
2
2
  // src/components/elements/JustInput.tsx
3
3
  const JustInput = ({ render, ...props }) => {
4
4
  return useRender({
@@ -1,3 +1,7 @@
1
- import { useRender } from "@base-ui/react/use-render";
2
- declare const JustSpan: ({ render, ...props }: useRender.ComponentProps<"span">) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
1
+ import { RenderProp } from "../../utils/render.js";
2
+ import { ComponentProps } from "react";
3
+ type JustSpanProps = ComponentProps<"span"> & {
4
+ render?: RenderProp<"span">;
5
+ };
6
+ declare const JustSpan: ({ render, ...props }: JustSpanProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
3
7
  export { JustSpan as default };
@@ -1,4 +1,4 @@
1
- import { useRender } from "@base-ui/react/use-render";
1
+ import { useRender } from "../../utils/render.js";
2
2
  // src/components/elements/JustSpan.tsx
3
3
  const JustSpan = ({ render, ...props }) => {
4
4
  return useRender({
@@ -1,13 +1,23 @@
1
- import { Field } from "@base-ui/react/field";
2
- import { ComponentProps } from "react";
1
+ import { RenderProp } from "../../utils/render.js";
3
2
  import { ZodType } from "zod";
3
+ export type FieldState = {
4
+ disabled: boolean;
5
+ /** The control has lost focus at least once. */
6
+ touched: boolean;
7
+ /** The value differs from the initial one. */
8
+ dirty: boolean;
9
+ /** `null` until the field is validated. */
10
+ valid: boolean | null;
11
+ filled: boolean;
12
+ focused: boolean;
13
+ };
4
14
  type BaseFormFieldProps = {
5
15
  schema: ZodType;
6
16
  name: string;
7
17
  label?: string;
8
- control?: ComponentProps<typeof Field.Control>["render"];
9
- renderLabel?: ComponentProps<typeof Field.Label>["render"];
10
- renderErrors?: ComponentProps<typeof Field.Error>["render"];
18
+ control?: RenderProp<"input", FieldState>;
19
+ renderLabel?: RenderProp<"label", FieldState>;
20
+ renderErrors?: RenderProp<"div", FieldState>;
11
21
  className?: string;
12
22
  };
13
23
  declare const BaseFormField: ({ schema, name, label, control, renderLabel, renderErrors, className }: BaseFormFieldProps) => import("react").JSX.Element;
@@ -1,8 +1,33 @@
1
- import { Switch } from "@base-ui/react/switch";
2
- import { ComponentProps, ReactNode } from "react";
3
- type BaseSwitchProps = ComponentProps<typeof Switch.Root> & {
1
+ import { ComponentProps, ReactNode, Ref } from "react";
2
+ import { RenderProp } from "../../utils/render.js";
3
+ export type SwitchState = {
4
+ checked: boolean;
5
+ disabled: boolean;
6
+ readOnly: boolean;
7
+ required: boolean;
8
+ };
9
+ type BaseSwitchProps = Omit<ComponentProps<"span">, "onChange" | "defaultChecked" | "defaultValue"> & {
4
10
  label: ReactNode;
5
- renderThumb?: ComponentProps<typeof Switch.Thumb>["render"];
11
+ render?: RenderProp<"span", SwitchState>;
12
+ renderThumb?: RenderProp<"span", SwitchState>;
13
+ name?: string;
14
+ /** Submitted when checked. */
15
+ value?: string;
16
+ /** Submitted when unchecked. */
17
+ uncheckedValue?: string;
18
+ checked?: boolean;
19
+ defaultChecked?: boolean;
20
+ /** Called with the new state and the DOM event that changed it. */
21
+ onCheckedChange?: (checked: boolean, event: Event) => void;
22
+ disabled?: boolean;
23
+ readOnly?: boolean;
24
+ required?: boolean;
25
+ /** The `id` of a `<form>` to submit with, when the switch isn't inside it. */
26
+ form?: string;
27
+ /** A ref to the hidden `<input>` that holds the value. */
28
+ inputRef?: Ref<HTMLInputElement>;
29
+ /** Set to `true` when `render` renders a `<button>`. */
30
+ nativeButton?: boolean;
6
31
  };
7
- declare const BaseSwitch: ({ label, renderThumb, value, uncheckedValue, className, ...otherProps }: BaseSwitchProps) => import("react").JSX.Element;
32
+ declare const BaseSwitch: ({ label, renderThumb, onCheckedChange, value, uncheckedValue, className, ...otherProps }: BaseSwitchProps) => import("react").JSX.Element;
8
33
  export { BaseSwitch as default };
@@ -3,12 +3,13 @@ import { cn } from "cn";
3
3
  import "react";
4
4
  import { jsx, jsxs } from "react/jsx-runtime";
5
5
  // src/components/form/BaseSwitch.tsx
6
- const BaseSwitch = ({ label, renderThumb, value = "true", uncheckedValue = "false", className, ...otherProps }) => /* @__PURE__ */ jsxs("label", {
6
+ const BaseSwitch = ({ label, renderThumb, onCheckedChange, value = "true", uncheckedValue = "false", className, ...otherProps }) => /* @__PURE__ */ jsxs("label", {
7
7
  className: "flex items-center gap-2 text-sm font-normal",
8
8
  children: [/* @__PURE__ */ jsx(Switch.Root, {
9
9
  ...otherProps,
10
10
  value,
11
11
  uncheckedValue,
12
+ onCheckedChange: onCheckedChange && ((checked, details) => onCheckedChange(checked, details.event)),
12
13
  className: cn("flex", "h-5", "w-9", "shrink-0", "p-0.5", "transition-colors", "duration-150", "ease-[ease]", "focus-visible:outline-2", "focus-visible:outline-offset-2", className),
13
14
  children: /* @__PURE__ */ jsx(Switch.Thumb, {
14
15
  className: cn("size-3.5", "transition-[translate,background-color]", "duration-150", "ease-[ease]", "data-checked:translate-x-4"),
@@ -1,9 +1,9 @@
1
- import { Field } from "@base-ui/react/field";
2
- import { ComponentProps } from "react";
1
+ import { FieldState } from "./BaseFormField.js";
2
+ import { RenderProp } from "../../utils/render.js";
3
3
  import { ZodType } from "zod";
4
4
  type ClientSideErrorProps = {
5
5
  schema: ZodType;
6
- render?: ComponentProps<typeof Field.Error>["render"];
6
+ render?: RenderProp<"div", FieldState>;
7
7
  };
8
8
  declare const ClientSideError: ({ schema, render }: ClientSideErrorProps) => import("react").JSX.Element[];
9
9
  export { ClientSideError as default };
@@ -1,8 +1,8 @@
1
- import { Field } from "@base-ui/react/field";
2
- import { ComponentProps } from "react";
1
+ import { FieldState } from "./BaseFormField.js";
2
+ import { RenderProp } from "../../utils/render.js";
3
3
  type ServerSideErrorProps = {
4
4
  name: string;
5
- render?: ComponentProps<typeof Field.Error>["render"];
5
+ render?: RenderProp<"div", FieldState>;
6
6
  };
7
7
  declare const ServerSideError: ({ name, render }: ServerSideErrorProps) => import("react").JSX.Element[];
8
8
  export { ServerSideError as default };
@@ -1,7 +1,7 @@
1
1
  import BaseInput from "./BaseInput.js";
2
- import BaseSwitch from "./BaseSwitch.js";
2
+ import BaseSwitch, { SwitchState } from "./BaseSwitch.js";
3
3
  import BaseForm from "./BaseForm.js";
4
- import BaseFormField from "./BaseFormField.js";
4
+ import BaseFormField, { FieldState } from "./BaseFormField.js";
5
5
  import useFormMessages from "./useFormMessages.js";
6
6
  import { DisplayedFormMessage, MessageFormatter } from "./FormContext.js";
7
- export { BaseForm, BaseFormField, BaseInput, BaseSwitch, type DisplayedFormMessage, type MessageFormatter, useFormMessages };
7
+ export { BaseForm, BaseFormField, BaseInput, BaseSwitch, type DisplayedFormMessage, type FieldState, type MessageFormatter, type SwitchState, useFormMessages };
@@ -1,2 +1,3 @@
1
1
  import { cn } from "./cn.js";
2
- export { cn };
2
+ import { IntrinsicTag, RenderProp, UseRenderOptions, mergeProps, useRender } from "./render.js";
3
+ export { type IntrinsicTag, type RenderProp, type UseRenderOptions, cn, mergeProps, useRender };
@@ -1 +1,3 @@
1
- export { cn } from "./cn.js";
1
+ import { cn } from "./cn.js";
2
+ import { mergeProps, useRender } from "./render.js";
3
+ export { cn, mergeProps, useRender };
@@ -0,0 +1,31 @@
1
+ import { ComponentPropsWithRef, JSX, ReactElement } from "react";
2
+ export type IntrinsicTag = keyof JSX.IntrinsicElements;
3
+ type RenderFunction<Tag extends IntrinsicTag, State> = (props: ComponentPropsWithRef<Tag>, state: State) => ReactElement;
4
+ /**
5
+ * Replaces the element a component renders.
6
+ * An element is rendered with the component's props merged into it.
7
+ * A function gets those props (spread them onto your element) and the component's state.
8
+ */
9
+ export type RenderProp<Tag extends IntrinsicTag, State = object> = ReactElement | RenderFunction<Tag, State>;
10
+ export type UseRenderOptions<Tag extends IntrinsicTag, State extends object> = {
11
+ /** The element rendered when `render` is missing. */
12
+ defaultTagName: Tag;
13
+ render?: RenderProp<Tag, State>;
14
+ /** Props of the rendered element. They are merged into the `render` element's own props. */
15
+ props?: ComponentPropsWithRef<Tag>;
16
+ /** Passed to a `render` function, and set as data attributes (`{ checked: true }` → `data-checked`). */
17
+ state?: State;
18
+ };
19
+ /**
20
+ * Renders `defaultTagName`, or the element given by `render`, with `props` merged into it.
21
+ * Build a component that takes a `render` prop with it.
22
+ */
23
+ export declare function useRender<Tag extends IntrinsicTag, State extends object = object>({ defaultTagName, render, props, state }: UseRenderOptions<Tag, State>): ReactElement;
24
+ type Props<Tag extends IntrinsicTag> = ComponentPropsWithRef<Tag>;
25
+ /**
26
+ * Merges props for one element, later ones winning.
27
+ * Event handlers are chained (later ones run first), `className`s are joined and `style`s are merged.
28
+ * `ref` is not merged.
29
+ */
30
+ export declare function mergeProps<Tag extends IntrinsicTag>(...props: (Props<Tag> | undefined)[]): Props<Tag>;
31
+ export {};
@@ -0,0 +1,24 @@
1
+ import { mergePropsN } from "@base-ui/react/merge-props";
2
+ import { useRender as useRender$1 } from "@base-ui/react/use-render";
3
+ // src/utils/render.ts
4
+ /**
5
+ * Renders `defaultTagName`, or the element given by `render`, with `props` merged into it.
6
+ * Build a component that takes a `render` prop with it.
7
+ */
8
+ function useRender({ defaultTagName, render, props, state }) {
9
+ return useRender$1({
10
+ defaultTagName,
11
+ render,
12
+ props,
13
+ state
14
+ });
15
+ }
16
+ /**
17
+ * Merges props for one element, later ones winning.
18
+ * Event handlers are chained (later ones run first), `className`s are joined and `style`s are merged.
19
+ * `ref` is not merged.
20
+ */
21
+ function mergeProps(...props) {
22
+ return mergePropsN(props);
23
+ }
24
+ export { mergeProps, useRender };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omer-x/tailwind-ui-kit",
3
- "version": "1.0.0-alpha.20",
3
+ "version": "1.0.0-alpha.21",
4
4
  "description": "Tailwind CSS-based React component library for building user interfaces",
5
5
  "keywords": [
6
6
  "tailwind",
@@ -69,6 +69,7 @@
69
69
  "test:watch": "vitest --coverage",
70
70
  "dev": "storybook dev -p 3000",
71
71
  "build": "tsdown",
72
+ "postbuild": "node scripts/check-public-types.ts",
72
73
  "build-storybook": "storybook build"
73
74
  },
74
75
  "dependencies": {