@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/TabBar.tsx
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface TabBarItem {
|
|
6
|
+
label: string;
|
|
7
|
+
href: string;
|
|
8
|
+
/** Decorative icon; the label is always shown. */
|
|
9
|
+
icon?: React.ReactNode;
|
|
10
|
+
current?: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface TabBarProps extends Omit<React.ComponentProps<"nav">, "children"> {
|
|
14
|
+
material?: Material;
|
|
15
|
+
/** Accessible name of the navigation landmark. */
|
|
16
|
+
label: string;
|
|
17
|
+
items: TabBarItem[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function TabBar({ material, label, items, className, ...props }: TabBarProps) {
|
|
21
|
+
return (
|
|
22
|
+
<nav
|
|
23
|
+
data-slot="tab-bar"
|
|
24
|
+
data-material={material}
|
|
25
|
+
aria-label={label}
|
|
26
|
+
className={cn("cn-tabbar", className)}
|
|
27
|
+
{...props}
|
|
28
|
+
>
|
|
29
|
+
{items.map((it) => (
|
|
30
|
+
<a
|
|
31
|
+
key={it.href}
|
|
32
|
+
href={it.href}
|
|
33
|
+
className="cn-tabbar__item"
|
|
34
|
+
aria-current={it.current ? "page" : undefined}
|
|
35
|
+
>
|
|
36
|
+
{it.icon ? (
|
|
37
|
+
<span className="cn-tabbar__icon" aria-hidden="true">
|
|
38
|
+
{it.icon}
|
|
39
|
+
</span>
|
|
40
|
+
) : null}
|
|
41
|
+
<span>{it.label}</span>
|
|
42
|
+
</a>
|
|
43
|
+
))}
|
|
44
|
+
</nav>
|
|
45
|
+
);
|
|
46
|
+
}
|
package/src/Table.tsx
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { TableVariant, TableSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface TableColumn<T> {
|
|
6
|
+
key: keyof T & string;
|
|
7
|
+
header: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface TableProps<T extends Record<string, React.ReactNode>> extends Omit<
|
|
11
|
+
React.ComponentProps<"table">,
|
|
12
|
+
"children"
|
|
13
|
+
> {
|
|
14
|
+
material?: Material;
|
|
15
|
+
variant?: TableVariant;
|
|
16
|
+
size?: TableSize;
|
|
17
|
+
columns: TableColumn<T>[];
|
|
18
|
+
rows: T[];
|
|
19
|
+
caption?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function Table<T extends Record<string, React.ReactNode>>({
|
|
23
|
+
className,
|
|
24
|
+
material,
|
|
25
|
+
variant = "default",
|
|
26
|
+
size = "md",
|
|
27
|
+
columns,
|
|
28
|
+
rows,
|
|
29
|
+
caption,
|
|
30
|
+
...props
|
|
31
|
+
}: TableProps<T>) {
|
|
32
|
+
return (
|
|
33
|
+
<table
|
|
34
|
+
data-slot="table"
|
|
35
|
+
data-material={material}
|
|
36
|
+
className={cn("cn-table", `cn-table--${variant}`, `cn-table--size-${size}`, className)}
|
|
37
|
+
{...props}
|
|
38
|
+
>
|
|
39
|
+
{caption ? <caption>{caption}</caption> : null}
|
|
40
|
+
<thead>
|
|
41
|
+
<tr>
|
|
42
|
+
{columns.map((c) => (
|
|
43
|
+
<th key={c.key} scope="col">
|
|
44
|
+
{c.header}
|
|
45
|
+
</th>
|
|
46
|
+
))}
|
|
47
|
+
</tr>
|
|
48
|
+
</thead>
|
|
49
|
+
<tbody>
|
|
50
|
+
{rows.map((row, i) => (
|
|
51
|
+
<tr key={i}>
|
|
52
|
+
{columns.map((c) => (
|
|
53
|
+
<td key={c.key}>{row[c.key]}</td>
|
|
54
|
+
))}
|
|
55
|
+
</tr>
|
|
56
|
+
))}
|
|
57
|
+
</tbody>
|
|
58
|
+
</table>
|
|
59
|
+
);
|
|
60
|
+
}
|
package/src/Tabs.tsx
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { TabsVariant, TabsSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface TabItem {
|
|
6
|
+
value: string;
|
|
7
|
+
label: string;
|
|
8
|
+
disabled?: boolean;
|
|
9
|
+
content: React.ReactNode;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface TabsProps extends Omit<React.ComponentProps<"div">, "onChange"> {
|
|
13
|
+
material?: Material;
|
|
14
|
+
items: TabItem[];
|
|
15
|
+
value?: string;
|
|
16
|
+
defaultValue?: string;
|
|
17
|
+
onValueChange?: (value: string) => void;
|
|
18
|
+
variant?: TabsVariant;
|
|
19
|
+
size?: TabsSize;
|
|
20
|
+
label: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function Tabs({
|
|
24
|
+
material,
|
|
25
|
+
className,
|
|
26
|
+
items,
|
|
27
|
+
value,
|
|
28
|
+
defaultValue,
|
|
29
|
+
onValueChange,
|
|
30
|
+
variant = "line",
|
|
31
|
+
size = "md",
|
|
32
|
+
label,
|
|
33
|
+
...props
|
|
34
|
+
}: TabsProps) {
|
|
35
|
+
const baseId = React.useId();
|
|
36
|
+
const firstEnabled = items.find((it) => !it.disabled)?.value ?? "";
|
|
37
|
+
const [internal, setInternal] = React.useState(defaultValue ?? firstEnabled);
|
|
38
|
+
const requested = value ?? internal;
|
|
39
|
+
// A value that names no enabled tab would leave no tab in the Tab order.
|
|
40
|
+
const active = items.some((it) => it.value === requested && !it.disabled) ? requested : firstEnabled;
|
|
41
|
+
const tabRefs = React.useRef<Array<HTMLButtonElement | null>>([]);
|
|
42
|
+
|
|
43
|
+
function select(next: string) {
|
|
44
|
+
if (value === undefined) setInternal(next);
|
|
45
|
+
onValueChange?.(next);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function onKeyDown(e: React.KeyboardEvent<HTMLButtonElement>, index: number) {
|
|
49
|
+
const enabled = items.map((it, i) => ({ it, i })).filter(({ it }) => !it.disabled);
|
|
50
|
+
const pos = enabled.findIndex(({ i }) => i === index);
|
|
51
|
+
// Arrow keys follow the visual order, which flips under dir=rtl.
|
|
52
|
+
const rtl = getComputedStyle(e.currentTarget).direction === "rtl";
|
|
53
|
+
const forward = rtl ? "ArrowLeft" : "ArrowRight";
|
|
54
|
+
const backward = rtl ? "ArrowRight" : "ArrowLeft";
|
|
55
|
+
let nextPos = pos;
|
|
56
|
+
if (e.key === forward) nextPos = (pos + 1) % enabled.length;
|
|
57
|
+
else if (e.key === backward) nextPos = (pos - 1 + enabled.length) % enabled.length;
|
|
58
|
+
else if (e.key === "Home") nextPos = 0;
|
|
59
|
+
else if (e.key === "End") nextPos = enabled.length - 1;
|
|
60
|
+
else return;
|
|
61
|
+
e.preventDefault();
|
|
62
|
+
const target = enabled[nextPos];
|
|
63
|
+
if (!target) return;
|
|
64
|
+
select(target.it.value);
|
|
65
|
+
tabRefs.current[target.i]?.focus();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<div
|
|
70
|
+
data-slot="tabs"
|
|
71
|
+
data-material={material}
|
|
72
|
+
className={cn("cn-tabs", `cn-tabs--${variant}`, `cn-tabs--size-${size}`, className)}
|
|
73
|
+
{...props}
|
|
74
|
+
>
|
|
75
|
+
<div className="cn-tabs__list" role="tablist" aria-label={label}>
|
|
76
|
+
{items.map((it, i) => {
|
|
77
|
+
const selected = it.value === active;
|
|
78
|
+
return (
|
|
79
|
+
<button
|
|
80
|
+
key={it.value}
|
|
81
|
+
ref={(el) => {
|
|
82
|
+
tabRefs.current[i] = el;
|
|
83
|
+
}}
|
|
84
|
+
role="tab"
|
|
85
|
+
type="button"
|
|
86
|
+
id={`${baseId}-tab-${i}`}
|
|
87
|
+
aria-selected={selected}
|
|
88
|
+
aria-controls={`${baseId}-panel-${i}`}
|
|
89
|
+
tabIndex={selected ? 0 : -1}
|
|
90
|
+
disabled={it.disabled}
|
|
91
|
+
className="cn-tabs__tab"
|
|
92
|
+
onClick={() => select(it.value)}
|
|
93
|
+
onKeyDown={(e) => onKeyDown(e, i)}
|
|
94
|
+
>
|
|
95
|
+
{it.label}
|
|
96
|
+
</button>
|
|
97
|
+
);
|
|
98
|
+
})}
|
|
99
|
+
</div>
|
|
100
|
+
{items.map((it, i) => (
|
|
101
|
+
<div
|
|
102
|
+
key={it.value}
|
|
103
|
+
role="tabpanel"
|
|
104
|
+
id={`${baseId}-panel-${i}`}
|
|
105
|
+
aria-labelledby={`${baseId}-tab-${i}`}
|
|
106
|
+
hidden={it.value !== active}
|
|
107
|
+
className="cn-tabs__panel"
|
|
108
|
+
>
|
|
109
|
+
{it.content}
|
|
110
|
+
</div>
|
|
111
|
+
))}
|
|
112
|
+
</div>
|
|
113
|
+
);
|
|
114
|
+
}
|
package/src/Textarea.tsx
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { TextareaSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface TextareaProps extends Omit<React.ComponentProps<"textarea">, "size"> {
|
|
6
|
+
material?: Material;
|
|
7
|
+
size?: TextareaSize;
|
|
8
|
+
label?: string;
|
|
9
|
+
error?: string;
|
|
10
|
+
hint?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function Textarea({
|
|
14
|
+
material,
|
|
15
|
+
className,
|
|
16
|
+
size = "md",
|
|
17
|
+
label,
|
|
18
|
+
error,
|
|
19
|
+
hint,
|
|
20
|
+
id,
|
|
21
|
+
...props
|
|
22
|
+
}: TextareaProps) {
|
|
23
|
+
const autoId = React.useId();
|
|
24
|
+
const taId = id ?? autoId;
|
|
25
|
+
const msgId = `${taId}-msg`;
|
|
26
|
+
const hintId = `${taId}-hint`;
|
|
27
|
+
const describedBy = [error ? msgId : null, hint ? hintId : null].filter(Boolean).join(" ") || undefined;
|
|
28
|
+
return (
|
|
29
|
+
<div className={cn("cn-field", className)} data-slot="textarea-field" data-material={material}>
|
|
30
|
+
{label ? (
|
|
31
|
+
<label className="cn-field__label" htmlFor={taId}>
|
|
32
|
+
{label}
|
|
33
|
+
</label>
|
|
34
|
+
) : null}
|
|
35
|
+
<textarea
|
|
36
|
+
id={taId}
|
|
37
|
+
className={cn("cn-textarea", `cn-input--size-${size}`, error && "cn-textarea--error")}
|
|
38
|
+
aria-invalid={error ? true : undefined}
|
|
39
|
+
aria-describedby={describedBy}
|
|
40
|
+
{...props}
|
|
41
|
+
/>
|
|
42
|
+
{hint && !error ? (
|
|
43
|
+
<span className="cn-field__hint" id={hintId}>
|
|
44
|
+
{hint}
|
|
45
|
+
</span>
|
|
46
|
+
) : null}
|
|
47
|
+
{error ? (
|
|
48
|
+
<span className="cn-field__message" id={msgId}>
|
|
49
|
+
{error}
|
|
50
|
+
</span>
|
|
51
|
+
) : null}
|
|
52
|
+
</div>
|
|
53
|
+
);
|
|
54
|
+
}
|
package/src/Toast.tsx
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { ToastVariant, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface ToastProps extends React.ComponentProps<"div"> {
|
|
6
|
+
material?: Material;
|
|
7
|
+
variant?: ToastVariant;
|
|
8
|
+
/** Auto-dismiss delay in ms; paused while hovered or focused. `0` keeps the toast until dismissed. */
|
|
9
|
+
duration?: number;
|
|
10
|
+
onDismiss?: () => void;
|
|
11
|
+
dismissLabel?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function Toast({
|
|
15
|
+
material,
|
|
16
|
+
className,
|
|
17
|
+
variant = "neutral",
|
|
18
|
+
duration = 6000,
|
|
19
|
+
onDismiss,
|
|
20
|
+
dismissLabel = "Dismiss",
|
|
21
|
+
children,
|
|
22
|
+
onMouseEnter,
|
|
23
|
+
onMouseLeave,
|
|
24
|
+
onFocus,
|
|
25
|
+
onBlur,
|
|
26
|
+
onKeyDown,
|
|
27
|
+
...props
|
|
28
|
+
}: ToastProps) {
|
|
29
|
+
const [visible, setVisible] = React.useState(true);
|
|
30
|
+
const [paused, setPaused] = React.useState(false);
|
|
31
|
+
const onDismissRef = React.useRef(onDismiss);
|
|
32
|
+
React.useLayoutEffect(() => {
|
|
33
|
+
onDismissRef.current = onDismiss;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const dismiss = React.useCallback(() => {
|
|
37
|
+
setVisible(false);
|
|
38
|
+
onDismissRef.current?.();
|
|
39
|
+
}, []);
|
|
40
|
+
|
|
41
|
+
React.useEffect(() => {
|
|
42
|
+
if (!visible || paused || duration <= 0) return;
|
|
43
|
+
const timer = setTimeout(dismiss, duration);
|
|
44
|
+
return () => clearTimeout(timer);
|
|
45
|
+
}, [visible, paused, duration, dismiss]);
|
|
46
|
+
|
|
47
|
+
if (!visible) return null;
|
|
48
|
+
const assertive = variant === "danger";
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div
|
|
52
|
+
data-slot="toast"
|
|
53
|
+
data-material={material}
|
|
54
|
+
role={assertive ? "alert" : "status"}
|
|
55
|
+
aria-live={assertive ? "assertive" : "polite"}
|
|
56
|
+
className={cn("cn-toast", `cn-toast--${variant}`, className)}
|
|
57
|
+
{...props}
|
|
58
|
+
onMouseEnter={(e) => {
|
|
59
|
+
onMouseEnter?.(e);
|
|
60
|
+
setPaused(true);
|
|
61
|
+
}}
|
|
62
|
+
onMouseLeave={(e) => {
|
|
63
|
+
onMouseLeave?.(e);
|
|
64
|
+
setPaused(false);
|
|
65
|
+
}}
|
|
66
|
+
onFocus={(e) => {
|
|
67
|
+
onFocus?.(e);
|
|
68
|
+
setPaused(true);
|
|
69
|
+
}}
|
|
70
|
+
onBlur={(e) => {
|
|
71
|
+
onBlur?.(e);
|
|
72
|
+
if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setPaused(false);
|
|
73
|
+
}}
|
|
74
|
+
onKeyDown={(e) => {
|
|
75
|
+
onKeyDown?.(e);
|
|
76
|
+
if (e.key === "Escape" && !e.defaultPrevented) {
|
|
77
|
+
e.preventDefault();
|
|
78
|
+
dismiss();
|
|
79
|
+
}
|
|
80
|
+
}}
|
|
81
|
+
>
|
|
82
|
+
<svg className="cn-toast__icon" viewBox="0 0 20 20" aria-hidden="true">
|
|
83
|
+
<circle cx="10" cy="10" r="8" fill="none" stroke="currentColor" strokeWidth="1.5" />
|
|
84
|
+
<path d="M10 6v5M10 14h.01" stroke="currentColor" strokeWidth="1.5" fill="none" />
|
|
85
|
+
</svg>
|
|
86
|
+
<div className="cn-toast__body">{children}</div>
|
|
87
|
+
<button
|
|
88
|
+
type="button"
|
|
89
|
+
className="cn-button cn-button--ghost cn-button--size-sm cn-toast__close"
|
|
90
|
+
aria-label={dismissLabel}
|
|
91
|
+
onClick={dismiss}
|
|
92
|
+
>
|
|
93
|
+
<span aria-hidden="true">×</span>
|
|
94
|
+
</button>
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
}
|
package/src/Toggle.tsx
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { ToggleSize, Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface ToggleProps extends Omit<React.ComponentProps<"input">, "size" | "type"> {
|
|
6
|
+
material?: Material;
|
|
7
|
+
size?: ToggleSize;
|
|
8
|
+
label?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function Toggle({ material, className, size = "md", label, id, onKeyDown, ...props }: ToggleProps) {
|
|
12
|
+
const autoId = React.useId();
|
|
13
|
+
const toggleId = id ?? autoId;
|
|
14
|
+
return (
|
|
15
|
+
<label
|
|
16
|
+
className={cn("cn-toggle", `cn-toggle--size-${size}`, className)}
|
|
17
|
+
htmlFor={toggleId}
|
|
18
|
+
data-slot="toggle"
|
|
19
|
+
data-material={material}
|
|
20
|
+
>
|
|
21
|
+
<span className="cn-toggle__track">
|
|
22
|
+
<input
|
|
23
|
+
id={toggleId}
|
|
24
|
+
className="cn-toggle__input"
|
|
25
|
+
type="checkbox"
|
|
26
|
+
role="switch"
|
|
27
|
+
{...props}
|
|
28
|
+
onKeyDown={(e) => {
|
|
29
|
+
onKeyDown?.(e);
|
|
30
|
+
// Switches toggle on Enter as well as Space; a native checkbox only does Space.
|
|
31
|
+
if (e.key === "Enter" && !e.defaultPrevented) {
|
|
32
|
+
e.preventDefault();
|
|
33
|
+
e.currentTarget.click();
|
|
34
|
+
}
|
|
35
|
+
}}
|
|
36
|
+
/>
|
|
37
|
+
<span className="cn-toggle__thumb" aria-hidden="true" />
|
|
38
|
+
</span>
|
|
39
|
+
{label ? <span className="cn-toggle__label">{label}</span> : null}
|
|
40
|
+
</label>
|
|
41
|
+
);
|
|
42
|
+
}
|
package/src/Tooltip.tsx
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { Material } from "@crystal-neuo/contract";
|
|
3
|
+
import { cn } from "./cn.ts";
|
|
4
|
+
|
|
5
|
+
export interface TooltipProps extends React.ComponentProps<"span"> {
|
|
6
|
+
material?: Material;
|
|
7
|
+
label: string;
|
|
8
|
+
children: React.ReactElement<{ "aria-describedby"?: string }>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function Tooltip({
|
|
12
|
+
material,
|
|
13
|
+
className,
|
|
14
|
+
label,
|
|
15
|
+
children,
|
|
16
|
+
onMouseEnter,
|
|
17
|
+
onMouseLeave,
|
|
18
|
+
onFocus,
|
|
19
|
+
onBlur,
|
|
20
|
+
...props
|
|
21
|
+
}: TooltipProps) {
|
|
22
|
+
const [open, setOpen] = React.useState(false);
|
|
23
|
+
const id = React.useId();
|
|
24
|
+
|
|
25
|
+
React.useEffect(() => {
|
|
26
|
+
if (!open) return;
|
|
27
|
+
function onKey(e: KeyboardEvent) {
|
|
28
|
+
if (e.key !== "Escape") return;
|
|
29
|
+
// Consume the key so an enclosing dialog stays open; a second Escape closes it.
|
|
30
|
+
e.preventDefault();
|
|
31
|
+
setOpen(false);
|
|
32
|
+
}
|
|
33
|
+
document.addEventListener("keydown", onKey);
|
|
34
|
+
return () => document.removeEventListener("keydown", onKey);
|
|
35
|
+
}, [open]);
|
|
36
|
+
|
|
37
|
+
// Keep any description the trigger already has.
|
|
38
|
+
const describedBy = [children.props["aria-describedby"], id].filter(Boolean).join(" ");
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<span
|
|
42
|
+
className={cn("cn-tooltip", className)}
|
|
43
|
+
data-slot="tooltip"
|
|
44
|
+
data-material={material}
|
|
45
|
+
{...props}
|
|
46
|
+
onMouseEnter={(e) => {
|
|
47
|
+
onMouseEnter?.(e);
|
|
48
|
+
setOpen(true);
|
|
49
|
+
}}
|
|
50
|
+
onMouseLeave={(e) => {
|
|
51
|
+
onMouseLeave?.(e);
|
|
52
|
+
setOpen(false);
|
|
53
|
+
}}
|
|
54
|
+
onFocus={(e) => {
|
|
55
|
+
onFocus?.(e);
|
|
56
|
+
setOpen(true);
|
|
57
|
+
}}
|
|
58
|
+
onBlur={(e) => {
|
|
59
|
+
onBlur?.(e);
|
|
60
|
+
setOpen(false);
|
|
61
|
+
}}
|
|
62
|
+
>
|
|
63
|
+
{React.cloneElement(children, { "aria-describedby": describedBy })}
|
|
64
|
+
{/* Always in the DOM so aria-describedby resolves when focus arrives. */}
|
|
65
|
+
<span role="tooltip" id={id} className="cn-tooltip__bubble" hidden={!open}>
|
|
66
|
+
{label}
|
|
67
|
+
</span>
|
|
68
|
+
</span>
|
|
69
|
+
);
|
|
70
|
+
}
|
package/src/cn.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Minimal class-name joiner. Crystal Neuo components compose `cn-*` classes
|
|
2
|
+
// (not Tailwind utilities), so a dependency-free joiner is enough — no need
|
|
3
|
+
// for clsx/tailwind-merge. Consumer `className` always comes last so it wins.
|
|
4
|
+
export function cn(...parts: Array<string | false | null | undefined>): string {
|
|
5
|
+
return parts.filter(Boolean).join(" ");
|
|
6
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Core
|
|
2
|
+
export { Button, type ButtonProps } from "./Button.tsx";
|
|
3
|
+
export { Card, type CardProps } from "./Card.tsx";
|
|
4
|
+
export { Input, type InputProps } from "./Input.tsx";
|
|
5
|
+
export { Toggle, type ToggleProps } from "./Toggle.tsx";
|
|
6
|
+
export { Nav, NavLink, type NavProps, type NavLinkProps } from "./Nav.tsx";
|
|
7
|
+
export { Modal, type ModalProps } from "./Modal.tsx";
|
|
8
|
+
export { Tabs, type TabsProps, type TabItem } from "./Tabs.tsx";
|
|
9
|
+
export { Tooltip, type TooltipProps } from "./Tooltip.tsx";
|
|
10
|
+
export { Badge, type BadgeProps } from "./Badge.tsx";
|
|
11
|
+
|
|
12
|
+
// Form
|
|
13
|
+
export { Select, type SelectProps, type SelectOption } from "./Select.tsx";
|
|
14
|
+
export { Checkbox, type CheckboxProps } from "./Checkbox.tsx";
|
|
15
|
+
export { Radio, type RadioProps, type RadioOption } from "./Radio.tsx";
|
|
16
|
+
export { Slider, type SliderProps } from "./Slider.tsx";
|
|
17
|
+
export { DatePicker, type DatePickerProps } from "./DatePicker.tsx";
|
|
18
|
+
export { Textarea, type TextareaProps } from "./Textarea.tsx";
|
|
19
|
+
export { FileUpload, type FileUploadProps } from "./FileUpload.tsx";
|
|
20
|
+
|
|
21
|
+
// Data
|
|
22
|
+
export { Table, type TableProps, type TableColumn } from "./Table.tsx";
|
|
23
|
+
export { List, ListItem, type ListProps, type ListItemProps } from "./List.tsx";
|
|
24
|
+
export { Avatar, type AvatarProps } from "./Avatar.tsx";
|
|
25
|
+
export { Progress, type ProgressProps } from "./Progress.tsx";
|
|
26
|
+
export { Skeleton, type SkeletonProps } from "./Skeleton.tsx";
|
|
27
|
+
export { Breadcrumb, type BreadcrumbProps, type Crumb } from "./Breadcrumb.tsx";
|
|
28
|
+
export { Pagination, type PaginationProps } from "./Pagination.tsx";
|
|
29
|
+
|
|
30
|
+
// Feedback
|
|
31
|
+
export { Toast, type ToastProps } from "./Toast.tsx";
|
|
32
|
+
export { Alert, type AlertProps } from "./Alert.tsx";
|
|
33
|
+
export { Dialog, type DialogProps } from "./Dialog.tsx";
|
|
34
|
+
export { Drawer, type DrawerProps } from "./Drawer.tsx";
|
|
35
|
+
|
|
36
|
+
// App shell
|
|
37
|
+
export { SegmentedControl, type SegmentedControlProps, type SegmentedOption } from "./SegmentedControl.tsx";
|
|
38
|
+
export { Sheet, type SheetProps } from "./Sheet.tsx";
|
|
39
|
+
export { TabBar, type TabBarProps, type TabBarItem } from "./TabBar.tsx";
|
|
40
|
+
export { Menu, type MenuProps, type MenuEntry } from "./Menu.tsx";
|
|
41
|
+
|
|
42
|
+
export { cn } from "./cn.ts";
|
|
43
|
+
export { useFocusTrap } from "./useFocusTrap.ts";
|
|
44
|
+
export { useModalDialog, closeOnScrim } from "./useModalDialog.ts";
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
const FOCUSABLE =
|
|
4
|
+
'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Traps focus inside `ref` while `active`, closes on Escape, and restores
|
|
8
|
+
* focus to the previously-focused element on deactivate.
|
|
9
|
+
*
|
|
10
|
+
* For non-modal surfaces that cannot be a native `<dialog>`. Modal, Dialog,
|
|
11
|
+
* and Drawer use `useModalDialog`, which gets inertness and top-layer
|
|
12
|
+
* stacking from the platform instead.
|
|
13
|
+
*/
|
|
14
|
+
export function useFocusTrap(
|
|
15
|
+
ref: React.RefObject<HTMLElement | null>,
|
|
16
|
+
active: boolean,
|
|
17
|
+
onClose: () => void,
|
|
18
|
+
): void {
|
|
19
|
+
// Latest callback without re-running the effect: an inline arrow would
|
|
20
|
+
// otherwise restore and re-grab focus on every parent render.
|
|
21
|
+
const onCloseRef = React.useRef(onClose);
|
|
22
|
+
React.useLayoutEffect(() => {
|
|
23
|
+
onCloseRef.current = onClose;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
React.useEffect(() => {
|
|
27
|
+
if (!active) return;
|
|
28
|
+
const node = ref.current;
|
|
29
|
+
if (!node) return;
|
|
30
|
+
const previouslyFocused = document.activeElement as HTMLElement | null;
|
|
31
|
+
|
|
32
|
+
const focusables = () => Array.from(node.querySelectorAll<HTMLElement>(FOCUSABLE));
|
|
33
|
+
// With nothing focusable inside, focus the container itself so Tab cannot
|
|
34
|
+
// walk the page behind it.
|
|
35
|
+
if (!node.hasAttribute("tabindex")) node.setAttribute("tabindex", "-1");
|
|
36
|
+
(focusables()[0] ?? node).focus();
|
|
37
|
+
|
|
38
|
+
function onKey(e: KeyboardEvent) {
|
|
39
|
+
// A nested surface already handled this key.
|
|
40
|
+
if (e.defaultPrevented) return;
|
|
41
|
+
if (e.key === "Escape") {
|
|
42
|
+
e.preventDefault();
|
|
43
|
+
onCloseRef.current();
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (e.key !== "Tab") return;
|
|
47
|
+
const items = focusables();
|
|
48
|
+
if (items.length === 0) {
|
|
49
|
+
e.preventDefault();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const first = items[0]!;
|
|
53
|
+
const last = items[items.length - 1]!;
|
|
54
|
+
if (e.shiftKey && (document.activeElement === first || document.activeElement === node)) {
|
|
55
|
+
e.preventDefault();
|
|
56
|
+
last.focus();
|
|
57
|
+
} else if (!e.shiftKey && document.activeElement === last) {
|
|
58
|
+
e.preventDefault();
|
|
59
|
+
first.focus();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Listen on the trapped node, not document, so Escape is scoped to the
|
|
64
|
+
// surface that has focus.
|
|
65
|
+
node.addEventListener("keydown", onKey);
|
|
66
|
+
return () => {
|
|
67
|
+
node.removeEventListener("keydown", onKey);
|
|
68
|
+
previouslyFocused?.focus?.();
|
|
69
|
+
};
|
|
70
|
+
}, [ref, active]);
|
|
71
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Drives a native `<dialog>` as a modal overlay from a controlled `open` prop.
|
|
5
|
+
* Shared by Modal, Dialog, and Drawer so their behavior cannot drift.
|
|
6
|
+
*
|
|
7
|
+
* `showModal()` gives the platform guarantees a hand-rolled overlay cannot:
|
|
8
|
+
* the rest of the page becomes inert, the dialog renders in the top layer
|
|
9
|
+
* (no portal, immune to transformed ancestors), only the topmost dialog
|
|
10
|
+
* receives Escape, and focus returns to the opener when it closes.
|
|
11
|
+
*/
|
|
12
|
+
export function useModalDialog(
|
|
13
|
+
ref: React.RefObject<HTMLDialogElement | null>,
|
|
14
|
+
open: boolean,
|
|
15
|
+
onClose: () => void,
|
|
16
|
+
): void {
|
|
17
|
+
// Read the latest callback without re-running the effects below, so an
|
|
18
|
+
// inline `onClose={() => setOpen(false)}` never closes and reopens the dialog.
|
|
19
|
+
const onCloseRef = React.useRef(onClose);
|
|
20
|
+
const openRef = React.useRef(open);
|
|
21
|
+
React.useLayoutEffect(() => {
|
|
22
|
+
onCloseRef.current = onClose;
|
|
23
|
+
openRef.current = open;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const openerRef = React.useRef<HTMLElement | null>(null);
|
|
27
|
+
|
|
28
|
+
React.useEffect(() => {
|
|
29
|
+
const dialog = ref.current;
|
|
30
|
+
if (!dialog) return;
|
|
31
|
+
if (open && !dialog.open) {
|
|
32
|
+
openerRef.current = document.activeElement as HTMLElement | null;
|
|
33
|
+
dialog.showModal();
|
|
34
|
+
} else if (!open && dialog.open) {
|
|
35
|
+
dialog.close();
|
|
36
|
+
restoreFocus(dialog, openerRef.current);
|
|
37
|
+
}
|
|
38
|
+
}, [ref, open]);
|
|
39
|
+
|
|
40
|
+
React.useEffect(() => {
|
|
41
|
+
const dialog = ref.current;
|
|
42
|
+
if (!dialog) return;
|
|
43
|
+
// Escape fires `cancel`; keep the dialog open until the owner says so.
|
|
44
|
+
function onCancel(e: Event) {
|
|
45
|
+
e.preventDefault();
|
|
46
|
+
onCloseRef.current();
|
|
47
|
+
}
|
|
48
|
+
// The browser may still close the dialog itself (repeated Escape without
|
|
49
|
+
// user activation); report it so the controlled `open` state follows.
|
|
50
|
+
function onNativeClose() {
|
|
51
|
+
if (openRef.current) onCloseRef.current();
|
|
52
|
+
}
|
|
53
|
+
dialog.addEventListener("cancel", onCancel);
|
|
54
|
+
dialog.addEventListener("close", onNativeClose);
|
|
55
|
+
return () => {
|
|
56
|
+
dialog.removeEventListener("cancel", onCancel);
|
|
57
|
+
dialog.removeEventListener("close", onNativeClose);
|
|
58
|
+
if (dialog.open) dialog.close();
|
|
59
|
+
};
|
|
60
|
+
}, [ref]);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The platform returns focus to the opener only if focus is still inside the
|
|
65
|
+
* dialog when it closes. The panel content may already have unmounted (focus
|
|
66
|
+
* fell to <body>), so restore explicitly unless focus has moved on elsewhere.
|
|
67
|
+
*/
|
|
68
|
+
export function restoreFocus(dialog: HTMLElement, opener: HTMLElement | null): void {
|
|
69
|
+
const active = document.activeElement;
|
|
70
|
+
const lost = !active || active === document.body || dialog.contains(active);
|
|
71
|
+
if (lost && opener?.isConnected) opener.focus();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Close when the press lands on the dialog's own box (the scrim), not its panel. */
|
|
75
|
+
export function closeOnScrim(onClose: () => void) {
|
|
76
|
+
return (e: React.MouseEvent<HTMLDialogElement>) => {
|
|
77
|
+
if (e.target !== e.currentTarget) return;
|
|
78
|
+
// The press would otherwise move focus to the (now hidden) dialog after it
|
|
79
|
+
// closes, undoing the return of focus to the opener.
|
|
80
|
+
e.preventDefault();
|
|
81
|
+
onClose();
|
|
82
|
+
};
|
|
83
|
+
}
|