@crystal-neuo/react 0.0.0-stage → 4.0.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 +21 -0
- package/README.md +6 -2
- package/dist/Alert.d.ts +11 -0
- package/dist/Alert.d.ts.map +1 -0
- package/dist/Alert.js +7 -0
- package/dist/Alert.js.map +1 -0
- package/dist/Avatar.d.ts +12 -0
- package/dist/Avatar.d.ts.map +1 -0
- package/dist/Avatar.js +14 -0
- package/dist/Avatar.js.map +1 -0
- package/dist/Badge.d.ts +9 -0
- package/dist/Badge.d.ts.map +1 -0
- package/dist/Badge.js +7 -0
- package/dist/Badge.js.map +1 -0
- package/dist/Breadcrumb.d.ts +13 -0
- package/dist/Breadcrumb.d.ts.map +1 -0
- package/dist/Breadcrumb.js +10 -0
- package/dist/Breadcrumb.js.map +1 -0
- package/dist/Button.d.ts +10 -0
- package/dist/Button.d.ts.map +1 -0
- package/dist/Button.js +11 -0
- package/dist/Button.js.map +1 -0
- package/dist/Card.d.ts +9 -0
- package/dist/Card.d.ts.map +1 -0
- package/dist/Card.js +7 -0
- package/dist/Card.js.map +1 -0
- package/dist/Checkbox.d.ts +11 -0
- package/dist/Checkbox.d.ts.map +1 -0
- package/dist/Checkbox.js +28 -0
- package/dist/Checkbox.js.map +1 -0
- package/dist/DatePicker.d.ts +10 -0
- package/dist/DatePicker.d.ts.map +1 -0
- package/dist/DatePicker.js +10 -0
- package/dist/DatePicker.js.map +1 -0
- package/dist/Dialog.d.ts +18 -0
- package/dist/Dialog.d.ts.map +1 -0
- package/dist/Dialog.js +15 -0
- package/dist/Dialog.js.map +1 -0
- package/dist/Drawer.d.ts +14 -0
- package/dist/Drawer.d.ts.map +1 -0
- package/dist/Drawer.js +11 -0
- package/dist/Drawer.js.map +1 -0
- package/dist/FileUpload.d.ts +15 -0
- package/dist/FileUpload.d.ts.map +1 -0
- package/dist/FileUpload.js +42 -0
- package/dist/FileUpload.js.map +1 -0
- package/dist/Input.d.ts +11 -0
- package/dist/Input.d.ts.map +1 -0
- package/dist/Input.js +12 -0
- package/dist/Input.js.map +1 -0
- package/dist/List.d.ts +13 -0
- package/dist/List.d.ts.map +1 -0
- package/dist/List.js +10 -0
- package/dist/List.js.map +1 -0
- package/dist/Menu.d.ts +14 -0
- package/dist/Menu.d.ts.map +1 -0
- package/dist/Menu.js +90 -0
- package/dist/Menu.js.map +1 -0
- package/dist/Modal.d.ts +13 -0
- package/dist/Modal.d.ts.map +1 -0
- package/dist/Modal.js +11 -0
- package/dist/Modal.js.map +1 -0
- package/dist/Nav.d.ts +14 -0
- package/dist/Nav.d.ts.map +1 -0
- package/dist/Nav.js +10 -0
- package/dist/Nav.js.map +1 -0
- package/dist/Pagination.d.ts +13 -0
- package/dist/Pagination.d.ts.map +1 -0
- package/dist/Pagination.js +12 -0
- package/dist/Pagination.js.map +1 -0
- package/dist/Progress.d.ts +12 -0
- package/dist/Progress.d.ts.map +1 -0
- package/dist/Progress.js +11 -0
- package/dist/Progress.js.map +1 -0
- package/dist/Radio.d.ts +21 -0
- package/dist/Radio.d.ts.map +1 -0
- package/dist/Radio.js +17 -0
- package/dist/Radio.js.map +1 -0
- package/dist/SegmentedControl.d.ts +23 -0
- package/dist/SegmentedControl.d.ts.map +1 -0
- package/dist/SegmentedControl.js +16 -0
- package/dist/SegmentedControl.js.map +1 -0
- package/dist/Select.d.ts +16 -0
- package/dist/Select.d.ts.map +1 -0
- package/dist/Select.js +10 -0
- package/dist/Select.js.map +1 -0
- package/dist/Sheet.d.ts +12 -0
- package/dist/Sheet.d.ts.map +1 -0
- package/dist/Sheet.js +11 -0
- package/dist/Sheet.js.map +1 -0
- package/dist/Skeleton.d.ts +9 -0
- package/dist/Skeleton.d.ts.map +1 -0
- package/dist/Skeleton.js +7 -0
- package/dist/Skeleton.js.map +1 -0
- package/dist/Slider.d.ts +10 -0
- package/dist/Slider.d.ts.map +1 -0
- package/dist/Slider.js +9 -0
- package/dist/Slider.js.map +1 -0
- package/dist/TabBar.d.ts +17 -0
- package/dist/TabBar.d.ts.map +1 -0
- package/dist/TabBar.js +7 -0
- package/dist/TabBar.js.map +1 -0
- package/dist/Table.d.ts +16 -0
- package/dist/Table.d.ts.map +1 -0
- package/dist/Table.js +7 -0
- package/dist/Table.js.map +1 -0
- package/dist/Tabs.d.ts +20 -0
- package/dist/Tabs.d.ts.map +1 -0
- package/dist/Tabs.js +49 -0
- package/dist/Tabs.js.map +1 -0
- package/dist/Textarea.d.ts +11 -0
- package/dist/Textarea.d.ts.map +1 -0
- package/dist/Textarea.js +12 -0
- package/dist/Textarea.js.map +1 -0
- package/dist/Toast.d.ts +12 -0
- package/dist/Toast.d.ts.map +1 -0
- package/dist/Toast.js +45 -0
- package/dist/Toast.js.map +1 -0
- package/dist/Toggle.d.ts +9 -0
- package/dist/Toggle.d.ts.map +1 -0
- package/dist/Toggle.js +16 -0
- package/dist/Toggle.js.map +1 -0
- package/dist/Tooltip.d.ts +11 -0
- package/dist/Tooltip.d.ts.map +1 -0
- package/dist/Tooltip.js +36 -0
- package/dist/Tooltip.js.map +1 -0
- package/dist/cn.d.ts +2 -0
- package/dist/cn.d.ts.map +1 -0
- package/dist/cn.js +7 -0
- package/dist/cn.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +40 -0
- package/dist/index.js.map +1 -0
- package/dist/useFocusTrap.d.ts +11 -0
- package/dist/useFocusTrap.d.ts.map +1 -0
- package/dist/useFocusTrap.js +67 -0
- package/dist/useFocusTrap.js.map +1 -0
- package/dist/useModalDialog.d.ts +20 -0
- package/dist/useModalDialog.d.ts.map +1 -0
- package/dist/useModalDialog.js +81 -0
- package/dist/useModalDialog.js.map +1 -0
- package/package.json +42 -4
- package/src/Alert.tsx +40 -0
- package/src/Avatar.tsx +47 -0
- package/src/Badge.tsx +20 -0
- package/src/Breadcrumb.tsx +52 -0
- package/src/Button.tsx +38 -0
- package/src/Card.tsx +20 -0
- package/src/Checkbox.tsx +65 -0
- package/src/DatePicker.tsx +46 -0
- package/src/Dialog.tsx +81 -0
- package/src/Drawer.tsx +57 -0
- package/src/FileUpload.tsx +116 -0
- package/src/Input.tsx +46 -0
- package/src/List.tsx +35 -0
- package/src/Menu.tsx +141 -0
- package/src/Modal.tsx +40 -0
- package/src/Nav.tsx +40 -0
- package/src/Pagination.tsx +78 -0
- package/src/Progress.tsx +43 -0
- package/src/Radio.tsx +71 -0
- package/src/SegmentedControl.tsx +72 -0
- package/src/Select.tsx +64 -0
- package/src/Sheet.tsx +40 -0
- package/src/Skeleton.tsx +21 -0
- package/src/Slider.tsx +34 -0
- package/src/TabBar.tsx +46 -0
- package/src/Table.tsx +60 -0
- package/src/Tabs.tsx +114 -0
- package/src/Textarea.tsx +54 -0
- package/src/Toast.tsx +97 -0
- package/src/Toggle.tsx +42 -0
- package/src/Tooltip.tsx +70 -0
- package/src/cn.ts +6 -0
- package/src/index.ts +44 -0
- package/src/useFocusTrap.ts +71 -0
- package/src/useModalDialog.ts +83 -0
package/src/List.tsx
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { ListVariant, ListSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface ListProps extends React.ComponentProps<"ul"> {
|
|
6
|
+
material?: Material;
|
|
7
|
+
variant?: ListVariant;
|
|
8
|
+
size?: ListSize;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function List({ material, className, variant = "plain", size = "md", ...props }: ListProps) {
|
|
12
|
+
return (
|
|
13
|
+
<ul
|
|
14
|
+
data-slot="list"
|
|
15
|
+
data-material={material}
|
|
16
|
+
className={cn("cn-list", `cn-list--${variant}`, `cn-list--size-${size}`, className)}
|
|
17
|
+
{...props}
|
|
18
|
+
/>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ListItemProps extends React.ComponentProps<"li"> {
|
|
23
|
+
material?: Material;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function ListItem({ material, className, ...props }: ListItemProps) {
|
|
27
|
+
return (
|
|
28
|
+
<li
|
|
29
|
+
data-slot="list-item"
|
|
30
|
+
data-material={material}
|
|
31
|
+
className={cn("cn-list__item", className)}
|
|
32
|
+
{...props}
|
|
33
|
+
/>
|
|
34
|
+
);
|
|
35
|
+
}
|
package/src/Menu.tsx
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { ButtonVariant, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
import { menuNavigation, supportsAnchorPositioning, type MenuEntry } from "@crystal-neuo/contract";
|
|
5
|
+
|
|
6
|
+
export type { MenuEntry };
|
|
7
|
+
|
|
8
|
+
export interface MenuProps {
|
|
9
|
+
material?: Material;
|
|
10
|
+
/** Trigger button content. */
|
|
11
|
+
label: React.ReactNode;
|
|
12
|
+
items: MenuEntry[];
|
|
13
|
+
triggerVariant?: ButtonVariant;
|
|
14
|
+
className?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function Menu({ material, label, items, triggerVariant = "secondary", className }: MenuProps) {
|
|
18
|
+
const id = React.useId();
|
|
19
|
+
const menuId = `${id}-menu`;
|
|
20
|
+
const anchor = `--cn-menu-${id.replace(/[^a-zA-Z0-9-]/g, "")}`;
|
|
21
|
+
const triggerRef = React.useRef<HTMLButtonElement>(null);
|
|
22
|
+
const menuRef = React.useRef<HTMLDivElement>(null);
|
|
23
|
+
const itemRefs = React.useRef<Array<HTMLButtonElement | null>>([]);
|
|
24
|
+
const [open, setOpen] = React.useState(false);
|
|
25
|
+
const actionable = items
|
|
26
|
+
.map((it, i) => ({ it, i }))
|
|
27
|
+
.filter(({ it }) => !("separator" in it) && !it.disabled);
|
|
28
|
+
|
|
29
|
+
function focusItem(index: number) {
|
|
30
|
+
itemRefs.current[index]?.focus();
|
|
31
|
+
}
|
|
32
|
+
function place() {
|
|
33
|
+
const menu = menuRef.current;
|
|
34
|
+
const trigger = triggerRef.current;
|
|
35
|
+
if (!menu || !trigger || supportsAnchorPositioning()) return;
|
|
36
|
+
// Fallback where CSS anchor positioning is unavailable.
|
|
37
|
+
const r = trigger.getBoundingClientRect();
|
|
38
|
+
menu.style.position = "fixed";
|
|
39
|
+
menu.style.inset = "auto";
|
|
40
|
+
menu.style.top = `${r.bottom}px`;
|
|
41
|
+
menu.style.left = `${Math.min(r.left, window.innerWidth - menu.offsetWidth - 8)}px`;
|
|
42
|
+
}
|
|
43
|
+
function show(focus: "first" | "last") {
|
|
44
|
+
const menu = menuRef.current;
|
|
45
|
+
if (!menu) return;
|
|
46
|
+
if (!menu.matches(":popover-open")) menu.showPopover();
|
|
47
|
+
place();
|
|
48
|
+
const target = focus === "first" ? actionable[0] : actionable[actionable.length - 1];
|
|
49
|
+
if (target) focusItem(target.i);
|
|
50
|
+
}
|
|
51
|
+
function hide(returnFocus = true) {
|
|
52
|
+
menuRef.current?.hidePopover();
|
|
53
|
+
if (returnFocus) triggerRef.current?.focus();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
React.useEffect(() => {
|
|
57
|
+
const menu = menuRef.current;
|
|
58
|
+
if (!menu) return;
|
|
59
|
+
// Light dismiss (outside click, Escape) closes the popover without us.
|
|
60
|
+
const onToggle = (e: Event) => {
|
|
61
|
+
const isOpen = (e as ToggleEvent).newState === "open";
|
|
62
|
+
setOpen(isOpen);
|
|
63
|
+
if (!isOpen && menu.contains(document.activeElement)) triggerRef.current?.focus();
|
|
64
|
+
};
|
|
65
|
+
menu.addEventListener("toggle", onToggle);
|
|
66
|
+
return () => menu.removeEventListener("toggle", onToggle);
|
|
67
|
+
}, []);
|
|
68
|
+
|
|
69
|
+
function onTriggerKey(e: React.KeyboardEvent) {
|
|
70
|
+
if (e.key === "ArrowDown" || e.key === "Enter" || e.key === " ") {
|
|
71
|
+
e.preventDefault();
|
|
72
|
+
show("first");
|
|
73
|
+
} else if (e.key === "ArrowUp") {
|
|
74
|
+
e.preventDefault();
|
|
75
|
+
show("last");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function onMenuKey(e: React.KeyboardEvent) {
|
|
79
|
+
if (e.key === "Tab") {
|
|
80
|
+
hide(false);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const current = itemRefs.current.findIndex((el) => el === document.activeElement);
|
|
84
|
+
const next = menuNavigation(items, current, e.key);
|
|
85
|
+
if (next !== null) {
|
|
86
|
+
e.preventDefault();
|
|
87
|
+
focusItem(next);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<div data-slot="menu" data-material={material} className={cn("cn-menu-root", className)}>
|
|
93
|
+
<button
|
|
94
|
+
ref={triggerRef}
|
|
95
|
+
type="button"
|
|
96
|
+
className={cn("cn-button", `cn-button--${triggerVariant}`, "cn-button--size-md")}
|
|
97
|
+
aria-haspopup="menu"
|
|
98
|
+
aria-expanded={open}
|
|
99
|
+
aria-controls={menuId}
|
|
100
|
+
style={{ anchorName: anchor } as React.CSSProperties}
|
|
101
|
+
onClick={() => (open ? hide() : show("first"))}
|
|
102
|
+
onKeyDown={onTriggerKey}
|
|
103
|
+
>
|
|
104
|
+
{label}
|
|
105
|
+
</button>
|
|
106
|
+
<div
|
|
107
|
+
ref={menuRef}
|
|
108
|
+
id={menuId}
|
|
109
|
+
popover="auto"
|
|
110
|
+
role="menu"
|
|
111
|
+
className="cn-menu"
|
|
112
|
+
style={{ positionAnchor: anchor } as React.CSSProperties}
|
|
113
|
+
onKeyDown={onMenuKey}
|
|
114
|
+
>
|
|
115
|
+
{items.map((it, i) =>
|
|
116
|
+
"separator" in it ? (
|
|
117
|
+
<div key={`sep-${i}`} role="separator" className="cn-menu__separator" />
|
|
118
|
+
) : (
|
|
119
|
+
<button
|
|
120
|
+
key={`${it.label}-${i}`}
|
|
121
|
+
ref={(el) => {
|
|
122
|
+
itemRefs.current[i] = el;
|
|
123
|
+
}}
|
|
124
|
+
type="button"
|
|
125
|
+
role="menuitem"
|
|
126
|
+
tabIndex={-1}
|
|
127
|
+
disabled={it.disabled}
|
|
128
|
+
className={cn("cn-menu__item", it.danger && "cn-menu__item--danger")}
|
|
129
|
+
onClick={() => {
|
|
130
|
+
it.onSelect?.();
|
|
131
|
+
hide();
|
|
132
|
+
}}
|
|
133
|
+
>
|
|
134
|
+
{it.label}
|
|
135
|
+
</button>
|
|
136
|
+
),
|
|
137
|
+
)}
|
|
138
|
+
</div>
|
|
139
|
+
</div>
|
|
140
|
+
);
|
|
141
|
+
}
|
package/src/Modal.tsx
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { ModalSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
import { closeOnScrim, useModalDialog } from "./useModalDialog.ts";
|
|
5
|
+
|
|
6
|
+
export interface ModalProps {
|
|
7
|
+
material?: Material;
|
|
8
|
+
open: boolean;
|
|
9
|
+
onClose: () => void;
|
|
10
|
+
title: string;
|
|
11
|
+
size?: ModalSize;
|
|
12
|
+
children?: React.ReactNode;
|
|
13
|
+
className?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function Modal({ material, open, onClose, title, size = "md", children, className }: ModalProps) {
|
|
17
|
+
const ref = React.useRef<HTMLDialogElement>(null);
|
|
18
|
+
const titleId = React.useId();
|
|
19
|
+
useModalDialog(ref, open, onClose);
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<dialog
|
|
23
|
+
ref={ref}
|
|
24
|
+
className="cn-scrim"
|
|
25
|
+
data-slot="modal-scrim"
|
|
26
|
+
data-material={material}
|
|
27
|
+
aria-labelledby={titleId}
|
|
28
|
+
onMouseDown={closeOnScrim(onClose)}
|
|
29
|
+
>
|
|
30
|
+
{open ? (
|
|
31
|
+
<div className={cn("cn-modal", `cn-modal--size-${size}`, className)}>
|
|
32
|
+
<h2 id={titleId} className="cn-modal__title">
|
|
33
|
+
{title}
|
|
34
|
+
</h2>
|
|
35
|
+
{children}
|
|
36
|
+
</div>
|
|
37
|
+
) : null}
|
|
38
|
+
</dialog>
|
|
39
|
+
);
|
|
40
|
+
}
|
package/src/Nav.tsx
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { NavVariant, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface NavProps extends React.ComponentProps<"nav"> {
|
|
6
|
+
material?: Material;
|
|
7
|
+
variant?: NavVariant;
|
|
8
|
+
label: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function Nav({ material, className, variant = "glass", label, children, ...props }: NavProps) {
|
|
12
|
+
return (
|
|
13
|
+
<nav
|
|
14
|
+
data-slot="nav"
|
|
15
|
+
data-material={material}
|
|
16
|
+
aria-label={label}
|
|
17
|
+
className={cn("cn-nav", `cn-nav--${variant}`, className)}
|
|
18
|
+
{...props}
|
|
19
|
+
>
|
|
20
|
+
{children}
|
|
21
|
+
</nav>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface NavLinkProps extends React.ComponentProps<"a"> {
|
|
26
|
+
material?: Material;
|
|
27
|
+
current?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function NavLink({ material, className, current, ...props }: NavLinkProps) {
|
|
31
|
+
return (
|
|
32
|
+
<a
|
|
33
|
+
data-slot="nav-link"
|
|
34
|
+
data-material={material}
|
|
35
|
+
className={cn("cn-nav__link", className)}
|
|
36
|
+
aria-current={current ? "page" : undefined}
|
|
37
|
+
{...props}
|
|
38
|
+
/>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { paginationRange, type PaginationSize, type Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface PaginationProps extends Omit<React.ComponentProps<"nav">, "onChange"> {
|
|
6
|
+
material?: Material;
|
|
7
|
+
size?: PaginationSize;
|
|
8
|
+
page: number;
|
|
9
|
+
pageCount: number;
|
|
10
|
+
onPageChange: (page: number) => void;
|
|
11
|
+
/** Pages shown either side of the current page before truncating. */
|
|
12
|
+
siblings?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function Pagination({
|
|
16
|
+
material,
|
|
17
|
+
className,
|
|
18
|
+
size = "md",
|
|
19
|
+
page,
|
|
20
|
+
pageCount,
|
|
21
|
+
onPageChange,
|
|
22
|
+
siblings = 1,
|
|
23
|
+
...props
|
|
24
|
+
}: PaginationProps) {
|
|
25
|
+
function go(p: number) {
|
|
26
|
+
if (p >= 1 && p <= pageCount && p !== page) onPageChange(p);
|
|
27
|
+
}
|
|
28
|
+
return (
|
|
29
|
+
<nav
|
|
30
|
+
data-slot="pagination"
|
|
31
|
+
data-material={material}
|
|
32
|
+
aria-label="Pagination"
|
|
33
|
+
className={cn("cn-pagination", `cn-pagination--size-${size}`, className)}
|
|
34
|
+
{...props}
|
|
35
|
+
>
|
|
36
|
+
<button
|
|
37
|
+
type="button"
|
|
38
|
+
className="cn-pagination__btn"
|
|
39
|
+
disabled={page <= 1}
|
|
40
|
+
aria-label="Previous page"
|
|
41
|
+
onClick={() => go(page - 1)}
|
|
42
|
+
>
|
|
43
|
+
<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
|
|
44
|
+
<path d="M10 4L6 8l4 4" fill="none" stroke="currentColor" strokeWidth="1.5" />
|
|
45
|
+
</svg>
|
|
46
|
+
</button>
|
|
47
|
+
{paginationRange(page, pageCount, siblings).map((p, i) =>
|
|
48
|
+
p === "gap" ? (
|
|
49
|
+
<span key={`gap-${i}`} className="cn-pagination__gap" aria-hidden="true">
|
|
50
|
+
…
|
|
51
|
+
</span>
|
|
52
|
+
) : (
|
|
53
|
+
<button
|
|
54
|
+
key={p}
|
|
55
|
+
type="button"
|
|
56
|
+
className="cn-pagination__btn"
|
|
57
|
+
aria-label={`Page ${p}`}
|
|
58
|
+
aria-current={p === page ? "page" : undefined}
|
|
59
|
+
onClick={() => go(p)}
|
|
60
|
+
>
|
|
61
|
+
{p}
|
|
62
|
+
</button>
|
|
63
|
+
),
|
|
64
|
+
)}
|
|
65
|
+
<button
|
|
66
|
+
type="button"
|
|
67
|
+
className="cn-pagination__btn"
|
|
68
|
+
disabled={page >= pageCount}
|
|
69
|
+
aria-label="Next page"
|
|
70
|
+
onClick={() => go(page + 1)}
|
|
71
|
+
>
|
|
72
|
+
<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true">
|
|
73
|
+
<path d="M6 4l4 4-4 4" fill="none" stroke="currentColor" strokeWidth="1.5" />
|
|
74
|
+
</svg>
|
|
75
|
+
</button>
|
|
76
|
+
</nav>
|
|
77
|
+
);
|
|
78
|
+
}
|
package/src/Progress.tsx
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { ProgressVariant, ProgressSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface ProgressProps extends React.ComponentProps<"div"> {
|
|
6
|
+
material?: Material;
|
|
7
|
+
variant?: ProgressVariant;
|
|
8
|
+
size?: ProgressSize;
|
|
9
|
+
value?: number;
|
|
10
|
+
max?: number;
|
|
11
|
+
label: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function Progress({
|
|
15
|
+
material,
|
|
16
|
+
className,
|
|
17
|
+
variant = "bar",
|
|
18
|
+
size = "md",
|
|
19
|
+
value = 0,
|
|
20
|
+
max = 100,
|
|
21
|
+
label,
|
|
22
|
+
...props
|
|
23
|
+
}: ProgressProps) {
|
|
24
|
+
const indeterminate = variant === "indeterminate";
|
|
25
|
+
const safeMax = max > 0 ? max : 100;
|
|
26
|
+
const now = Math.max(0, Math.min(safeMax, value));
|
|
27
|
+
const pct = (now / safeMax) * 100;
|
|
28
|
+
return (
|
|
29
|
+
<div
|
|
30
|
+
data-slot="progress"
|
|
31
|
+
data-material={material}
|
|
32
|
+
role="progressbar"
|
|
33
|
+
aria-label={label}
|
|
34
|
+
aria-valuemin={indeterminate ? undefined : 0}
|
|
35
|
+
aria-valuemax={indeterminate ? undefined : safeMax}
|
|
36
|
+
aria-valuenow={indeterminate ? undefined : now}
|
|
37
|
+
className={cn("cn-progress", `cn-progress--${variant}`, `cn-progress--size-${size}`, className)}
|
|
38
|
+
{...props}
|
|
39
|
+
>
|
|
40
|
+
<div className="cn-progress__fill" style={indeterminate ? undefined : { inlineSize: `${pct}%` }} />
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
}
|
package/src/Radio.tsx
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { RadioSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface RadioOption {
|
|
6
|
+
value: string;
|
|
7
|
+
label: string;
|
|
8
|
+
disabled?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface RadioProps {
|
|
12
|
+
material?: Material;
|
|
13
|
+
name: string;
|
|
14
|
+
label: string;
|
|
15
|
+
options: RadioOption[];
|
|
16
|
+
value?: string;
|
|
17
|
+
defaultValue?: string;
|
|
18
|
+
onValueChange?: (value: string) => void;
|
|
19
|
+
size?: RadioSize;
|
|
20
|
+
error?: boolean;
|
|
21
|
+
className?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function Radio({
|
|
25
|
+
material,
|
|
26
|
+
name,
|
|
27
|
+
label,
|
|
28
|
+
options,
|
|
29
|
+
value,
|
|
30
|
+
defaultValue,
|
|
31
|
+
onValueChange,
|
|
32
|
+
size = "md",
|
|
33
|
+
error,
|
|
34
|
+
className,
|
|
35
|
+
}: RadioProps) {
|
|
36
|
+
const [internal, setInternal] = React.useState(defaultValue ?? "");
|
|
37
|
+
const selected = value ?? internal;
|
|
38
|
+
function select(next: string) {
|
|
39
|
+
if (value === undefined) setInternal(next);
|
|
40
|
+
onValueChange?.(next);
|
|
41
|
+
}
|
|
42
|
+
return (
|
|
43
|
+
<div
|
|
44
|
+
role="radiogroup"
|
|
45
|
+
aria-label={label}
|
|
46
|
+
className={cn("cn-radiogroup", error && "cn-choice--error", className)}
|
|
47
|
+
data-slot="radio-group"
|
|
48
|
+
data-material={material}
|
|
49
|
+
>
|
|
50
|
+
{options.map((o) => {
|
|
51
|
+
const id = `${name}-${o.value}`;
|
|
52
|
+
return (
|
|
53
|
+
<label key={o.value} className={cn("cn-choice", `cn-choice--size-${size}`)} htmlFor={id}>
|
|
54
|
+
<input
|
|
55
|
+
id={id}
|
|
56
|
+
type="radio"
|
|
57
|
+
name={name}
|
|
58
|
+
value={o.value}
|
|
59
|
+
className="cn-choice__input"
|
|
60
|
+
checked={selected === o.value}
|
|
61
|
+
disabled={o.disabled}
|
|
62
|
+
aria-invalid={error ? true : undefined}
|
|
63
|
+
onChange={() => select(o.value)}
|
|
64
|
+
/>
|
|
65
|
+
<span className="cn-choice__label">{o.label}</span>
|
|
66
|
+
</label>
|
|
67
|
+
);
|
|
68
|
+
})}
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { SegmentedControlVariant, SegmentedControlSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface SegmentedOption {
|
|
6
|
+
value: string;
|
|
7
|
+
label: string;
|
|
8
|
+
disabled?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SegmentedControlProps {
|
|
12
|
+
material?: Material;
|
|
13
|
+
/** Accessible name of the group (rendered as a visually hidden legend). */
|
|
14
|
+
label: string;
|
|
15
|
+
options: SegmentedOption[];
|
|
16
|
+
value?: string;
|
|
17
|
+
defaultValue?: string;
|
|
18
|
+
onValueChange?: (value: string) => void;
|
|
19
|
+
/** Radio group name; generated when omitted. */
|
|
20
|
+
name?: string;
|
|
21
|
+
variant?: SegmentedControlVariant;
|
|
22
|
+
size?: SegmentedControlSize;
|
|
23
|
+
className?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function SegmentedControl({
|
|
27
|
+
material,
|
|
28
|
+
label,
|
|
29
|
+
options,
|
|
30
|
+
value,
|
|
31
|
+
defaultValue,
|
|
32
|
+
onValueChange,
|
|
33
|
+
name,
|
|
34
|
+
variant = "default",
|
|
35
|
+
size = "md",
|
|
36
|
+
className,
|
|
37
|
+
}: SegmentedControlProps) {
|
|
38
|
+
const autoName = React.useId();
|
|
39
|
+
const groupName = name ?? autoName;
|
|
40
|
+
const [internal, setInternal] = React.useState(
|
|
41
|
+
defaultValue ?? options.find((o) => !o.disabled)?.value ?? "",
|
|
42
|
+
);
|
|
43
|
+
const selected = value ?? internal;
|
|
44
|
+
function select(next: string) {
|
|
45
|
+
if (value === undefined) setInternal(next);
|
|
46
|
+
onValueChange?.(next);
|
|
47
|
+
}
|
|
48
|
+
return (
|
|
49
|
+
// Native radios give arrow-key selection, RTL order, and form submission.
|
|
50
|
+
<fieldset
|
|
51
|
+
data-slot="segmented-control"
|
|
52
|
+
data-material={material}
|
|
53
|
+
className={cn("cn-segmented", `cn-segmented--${variant}`, `cn-segmented--size-${size}`, className)}
|
|
54
|
+
>
|
|
55
|
+
<legend>{label}</legend>
|
|
56
|
+
{options.map((o) => (
|
|
57
|
+
<label key={o.value} className="cn-segmented__item">
|
|
58
|
+
<input
|
|
59
|
+
type="radio"
|
|
60
|
+
className="cn-segmented__input"
|
|
61
|
+
name={groupName}
|
|
62
|
+
value={o.value}
|
|
63
|
+
checked={selected === o.value}
|
|
64
|
+
disabled={o.disabled}
|
|
65
|
+
onChange={() => select(o.value)}
|
|
66
|
+
/>
|
|
67
|
+
<span className="cn-segmented__label">{o.label}</span>
|
|
68
|
+
</label>
|
|
69
|
+
))}
|
|
70
|
+
</fieldset>
|
|
71
|
+
);
|
|
72
|
+
}
|
package/src/Select.tsx
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { SelectSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface SelectOption {
|
|
6
|
+
value: string;
|
|
7
|
+
label: string;
|
|
8
|
+
disabled?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SelectProps extends Omit<React.ComponentProps<"select">, "size"> {
|
|
12
|
+
material?: Material;
|
|
13
|
+
size?: SelectSize;
|
|
14
|
+
label?: string;
|
|
15
|
+
error?: string;
|
|
16
|
+
options: SelectOption[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function Select({
|
|
20
|
+
material,
|
|
21
|
+
className,
|
|
22
|
+
size = "md",
|
|
23
|
+
label,
|
|
24
|
+
error,
|
|
25
|
+
options,
|
|
26
|
+
id,
|
|
27
|
+
...props
|
|
28
|
+
}: SelectProps) {
|
|
29
|
+
const autoId = React.useId();
|
|
30
|
+
const selectId = id ?? autoId;
|
|
31
|
+
const msgId = `${selectId}-msg`;
|
|
32
|
+
return (
|
|
33
|
+
<div className={cn("cn-field", className)} data-slot="select-field" data-material={material}>
|
|
34
|
+
{label ? (
|
|
35
|
+
<label className="cn-field__label" htmlFor={selectId}>
|
|
36
|
+
{label}
|
|
37
|
+
</label>
|
|
38
|
+
) : null}
|
|
39
|
+
<div className="cn-select">
|
|
40
|
+
<select
|
|
41
|
+
id={selectId}
|
|
42
|
+
className={cn("cn-select__native", `cn-input--size-${size}`, error && "cn-select__native--error")}
|
|
43
|
+
aria-invalid={error ? true : undefined}
|
|
44
|
+
aria-describedby={error ? msgId : undefined}
|
|
45
|
+
{...props}
|
|
46
|
+
>
|
|
47
|
+
{options.map((o) => (
|
|
48
|
+
<option key={o.value} value={o.value} disabled={o.disabled}>
|
|
49
|
+
{o.label}
|
|
50
|
+
</option>
|
|
51
|
+
))}
|
|
52
|
+
</select>
|
|
53
|
+
<svg className="cn-select__chevron" viewBox="0 0 16 16" aria-hidden="true">
|
|
54
|
+
<path d="M4 6l4 4 4-4" fill="none" stroke="currentColor" strokeWidth="1.5" />
|
|
55
|
+
</svg>
|
|
56
|
+
</div>
|
|
57
|
+
{error ? (
|
|
58
|
+
<span className="cn-field__message" id={msgId}>
|
|
59
|
+
{error}
|
|
60
|
+
</span>
|
|
61
|
+
) : null}
|
|
62
|
+
</div>
|
|
63
|
+
);
|
|
64
|
+
}
|
package/src/Sheet.tsx
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
import { closeOnScrim, useModalDialog } from "./useModalDialog.ts";
|
|
5
|
+
|
|
6
|
+
export interface SheetProps {
|
|
7
|
+
material?: Material;
|
|
8
|
+
open: boolean;
|
|
9
|
+
onClose: () => void;
|
|
10
|
+
title: string;
|
|
11
|
+
children?: React.ReactNode;
|
|
12
|
+
className?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function Sheet({ material, open, onClose, title, children, className }: SheetProps) {
|
|
16
|
+
const ref = React.useRef<HTMLDialogElement>(null);
|
|
17
|
+
const titleId = React.useId();
|
|
18
|
+
useModalDialog(ref, open, onClose);
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<dialog
|
|
22
|
+
ref={ref}
|
|
23
|
+
className="cn-scrim cn-scrim--sheet"
|
|
24
|
+
data-slot="sheet-scrim"
|
|
25
|
+
data-material={material}
|
|
26
|
+
aria-labelledby={titleId}
|
|
27
|
+
onMouseDown={closeOnScrim(onClose)}
|
|
28
|
+
>
|
|
29
|
+
{open ? (
|
|
30
|
+
<div className={cn("cn-sheet", className)}>
|
|
31
|
+
<span className="cn-sheet__grabber" aria-hidden="true" />
|
|
32
|
+
<h2 id={titleId} className="cn-sheet__title">
|
|
33
|
+
{title}
|
|
34
|
+
</h2>
|
|
35
|
+
{children}
|
|
36
|
+
</div>
|
|
37
|
+
) : null}
|
|
38
|
+
</dialog>
|
|
39
|
+
);
|
|
40
|
+
}
|
package/src/Skeleton.tsx
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { SkeletonVariant, SkeletonSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface SkeletonProps extends React.ComponentProps<"div"> {
|
|
6
|
+
material?: Material;
|
|
7
|
+
variant?: SkeletonVariant;
|
|
8
|
+
size?: SkeletonSize;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function Skeleton({ material, className, variant = "block", size = "md", ...props }: SkeletonProps) {
|
|
12
|
+
return (
|
|
13
|
+
<div
|
|
14
|
+
data-slot="skeleton"
|
|
15
|
+
data-material={material}
|
|
16
|
+
aria-hidden="true"
|
|
17
|
+
className={cn("cn-skeleton", `cn-skeleton--${variant}`, `cn-skeleton--size-${size}`, className)}
|
|
18
|
+
{...props}
|
|
19
|
+
/>
|
|
20
|
+
);
|
|
21
|
+
}
|
package/src/Slider.tsx
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { SliderSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface SliderProps extends Omit<React.ComponentProps<"input">, "size" | "type"> {
|
|
6
|
+
material?: Material;
|
|
7
|
+
size?: SliderSize;
|
|
8
|
+
label: string;
|
|
9
|
+
valueText?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function Slider({ material, className, size = "md", label, valueText, id, ...props }: SliderProps) {
|
|
13
|
+
const autoId = React.useId();
|
|
14
|
+
const sliderId = id ?? autoId;
|
|
15
|
+
return (
|
|
16
|
+
<div
|
|
17
|
+
className={cn("cn-slider", `cn-slider--size-${size}`, className)}
|
|
18
|
+
data-slot="slider"
|
|
19
|
+
data-material={material}
|
|
20
|
+
>
|
|
21
|
+
<label className="cn-field__label" htmlFor={sliderId}>
|
|
22
|
+
{label}
|
|
23
|
+
</label>
|
|
24
|
+
<input
|
|
25
|
+
id={sliderId}
|
|
26
|
+
type="range"
|
|
27
|
+
className="cn-slider__input"
|
|
28
|
+
aria-label={label}
|
|
29
|
+
aria-valuetext={valueText}
|
|
30
|
+
{...props}
|
|
31
|
+
/>
|
|
32
|
+
</div>
|
|
33
|
+
);
|
|
34
|
+
}
|