@polyfen/polykit-react 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 The Polyfen Group LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,94 @@
1
- # Temporary Holding Version
1
+ # @polyfen/polykit-react
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Polykit for React. The components render the [Polykit CSS](../css/README.md) classes, take their behavior from [Radix Primitives](https://www.radix-ui.com/primitives) (or the browser, where the CSS is built on a native element), and use [shadcn/ui](https://ui.shadcn.com)'s names and props, so code written against shadcn moves over with few changes.
4
+
5
+ Status: **0.x, first stage of v2** ([ADR 0035](../../docs/adr/0035-react-package.md)). The API may still change between minor versions.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add @polyfen/polykit-react @polyfen/polykit-css @polyfen/polykit-tokens react react-dom
11
+ ```
12
+
13
+ Load the CSS once, tokens first:
14
+
15
+ ```tsx
16
+ import '@polyfen/polykit-tokens/tokens.css';
17
+ import '@polyfen/polykit-css/polykit.css';
18
+ ```
19
+
20
+ Icons come from the [icons package](../icons/README.md)'s sprite: inline it in the page once (recommended), or copy `sprite.svg` to your assets and tell the components where it is:
21
+
22
+ ```tsx
23
+ import { IconProvider } from '@polyfen/polykit-react';
24
+
25
+ <IconProvider spriteUrl="/assets/sprite.svg">{app}</IconProvider>;
26
+ ```
27
+
28
+ Components that hold state or use hooks are marked `"use client"`, so they work in the Next.js App Router.
29
+
30
+ ## Use
31
+
32
+ ```tsx
33
+ import { Button, Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, Field, Input, Toaster, toast } from '@polyfen/polykit-react';
34
+
35
+ <Dialog>
36
+ <DialogTrigger asChild>
37
+ <Button>Rename…</Button>
38
+ </DialogTrigger>
39
+ <DialogContent>
40
+ <DialogHeader>
41
+ <DialogTitle>Rename Atlas</DialogTitle>
42
+ </DialogHeader>
43
+ <Field label="Name" hint="Shown in the sidebar.">
44
+ <Input defaultValue="Atlas" />
45
+ </Field>
46
+ </DialogContent>
47
+ </Dialog>;
48
+
49
+ <Button variant="primary" onClick={() => toast('Saved')}>Save</Button>;
50
+ <Toaster />;
51
+ ```
52
+
53
+ ## Components
54
+
55
+ | Polykit React | CSS | Behavior | shadcn/ui equivalent |
56
+ | --- | --- | --- | --- |
57
+ | `Button`, `buttonClassName()` | `.pk-button` | native | Button |
58
+ | `IconButton` | `.pk-icon-button` | native | Button `size="icon"` |
59
+ | `Link` | `.pk-link` | native | Button `variant="link"` |
60
+ | `Icon`, `IconProvider` | `.pk-icon` | sprite `<use>` | (lucide-react) |
61
+ | `Badge`, `StatusPill` | `.pk-badge`, `.pk-status` | — | Badge |
62
+ | `Card` | `.pk-card` | — | Card (the surface only) |
63
+ | `Banner`, `BannerText`, `BannerActions` | `.pk-banner` | — | Alert |
64
+ | `Callout`, `Empty`, `Progress`, `Separator` | `.pk-callout`, `.pk-empty`, `.pk-progress`, `.pk-separator` | — | —, Empty, Progress, Separator |
65
+ | `Avatar`, `AvatarImage`, `AvatarFallback` | `.pk-avatar`, `.pk-monogram` | — | Avatar |
66
+ | `Field`, `Label`, `Input`, `Textarea`, `NativeSelect`, `FormGrid`, `FormActions` | `.pk-field`, `.pk-input` … | native | Field, Label, Input, Textarea, NativeSelect |
67
+ | `Checkbox`, `RadioGroup`, `RadioGroupItem`, `Choice` | `.pk-checkbox`, `.pk-radio`, `.pk-choice` | native inputs | Checkbox, RadioGroup |
68
+ | `Switch` | `.pk-switch` | Radix Switch | Switch |
69
+ | `CheckRound` | `.pk-check-round` | Radix Toggle | Checkbox (round) |
70
+ | `Chip` | `.pk-chip` | Radix Toggle | Toggle |
71
+ | `SegmentedControl`, `SegmentedControlItem` | `.pk-segmented` | Radix ToggleGroup (single) | ToggleGroup `type="single"` |
72
+ | `Tabs`, `TabsList`, `TabsTrigger`, `TabsContent`, `TabsCount` | `.pk-tabs` | Radix Tabs | Tabs |
73
+ | `NavTabs`, `NavTab` | `.pk-tabs` (links) | native | NavigationMenu |
74
+ | `DropdownMenu*` | `.pk-menu` | Radix DropdownMenu | DropdownMenu |
75
+ | `ContextMenu*` | `.pk-menu` | Radix ContextMenu | ContextMenu |
76
+ | `Popover*` | `.pk-popover` | Radix Popover | Popover |
77
+ | `Dialog*`, `AlertDialog*` | `.pk-dialog` | native `<dialog>` | Dialog, AlertDialog |
78
+ | `Toaster`, `toast()` | `.pk-toast` | own (one at a time, as `toast.js`) | Sonner |
79
+ | `Table*` | `.pk-table` | native | Table |
80
+
81
+ ### Where Polykit differs from shadcn
82
+
83
+ - **Button variants.** shadcn's `default` is a filled button; in Polykit that is `primary`, and `default` is the bordered button. `outline` and `secondary` map to Polykit's `default`, `destructive` to `danger`. Sizes are `default` (shadcn's `sm` maps to it), `md` and `lg`.
84
+ - **Dialog** is the native `<dialog>`, as in the CSS: the browser supplies the backdrop, the focus trap and Escape. `DialogHeader` holds the close button; `DialogBody` and `DialogFooter` follow. Focus goes to the first field (an alert dialog starts on Cancel) and returns to the trigger.
85
+ - **Checkbox and radios** are the browser's inputs, so forms submit them as usual; `onCheckedChange` and `onValueChange` work as in shadcn.
86
+ - **Select** is `NativeSelect` only. For a custom listbox, use shadcn's Select through the [token bridge](../tokens/README.md).
87
+
88
+ ### Not here yet
89
+
90
+ Anything else falls back to shadcn/ui, themed by `@polyfen/polykit-tokens/shadcn.css` ([fallback policy](../../docs/FALLBACK.md)): Tooltip, Select, Combobox, Command, Accordion, Sheet and the rest. The app shell, side panel, board, calendar and chat come in a later stage.
91
+
92
+ ## License
93
+
94
+ [MIT](LICENSE), Copyright (c) 2026 The Polyfen Group LLC.
@@ -0,0 +1,19 @@
1
+ import { type HTMLAttributes, type ImgHTMLAttributes } from 'react';
2
+ import type { CategoryColor } from './display.js';
3
+ export interface AvatarProps extends HTMLAttributes<HTMLSpanElement> {
4
+ /** `sm` 24, default 32, `lg` 40, `xl` 56. */
5
+ size?: 'sm' | 'default' | 'lg' | 'xl';
6
+ }
7
+ /**
8
+ * A person's picture, round (`.pk-avatar`). Put an `AvatarFallback` (the initial) and an
9
+ * `AvatarImage` inside; the image covers the fallback and steps aside if it cannot load.
10
+ */
11
+ export declare const Avatar: import("react").ForwardRefExoticComponent<AvatarProps & import("react").RefAttributes<HTMLSpanElement>>;
12
+ /** The picture. With the person's name beside the avatar, give it `alt=""`. */
13
+ export declare const AvatarImage: import("react").ForwardRefExoticComponent<ImgHTMLAttributes<HTMLImageElement> & import("react").RefAttributes<HTMLImageElement>>;
14
+ export interface AvatarFallbackProps extends HTMLAttributes<HTMLSpanElement> {
15
+ /** A categorical hue for the monogram. Without one it is an outline. */
16
+ color?: CategoryColor;
17
+ }
18
+ /** The initial underneath the picture (`.pk-monogram`). It is decoration: name the person. */
19
+ export declare const AvatarFallback: import("react").ForwardRefExoticComponent<AvatarFallbackProps & import("react").RefAttributes<HTMLSpanElement>>;
@@ -0,0 +1,27 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { forwardRef, useState, } from 'react';
4
+ import { cx } from '../lib/cx.js';
5
+ /**
6
+ * A person's picture, round (`.pk-avatar`). Put an `AvatarFallback` (the initial) and an
7
+ * `AvatarImage` inside; the image covers the fallback and steps aside if it cannot load.
8
+ */
9
+ export const Avatar = forwardRef(function Avatar({ size = 'default', className, ...props }, ref) {
10
+ return (_jsx("span", { ref: ref, className: cx('pk-avatar', size !== 'default' && `pk-avatar--${size}`, className), ...props }));
11
+ });
12
+ /** The picture. With the person's name beside the avatar, give it `alt=""`. */
13
+ export const AvatarImage = forwardRef(function AvatarImage({ onError, alt = '', ...props }, ref) {
14
+ const [failed, setFailed] = useState(false);
15
+ if (failed)
16
+ return null;
17
+ return (_jsx("img", { ref: ref, alt: alt, onError: (event) => {
18
+ setFailed(true);
19
+ onError?.(event);
20
+ }, ...props }));
21
+ });
22
+ /** The initial underneath the picture (`.pk-monogram`). It is decoration: name the person. */
23
+ export const AvatarFallback = forwardRef(function AvatarFallback({ color, className, style, ...props }, ref) {
24
+ return (_jsx("span", { ref: ref, "aria-hidden": "true", className: cx('pk-monogram', className), style: color
25
+ ? { '--pk-monogram-color': `var(--pk-color-cat-${color})`, ...style }
26
+ : style, ...props }));
27
+ });
@@ -0,0 +1,45 @@
1
+ import { type ButtonHTMLAttributes } from 'react';
2
+ /**
3
+ * Polykit's variants, plus shadcn's names where one maps cleanly. Note that shadcn's `default`
4
+ * is a filled button, which is Polykit's `primary`; Polykit's `default` is the bordered button.
5
+ */
6
+ export type ButtonVariant = 'default' | 'primary' | 'ghost' | 'danger' | 'accent' | 'accent-tint' | 'accent-secondary' | 'accent-secondary-tint' | 'frame'
7
+ /** shadcn: the bordered button (Polykit's `default`). */
8
+ | 'outline'
9
+ /** shadcn: the bordered button (Polykit's `default`). */
10
+ | 'secondary'
11
+ /** shadcn: Polykit's `danger`. */
12
+ | 'destructive';
13
+ /** `default` is the compact 32px button; `md` and `lg` are taller. shadcn's `sm` is the default. */
14
+ export type ButtonSize = 'default' | 'sm' | 'md' | 'lg';
15
+ /** The class names of a Polykit button, for an element that is not a `Button` (a router link). */
16
+ export declare function buttonClassName({ variant, size, pill, className, }?: {
17
+ variant?: ButtonVariant;
18
+ size?: ButtonSize;
19
+ pill?: boolean;
20
+ className?: string;
21
+ }): string;
22
+ export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
23
+ variant?: ButtonVariant;
24
+ size?: ButtonSize;
25
+ /** Fully rounded ends. */
26
+ pill?: boolean;
27
+ /**
28
+ * The action is under way: the button says so (`aria-busy`), keeps its color and focus, and
29
+ * ignores clicks until it is done.
30
+ */
31
+ loading?: boolean;
32
+ /** Render the child element (a link) with the button's classes instead of a `<button>`. */
33
+ asChild?: boolean;
34
+ }
35
+ /** A button (`.pk-button`). Ink is the primary action; the accent fills only the accent variants. */
36
+ export declare const Button: import("react").ForwardRefExoticComponent<ButtonProps & import("react").RefAttributes<HTMLButtonElement>>;
37
+ export interface IconButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
38
+ /** `frame` is for the dark top bar. */
39
+ variant?: 'default' | 'frame';
40
+ /** An icon button has no text, so it must have a name. */
41
+ 'aria-label': string;
42
+ asChild?: boolean;
43
+ }
44
+ /** A square button that holds one icon (`.pk-icon-button`). shadcn: `Button size="icon"`. */
45
+ export declare const IconButton: import("react").ForwardRefExoticComponent<IconButtonProps & import("react").RefAttributes<HTMLButtonElement>>;
@@ -0,0 +1,44 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { Slot } from 'radix-ui';
3
+ import { forwardRef } from 'react';
4
+ import { cx } from '../lib/cx.js';
5
+ const VARIANT = {
6
+ default: null,
7
+ outline: null,
8
+ secondary: null,
9
+ primary: 'pk-button--primary',
10
+ ghost: 'pk-button--ghost',
11
+ danger: 'pk-button--danger',
12
+ destructive: 'pk-button--danger',
13
+ accent: 'pk-button--accent',
14
+ 'accent-tint': 'pk-button--accent-tint',
15
+ 'accent-secondary': 'pk-button--accent-secondary',
16
+ 'accent-secondary-tint': 'pk-button--accent-secondary-tint',
17
+ frame: 'pk-button--frame',
18
+ };
19
+ const SIZE = {
20
+ default: null,
21
+ sm: null,
22
+ md: 'pk-button--md',
23
+ lg: 'pk-button--lg',
24
+ };
25
+ /** The class names of a Polykit button, for an element that is not a `Button` (a router link). */
26
+ export function buttonClassName({ variant = 'default', size = 'default', pill = false, className, } = {}) {
27
+ return cx('pk-button', VARIANT[variant], SIZE[size], pill && 'pk-button--pill', className);
28
+ }
29
+ /** A button (`.pk-button`). Ink is the primary action; the accent fills only the accent variants. */
30
+ export const Button = forwardRef(function Button({ variant, size, pill, loading, asChild, className, type, onClick, ...props }, ref) {
31
+ const Comp = asChild ? Slot.Root : 'button';
32
+ return (_jsx(Comp, { ref: ref, className: buttonClassName({ variant, size, pill, className }), type: asChild ? type : (type ?? 'button'), "aria-busy": loading || undefined, onClick: (event) => {
33
+ if (loading) {
34
+ event.preventDefault();
35
+ return;
36
+ }
37
+ onClick?.(event);
38
+ }, ...props }));
39
+ });
40
+ /** A square button that holds one icon (`.pk-icon-button`). shadcn: `Button size="icon"`. */
41
+ export const IconButton = forwardRef(function IconButton({ variant = 'default', asChild, className, type, ...props }, ref) {
42
+ const Comp = asChild ? Slot.Root : 'button';
43
+ return (_jsx(Comp, { ref: ref, className: cx('pk-icon-button', variant === 'frame' && 'pk-icon-button--frame', className), type: asChild ? type : (type ?? 'button'), ...props }));
44
+ });
@@ -0,0 +1,53 @@
1
+ import { Switch as SwitchPrimitive, Toggle } from 'radix-ui';
2
+ import { type ComponentPropsWithoutRef, type HTMLAttributes, type InputHTMLAttributes, type LabelHTMLAttributes } from 'react';
3
+ /** The label that lines a checkbox, radio or switch up with its text (`.pk-choice`). */
4
+ export declare const Choice: import("react").ForwardRefExoticComponent<LabelHTMLAttributes<HTMLLabelElement> & import("react").RefAttributes<HTMLLabelElement>>;
5
+ export type CheckedState = boolean | 'indeterminate';
6
+ export interface CheckboxProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'type' | 'checked' | 'defaultChecked'> {
7
+ checked?: CheckedState;
8
+ defaultChecked?: boolean;
9
+ /** shadcn's change handler; `onChange` works too. */
10
+ onCheckedChange?: (checked: boolean) => void;
11
+ }
12
+ /**
13
+ * A checkbox (`.pk-checkbox`): the browser's own input, so forms, the keyboard and screen readers
14
+ * work as usual. `checked="indeterminate"` shows the dash. Wrap it in a `Choice` with its text.
15
+ */
16
+ export declare const Checkbox: import("react").ForwardRefExoticComponent<CheckboxProps & import("react").RefAttributes<HTMLInputElement>>;
17
+ export interface RadioGroupProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange' | 'defaultValue'> {
18
+ value?: string;
19
+ defaultValue?: string;
20
+ onValueChange?: (value: string) => void;
21
+ /** The radios' shared name; one is made up when it is left out. */
22
+ name?: string;
23
+ disabled?: boolean;
24
+ required?: boolean;
25
+ }
26
+ /**
27
+ * A set of radios (`role="radiogroup"`). Name it with `aria-label` or `aria-labelledby`, and put a
28
+ * `RadioGroupItem` in a `Choice` for each option. The radios are native, so the arrow keys move
29
+ * between them.
30
+ */
31
+ export declare const RadioGroup: import("react").ForwardRefExoticComponent<RadioGroupProps & import("react").RefAttributes<HTMLDivElement>>;
32
+ export interface RadioGroupItemProps extends Omit<InputHTMLAttributes<HTMLInputElement>, 'type' | 'value' | 'name'> {
33
+ value: string;
34
+ }
35
+ /** One radio (`.pk-radio`) in a `RadioGroup`. */
36
+ export declare const RadioGroupItem: import("react").ForwardRefExoticComponent<RadioGroupItemProps & import("react").RefAttributes<HTMLInputElement>>;
37
+ export type SwitchProps = ComponentPropsWithoutRef<typeof SwitchPrimitive.Root>;
38
+ /**
39
+ * A switch (`.pk-switch`): an on/off setting that takes effect at once. A Radix switch (a button
40
+ * with `role="switch"`); name it with a `Choice` around it or `aria-label`.
41
+ */
42
+ export declare const Switch: import("react").ForwardRefExoticComponent<Omit<SwitchPrimitive.SwitchProps & import("react").RefAttributes<HTMLButtonElement>, "ref"> & import("react").RefAttributes<HTMLButtonElement>>;
43
+ export interface CheckRoundProps extends ComponentPropsWithoutRef<typeof Toggle.Root> {
44
+ /** Name it after what it completes ("Mark Draft the brief done"). */
45
+ 'aria-label': string;
46
+ /** The completion is being saved. */
47
+ loading?: boolean;
48
+ }
49
+ /**
50
+ * The completion control (`.pk-check-round`): a round toggle that turns green when pressed. Pair
51
+ * it with an Undo toast. Its tick is the sprite's `check-bold`.
52
+ */
53
+ export declare const CheckRound: import("react").ForwardRefExoticComponent<CheckRoundProps & import("react").RefAttributes<HTMLButtonElement>>;
@@ -0,0 +1,73 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { Switch as SwitchPrimitive, Toggle } from 'radix-ui';
4
+ import { createContext, forwardRef, useContext, useEffect, useId, useRef, } from 'react';
5
+ import { composeRefs } from '../lib/compose-refs.js';
6
+ import { cx } from '../lib/cx.js';
7
+ import { useControllableState } from '../lib/use-controllable.js';
8
+ import { Icon } from './icon.js';
9
+ /** The label that lines a checkbox, radio or switch up with its text (`.pk-choice`). */
10
+ export const Choice = forwardRef(function Choice({ className, ...props }, ref) {
11
+ return _jsx("label", { ref: ref, className: cx('pk-choice', className), ...props });
12
+ });
13
+ /**
14
+ * A checkbox (`.pk-checkbox`): the browser's own input, so forms, the keyboard and screen readers
15
+ * work as usual. `checked="indeterminate"` shows the dash. Wrap it in a `Choice` with its text.
16
+ */
17
+ export const Checkbox = forwardRef(function Checkbox({ checked, onCheckedChange, onChange, className, ...props }, ref) {
18
+ const inner = useRef(null);
19
+ const indeterminate = checked === 'indeterminate';
20
+ useEffect(() => {
21
+ if (inner.current)
22
+ inner.current.indeterminate = indeterminate;
23
+ }, [indeterminate]);
24
+ return (_jsx("input", { ref: composeRefs(inner, ref), type: "checkbox", className: cx('pk-checkbox', className), checked: checked === undefined ? undefined : checked === true, "aria-checked": indeterminate ? 'mixed' : undefined, onChange: (event) => {
25
+ onChange?.(event);
26
+ onCheckedChange?.(event.currentTarget.checked);
27
+ }, ...props }));
28
+ });
29
+ const RadioContext = createContext(null);
30
+ /**
31
+ * A set of radios (`role="radiogroup"`). Name it with `aria-label` or `aria-labelledby`, and put a
32
+ * `RadioGroupItem` in a `Choice` for each option. The radios are native, so the arrow keys move
33
+ * between them.
34
+ */
35
+ export const RadioGroup = forwardRef(function RadioGroup({ value, defaultValue, onValueChange, name, disabled, required, ...props }, ref) {
36
+ const auto = useId();
37
+ const [current, setValue] = useControllableState(value, defaultValue, (next) => {
38
+ if (next !== undefined)
39
+ onValueChange?.(next);
40
+ });
41
+ return (_jsx(RadioContext.Provider, { value: {
42
+ name: name ?? `pk-radio-${auto}`,
43
+ value: current,
44
+ setValue,
45
+ disabled,
46
+ required,
47
+ }, children: _jsx("div", { ref: ref, role: "radiogroup", ...props }) }));
48
+ });
49
+ /** One radio (`.pk-radio`) in a `RadioGroup`. */
50
+ export const RadioGroupItem = forwardRef(function RadioGroupItem({ value, className, disabled, onChange, ...props }, ref) {
51
+ const group = useContext(RadioContext);
52
+ if (!group)
53
+ throw new Error('RadioGroupItem must be inside a RadioGroup.');
54
+ return (_jsx("input", { ref: ref, type: "radio", className: cx('pk-radio', className), name: group.name, value: value, checked: group.value === value, disabled: disabled ?? group.disabled, required: group.required, onChange: (event) => {
55
+ onChange?.(event);
56
+ if (event.currentTarget.checked)
57
+ group.setValue(value);
58
+ }, ...props }));
59
+ });
60
+ /**
61
+ * A switch (`.pk-switch`): an on/off setting that takes effect at once. A Radix switch (a button
62
+ * with `role="switch"`); name it with a `Choice` around it or `aria-label`.
63
+ */
64
+ export const Switch = forwardRef(function Switch({ className, ...props }, ref) {
65
+ return _jsx(SwitchPrimitive.Root, { ref: ref, className: cx('pk-switch', className), ...props });
66
+ });
67
+ /**
68
+ * The completion control (`.pk-check-round`): a round toggle that turns green when pressed. Pair
69
+ * it with an Undo toast. Its tick is the sprite's `check-bold`.
70
+ */
71
+ export const CheckRound = forwardRef(function CheckRound({ className, loading, children, ...props }, ref) {
72
+ return (_jsx(Toggle.Root, { ref: ref, className: cx('pk-check-round', className), "aria-busy": loading || undefined, ...props, children: children ?? _jsx(Icon, { name: "check-bold" }) }));
73
+ });
@@ -0,0 +1,65 @@
1
+ import { type ButtonHTMLAttributes, type DialogHTMLAttributes, type FormHTMLAttributes, type HTMLAttributes, type ReactNode } from 'react';
2
+ export interface DialogProps {
3
+ open?: boolean;
4
+ defaultOpen?: boolean;
5
+ onOpenChange?: (open: boolean) => void;
6
+ children?: ReactNode;
7
+ }
8
+ /**
9
+ * A modal dialog on the native `<dialog>` (`.pk-dialog`): the browser gives the backdrop, the
10
+ * focus trap, Escape and the top layer. The parts and props follow shadcn's Dialog.
11
+ */
12
+ export declare function Dialog({ open, defaultOpen, onOpenChange, children }: DialogProps): import("react").JSX.Element;
13
+ /** An alert dialog: the same, with `role="alertdialog"` on its content. shadcn: AlertDialog. */
14
+ export declare const AlertDialog: typeof Dialog;
15
+ interface AsChildButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
16
+ asChild?: boolean;
17
+ }
18
+ /** Opens the dialog. Its label should end with "…". */
19
+ export declare const DialogTrigger: import("react").ForwardRefExoticComponent<AsChildButtonProps & import("react").RefAttributes<HTMLButtonElement>>;
20
+ export declare const AlertDialogTrigger: import("react").ForwardRefExoticComponent<AsChildButtonProps & import("react").RefAttributes<HTMLButtonElement>>;
21
+ /** Closes the dialog. In an alert dialog, the first one (Cancel) gets the focus when it opens. */
22
+ export declare const DialogClose: import("react").ForwardRefExoticComponent<AsChildButtonProps & import("react").RefAttributes<HTMLButtonElement>>;
23
+ export declare const AlertDialogCancel: import("react").ForwardRefExoticComponent<AsChildButtonProps & import("react").RefAttributes<HTMLButtonElement>>;
24
+ export interface DialogContentProps extends DialogHTMLAttributes<HTMLDialogElement> {
25
+ /** `lg` is a little wider. */
26
+ size?: 'default' | 'lg';
27
+ /** The dialog is one form: the form (`DialogForm`) supplies the padding. */
28
+ form?: boolean;
29
+ /** `alertdialog` for a confirmation of something that cannot be undone. */
30
+ role?: 'dialog' | 'alertdialog';
31
+ /** A click on the backdrop closes it. Leave it off for destructive confirmations. */
32
+ closeOnBackdrop?: boolean;
33
+ /** Keep the children in the page while the dialog is closed (it keeps their state). */
34
+ forceMount?: boolean;
35
+ }
36
+ /** The dialog itself. Name it with a `DialogTitle`. */
37
+ export declare const DialogContent: import("react").ForwardRefExoticComponent<DialogContentProps & import("react").RefAttributes<HTMLDialogElement>>;
38
+ export declare const AlertDialogContent: import("react").ForwardRefExoticComponent<DialogContentProps & import("react").RefAttributes<HTMLDialogElement>>;
39
+ export interface DialogHeaderProps extends HTMLAttributes<HTMLDivElement> {
40
+ /** The close button in the corner (the sprite's `x`). On by default, as in shadcn. */
41
+ showCloseButton?: boolean;
42
+ /** The close button's name. */
43
+ closeLabel?: string;
44
+ }
45
+ /** The head (`.pk-dialog__head`): the title and lede, and the close button. */
46
+ export declare const DialogHeader: import("react").ForwardRefExoticComponent<DialogHeaderProps & import("react").RefAttributes<HTMLDivElement>>;
47
+ export declare const AlertDialogHeader: import("react").ForwardRefExoticComponent<DialogHeaderProps & import("react").RefAttributes<HTMLDivElement>>;
48
+ export interface DialogTitleProps extends HTMLAttributes<HTMLHeadingElement> {
49
+ /** The smaller heading, for a dialog that is one form. */
50
+ size?: 'default' | 'md';
51
+ }
52
+ /** The dialog's name (`.pk-dialog__title`). A destructive confirm names the thing. */
53
+ export declare const DialogTitle: import("react").ForwardRefExoticComponent<DialogTitleProps & import("react").RefAttributes<HTMLHeadingElement>>;
54
+ export declare const AlertDialogTitle: import("react").ForwardRefExoticComponent<DialogTitleProps & import("react").RefAttributes<HTMLHeadingElement>>;
55
+ /** A line under the title (`.pk-dialog__lede`). */
56
+ export declare const DialogDescription: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLParagraphElement> & import("react").RefAttributes<HTMLParagraphElement>>;
57
+ export declare const AlertDialogDescription: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLParagraphElement> & import("react").RefAttributes<HTMLParagraphElement>>;
58
+ /** The body (`.pk-dialog__body`). */
59
+ export declare const DialogBody: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & import("react").RefAttributes<HTMLDivElement>>;
60
+ /** The buttons (`.pk-dialog__actions`): Cancel, then the main button. */
61
+ export declare const DialogFooter: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & import("react").RefAttributes<HTMLDivElement>>;
62
+ export declare const AlertDialogFooter: import("react").ForwardRefExoticComponent<HTMLAttributes<HTMLDivElement> & import("react").RefAttributes<HTMLDivElement>>;
63
+ /** A dialog that is one form (`DialogContent form`): the form holds the fields and the buttons. */
64
+ export declare const DialogForm: import("react").ForwardRefExoticComponent<FormHTMLAttributes<HTMLFormElement> & import("react").RefAttributes<HTMLFormElement>>;
65
+ export {};
@@ -0,0 +1,178 @@
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { Slot } from 'radix-ui';
4
+ import { createContext, forwardRef, useContext, useEffect, useId, useRef, useState, } from 'react';
5
+ import { composeRefs } from '../lib/compose-refs.js';
6
+ import { cx } from '../lib/cx.js';
7
+ import { useControllableState } from '../lib/use-controllable.js';
8
+ import { IconButton } from './button.js';
9
+ import { Icon } from './icon.js';
10
+ const DialogContext = createContext(null);
11
+ function useDialog(part) {
12
+ const ctx = useContext(DialogContext);
13
+ if (!ctx)
14
+ throw new Error(`${part} must be inside a Dialog.`);
15
+ return ctx;
16
+ }
17
+ /**
18
+ * A modal dialog on the native `<dialog>` (`.pk-dialog`): the browser gives the backdrop, the
19
+ * focus trap, Escape and the top layer. The parts and props follow shadcn's Dialog.
20
+ */
21
+ export function Dialog({ open, defaultOpen = false, onOpenChange, children }) {
22
+ const [isOpen, setOpen] = useControllableState(open, defaultOpen, onOpenChange);
23
+ const trigger = useRef(null);
24
+ const id = useId();
25
+ const [described, setDescribed] = useState(false);
26
+ return (_jsx(DialogContext.Provider, { value: {
27
+ open: isOpen,
28
+ setOpen,
29
+ trigger,
30
+ titleId: `pk-dialog-${id}-title`,
31
+ descriptionId: `pk-dialog-${id}-description`,
32
+ described,
33
+ setDescribed,
34
+ }, children: children }));
35
+ }
36
+ /** An alert dialog: the same, with `role="alertdialog"` on its content. shadcn: AlertDialog. */
37
+ export const AlertDialog = Dialog;
38
+ /** Opens the dialog. Its label should end with "…". */
39
+ export const DialogTrigger = forwardRef(function DialogTrigger({ asChild, onClick, type, ...props }, ref) {
40
+ const dialog = useDialog('DialogTrigger');
41
+ const Comp = asChild ? Slot.Root : 'button';
42
+ return (_jsx(Comp, { ref: ref, type: asChild ? type : (type ?? 'button'), "aria-haspopup": "dialog", "aria-expanded": dialog.open, onClick: (event) => {
43
+ onClick?.(event);
44
+ if (event.defaultPrevented)
45
+ return;
46
+ dialog.trigger.current = event.currentTarget;
47
+ dialog.setOpen(true);
48
+ }, ...props }));
49
+ });
50
+ export const AlertDialogTrigger = DialogTrigger;
51
+ /** Closes the dialog. In an alert dialog, the first one (Cancel) gets the focus when it opens. */
52
+ export const DialogClose = forwardRef(function DialogClose({ asChild, onClick, type, ...props }, ref) {
53
+ const dialog = useDialog('DialogClose');
54
+ const Comp = asChild ? Slot.Root : 'button';
55
+ return (_jsx(Comp, { ref: ref, type: asChild ? type : (type ?? 'button'), "data-pk-dialog-close": "button", onClick: (event) => {
56
+ onClick?.(event);
57
+ if (!event.defaultPrevented)
58
+ dialog.setOpen(false);
59
+ }, ...props }));
60
+ });
61
+ export const AlertDialogCancel = DialogClose;
62
+ const FIELD = 'input:not([type="hidden"]):not([disabled]), select:not([disabled]), textarea:not([disabled])';
63
+ const BUTTON = 'button:not([disabled]):not([data-pk-dialog-close])';
64
+ /**
65
+ * Where focus goes when the dialog opens (as `behaviors/dialog.js` does): an element marked
66
+ * `data-autofocus`, else in an alert dialog the way out (Cancel), else the first field, else the
67
+ * first button that is not a close button.
68
+ */
69
+ function focusFirst(dialog) {
70
+ const usable = (el) => !el.closest('[hidden], [inert]');
71
+ const first = (selector) => [...dialog.querySelectorAll(selector)].find(usable);
72
+ const target = first('[data-autofocus], [autofocus]') ??
73
+ (dialog.getAttribute('role') === 'alertdialog'
74
+ ? (first('[data-pk-dialog-close="button"]') ?? first('[data-pk-dialog-close]'))
75
+ : undefined) ??
76
+ first(FIELD) ??
77
+ first(BUTTON);
78
+ target?.focus();
79
+ }
80
+ /** The dialog itself. Name it with a `DialogTitle`. */
81
+ export const DialogContent = forwardRef(function DialogContent({ size = 'default', form, role, closeOnBackdrop, forceMount, className, children, onCancel, onClose, onPointerDown, onClick, ...props }, ref) {
82
+ const dialog = useDialog('DialogContent');
83
+ const node = useRef(null);
84
+ const pressedOutside = useRef(false);
85
+ useEffect(() => {
86
+ const el = node.current;
87
+ if (!el)
88
+ return;
89
+ if (dialog.open && !el.open) {
90
+ el.showModal();
91
+ focusFirst(el);
92
+ }
93
+ else if (!dialog.open && el.open) {
94
+ el.close();
95
+ }
96
+ }, [dialog.open]);
97
+ // Give focus back to the trigger when the dialog closes, in every browser (Safari does not
98
+ // focus a clicked button, so it has nothing to return to on its own).
99
+ const wasOpen = useRef(dialog.open);
100
+ useEffect(() => {
101
+ if (wasOpen.current && !dialog.open) {
102
+ const trigger = dialog.trigger.current;
103
+ if (trigger?.isConnected)
104
+ trigger.focus();
105
+ }
106
+ wasOpen.current = dialog.open;
107
+ }, [dialog.open, dialog.trigger]);
108
+ const outside = (event) => {
109
+ const el = node.current;
110
+ if (!el || event.target !== el)
111
+ return false;
112
+ const box = el.getBoundingClientRect();
113
+ return (event.clientX < box.left ||
114
+ event.clientX > box.right ||
115
+ event.clientY < box.top ||
116
+ event.clientY > box.bottom);
117
+ };
118
+ return (_jsx("dialog", { ref: composeRefs(node, ref), className: cx('pk-dialog', size === 'lg' && 'pk-dialog--lg', form && 'pk-dialog--form', className), role: role === 'alertdialog' ? 'alertdialog' : undefined, "aria-labelledby": dialog.titleId, "aria-describedby": dialog.described ? dialog.descriptionId : undefined, onCancel: (event) => {
119
+ // Escape: let the state close it, so a controlled dialog stays in charge.
120
+ onCancel?.(event);
121
+ const kept = event.defaultPrevented;
122
+ event.preventDefault();
123
+ if (!kept)
124
+ dialog.setOpen(false);
125
+ }, onClose: (event) => {
126
+ onClose?.(event);
127
+ // A <form method="dialog"> submit closes the element directly.
128
+ if (dialog.open)
129
+ dialog.setOpen(false);
130
+ }, onPointerDown: (event) => {
131
+ onPointerDown?.(event);
132
+ pressedOutside.current = outside(event);
133
+ }, onClick: (event) => {
134
+ onClick?.(event);
135
+ if (closeOnBackdrop && pressedOutside.current && outside(event))
136
+ dialog.setOpen(false);
137
+ pressedOutside.current = false;
138
+ }, ...props, children: dialog.open || forceMount ? children : null }));
139
+ });
140
+ export const AlertDialogContent = forwardRef(function AlertDialogContent(props, ref) {
141
+ return _jsx(DialogContent, { ref: ref, role: "alertdialog", ...props });
142
+ });
143
+ /** The head (`.pk-dialog__head`): the title and lede, and the close button. */
144
+ export const DialogHeader = forwardRef(function DialogHeader({ showCloseButton = true, closeLabel = 'Close', className, children, ...props }, ref) {
145
+ const dialog = useDialog('DialogHeader');
146
+ return (_jsxs("div", { ref: ref, className: cx('pk-dialog__head', className), ...props, children: [_jsx("div", { children: children }), showCloseButton && (_jsx(IconButton, { className: "pk-dialog__close", "aria-label": closeLabel, "data-pk-dialog-close": "icon", onClick: () => dialog.setOpen(false), children: _jsx(Icon, { name: "x" }) }))] }));
147
+ });
148
+ export const AlertDialogHeader = DialogHeader;
149
+ /** The dialog's name (`.pk-dialog__title`). A destructive confirm names the thing. */
150
+ export const DialogTitle = forwardRef(function DialogTitle({ size = 'default', className, ...props }, ref) {
151
+ const dialog = useDialog('DialogTitle');
152
+ return (_jsx("h2", { ref: ref, id: dialog.titleId, className: cx('pk-dialog__title', size === 'md' && 'pk-dialog__title--md', className), ...props }));
153
+ });
154
+ export const AlertDialogTitle = DialogTitle;
155
+ /** A line under the title (`.pk-dialog__lede`). */
156
+ export const DialogDescription = forwardRef(function DialogDescription({ className, ...props }, ref) {
157
+ const dialog = useDialog('DialogDescription');
158
+ const { setDescribed } = dialog;
159
+ useEffect(() => {
160
+ setDescribed(true);
161
+ return () => setDescribed(false);
162
+ }, [setDescribed]);
163
+ return (_jsx("p", { ref: ref, id: dialog.descriptionId, className: cx('pk-dialog__lede', className), ...props }));
164
+ });
165
+ export const AlertDialogDescription = DialogDescription;
166
+ /** The body (`.pk-dialog__body`). */
167
+ export const DialogBody = forwardRef(function DialogBody({ className, ...props }, ref) {
168
+ return _jsx("div", { ref: ref, className: cx('pk-dialog__body', className), ...props });
169
+ });
170
+ /** The buttons (`.pk-dialog__actions`): Cancel, then the main button. */
171
+ export const DialogFooter = forwardRef(function DialogFooter({ className, ...props }, ref) {
172
+ return _jsx("div", { ref: ref, className: cx('pk-dialog__actions', className), ...props });
173
+ });
174
+ export const AlertDialogFooter = DialogFooter;
175
+ /** A dialog that is one form (`DialogContent form`): the form holds the fields and the buttons. */
176
+ export const DialogForm = forwardRef(function DialogForm({ className, ...props }, ref) {
177
+ return (_jsx("form", { ref: ref, className: cx('pk-dialog__form', 'pk-form-grid', className), ...props }));
178
+ });