@polyfen/polykit-react 0.0.0-stage → 0.2.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.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +166 -2
  3. package/dist/components/avatar.d.ts +19 -0
  4. package/dist/components/avatar.js +27 -0
  5. package/dist/components/button.d.ts +45 -0
  6. package/dist/components/button.js +44 -0
  7. package/dist/components/choice.d.ts +53 -0
  8. package/dist/components/choice.js +73 -0
  9. package/dist/components/dialog.d.ts +65 -0
  10. package/dist/components/dialog.js +178 -0
  11. package/dist/components/display.d.ts +61 -0
  12. package/dist/components/display.js +63 -0
  13. package/dist/components/field.d.ts +44 -0
  14. package/dist/components/field.js +62 -0
  15. package/dist/components/icon.d.ts +29 -0
  16. package/dist/components/icon.js +17 -0
  17. package/dist/components/link.d.ts +10 -0
  18. package/dist/components/link.js +9 -0
  19. package/dist/components/menu.d.ts +35 -0
  20. package/dist/components/menu.js +60 -0
  21. package/dist/components/navigation.d.ts +42 -0
  22. package/dist/components/navigation.js +47 -0
  23. package/dist/components/page.d.ts +97 -0
  24. package/dist/components/page.js +120 -0
  25. package/dist/components/popover.d.ts +10 -0
  26. package/dist/components/popover.js +16 -0
  27. package/dist/components/shell.d.ts +153 -0
  28. package/dist/components/shell.js +359 -0
  29. package/dist/components/table.d.ts +41 -0
  30. package/dist/components/table.js +35 -0
  31. package/dist/components/theme.d.ts +10 -0
  32. package/dist/components/theme.js +41 -0
  33. package/dist/components/toast.d.ts +27 -0
  34. package/dist/components/toast.js +93 -0
  35. package/dist/index.d.ts +18 -0
  36. package/dist/index.js +20 -0
  37. package/dist/lib/boot-script.d.ts +9 -0
  38. package/dist/lib/boot-script.js +26 -0
  39. package/dist/lib/compose-refs.d.ts +3 -0
  40. package/dist/lib/compose-refs.js +11 -0
  41. package/dist/lib/cx.d.ts +4 -0
  42. package/dist/lib/cx.js +4 -0
  43. package/dist/lib/dom.d.ts +10 -0
  44. package/dist/lib/dom.js +29 -0
  45. package/dist/lib/use-controllable.d.ts +5 -0
  46. package/dist/lib/use-controllable.js +19 -0
  47. package/package.json +50 -3
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
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';
17
+ export { polykitBootScript } from './lib/boot-script.js';
18
+ export { useTheme } from './components/theme.js';
19
+ export { AppShell, Content, Page, Search, ShellMain, SidePanel, SidePanelBody, SidePanelFooter, SidePanelHeader, Sidebar, SidebarBrand, SidebarCollapse, SidebarFoot, SidebarGroup, SidebarItem, SidebarNav, SidebarSubItem, SidebarThemeToggle, Topbar, TopbarContext, TopbarEnd, TopbarMenuButton, TopbarMid, TopbarStart, Wordmark, useAppShell, } from './components/shell.js';
20
+ export { Count, Group, Meta, MetaItem, PageHeader, PageHeaderActions, PageHeaderDescription, PageHeaderTitle, Pagination, Row, pageRange, } from './components/page.js';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The body of @polyfen/polykit-css's boot.js, for frameworks that render <head> themselves:
3
+ *
4
+ * <script dangerouslySetInnerHTML={{ __html: polykitBootScript }} />
5
+ *
6
+ * Run it as a blocking script in <head>, so the saved theme and sidebar apply before the first
7
+ * paint. A test keeps it equal to boot.js.
8
+ */
9
+ export declare const polykitBootScript = "(function () {\n var root = document.documentElement;\n var theme = null;\n var sidebar = null;\n try {\n theme = localStorage.getItem('pk.theme');\n sidebar = localStorage.getItem('pk.sidebar');\n } catch (e) {\n /* use the defaults */\n }\n if (theme === 'light' || theme === 'dark') root.setAttribute('data-theme', theme);\n if (\n sidebar === 'rail' ||\n (sidebar === null && window.matchMedia('(max-width: 1100px)').matches)\n ) {\n root.setAttribute('data-sidebar', 'rail');\n }\n})();";
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The body of @polyfen/polykit-css's boot.js, for frameworks that render <head> themselves:
3
+ *
4
+ * <script dangerouslySetInnerHTML={{ __html: polykitBootScript }} />
5
+ *
6
+ * Run it as a blocking script in <head>, so the saved theme and sidebar apply before the first
7
+ * paint. A test keeps it equal to boot.js.
8
+ */
9
+ export const polykitBootScript = `(function () {
10
+ var root = document.documentElement;
11
+ var theme = null;
12
+ var sidebar = null;
13
+ try {
14
+ theme = localStorage.getItem('pk.theme');
15
+ sidebar = localStorage.getItem('pk.sidebar');
16
+ } catch (e) {
17
+ /* use the defaults */
18
+ }
19
+ if (theme === 'light' || theme === 'dark') root.setAttribute('data-theme', theme);
20
+ if (
21
+ sidebar === 'rail' ||
22
+ (sidebar === null && window.matchMedia('(max-width: 1100px)').matches)
23
+ ) {
24
+ root.setAttribute('data-sidebar', 'rail');
25
+ }
26
+ })();`;
@@ -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,10 @@
1
+ import { useLayoutEffect } from 'react';
2
+ /** useLayoutEffect in the browser, useEffect on the server (where neither runs). */
3
+ export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
4
+ /** Reads localStorage; a per-viewer convenience, so storage that is missing or blocked is fine. */
5
+ export declare function readStorage(key: string): string | null;
6
+ export declare function writeStorage(key: string, value: string): void;
7
+ /** Matches a media query, if the browser can. */
8
+ export declare function matches(query: string): boolean;
9
+ export declare const FOCUSABLE = "a[href], button:not([disabled]), [tabindex]:not([tabindex=\"-1\"])";
10
+ export declare const FIELD = "input:not([type=\"hidden\"]):not([disabled]), select:not([disabled]), textarea:not([disabled])";
@@ -0,0 +1,29 @@
1
+ 'use client';
2
+ import { useEffect, useLayoutEffect } from 'react';
3
+ /** useLayoutEffect in the browser, useEffect on the server (where neither runs). */
4
+ export const useIsomorphicLayoutEffect = typeof document === 'undefined' ? useEffect : useLayoutEffect;
5
+ /** Reads localStorage; a per-viewer convenience, so storage that is missing or blocked is fine. */
6
+ export function readStorage(key) {
7
+ try {
8
+ return window.localStorage.getItem(key);
9
+ }
10
+ catch {
11
+ return null;
12
+ }
13
+ }
14
+ export function writeStorage(key, value) {
15
+ try {
16
+ window.localStorage.setItem(key, value);
17
+ }
18
+ catch {
19
+ /* storage may be missing or blocked */
20
+ }
21
+ }
22
+ /** Matches a media query, if the browser can. */
23
+ export function matches(query) {
24
+ return typeof window !== 'undefined' && typeof window.matchMedia === 'function'
25
+ ? window.matchMedia(query).matches
26
+ : false;
27
+ }
28
+ export const FOCUSABLE = 'a[href], button:not([disabled]), [tabindex]:not([tabindex="-1"])';
29
+ export const FIELD = 'input:not([type="hidden"]):not([disabled]), select:not([disabled]), textarea:not([disabled])';
@@ -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.2.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-css": "^0.4.0",
33
+ "@polyfen/polykit-tokens": "^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-css": "^0.4.0",
47
+ "@polyfen/polykit-tokens": "^0.4.0"
48
+ },
49
+ "scripts": {
50
+ "build": "node scripts/build.mjs",
51
+ "test": "node scripts/build.mjs && vitest run"
52
+ }
6
53
  }