@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.
@@ -0,0 +1,27 @@
1
+ import { type HTMLAttributes } from 'react';
2
+ export interface ToastOptions {
3
+ /** `error` is for failures: the danger fill, and it stays twice as long. */
4
+ variant?: 'default' | 'error';
5
+ /** One action, usually Undo. A toast with an action stays twice as long. */
6
+ action?: {
7
+ label: string;
8
+ onClick: () => void;
9
+ };
10
+ /** Milliseconds. 4000 by default; 8000 with an action or an error. */
11
+ duration?: number;
12
+ }
13
+ declare function show(message: string, options?: ToastOptions): number;
14
+ /** Shows a toast. Returns its id. shadcn: Sonner's `toast()`. */
15
+ export declare const toast: typeof show & {
16
+ error: (message: string, options?: Omit<ToastOptions, 'variant'>) => number;
17
+ /** Removes the toast (or only the one with this id, if it is still showing). */
18
+ dismiss: (id?: number) => void;
19
+ };
20
+ export type ToasterProps = Omit<HTMLAttributes<HTMLDivElement>, 'children'>;
21
+ /**
22
+ * Where toasts show (`.pk-toast`). Render one, near the end of the app. It stays in the page as a
23
+ * live region and is invisible while empty, so a screen reader hears each message arrive. The
24
+ * timer pauses while the pointer or the focus is on the toast.
25
+ */
26
+ export declare function Toaster({ className, ...props }: ToasterProps): import("react").JSX.Element;
27
+ export {};
@@ -0,0 +1,93 @@
1
+ 'use client';
2
+ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { useEffect, useRef, useSyncExternalStore } from 'react';
4
+ import { cx } from '../lib/cx.js';
5
+ /*
6
+ * One toast at a time, as Polykit's toast.js: a new toast replaces the old. The store lives at
7
+ * module level so `toast()` can be called from anywhere, as with Sonner.
8
+ */
9
+ let current = null;
10
+ let nextId = 1;
11
+ const listeners = new Set();
12
+ const emit = () => listeners.forEach((listener) => listener());
13
+ function show(message, options = {}) {
14
+ current = { id: nextId++, message, ...options };
15
+ emit();
16
+ return current.id;
17
+ }
18
+ /** Shows a toast. Returns its id. shadcn: Sonner's `toast()`. */
19
+ export const toast = Object.assign(show, {
20
+ error: (message, options = {}) => show(message, { ...options, variant: 'error' }),
21
+ /** Removes the toast (or only the one with this id, if it is still showing). */
22
+ dismiss: (id) => {
23
+ if (current && (id === undefined || current.id === id)) {
24
+ current = null;
25
+ emit();
26
+ }
27
+ },
28
+ });
29
+ const subscribe = (listener) => {
30
+ listeners.add(listener);
31
+ return () => listeners.delete(listener);
32
+ };
33
+ /**
34
+ * Where toasts show (`.pk-toast`). Render one, near the end of the app. It stays in the page as a
35
+ * live region and is invisible while empty, so a screen reader hears each message arrive. The
36
+ * timer pauses while the pointer or the focus is on the toast.
37
+ */
38
+ export function Toaster({ className, ...props }) {
39
+ const state = useSyncExternalStore(subscribe, () => current, () => null);
40
+ const paused = useRef({ hover: false, focus: false });
41
+ const returnTo = useRef(null);
42
+ const timer = useRef(undefined);
43
+ const remaining = useRef(0);
44
+ const started = useRef(0);
45
+ const stop = () => {
46
+ if (timer.current)
47
+ clearTimeout(timer.current);
48
+ timer.current = undefined;
49
+ };
50
+ const start = () => {
51
+ stop();
52
+ if (!state || paused.current.hover || paused.current.focus)
53
+ return;
54
+ started.current = Date.now();
55
+ const id = state.id;
56
+ timer.current = setTimeout(() => toast.dismiss(id), remaining.current);
57
+ };
58
+ const pause = (why) => {
59
+ if (!paused.current.hover && !paused.current.focus && timer.current) {
60
+ remaining.current -= Date.now() - started.current;
61
+ }
62
+ paused.current[why] = true;
63
+ stop();
64
+ };
65
+ const resume = (why) => {
66
+ paused.current[why] = false;
67
+ start();
68
+ };
69
+ useEffect(() => {
70
+ if (!state)
71
+ return stop;
72
+ remaining.current = state.duration ?? (state.action || state.variant === 'error' ? 8000 : 4000);
73
+ start();
74
+ return stop;
75
+ }, [state]);
76
+ return (_jsx("div", { role: "status", "aria-live": "polite", className: cx('pk-toast', state?.variant === 'error' && 'pk-toast--error', className), onPointerEnter: () => pause('hover'), onPointerLeave: () => resume('hover'), onFocus: (event) => {
77
+ if (!event.currentTarget.contains(event.relatedTarget)) {
78
+ returnTo.current = event.relatedTarget;
79
+ }
80
+ pause('focus');
81
+ }, onBlur: (event) => {
82
+ if (!event.currentTarget.contains(event.relatedTarget))
83
+ resume('focus');
84
+ }, ...props, children: state ? (_jsxs(_Fragment, { children: [state.message, state.action ? (_jsxs(_Fragment, { children: [' ', _jsx("button", { type: "button", className: "pk-toast__action", onClick: () => {
85
+ const back = returnTo.current;
86
+ state.action.onClick();
87
+ toast.dismiss(state.id);
88
+ paused.current.focus = false;
89
+ // Focus was on the action, which is gone now: give it back.
90
+ if (back?.isConnected)
91
+ back.focus();
92
+ }, children: state.action.label })] })) : null] })) : null }));
93
+ }
@@ -0,0 +1,14 @@
1
+ export { cx, type ClassValue } from './lib/cx.js';
2
+ export { Icon, IconProvider, type IconName, type IconProps, type IconProviderProps, } from './components/icon.js';
3
+ export { Button, IconButton, buttonClassName, type ButtonProps, type ButtonSize, type ButtonVariant, type IconButtonProps, } from './components/button.js';
4
+ export { Link, type LinkProps, type LinkVariant } from './components/link.js';
5
+ export { Badge, Banner, BannerActions, BannerText, Callout, Card, Empty, Progress, Separator, StatusPill, type BadgeProps, type BadgeVariant, type BannerProps, type CalloutProps, type CardProps, type CategoryColor, type EmptyProps, type ProgressProps, type SeparatorProps, type Tone, } from './components/display.js';
6
+ export { Avatar, AvatarFallback, AvatarImage, type AvatarFallbackProps, type AvatarProps, } from './components/avatar.js';
7
+ export { Field, FormActions, FormGrid, Input, Label, NativeSelect, Textarea, type FieldProps, type InputProps, type NativeSelectProps, } from './components/field.js';
8
+ export { CheckRound, Checkbox, Choice, RadioGroup, RadioGroupItem, Switch, type CheckRoundProps, type CheckboxProps, type CheckedState, type RadioGroupItemProps, type RadioGroupProps, type SwitchProps, } from './components/choice.js';
9
+ export { Chip, NavTab, NavTabs, SegmentedControl, SegmentedControlItem, Tabs, TabsContent, TabsCount, TabsList, TabsTrigger, type ChipProps, type NavTabProps, type SegmentedControlProps, } from './components/navigation.js';
10
+ export { ContextMenu, ContextMenuCheckboxItem, ContextMenuContent, ContextMenuGroup, ContextMenuItem, ContextMenuLabel, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuSeparator, ContextMenuTrigger, DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuPortal, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuSub, DropdownMenuTrigger, type DropdownMenuContentProps, type MenuItemVariantProps, } from './components/menu.js';
11
+ export { Popover, PopoverAnchor, PopoverClose, PopoverContent, PopoverTrigger, } from './components/popover.js';
12
+ export { AlertDialog, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, Dialog, DialogBody, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogForm, DialogHeader, DialogTitle, DialogTrigger, type DialogContentProps, type DialogHeaderProps, type DialogProps, type DialogTitleProps, } from './components/dialog.js';
13
+ export { Toaster, toast, type ToastOptions, type ToasterProps } from './components/toast.js';
14
+ export { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, type TableCaptionProps, type TableCellProps, type TableHeadProps, type TableProps, type TableRowProps, } from './components/table.js';
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ // @polyfen/polykit-react: React components that render the Polykit CSS classes.
2
+ // Load @polyfen/polykit-tokens/tokens.css and @polyfen/polykit-css/polykit.css once in the app.
3
+ export { cx } from './lib/cx.js';
4
+ export { Icon, IconProvider, } from './components/icon.js';
5
+ export { Button, IconButton, buttonClassName, } from './components/button.js';
6
+ export { Link } from './components/link.js';
7
+ export { Badge, Banner, BannerActions, BannerText, Callout, Card, Empty, Progress, Separator, StatusPill, } from './components/display.js';
8
+ export { Avatar, AvatarFallback, AvatarImage, } from './components/avatar.js';
9
+ export { Field, FormActions, FormGrid, Input, Label, NativeSelect, Textarea, } from './components/field.js';
10
+ export { CheckRound, Checkbox, Choice, RadioGroup, RadioGroupItem, Switch, } from './components/choice.js';
11
+ export { Chip, NavTab, NavTabs, SegmentedControl, SegmentedControlItem, Tabs, TabsContent, TabsCount, TabsList, TabsTrigger, } from './components/navigation.js';
12
+ export { ContextMenu, ContextMenuCheckboxItem, ContextMenuContent, ContextMenuGroup, ContextMenuItem, ContextMenuLabel, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuSeparator, ContextMenuTrigger, DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuPortal, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuSub, DropdownMenuTrigger, } from './components/menu.js';
13
+ export { Popover, PopoverAnchor, PopoverClose, PopoverContent, PopoverTrigger, } from './components/popover.js';
14
+ export { AlertDialog, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, Dialog, DialogBody, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogForm, DialogHeader, DialogTitle, DialogTrigger, } from './components/dialog.js';
15
+ export { Toaster, toast } from './components/toast.js';
16
+ export { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from './components/table.js';
@@ -0,0 +1,3 @@
1
+ import type { Ref } from 'react';
2
+ /** One ref callback that sets every ref it is given. */
3
+ export declare function composeRefs<T>(...refs: (Ref<T> | undefined)[]): (node: T | null) => void;
@@ -0,0 +1,11 @@
1
+ /** One ref callback that sets every ref it is given. */
2
+ export function composeRefs(...refs) {
3
+ return (node) => {
4
+ for (const ref of refs) {
5
+ if (typeof ref === 'function')
6
+ ref(node);
7
+ else if (ref)
8
+ ref.current = node;
9
+ }
10
+ };
11
+ }
@@ -0,0 +1,4 @@
1
+ /** A class value: falsy parts are dropped, so `cx('pk-a', on && 'pk-b')` reads well. */
2
+ export type ClassValue = string | false | null | undefined | 0;
3
+ /** Joins class names. Polykit's classes never conflict, so this needs no merge step. */
4
+ export declare function cx(...values: ClassValue[]): string;
package/dist/lib/cx.js ADDED
@@ -0,0 +1,4 @@
1
+ /** Joins class names. Polykit's classes never conflict, so this needs no merge step. */
2
+ export function cx(...values) {
3
+ return values.filter(Boolean).join(' ');
4
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * State that a parent may own (`value` and `onChange`) or leave to the component
3
+ * (`defaultValue`), as Radix and shadcn components do.
4
+ */
5
+ export declare function useControllableState<T>(value: T | undefined, defaultValue: T, onChange?: (next: T) => void): [T, (next: T) => void];
@@ -0,0 +1,19 @@
1
+ 'use client';
2
+ import { useCallback, useRef, useState } from 'react';
3
+ /**
4
+ * State that a parent may own (`value` and `onChange`) or leave to the component
5
+ * (`defaultValue`), as Radix and shadcn components do.
6
+ */
7
+ export function useControllableState(value, defaultValue, onChange) {
8
+ const [own, setOwn] = useState(defaultValue);
9
+ const controlled = value !== undefined;
10
+ const current = controlled ? value : own;
11
+ const onChangeRef = useRef(onChange);
12
+ onChangeRef.current = onChange;
13
+ const set = useCallback((next) => {
14
+ if (!controlled)
15
+ setOwn(next);
16
+ onChangeRef.current?.(next);
17
+ }, [controlled]);
18
+ return [current, set];
19
+ }
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
2
  "name": "@polyfen/polykit-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Polykit for React: components that render the Polykit CSS classes, with Radix Primitives for behavior and shadcn/ui-compatible names and props.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "./package.json": "./package.json"
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "README.md",
19
+ "LICENSE"
20
+ ],
21
+ "sideEffects": false,
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "dependencies": {
26
+ "radix-ui": "^1.8.1",
27
+ "@polyfen/polykit-icons": "^0.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "react": "^18.2.0 || ^19.0.0",
31
+ "react-dom": "^18.2.0 || ^19.0.0",
32
+ "@polyfen/polykit-tokens": "^0.4.0",
33
+ "@polyfen/polykit-css": "^0.4.0"
34
+ },
35
+ "devDependencies": {
36
+ "@testing-library/react": "^16.3.3",
37
+ "@testing-library/user-event": "^14.6.13",
38
+ "@types/react": "^19.3.0",
39
+ "@types/react-dom": "^19.3.0",
40
+ "axe-core": "^4.14.1",
41
+ "jsdom": "^31.0.1",
42
+ "react": "^19.3.1",
43
+ "react-dom": "^19.3.1",
44
+ "typescript": "^7.0.2",
45
+ "vitest": "^5.1.1",
46
+ "@polyfen/polykit-tokens": "^0.4.0",
47
+ "@polyfen/polykit-css": "^0.4.0"
48
+ },
49
+ "scripts": {
50
+ "build": "node scripts/build.mjs",
51
+ "test": "node scripts/build.mjs && vitest run"
52
+ }
6
53
  }