@seanyeh77/ui 0.45.0 → 0.47.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/dist/bundle.js +19 -19
- package/dist/components/Alert/Alert.d.ts +2 -1
- package/dist/components/Alert/Alert.js +15 -17
- package/dist/components/Alert/Alert.js.map +1 -1
- package/dist/components/AppShell/AppShell.d.ts +3 -3
- package/dist/components/AppShell/AppShell.js +9 -10
- package/dist/components/AppShell/AppShell.js.map +1 -1
- package/dist/components/Badge/Badge.d.ts +4 -4
- package/dist/components/Badge/Badge.js +7 -1
- package/dist/components/Badge/Badge.js.map +1 -1
- package/dist/components/Breadcrumb/Breadcrumb.js +1 -0
- package/dist/components/Breadcrumb/Breadcrumb.js.map +1 -1
- package/dist/components/Calendar/Calendar.js +6 -2
- package/dist/components/Calendar/Calendar.js.map +1 -1
- package/dist/components/ConsoleLayout/ConsoleLayout.js +15 -12
- package/dist/components/ConsoleLayout/ConsoleLayout.js.map +1 -1
- package/dist/components/DataTable/DataTable.d.ts +8 -0
- package/dist/components/DataTable/DataTable.js +188 -34
- package/dist/components/DataTable/DataTable.js.map +1 -1
- package/dist/components/DataTable/filters.js +10 -4
- package/dist/components/DataTable/filters.js.map +1 -1
- package/dist/components/DatePicker/DatePicker.js +1 -0
- package/dist/components/DatePicker/DatePicker.js.map +1 -1
- package/dist/components/DateRangePicker/DateRangePicker.js +1 -0
- package/dist/components/DateRangePicker/DateRangePicker.js.map +1 -1
- package/dist/components/DetailList/DetailList.d.ts +8 -1
- package/dist/components/DetailList/DetailList.js +16 -2
- package/dist/components/DetailList/DetailList.js.map +1 -1
- package/dist/components/EmptyState/EmptyState.js +19 -1
- package/dist/components/EmptyState/EmptyState.js.map +1 -1
- package/dist/components/EventTimeline/EventTimeline.js +6 -3
- package/dist/components/EventTimeline/EventTimeline.js.map +1 -1
- package/dist/components/FileUpload/FileUpload.js +2 -2
- package/dist/components/FileUpload/FileUpload.js.map +1 -1
- package/dist/components/Grid/Grid.d.ts +5 -2
- package/dist/components/Grid/Grid.js +4 -3
- package/dist/components/Grid/Grid.js.map +1 -1
- package/dist/components/InlineCode/InlineCode.js +7 -6
- package/dist/components/InlineCode/InlineCode.js.map +1 -1
- package/dist/components/KpiCard/KpiCard.d.ts +6 -4
- package/dist/components/KpiCard/KpiCard.js +6 -12
- package/dist/components/KpiCard/KpiCard.js.map +1 -1
- package/dist/components/LayerGraph/LayerGraph.js +47 -13
- package/dist/components/LayerGraph/LayerGraph.js.map +1 -1
- package/dist/components/MultiSelect/MultiSelect.js +73 -45
- package/dist/components/MultiSelect/MultiSelect.js.map +1 -1
- package/dist/components/NotificationBell/NotificationBell.d.ts +6 -3
- package/dist/components/NotificationBell/NotificationBell.js +14 -3
- package/dist/components/NotificationBell/NotificationBell.js.map +1 -1
- package/dist/components/PageHeader/PageHeader.d.ts +3 -2
- package/dist/components/PageHeader/PageHeader.js +10 -1
- package/dist/components/PageHeader/PageHeader.js.map +1 -1
- package/dist/components/RelativeTime/RelativeTime.js +4 -3
- package/dist/components/RelativeTime/RelativeTime.js.map +1 -1
- package/dist/components/Select/Select.js +1 -1
- package/dist/components/Select/Select.js.map +1 -1
- package/dist/components/SettingsLayout/SettingsLayout.d.ts +6 -4
- package/dist/components/SettingsLayout/SettingsLayout.js +38 -11
- package/dist/components/SettingsLayout/SettingsLayout.js.map +1 -1
- package/dist/components/SideNav/SideNav.d.ts +10 -5
- package/dist/components/SideNav/SideNav.js +10 -6
- package/dist/components/SideNav/SideNav.js.map +1 -1
- package/dist/components/SplitPane/SplitPane.d.ts +2 -2
- package/dist/components/SplitPane/SplitPane.js +29 -9
- package/dist/components/SplitPane/SplitPane.js.map +1 -1
- package/dist/components/TextField/TextField.d.ts +8 -2
- package/dist/components/TextField/TextField.js +59 -13
- package/dist/components/TextField/TextField.js.map +1 -1
- package/dist/components/TopBar/TopBar.d.ts +2 -2
- package/dist/components/TopBar/TopBar.js.map +1 -1
- package/dist/components/Workbench/Workbench.d.ts +8 -5
- package/dist/components/Workbench/Workbench.js +184 -157
- package/dist/components/Workbench/Workbench.js.map +1 -1
- package/dist/components/ZoneMap/ZoneMap.js +4 -4
- package/dist/components/ZoneMap/ZoneMap.js.map +1 -1
- package/dist/components/useFormErrors/useFormErrors.d.ts +5 -2
- package/dist/components/useFormErrors/useFormErrors.js +10 -4
- package/dist/components/useFormErrors/useFormErrors.js.map +1 -1
- package/dist/css/AccentPicker.css +19 -0
- package/dist/css/Alert.css +30 -7
- package/dist/css/AppShell.css +12 -0
- package/dist/css/Calendar.css +7 -0
- package/dist/css/ConsoleLayout.css +3 -0
- package/dist/css/DataTable.css +70 -8
- package/dist/css/DetailList.css +11 -0
- package/dist/css/EmptyState.css +8 -0
- package/dist/css/EventTimeline.css +11 -4
- package/dist/css/Grid.css +15 -0
- package/dist/css/KpiCard.css +1 -0
- package/dist/css/LayerGraph.css +1 -1
- package/dist/css/LoadingBar.css +4 -1
- package/dist/css/LoginCard.css +1 -1
- package/dist/css/MobileTabBar.css +6 -4
- package/dist/css/MultiSelect.css +23 -3
- package/dist/css/Select.css +9 -0
- package/dist/css/SettingsLayout.css +29 -14
- package/dist/css/Tabs.css +36 -17
- package/dist/css/Toast.css +7 -0
- package/dist/css/TopBar.css +6 -3
- package/dist/css/Workbench.css +10 -5
- package/dist/css/base.css +90 -3
- package/dist/css/end.css +5 -2
- package/dist/fonts-latin.css +2 -0
- package/dist/fonts-tc.css +2 -0
- package/dist/format.d.ts +4 -3
- package/dist/format.js +13 -12
- package/dist/format.js.map +1 -1
- package/dist/i18n.js +17 -6
- package/dist/i18n.js.map +1 -1
- package/dist/icons/Icon.d.ts +2 -1
- package/dist/icons/Icon.js +4 -3
- package/dist/icons/Icon.js.map +1 -1
- package/dist/icons/all.js +23 -1
- package/dist/icons/all.js.map +1 -1
- package/dist/icons/names.d.ts +22 -0
- package/dist/icons/names.js +11 -0
- package/dist/icons/names.js.map +1 -1
- package/dist/icons/ph/credit-card.d.ts +10 -0
- package/dist/icons/ph/credit-card.js +11 -0
- package/dist/icons/ph/credit-card.js.map +1 -0
- package/dist/icons/ph/map-pin.d.ts +10 -0
- package/dist/icons/ph/map-pin.js +11 -0
- package/dist/icons/ph/map-pin.js.map +1 -0
- package/dist/icons/ph/package.d.ts +10 -0
- package/dist/icons/ph/package.js +11 -0
- package/dist/icons/ph/package.js.map +1 -0
- package/dist/icons/ph/pause.d.ts +10 -0
- package/dist/icons/ph/pause.js +11 -0
- package/dist/icons/ph/pause.js.map +1 -0
- package/dist/icons/ph/phone.d.ts +10 -0
- package/dist/icons/ph/phone.js +11 -0
- package/dist/icons/ph/phone.js.map +1 -0
- package/dist/icons/ph/play.d.ts +10 -0
- package/dist/icons/ph/play.js +11 -0
- package/dist/icons/ph/play.js.map +1 -0
- package/dist/icons/ph/prohibit.d.ts +10 -0
- package/dist/icons/ph/prohibit.js +11 -0
- package/dist/icons/ph/prohibit.js.map +1 -0
- package/dist/icons/ph/receipt.d.ts +10 -0
- package/dist/icons/ph/receipt.js +11 -0
- package/dist/icons/ph/receipt.js.map +1 -0
- package/dist/icons/ph/shopping-cart.d.ts +10 -0
- package/dist/icons/ph/shopping-cart.js +11 -0
- package/dist/icons/ph/shopping-cart.js.map +1 -0
- package/dist/icons/ph/storefront.d.ts +10 -0
- package/dist/icons/ph/storefront.js +11 -0
- package/dist/icons/ph/storefront.js.map +1 -0
- package/dist/icons/ph/truck.d.ts +10 -0
- package/dist/icons/ph/truck.js +11 -0
- package/dist/icons/ph/truck.js.map +1 -0
- package/dist/icons/store.d.ts +3 -1
- package/dist/icons/store.js +17 -12
- package/dist/icons/store.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/internal/FloatingPanel.js +5 -2
- package/dist/internal/FloatingPanel.js.map +1 -1
- package/dist/internal/clickSteps.js +11 -0
- package/dist/internal/clickSteps.js.map +1 -0
- package/dist/internal/util.js +7 -1
- package/dist/internal/util.js.map +1 -1
- package/dist/styles.css +499 -162
- package/dist/theme.d.ts +7 -0
- package/dist/theme.js +39 -15
- package/dist/theme.js.map +1 -1
- package/docs/Alert.md +1 -1
- package/docs/AppShell.md +4 -4
- package/docs/Badge.md +2 -1
- package/docs/BarChart.md +1 -1
- package/docs/Checkbox.md +2 -2
- package/docs/CodeView.md +12 -22
- package/docs/ConsoleLayout.md +1 -0
- package/docs/DataTable.md +16 -11
- package/docs/DatePicker.md +1 -1
- package/docs/DateRangePicker.md +1 -0
- package/docs/DetailList.md +12 -1
- package/docs/Drawer.md +1 -1
- package/docs/EmptyState.md +1 -0
- package/docs/EventTimeline.md +1 -1
- package/docs/Format.md +2 -1
- package/docs/Graph.md +1 -0
- package/docs/Grid.md +9 -0
- package/docs/KpiCard.md +5 -4
- package/docs/LoadingBar.md +1 -1
- package/docs/LoginCard.md +1 -1
- package/docs/Markdown.md +4 -37
- package/docs/MobileTabBar.md +1 -1
- package/docs/MultiSelect.md +3 -2
- package/docs/NotificationBell.md +2 -2
- package/docs/PageHeader.md +2 -1
- package/docs/RadioGroup.md +1 -1
- package/docs/RelativeTime.md +2 -2
- package/docs/SettingsLayout.md +5 -4
- package/docs/SideNav.md +2 -1
- package/docs/Skeleton.md +2 -2
- package/docs/SplitPane.md +3 -9
- package/docs/Tabs.md +14 -11
- package/docs/Tag.md +4 -3
- package/docs/TextField.md +2 -0
- package/docs/Toast.md +1 -1
- package/docs/TopBar.md +2 -2
- package/docs/Workbench.md +9 -6
- package/docs/agents.md +13 -13
- package/docs/data.md +2 -1
- package/docs/guidelines.md +4 -4
- package/docs/icons.md +2 -2
- package/docs/layout.md +3 -3
- package/docs/theme.md +6 -3
- package/docs/useFormErrors.md +2 -1
- package/package.json +4 -2
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { tr } from "../../i18n.js";
|
|
3
|
-
import { cx } from "../../internal/util.js";
|
|
3
|
+
import { cx, useNarrow } from "../../internal/util.js";
|
|
4
|
+
import { Tabs } from "../Tabs/Tabs.js";
|
|
5
|
+
import { Button } from "../Button/Button.js";
|
|
4
6
|
import { devWarn } from "../../internal/dev.js";
|
|
5
7
|
import { ShellTitle } from "../../internal/shell.js";
|
|
6
8
|
import { announce } from "../../announce.js";
|
|
@@ -9,6 +11,8 @@ import { codeText } from "../InlineCode/InlineCode.js";
|
|
|
9
11
|
import * as React from "react";
|
|
10
12
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
11
13
|
// src/components/SettingsLayout/SettingsLayout.tsx
|
|
14
|
+
const HASH_PREFIX = "settings-";
|
|
15
|
+
const hashRouted = () => typeof location !== "undefined" && location.hash.startsWith("#/");
|
|
12
16
|
/** A settings page: categories on the left, their sections on the right. Changes save at once. */
|
|
13
17
|
function SettingsLayout({ title, categories, value, onChange, urlSync = true, toast: toast$1, className }) {
|
|
14
18
|
const shell = React.useContext(ShellTitle);
|
|
@@ -21,8 +25,9 @@ function SettingsLayout({ title, categories, value, onChange, urlSync = true, to
|
|
|
21
25
|
devWarn(title && shell.title, "SettingsLayout", "the page title shows twice: AppShell has `title` and SettingsLayout has `title`. Inside AppShell, give the title to AppShell only.");
|
|
22
26
|
const sync = urlSync && value === void 0;
|
|
23
27
|
const fromHash = () => {
|
|
24
|
-
if (!sync || typeof location === "undefined") return void 0;
|
|
25
|
-
const
|
|
28
|
+
if (!sync || typeof location === "undefined" || hashRouted()) return void 0;
|
|
29
|
+
const raw = decodeURIComponent(location.hash.slice(1));
|
|
30
|
+
const id = raw.startsWith(HASH_PREFIX) ? raw.slice(9) : raw;
|
|
26
31
|
return categories.some((c) => c.id === id) ? id : void 0;
|
|
27
32
|
};
|
|
28
33
|
const [own, setOwn] = React.useState(() => fromHash() ?? value ?? categories[0]?.id);
|
|
@@ -37,11 +42,14 @@ function SettingsLayout({ title, categories, value, onChange, urlSync = true, to
|
|
|
37
42
|
window.addEventListener("hashchange", onHash);
|
|
38
43
|
return () => window.removeEventListener("hashchange", onHash);
|
|
39
44
|
}, [sync]);
|
|
40
|
-
const
|
|
45
|
+
const narrow = useNarrow();
|
|
46
|
+
const choose = (c, said = false) => {
|
|
41
47
|
if (c.id === item?.id) return;
|
|
42
48
|
setOwn(c.id);
|
|
43
|
-
if (sync
|
|
44
|
-
|
|
49
|
+
if (sync && typeof window !== "undefined" && window.history?.replaceState && !hashRouted()) try {
|
|
50
|
+
window.history.replaceState(window.history.state, "", location.pathname + location.search + "#settings-" + encodeURIComponent(c.id));
|
|
51
|
+
} catch {}
|
|
52
|
+
if (!said) announce(c.label);
|
|
45
53
|
onChange?.(c.id);
|
|
46
54
|
};
|
|
47
55
|
return /* @__PURE__ */ jsxs("div", {
|
|
@@ -53,7 +61,19 @@ function SettingsLayout({ title, categories, value, onChange, urlSync = true, to
|
|
|
53
61
|
}) : null,
|
|
54
62
|
/* @__PURE__ */ jsxs("div", {
|
|
55
63
|
className: "ui-settings-body",
|
|
56
|
-
children: [/* @__PURE__ */ jsx(
|
|
64
|
+
children: [narrow ? /* @__PURE__ */ jsx(Tabs, {
|
|
65
|
+
className: "ui-settings-tabs",
|
|
66
|
+
label: tr("設定分類"),
|
|
67
|
+
items: categories.map((c) => ({
|
|
68
|
+
id: c.id,
|
|
69
|
+
label: c.label
|
|
70
|
+
})),
|
|
71
|
+
value: item?.id ?? "",
|
|
72
|
+
onChange: (id) => {
|
|
73
|
+
const c = categories.find((x) => x.id === id);
|
|
74
|
+
if (c) choose(c, true);
|
|
75
|
+
}
|
|
76
|
+
}) : /* @__PURE__ */ jsx("nav", {
|
|
57
77
|
className: "ui-settings-nav",
|
|
58
78
|
"aria-label": tr("設定分類"),
|
|
59
79
|
children: categories.map((c) => {
|
|
@@ -76,13 +96,19 @@ function SettingsLayout({ title, categories, value, onChange, urlSync = true, to
|
|
|
76
96
|
});
|
|
77
97
|
}
|
|
78
98
|
/** One group of settings with a heading. It is a card on its own; inside a Card it drops its shadow. */
|
|
79
|
-
function SettingsSection({ title, description, children, className }) {
|
|
99
|
+
function SettingsSection({ title, description, actions, children, className }) {
|
|
80
100
|
return /* @__PURE__ */ jsxs("section", {
|
|
81
101
|
className: cx("ui-settings-section", className),
|
|
82
102
|
children: [
|
|
83
|
-
title ? /* @__PURE__ */
|
|
84
|
-
className: "ui-settings-
|
|
85
|
-
children: title
|
|
103
|
+
title || actions ? /* @__PURE__ */ jsxs("div", {
|
|
104
|
+
className: "ui-settings-head",
|
|
105
|
+
children: [title ? /* @__PURE__ */ jsx("h2", {
|
|
106
|
+
className: "ui-settings-h",
|
|
107
|
+
children: title
|
|
108
|
+
}) : null, actions ? /* @__PURE__ */ jsx("div", {
|
|
109
|
+
className: "ui-settings-actions",
|
|
110
|
+
children: actions
|
|
111
|
+
}) : null]
|
|
86
112
|
}) : null,
|
|
87
113
|
description ? /* @__PURE__ */ jsx("p", {
|
|
88
114
|
className: "ui-settings-desc",
|
|
@@ -96,6 +122,7 @@ function SettingsSection({ title, description, children, className }) {
|
|
|
96
122
|
* `<label>`, so clicking the text toggles a Switch. */
|
|
97
123
|
function SettingsRow({ label, description, children, className }) {
|
|
98
124
|
const id = React.useId();
|
|
125
|
+
devWarn(React.isValidElement(children) && children.type === Button, "SettingsRow", "a Button inside SettingsRow is pressed by a click anywhere on the row, since the row is a <label>. Put the action in SettingsSection's actions instead.");
|
|
99
126
|
const control = description && React.isValidElement(children) ? React.cloneElement(children, { "aria-describedby": id }) : children;
|
|
100
127
|
return /* @__PURE__ */ jsxs("label", {
|
|
101
128
|
className: cx("ui-settings-row", className),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SettingsLayout.js","names":["toast"],"sources":["../../../src/components/SettingsLayout/SettingsLayout.tsx"],"sourcesContent":["import * as React from 'react';\nimport { tr } from '../../i18n';\nimport { cx } from '../../internal/util';\nimport { devWarn } from '../../internal/dev';\nimport { ShellTitle } from '../../internal/shell';\nimport { announce } from '../../announce';\nimport { AutoToaster, toast as showToast } from '../Toast/Toast';\nimport { codeText } from '../InlineCode/InlineCode';\n\nexport interface SettingsCategory {\n /** Also the URL hash of the category (#notifications): letters, digits and hyphens. */\n id: string;\n /** The name in the category list, a noun: 「通知」. */\n label: string;\n /** SettingsSections; a DangerZone goes last in the last category. */\n content: React.ReactNode;\n}\n\nexport interface SettingsLayoutProps {\n /** The page title. Inside AppShell leave it out and give AppShell title=\"設定\": the title shows once. */\n title?: string;\n /** The categories, in order. */\n categories: SettingsCategory[];\n /** Controlled category id. */\n value?: string;\n /** Called with the category id when another category is chosen. */\n onChange?: (id: string) => void;\n /** Uncontrolled: keep the open category in the URL hash (#notifications), so a reload or a shared link opens it.\n * Default true. */\n urlSync?: boolean;\n /** @deprecated call toast('已儲存', { id: 'saved' }) when a change is saved; AppShell shows it. Until removed: a\n * non-empty text shows through toast(), an empty one closes it. */\n toast?: string;\n /** Extra class on the root element. */\n className?: string;\n}\n\n/** A settings page: categories on the left, their sections on the right. Changes save at once. */\nexport function SettingsLayout({ title, categories, value, onChange, urlSync = true, toast, className }: SettingsLayoutProps) {\n const shell = React.useContext(ShellTitle);\n devWarn(\n toast !== undefined,\n 'SettingsLayout',\n \"toast is deprecated: call toast('已儲存', { id: 'saved' }) from @seanyeh77/ui when a change is saved. AppShell shows it above the phone tab bar; outside AppShell put one <Toaster /> on the page.\",\n );\n // The old prop, through the toast system: one toast that restarts when the text changes, closed when it is emptied.\n React.useEffect(() => {\n if (toast === undefined) return;\n if (toast) showToast(toast, { id: 'ui-settings-saved' });\n else showToast.close('ui-settings-saved');\n }, [toast]);\n devWarn(\n title && shell.title,\n 'SettingsLayout',\n 'the page title shows twice: AppShell has `title` and SettingsLayout has `title`. Inside AppShell, give the title to AppShell only.',\n );\n const sync = urlSync && value === undefined;\n const fromHash = () => {\n if (!sync || typeof location === 'undefined') return undefined;\n const id = decodeURIComponent(location.hash.slice(1));\n return categories.some((c) => c.id === id) ? id : undefined;\n };\n const [own, setOwn] = React.useState<string | undefined>(() => fromHash() ?? value ?? categories[0]?.id);\n const active = value ?? own;\n const item = categories.find((c) => c.id === active) ?? categories[0];\n // A link or the back button that changes the hash opens that category.\n React.useEffect(() => {\n if (!sync) return;\n const onHash = () => {\n const id = fromHash();\n if (id) setOwn(id);\n };\n window.addEventListener('hashchange', onHash);\n return () => window.removeEventListener('hashchange', onHash);\n // eslint-disable-next-line react-hooks/exhaustive-deps -- categories are read when the hash changes\n }, [sync]);\n const choose = (c: SettingsCategory) => {\n if (c.id === item?.id) return;\n setOwn(c.id);\n if (sync) history.replaceState(history.state, '', location.pathname + location.search + '#' + encodeURIComponent(c.id));\n announce(c.label);\n onChange?.(c.id);\n };\n return (\n <div className={cx('ui-settings', className)}>\n {title ? <h1 className=\"ui-settings-title\">{title}</h1> : null}\n <div className=\"ui-settings-body\">\n <nav className=\"ui-settings-nav\" aria-label={tr('設定分類')}>\n {categories.map((c) => {\n const on = c.id === item?.id;\n return (\n <button\n key={c.id}\n type=\"button\"\n className={cx('ui-settings-cat', on && 'is-on')}\n aria-current={on ? 'page' : undefined}\n onClick={() => choose(c)}\n >\n {c.label}\n </button>\n );\n })}\n </nav>\n <div className=\"ui-settings-content\">{item?.content}</div>\n </div>\n {toast !== undefined ? <AutoToaster /> : null}\n </div>\n );\n}\n\nexport interface SettingsSectionProps {\n /** The section's heading, 17 px like a Card title. */\n title?: string;\n /** One sentence under the title; `backticks` are code. */\n description?: string;\n /** Fields and SettingsRows. */\n children?: React.ReactNode;\n /** Extra class on the root element. */\n className?: string;\n}\n\n/** One group of settings with a heading. It is a card on its own; inside a Card it drops its shadow. */\nexport function SettingsSection({ title, description, children, className }: SettingsSectionProps) {\n return (\n <section className={cx('ui-settings-section', className)}>\n {title ? <h2 className=\"ui-settings-h\">{title}</h2> : null}\n {description ? <p className=\"ui-settings-desc\">{codeText(description)}</p> : null}\n {children}\n </section>\n );\n}\n\nexport interface SettingsRowProps {\n /** What the setting does, a short noun phrase: 「部署失敗時寄 Email」. Also give it to the Switch's `label`. */\n label: string;\n /** One sentence under the label; `backticks` are code. */\n description?: string;\n /** The control at the right end: a Switch (or a Select). */\n children: React.ReactElement<{ 'aria-describedby'?: string }>;\n /** Extra class on the root element. */\n className?: string;\n}\n\n/** One setting on one row: the label and a sentence on the left, the control on the right. The whole row is a\n * `<label>`, so clicking the text toggles a Switch. */\nexport function SettingsRow({ label, description, children, className }: SettingsRowProps) {\n const id = React.useId();\n const control = description && React.isValidElement(children) ? React.cloneElement(children, { 'aria-describedby': id }) : children;\n return (\n <label className={cx('ui-settings-row', className)}>\n <span className=\"ui-settings-rowtext\">\n <span className=\"ui-settings-rowlabel\">{label}</span>\n {description ? (\n <span id={id} className=\"ui-settings-rowdesc\">\n {codeText(description)}\n </span>\n ) : null}\n </span>\n {control}\n </label>\n );\n}\n"],"mappings":";;;;;;;;;;;;AAsCA,SAAgB,eAAe,EAAE,OAAO,YAAY,OAAO,UAAU,UAAU,MAAM,OAAA,SAAO,aAAkC;CAC5H,MAAM,QAAQ,MAAM,WAAW,UAAU;CACzC,QACEA,YAAU,KAAA,GACV,kBACA,iMACF;CAEA,MAAM,gBAAgB;EACpB,IAAIA,YAAU,KAAA,GAAW;EACzB,IAAIA,SAAO,MAAUA,SAAO,EAAE,IAAI,oBAAoB,CAAC;OAClD,MAAU,MAAM,mBAAmB;CAC1C,GAAG,CAACA,OAAK,CAAC;CACV,QACE,SAAS,MAAM,OACf,kBACA,oIACF;CACA,MAAM,OAAO,WAAW,UAAU,KAAA;CAClC,MAAM,iBAAiB;EACrB,IAAI,CAAC,QAAQ,OAAO,aAAa,aAAa,OAAO,KAAA;EACrD,MAAM,KAAK,mBAAmB,SAAS,KAAK,MAAM,CAAC,CAAC;EACpD,OAAO,WAAW,MAAM,MAAM,EAAE,OAAO,EAAE,IAAI,KAAK,KAAA;CACpD;CACA,MAAM,CAAC,KAAK,UAAU,MAAM,eAAmC,SAAS,KAAK,SAAS,WAAW,EAAE,EAAE,EAAE;CACvG,MAAM,SAAS,SAAS;CACxB,MAAM,OAAO,WAAW,MAAM,MAAM,EAAE,OAAO,MAAM,KAAK,WAAW;CAEnE,MAAM,gBAA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AD;IAAI,WAAU;IAAiB,UAAA;GAAU,CAAA,IAAI;GACrD,cAAc,oBAAC,KAAD;IAAG,WAAU;IAAoB,UAAA,SAAS,WAAW;GAAK,CAAA,IAAI;GAC5E;EACM;;AAEb;;;AAeA,SAAgB,YAAY,EAAE,OAAO,aAAa,UAAU,aAA+B;CACzF,MAAM,KAAK,MAAM,MAAM;CACvB,MAAM,UAAU,eAAe,MAAM,eAAe,QAAQ,IAAI,MAAM,aAAa,UAAU,EAAE,oBAAoB,GAAG,CAAC,IAAI;CAC3H,OACE,qBAAC,SAAD;EAAO,WAAW,GAAG,mBAAmB,SAAS;EAAjD,UAAA,CACE,qBAAC,QAAD;GAAM,WAAU;GAAhB,UAAA,CACE,oBAAC,QAAD;IAAM,WAAU;IAAwB,UAAA;GAAY,CAAA,GACnD,cACC,oBAAC,QAAD;IAAU;IAAI,WAAU;IACrB,UAAA,SAAS,WAAW;GACjB,CAAA,IACJ,IACA;EACL,CAAA,GAAA,OACI;;AAEX"}
|
|
1
|
+
{"version":3,"file":"SettingsLayout.js","names":["toast"],"sources":["../../../src/components/SettingsLayout/SettingsLayout.tsx"],"sourcesContent":["import * as React from 'react';\nimport { tr } from '../../i18n';\nimport { cx, useNarrow } from '../../internal/util';\nimport { Tabs } from '../Tabs/Tabs';\nimport { Button } from '../Button/Button';\nimport { devWarn } from '../../internal/dev';\nimport { ShellTitle } from '../../internal/shell';\nimport { announce } from '../../announce';\nimport { AutoToaster, toast as showToast } from '../Toast/Toast';\nimport { codeText } from '../InlineCode/InlineCode';\n\nexport interface SettingsCategory {\n /** Also the URL hash of the category (#settings-notifications): letters, digits and hyphens. */\n id: string;\n /** The name in the category list, a noun: 「通知」. */\n label: string;\n /** SettingsSections; a DangerZone goes last in the last category. */\n content: React.ReactNode;\n}\n\nexport interface SettingsLayoutProps {\n /** The page title. Inside AppShell leave it out and give AppShell title=\"設定\": the title shows once. */\n title?: string;\n /** The categories, in order. */\n categories: SettingsCategory[];\n /** Controlled category id. */\n value?: string;\n /** Called with the category id when another category is chosen. */\n onChange?: (id: string) => void;\n /** Uncontrolled: keep the open category in the URL hash (#settings-notifications), so a reload or a shared link\n * opens it. Off by itself while the hash is a hash router's route (#/settings). Default true. */\n urlSync?: boolean;\n /** @deprecated call toast('已儲存', { id: 'saved' }) when a change is saved; AppShell shows it. Until removed: a\n * non-empty text shows through toast(), an empty one closes it. */\n toast?: string;\n /** Extra class on the root element. */\n className?: string;\n}\n\n// The hash names the category with a prefix no other part of the page uses: #settings-notifications.\nconst HASH_PREFIX = 'settings-';\n// A hash router keeps its route in the hash (#/settings): the category then stays out of it, or the route would go.\nconst hashRouted = () => typeof location !== 'undefined' && location.hash.startsWith('#/');\n\n/** A settings page: categories on the left, their sections on the right. Changes save at once. */\nexport function SettingsLayout({ title, categories, value, onChange, urlSync = true, toast, className }: SettingsLayoutProps) {\n const shell = React.useContext(ShellTitle);\n devWarn(\n toast !== undefined,\n 'SettingsLayout',\n \"toast is deprecated: call toast('已儲存', { id: 'saved' }) from @seanyeh77/ui when a change is saved. AppShell shows it above the phone tab bar; outside AppShell put one <Toaster /> on the page.\",\n );\n // The old prop, through the toast system: one toast that restarts when the text changes, closed when it is emptied.\n React.useEffect(() => {\n if (toast === undefined) return;\n if (toast) showToast(toast, { id: 'ui-settings-saved' });\n else showToast.close('ui-settings-saved');\n }, [toast]);\n devWarn(\n title && shell.title,\n 'SettingsLayout',\n 'the page title shows twice: AppShell has `title` and SettingsLayout has `title`. Inside AppShell, give the title to AppShell only.',\n );\n const sync = urlSync && value === undefined;\n const fromHash = () => {\n if (!sync || typeof location === 'undefined' || hashRouted()) return undefined;\n // #settings-notifications, and the plain #notifications of links written before the prefix.\n const raw = decodeURIComponent(location.hash.slice(1));\n const id = raw.startsWith(HASH_PREFIX) ? raw.slice(HASH_PREFIX.length) : raw;\n return categories.some((c) => c.id === id) ? id : undefined;\n };\n const [own, setOwn] = React.useState<string | undefined>(() => fromHash() ?? value ?? categories[0]?.id);\n const active = value ?? own;\n const item = categories.find((c) => c.id === active) ?? categories[0];\n // A link or the back button that changes the hash opens that category.\n React.useEffect(() => {\n if (!sync) return;\n const onHash = () => {\n const id = fromHash();\n if (id) setOwn(id);\n };\n window.addEventListener('hashchange', onHash);\n return () => window.removeEventListener('hashchange', onHash);\n // eslint-disable-next-line react-hooks/exhaustive-deps -- categories are read when the hash changes\n }, [sync]);\n const narrow = useNarrow();\n const choose = (c: SettingsCategory, said = false) => {\n if (c.id === item?.id) return;\n setOwn(c.id);\n if (sync && typeof window !== 'undefined' && window.history?.replaceState && !hashRouted()) {\n // A sandboxed iframe can refuse history changes: the category still changes, only the address stays.\n try {\n window.history.replaceState(window.history.state, '', location.pathname + location.search + '#' + HASH_PREFIX + encodeURIComponent(c.id));\n } catch {\n /* the address is not updated */\n }\n }\n // Tabs (on a phone) say the name themselves.\n if (!said) announce(c.label);\n onChange?.(c.id);\n };\n return (\n <div className={cx('ui-settings', className)}>\n {title ? <h1 className=\"ui-settings-title\">{title}</h1> : null}\n <div className=\"ui-settings-body\">\n {narrow ? (\n // On a phone: the same Tabs as everywhere else (a row that scrolls sideways when the names do not fit).\n <Tabs\n className=\"ui-settings-tabs\"\n label={tr('設定分類')}\n items={categories.map((c) => ({ id: c.id, label: c.label }))}\n value={item?.id ?? ''}\n onChange={(id) => {\n const c = categories.find((x) => x.id === id);\n if (c) choose(c, true);\n }}\n />\n ) : (\n <nav className=\"ui-settings-nav\" aria-label={tr('設定分類')}>\n {categories.map((c) => {\n const on = c.id === item?.id;\n return (\n <button\n key={c.id}\n type=\"button\"\n className={cx('ui-settings-cat', on && 'is-on')}\n aria-current={on ? 'page' : undefined}\n onClick={() => choose(c)}\n >\n {c.label}\n </button>\n );\n })}\n </nav>\n )}\n <div className=\"ui-settings-content\">{item?.content}</div>\n </div>\n {toast !== undefined ? <AutoToaster /> : null}\n </div>\n );\n}\n\nexport interface SettingsSectionProps {\n /** The section's heading, 17 px like a Card title. */\n title?: string;\n /** One sentence under the title; `backticks` are code. */\n description?: string;\n /** Quiet buttons at the right end of the title row, like Card's: 「還原預設」. Not inside SettingsRows. */\n actions?: React.ReactNode;\n /** Fields and SettingsRows. */\n children?: React.ReactNode;\n /** Extra class on the root element. */\n className?: string;\n}\n\n/** One group of settings with a heading. It is a card on its own; inside a Card it drops its shadow. */\nexport function SettingsSection({ title, description, actions, children, className }: SettingsSectionProps) {\n return (\n <section className={cx('ui-settings-section', className)}>\n {title || actions ? (\n <div className=\"ui-settings-head\">\n {title ? <h2 className=\"ui-settings-h\">{title}</h2> : null}\n {actions ? <div className=\"ui-settings-actions\">{actions}</div> : null}\n </div>\n ) : null}\n {description ? <p className=\"ui-settings-desc\">{codeText(description)}</p> : null}\n {children}\n </section>\n );\n}\n\nexport interface SettingsRowProps {\n /** What the setting does, a short noun phrase: 「部署失敗時寄 Email」. Also give it to the Switch's `label`. */\n label: string;\n /** One sentence under the label; `backticks` are code. */\n description?: string;\n /** The control at the right end: a Switch (or a Select). */\n children: React.ReactElement<{ 'aria-describedby'?: string }>;\n /** Extra class on the root element. */\n className?: string;\n}\n\n/** One setting on one row: the label and a sentence on the left, the control on the right. The whole row is a\n * `<label>`, so clicking the text toggles a Switch. */\nexport function SettingsRow({ label, description, children, className }: SettingsRowProps) {\n const id = React.useId();\n // The row is a <label>: a button inside it is pressed by a click anywhere on the row.\n devWarn(\n React.isValidElement(children) && children.type === Button,\n 'SettingsRow',\n \"a Button inside SettingsRow is pressed by a click anywhere on the row, since the row is a <label>. Put the action in SettingsSection's actions instead.\",\n );\n const control = description && React.isValidElement(children) ? React.cloneElement(children, { 'aria-describedby': id }) : children;\n return (\n <label className={cx('ui-settings-row', className)}>\n <span className=\"ui-settings-rowtext\">\n <span className=\"ui-settings-rowlabel\">{label}</span>\n {description ? (\n <span id={id} className=\"ui-settings-rowdesc\">\n {codeText(description)}\n </span>\n ) : null}\n </span>\n {control}\n </label>\n );\n}\n"],"mappings":";;;;;;;;;;;;;AAwCA,MAAM,cAAc;AAEpB,MAAM,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;AAgBA,SAAgB,gBAAgB,EAAE,OAAO,aAAa,SAAS,UAAU,aAAmC;CAC1G,OACE,qBAAC,WAAD;EAAS,WAAW,GAAG,uBAAuB,SAAS;EAAvD,UAAA;GACG,SAAS,UACR,qBAAC,OAAD;IAAK,WAAU;IAAf,UAAA,CACG,QAAQ,oBAAC,MAAD;KAAI,WAAU;KAAiB,UAAA;IAAU,CAAA,IAAI,MACrD,UAAU,oBAAC,OAAD;KAAK,WAAU;KAAuB,UAAA;IAAa,CAAA,IAAI,IAC/D;GACH,CAAA,IAAA;GACH,cAAc,oBAAC,KAAD;IAAG,WAAU;IAAoB,UAAA,SAAS,WAAW;GAAK,CAAA,IAAI;GAC5E;EACM;;AAEb;;;AAeA,SAAgB,YAAY,EAAE,OAAO,aAAa,UAAU,aAA+B;CACzF,MAAM,KAAK,MAAM,MAAM;CAEvB,QACE,MAAM,eAAe,QAAQ,KAAK,SAAS,SAAS,QACpD,eACA,yJACF;CACA,MAAM,UAAU,eAAe,MAAM,eAAe,QAAQ,IAAI,MAAM,aAAa,UAAU,EAAE,oBAAoB,GAAG,CAAC,IAAI;CAC3H,OACE,qBAAC,SAAD;EAAO,WAAW,GAAG,mBAAmB,SAAS;EAAjD,UAAA,CACE,qBAAC,QAAD;GAAM,WAAU;GAAhB,UAAA,CACE,oBAAC,QAAD;IAAM,WAAU;IAAwB,UAAA;GAAY,CAAA,GACnD,cACC,oBAAC,QAAD;IAAU;IAAI,WAAU;IACrB,UAAA,SAAS,WAAW;GACjB,CAAA,IACJ,IACA;EACL,CAAA,GAAA,OACI;;AAEX"}
|
|
@@ -31,12 +31,17 @@ export interface SideNavProps {
|
|
|
31
31
|
onChange?: (id: string) => void;
|
|
32
32
|
/** 72 px with icons only, labels as tooltips. */
|
|
33
33
|
collapsed?: boolean;
|
|
34
|
-
/** Adds the collapse button at the bottom left. */
|
|
35
|
-
|
|
36
|
-
/** Adds the pin button at the bottom left (AppShell): pinned, the sidebar stays; not pinned, it hides to icons. */
|
|
37
|
-
onPin?: () => void;
|
|
34
|
+
/** Adds the collapse button at the bottom left; called with the state it asks for. */
|
|
35
|
+
onCollapsedChange?: (collapsed: boolean) => void;
|
|
38
36
|
/** Whether the sidebar is pinned: the pin button is filled and says 「取消釘選」. */
|
|
39
37
|
pinned?: boolean;
|
|
38
|
+
/** Adds the pin button at the bottom left (AppShell): pinned, the sidebar stays; not pinned, it hides to icons.
|
|
39
|
+
* Called with the state it asks for. */
|
|
40
|
+
onPinnedChange?: (pinned: boolean) => void;
|
|
41
|
+
/** @deprecated Use `onCollapsedChange`. */
|
|
42
|
+
onToggle?: () => void;
|
|
43
|
+
/** @deprecated Use `onPinnedChange`. */
|
|
44
|
+
onPin?: () => void;
|
|
40
45
|
/** The product at the top. Prefer { mark, name, href }: collapsed (72 px) it shows only the mark, still a link
|
|
41
46
|
* named by `name`; open, the mark with the name fading in beside it. Text alone gets its first character in an
|
|
42
47
|
* accent square as the mark. Any other element is hidden while collapsed. */
|
|
@@ -47,4 +52,4 @@ export interface SideNavProps {
|
|
|
47
52
|
className?: string;
|
|
48
53
|
}
|
|
49
54
|
/** The 240 px dashboard sidebar (72 px collapsed). The selection bar moves like a droplet. */
|
|
50
|
-
export declare function SideNav({ sections, value, onChange, collapsed, onToggle, onPin,
|
|
55
|
+
export declare function SideNav({ sections, value, onChange, collapsed, onCollapsedChange, pinned, onPinnedChange, onToggle, onPin, brand, label, className }: SideNavProps): React.JSX.Element;
|
|
@@ -57,7 +57,11 @@ function NavBrandRow({ brand, collapsed }) {
|
|
|
57
57
|
});
|
|
58
58
|
}
|
|
59
59
|
/** The 240 px dashboard sidebar (72 px collapsed). The selection bar moves like a droplet. */
|
|
60
|
-
function SideNav({ sections, value, onChange, collapsed = false,
|
|
60
|
+
function SideNav({ sections, value, onChange, collapsed = false, onCollapsedChange, pinned = false, onPinnedChange, onToggle, onPin, brand, label, className }) {
|
|
61
|
+
devWarn(onToggle && !onCollapsedChange, "SideNav", "`onToggle` is deprecated. Pass onCollapsedChange={(collapsed) => …}: it gets the state to set.");
|
|
62
|
+
devWarn(onPin && !onPinnedChange, "SideNav", "`onPin` is deprecated. Pass onPinnedChange={(pinned) => …}: it gets the state to set.");
|
|
63
|
+
const toggle = onCollapsedChange ? () => onCollapsedChange(!collapsed) : onToggle;
|
|
64
|
+
const pin = onPinnedChange ? () => onPinnedChange(!pinned) : onPin;
|
|
61
65
|
const refs = React.useRef({});
|
|
62
66
|
const wrap = React.useRef(null);
|
|
63
67
|
const [bar, setBar] = React.useState({
|
|
@@ -183,9 +187,9 @@ function SideNav({ sections, value, onChange, collapsed = false, onToggle, onPin
|
|
|
183
187
|
}) : null, sec.items.flatMap((it) => row(it, false))] }, i))]
|
|
184
188
|
})
|
|
185
189
|
}),
|
|
186
|
-
|
|
190
|
+
toggle || pin ? /* @__PURE__ */ jsxs("div", {
|
|
187
191
|
className: "ui-nav-foot",
|
|
188
|
-
children: [
|
|
192
|
+
children: [pin ? /* @__PURE__ */ jsx(Tooltip, {
|
|
189
193
|
label: pinned ? tr("取消釘選") : tr("釘選側邊欄"),
|
|
190
194
|
side: "right",
|
|
191
195
|
describe: false,
|
|
@@ -194,14 +198,14 @@ function SideNav({ sections, value, onChange, collapsed = false, onToggle, onPin
|
|
|
194
198
|
className: cx("ui-nav-collapse", "ui-nav-pin", pinned && "is-on"),
|
|
195
199
|
"aria-pressed": pinned,
|
|
196
200
|
"aria-label": pinned ? tr("取消釘選") : tr("釘選側邊欄"),
|
|
197
|
-
onClick:
|
|
201
|
+
onClick: pin,
|
|
198
202
|
children: /* @__PURE__ */ jsx(Icon, {
|
|
199
203
|
name: "push-pin",
|
|
200
204
|
size: 22,
|
|
201
205
|
filled: pinned
|
|
202
206
|
})
|
|
203
207
|
})
|
|
204
|
-
}) : null,
|
|
208
|
+
}) : null, toggle ? /* @__PURE__ */ jsx(Tooltip, {
|
|
205
209
|
label: toggleLabel,
|
|
206
210
|
side: "right",
|
|
207
211
|
describe: false,
|
|
@@ -209,7 +213,7 @@ function SideNav({ sections, value, onChange, collapsed = false, onToggle, onPin
|
|
|
209
213
|
type: "button",
|
|
210
214
|
className: "ui-nav-collapse",
|
|
211
215
|
"aria-label": toggleLabel,
|
|
212
|
-
onClick:
|
|
216
|
+
onClick: toggle,
|
|
213
217
|
children: /* @__PURE__ */ jsx(Icon, {
|
|
214
218
|
name: "sidebar-simple",
|
|
215
219
|
size: 22
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SideNav.js","names":["phCaretRight","phPushPin","phSidebarSimple"],"sources":["../../../src/components/SideNav/SideNav.tsx"],"sourcesContent":["import * as React from 'react';\nimport { tr } from '../../i18n';\nimport { Icon, IconOf, type IconSource } from '../../icons/Icon';\nimport { dropTransition, useMoving } from '../../motion';\nimport { announce } from '../../announce';\nimport { cx } from '../../internal/util';\nimport { devWarn } from '../../internal/dev';\nimport { Tooltip } from '../Tooltip/Tooltip';\nimport { includeIcons } from '../../icons/store';\nimport phCaretRight from '../../icons/ph/caret-right';\nimport phPushPin from '../../icons/ph/push-pin';\nimport phSidebarSimple from '../../icons/ph/sidebar-simple';\n\nincludeIcons(phCaretRight, phPushPin, phSidebarSimple);\n\nexport interface NavItem {\n id: string;\n label: string;\n /** Top-level items have an icon; the selected one is filled. */\n icon?: IconSource;\n /** Second level, shown under the item; it opens and closes. */\n children?: NavItem[];\n}\n\nexport interface NavSection {\n items: NavItem[];\n}\n\n/** The product at the top of the sidebar. */\nexport interface NavBrand {\n /** Logo or colour block, at most 24 px: stays where it is, collapsed and open. Default: the first character of\n * `name` in an accent square. */\n mark?: React.ReactNode;\n /** The product name: shown beside the mark when the sidebar is open, the link's name when collapsed. */\n name: string;\n /** Usually the home page. */\n href?: string;\n onClick?: (e: React.MouseEvent<HTMLElement>) => void;\n}\n\nconst isBrand = (b: unknown): b is NavBrand =>\n !!b && typeof b === 'object' && !React.isValidElement(b) && !Array.isArray(b) && typeof (b as NavBrand).name === 'string';\n\nfunction NavBrandRow({ brand, collapsed }: { brand: React.ReactNode | NavBrand; collapsed: boolean }) {\n const spec: NavBrand | null = isBrand(brand) ? brand : typeof brand === 'string' ? { name: brand } : null;\n devWarn(\n !spec && collapsed,\n 'SideNav',\n 'brand is an element, so the collapsed sidebar cannot tell which part is the logo and hides all of it. Pass brand={{ mark, name, href }}: collapsed shows mark, expanded adds the name.',\n );\n if (!spec) return <div className=\"ui-nav-brand\">{collapsed ? null : (brand as React.ReactNode)}</div>;\n const inner = (\n <>\n <span className=\"ui-nav-mark\" aria-hidden=\"true\">\n {spec.mark ?? <span className=\"ui-nav-monogram\">{Array.from(spec.name)[0]}</span>}\n </span>\n <span className=\"ui-nav-brand-name\">{spec.name}</span>\n </>\n );\n // Collapsed, the name is invisible but the link keeps it as its label.\n const label = collapsed ? spec.name : undefined;\n return (\n <div className=\"ui-nav-brand has-mark\">\n {spec.href ? (\n <a className=\"ui-nav-brand-link\" href={spec.href} onClick={spec.onClick} aria-label={label}>\n {inner}\n </a>\n ) : spec.onClick ? (\n <button type=\"button\" className=\"ui-nav-brand-link\" onClick={spec.onClick} aria-label={label}>\n {inner}\n </button>\n ) : (\n <span className=\"ui-nav-brand-link\" aria-label={label} role={label ? 'img' : undefined}>\n {inner}\n </span>\n )}\n </div>\n );\n}\n\nexport interface SideNavProps {\n /** Groups separated by a divider. */\n sections: NavSection[];\n /** Id of the current page. */\n value: string;\n /** Called with the id of the item the person picks. */\n onChange?: (id: string) => void;\n /** 72 px with icons only, labels as tooltips. */\n collapsed?: boolean;\n /** Adds the collapse button at the bottom left. */\n onToggle?: () => void;\n /** Adds the pin button at the bottom left (AppShell): pinned, the sidebar stays; not pinned, it hides to icons. */\n onPin?: () => void;\n /** Whether the sidebar is pinned: the pin button is filled and says 「取消釘選」. */\n pinned?: boolean;\n /** The product at the top. Prefer { mark, name, href }: collapsed (72 px) it shows only the mark, still a link\n * named by `name`; open, the mark with the name fading in beside it. Text alone gets its first character in an\n * accent square as the mark. Any other element is hidden while collapsed. */\n brand?: React.ReactNode | NavBrand;\n /** Default 「主要導覽」. */\n label?: string;\n /** Extra class on the root element. */\n className?: string;\n}\n\n/** The 240 px dashboard sidebar (72 px collapsed). The selection bar moves like a droplet. */\nexport function SideNav({ sections, value, onChange, collapsed = false, onToggle, onPin, pinned = false, brand, label, className }: SideNavProps) {\n const refs = React.useRef<Record<string, HTMLButtonElement | null>>({});\n const wrap = React.useRef<HTMLDivElement>(null);\n const [bar, setBar] = React.useState({ top: 0, bottom: 0, show: false, down: true });\n const last = React.useRef<{ top: number; bottom: number } | null>(null);\n const [moving, bump] = useMoving();\n // A page change is announced (not the first page): screen readers hear where they are now.\n const firstValue = React.useRef<string | undefined>(value);\n React.useEffect(() => {\n if (value === firstValue.current) return;\n firstValue.current = undefined;\n const all = sections.flatMap((sec) => sec.items.flatMap((it) => [it, ...(it.children ?? [])]));\n const it = all.find((x) => x.id === value);\n if (it) announce(it.label);\n // eslint-disable-next-line react-hooks/exhaustive-deps -- only when the page changes\n }, [value]);\n const [closed, setClosed] = React.useState<Record<string, boolean>>({});\n const closedKey = Object.keys(closed)\n .filter((k) => closed[k])\n .join();\n React.useLayoutEffect(() => {\n let el = refs.current[value];\n if (!el) {\n const parent = sections.flatMap((s) => s.items).find((it) => it.children?.some((c) => c.id === value));\n el = parent ? refs.current[parent.id] : null;\n }\n const box = wrap.current;\n if (!el || !box) return;\n const y = el.getBoundingClientRect().top - box.getBoundingClientRect().top;\n const top = Math.round(y) + 8;\n const bottom = Math.round(box.clientHeight - y - el.offsetHeight) + 8;\n const prev = last.current;\n if (prev && prev.top === top && prev.bottom === bottom) return;\n if (prev && prev.top !== top) bump();\n last.current = { top, bottom };\n setBar({ top, bottom, show: true, down: !prev || top >= prev.top });\n }, [value, collapsed, closedKey, sections, bump]);\n\n const row = (it: NavItem, sub: boolean): React.ReactNode[] => {\n const kids = it.children ?? [];\n const hasKids = kids.length > 0 && !collapsed;\n const isOpen = !closed[it.id];\n const selected = value === it.id || ((!hasKids || !isOpen) && kids.some((c) => c.id === value));\n const out: React.ReactNode[] = [\n <div key={it.id} className=\"ui-nav-row\">\n <button\n type=\"button\"\n ref={(n) => {\n refs.current[it.id] = n;\n }}\n className={cx('ui-nav-item', sub && 'is-sub', selected && 'is-selected')}\n aria-current={selected ? 'page' : undefined}\n aria-expanded={hasKids ? isOpen : undefined}\n aria-label={collapsed ? it.label : undefined}\n // Collapsed, a parent opens its first page; the page switches between its siblings itself.\n onClick={() => (hasKids ? setClosed({ ...closed, [it.id]: isOpen }) : onChange?.(kids[0]?.id ?? it.id))}\n >\n {it.icon && !sub ? <IconOf icon={it.icon} size={22} filled={selected} className=\"ui-nav-icon\" /> : null}\n {collapsed ? null : <span className=\"ui-nav-label\">{it.label}</span>}\n {hasKids ? <Icon name=\"caret-right\" size={16} className={cx('ui-nav-chev', isOpen && 'is-open')} /> : null}\n </button>\n {collapsed ? (\n <span className=\"ui-nav-tip\" role=\"tooltip\">\n {it.label}\n </span>\n ) : null}\n </div>,\n ];\n if (hasKids && isOpen) for (const c of kids) out.push(...row(c, true));\n return out;\n };\n\n const toggleLabel = collapsed ? tr('展開側邊欄') : tr('收合側邊欄');\n return (\n <aside className={cx('ui-sidenav', collapsed && 'is-collapsed', className)}>\n {brand ? <NavBrandRow brand={brand} collapsed={collapsed} /> : null}\n <nav className=\"ui-nav-list\" aria-label={label || tr('主要導覽')}>\n <div ref={wrap} className=\"ui-nav-inner\">\n {bar.show ? (\n <span\n className=\"ui-nav-bar\"\n aria-hidden=\"true\"\n style={{ top: bar.top, bottom: bar.bottom, transform: moving ? 'scaleX(.7)' : 'none', transition: dropTransition('top', 'bottom', bar.down) }}\n />\n ) : null}\n {sections.map((sec, i) => (\n <React.Fragment key={i}>\n {i > 0 ? <div className=\"ui-nav-divider\" role=\"separator\" /> : null}\n {sec.items.flatMap((it) => row(it, false))}\n </React.Fragment>\n ))}\n </div>\n </nav>\n {onToggle || onPin ? (\n <div className=\"ui-nav-foot\">\n {onPin ? (\n <Tooltip label={pinned ? tr('取消釘選') : tr('釘選側邊欄')} side=\"right\" describe={false}>\n <button\n type=\"button\"\n className={cx('ui-nav-collapse', 'ui-nav-pin', pinned && 'is-on')}\n aria-pressed={pinned}\n aria-label={pinned ? tr('取消釘選') : tr('釘選側邊欄')}\n onClick={onPin}\n >\n <Icon name=\"push-pin\" size={22} filled={pinned} />\n </button>\n </Tooltip>\n ) : null}\n {onToggle ? (\n <Tooltip label={toggleLabel} side=\"right\" describe={false}>\n <button type=\"button\" className=\"ui-nav-collapse\" aria-label={toggleLabel} onClick={onToggle}>\n <Icon name=\"sidebar-simple\" size={22} />\n </button>\n </Tooltip>\n ) : null}\n </div>\n ) : null}\n </aside>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;AAaA,aAAaA,qBAAcC,kBAAWC,sBAAe;AA2BrD,MAAM,WAAW,MACf,CAAC,CAAC,KAAK,OAAO,MAAM,YAAY,CAAC,MAAM,eAAe,CAAC,KAAK,CAAC,MAAM,QAAQ,CAAC,KAAK,OAAQ,EAAe,SAAS;AAEnH,SAAS,YAAY,EAAE,OAAO,aAAwE;CACpG,MAAM,OAAwB,QAAQ,KAAK,IAAI,QAAQ,OAAO,UAAU,WAAW,EAAE,MAAM,MAAM,IAAI;CACrG,QACE,CAAC,QAAQ,WACT,WACA,wLACF;CACA,IAAI,CAAC,MAAM,OAAO,oBAAC,OAAD;EAAK,WAAU;EAAgB,UAAA,YAAY,OAAQ;CAA+B,CAAA;CACpG,MAAM,QACJ,qBAAA,UAAA,EAAA,UAAA,CACE,oBAAC,QAAD;EAAM,WAAU;EAAc,eAAY;EACvC,UAAA,KAAK,QAAQ,oBAAC,QAAD;GAAM,WAAU;GAAmB,UAAA,MAAM,KAAK,KAAK,IAAI,CAAC,CAAC;EAAS,CAAA;CAC5E,CAAA,GACN,oBAAC,QAAD;EAAM,WAAU;EAAqB,UAAA,KAAK;CAAW,CAAA,CACrD,EAAA,CAAA;CAGJ,MAAM,QAAQ,YAAY,KAAK,OAAO,KAAA;CACtC,OACE,oBAA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UAAU,IAAI,IAAI;MAAE;KAC7I,CAAA,IACC,MACH,SAAS,KAAK,KAAK,MAClB,qBAAC,MAAM,UAAP,EAAA,UAAA,CACG,IAAI,IAAI,oBAAC,OAAD;MAAK,WAAU;MAAiB,MAAK;KAAa,CAAA,IAAI,MAC9D,IAAI,MAAM,SAAS,OAAO,IAAI,IAAI,KAAK,CAAC,CAC3B,EAAA,GAHK,CAGL,CACjB,CACE;;GACF,CAAA;GACJ,YAAY,QACX,qBAAC,OAAD;IAAK,WAAU;IAAf,UAAA,CACG,QACC,oBAAC,SAAD;KAAS,OAAO,SAAS,GAAG,MAAM,IAAI,GAAG,OAAO;KAAG,MAAK;KAAQ,UAAU;KACxE,UAAA,oBAAC,UAAD;MACE,MAAK;MACL,WAAW,GAAG,mBAAmB,cAAc,UAAU,OAAO;MAChE,gBAAc;MACd,cAAY,SAAS,GAAG,MAAM,IAAI,GAAG,OAAO;MAC5C,SAAS;MAET,UAAA,oBAAC,MAAD;OAAM,MAAK;OAAW,MAAM;OAAI,QAAQ;MAAS,CAAA;KAC3C,CAAA;IACD,CAAA,IACP,MACH,WACC,oBAAC,SAAD;KAAS,OAAO;KAAa,MAAK;KAAQ,UAAU;KAClD,UAAA,oBAAC,UAAD;MAAQ,MAAK;MAAS,WAAU;MAAkB,cAAY;MAAa,SAAS;MAClF,UAAA,oBAAC,MAAD;OAAM,MAAK;OAAiB,MAAM;MAAK,CAAA;KACjC,CAAA;IACD,CAAA,IACP,IACD;GACH,CAAA,IAAA;EACC;;AAEX"}
|
|
1
|
+
{"version":3,"file":"SideNav.js","names":["phCaretRight","phPushPin","phSidebarSimple"],"sources":["../../../src/components/SideNav/SideNav.tsx"],"sourcesContent":["import * as React from 'react';\nimport { tr } from '../../i18n';\nimport { Icon, IconOf, type IconSource } from '../../icons/Icon';\nimport { dropTransition, useMoving } from '../../motion';\nimport { announce } from '../../announce';\nimport { cx } from '../../internal/util';\nimport { devWarn } from '../../internal/dev';\nimport { Tooltip } from '../Tooltip/Tooltip';\nimport { includeIcons } from '../../icons/store';\nimport phCaretRight from '../../icons/ph/caret-right';\nimport phPushPin from '../../icons/ph/push-pin';\nimport phSidebarSimple from '../../icons/ph/sidebar-simple';\n\nincludeIcons(phCaretRight, phPushPin, phSidebarSimple);\n\nexport interface NavItem {\n id: string;\n label: string;\n /** Top-level items have an icon; the selected one is filled. */\n icon?: IconSource;\n /** Second level, shown under the item; it opens and closes. */\n children?: NavItem[];\n}\n\nexport interface NavSection {\n items: NavItem[];\n}\n\n/** The product at the top of the sidebar. */\nexport interface NavBrand {\n /** Logo or colour block, at most 24 px: stays where it is, collapsed and open. Default: the first character of\n * `name` in an accent square. */\n mark?: React.ReactNode;\n /** The product name: shown beside the mark when the sidebar is open, the link's name when collapsed. */\n name: string;\n /** Usually the home page. */\n href?: string;\n onClick?: (e: React.MouseEvent<HTMLElement>) => void;\n}\n\nconst isBrand = (b: unknown): b is NavBrand =>\n !!b && typeof b === 'object' && !React.isValidElement(b) && !Array.isArray(b) && typeof (b as NavBrand).name === 'string';\n\nfunction NavBrandRow({ brand, collapsed }: { brand: React.ReactNode | NavBrand; collapsed: boolean }) {\n const spec: NavBrand | null = isBrand(brand) ? brand : typeof brand === 'string' ? { name: brand } : null;\n devWarn(\n !spec && collapsed,\n 'SideNav',\n 'brand is an element, so the collapsed sidebar cannot tell which part is the logo and hides all of it. Pass brand={{ mark, name, href }}: collapsed shows mark, expanded adds the name.',\n );\n if (!spec) return <div className=\"ui-nav-brand\">{collapsed ? null : (brand as React.ReactNode)}</div>;\n const inner = (\n <>\n <span className=\"ui-nav-mark\" aria-hidden=\"true\">\n {spec.mark ?? <span className=\"ui-nav-monogram\">{Array.from(spec.name)[0]}</span>}\n </span>\n <span className=\"ui-nav-brand-name\">{spec.name}</span>\n </>\n );\n // Collapsed, the name is invisible but the link keeps it as its label.\n const label = collapsed ? spec.name : undefined;\n return (\n <div className=\"ui-nav-brand has-mark\">\n {spec.href ? (\n <a className=\"ui-nav-brand-link\" href={spec.href} onClick={spec.onClick} aria-label={label}>\n {inner}\n </a>\n ) : spec.onClick ? (\n <button type=\"button\" className=\"ui-nav-brand-link\" onClick={spec.onClick} aria-label={label}>\n {inner}\n </button>\n ) : (\n <span className=\"ui-nav-brand-link\" aria-label={label} role={label ? 'img' : undefined}>\n {inner}\n </span>\n )}\n </div>\n );\n}\n\nexport interface SideNavProps {\n /** Groups separated by a divider. */\n sections: NavSection[];\n /** Id of the current page. */\n value: string;\n /** Called with the id of the item the person picks. */\n onChange?: (id: string) => void;\n /** 72 px with icons only, labels as tooltips. */\n collapsed?: boolean;\n /** Adds the collapse button at the bottom left; called with the state it asks for. */\n onCollapsedChange?: (collapsed: boolean) => void;\n /** Whether the sidebar is pinned: the pin button is filled and says 「取消釘選」. */\n pinned?: boolean;\n /** Adds the pin button at the bottom left (AppShell): pinned, the sidebar stays; not pinned, it hides to icons.\n * Called with the state it asks for. */\n onPinnedChange?: (pinned: boolean) => void;\n /** @deprecated Use `onCollapsedChange`. */\n onToggle?: () => void;\n /** @deprecated Use `onPinnedChange`. */\n onPin?: () => void;\n /** The product at the top. Prefer { mark, name, href }: collapsed (72 px) it shows only the mark, still a link\n * named by `name`; open, the mark with the name fading in beside it. Text alone gets its first character in an\n * accent square as the mark. Any other element is hidden while collapsed. */\n brand?: React.ReactNode | NavBrand;\n /** Default 「主要導覽」. */\n label?: string;\n /** Extra class on the root element. */\n className?: string;\n}\n\n/** The 240 px dashboard sidebar (72 px collapsed). The selection bar moves like a droplet. */\nexport function SideNav({\n sections,\n value,\n onChange,\n collapsed = false,\n onCollapsedChange,\n pinned = false,\n onPinnedChange,\n onToggle,\n onPin,\n brand,\n label,\n className,\n}: SideNavProps) {\n devWarn(onToggle && !onCollapsedChange, 'SideNav', '`onToggle` is deprecated. Pass onCollapsedChange={(collapsed) => …}: it gets the state to set.');\n devWarn(onPin && !onPinnedChange, 'SideNav', '`onPin` is deprecated. Pass onPinnedChange={(pinned) => …}: it gets the state to set.');\n const toggle = onCollapsedChange ? () => onCollapsedChange(!collapsed) : onToggle;\n const pin = onPinnedChange ? () => onPinnedChange(!pinned) : onPin;\n const refs = React.useRef<Record<string, HTMLButtonElement | null>>({});\n const wrap = React.useRef<HTMLDivElement>(null);\n const [bar, setBar] = React.useState({ top: 0, bottom: 0, show: false, down: true });\n const last = React.useRef<{ top: number; bottom: number } | null>(null);\n const [moving, bump] = useMoving();\n // A page change is announced (not the first page): screen readers hear where they are now.\n const firstValue = React.useRef<string | undefined>(value);\n React.useEffect(() => {\n if (value === firstValue.current) return;\n firstValue.current = undefined;\n const all = sections.flatMap((sec) => sec.items.flatMap((it) => [it, ...(it.children ?? [])]));\n const it = all.find((x) => x.id === value);\n if (it) announce(it.label);\n // eslint-disable-next-line react-hooks/exhaustive-deps -- only when the page changes\n }, [value]);\n const [closed, setClosed] = React.useState<Record<string, boolean>>({});\n const closedKey = Object.keys(closed)\n .filter((k) => closed[k])\n .join();\n React.useLayoutEffect(() => {\n let el = refs.current[value];\n if (!el) {\n const parent = sections.flatMap((s) => s.items).find((it) => it.children?.some((c) => c.id === value));\n el = parent ? refs.current[parent.id] : null;\n }\n const box = wrap.current;\n if (!el || !box) return;\n const y = el.getBoundingClientRect().top - box.getBoundingClientRect().top;\n const top = Math.round(y) + 8;\n const bottom = Math.round(box.clientHeight - y - el.offsetHeight) + 8;\n const prev = last.current;\n if (prev && prev.top === top && prev.bottom === bottom) return;\n if (prev && prev.top !== top) bump();\n last.current = { top, bottom };\n setBar({ top, bottom, show: true, down: !prev || top >= prev.top });\n }, [value, collapsed, closedKey, sections, bump]);\n\n const row = (it: NavItem, sub: boolean): React.ReactNode[] => {\n const kids = it.children ?? [];\n const hasKids = kids.length > 0 && !collapsed;\n const isOpen = !closed[it.id];\n const selected = value === it.id || ((!hasKids || !isOpen) && kids.some((c) => c.id === value));\n const out: React.ReactNode[] = [\n <div key={it.id} className=\"ui-nav-row\">\n <button\n type=\"button\"\n ref={(n) => {\n refs.current[it.id] = n;\n }}\n className={cx('ui-nav-item', sub && 'is-sub', selected && 'is-selected')}\n aria-current={selected ? 'page' : undefined}\n aria-expanded={hasKids ? isOpen : undefined}\n aria-label={collapsed ? it.label : undefined}\n // Collapsed, a parent opens its first page; the page switches between its siblings itself.\n onClick={() => (hasKids ? setClosed({ ...closed, [it.id]: isOpen }) : onChange?.(kids[0]?.id ?? it.id))}\n >\n {it.icon && !sub ? <IconOf icon={it.icon} size={22} filled={selected} className=\"ui-nav-icon\" /> : null}\n {collapsed ? null : <span className=\"ui-nav-label\">{it.label}</span>}\n {hasKids ? <Icon name=\"caret-right\" size={16} className={cx('ui-nav-chev', isOpen && 'is-open')} /> : null}\n </button>\n {collapsed ? (\n <span className=\"ui-nav-tip\" role=\"tooltip\">\n {it.label}\n </span>\n ) : null}\n </div>,\n ];\n if (hasKids && isOpen) for (const c of kids) out.push(...row(c, true));\n return out;\n };\n\n const toggleLabel = collapsed ? tr('展開側邊欄') : tr('收合側邊欄');\n return (\n <aside className={cx('ui-sidenav', collapsed && 'is-collapsed', className)}>\n {brand ? <NavBrandRow brand={brand} collapsed={collapsed} /> : null}\n <nav className=\"ui-nav-list\" aria-label={label || tr('主要導覽')}>\n <div ref={wrap} className=\"ui-nav-inner\">\n {bar.show ? (\n <span\n className=\"ui-nav-bar\"\n aria-hidden=\"true\"\n style={{ top: bar.top, bottom: bar.bottom, transform: moving ? 'scaleX(.7)' : 'none', transition: dropTransition('top', 'bottom', bar.down) }}\n />\n ) : null}\n {sections.map((sec, i) => (\n <React.Fragment key={i}>\n {i > 0 ? <div className=\"ui-nav-divider\" role=\"separator\" /> : null}\n {sec.items.flatMap((it) => row(it, false))}\n </React.Fragment>\n ))}\n </div>\n </nav>\n {toggle || pin ? (\n <div className=\"ui-nav-foot\">\n {pin ? (\n <Tooltip label={pinned ? tr('取消釘選') : tr('釘選側邊欄')} side=\"right\" describe={false}>\n <button\n type=\"button\"\n className={cx('ui-nav-collapse', 'ui-nav-pin', pinned && 'is-on')}\n aria-pressed={pinned}\n aria-label={pinned ? tr('取消釘選') : tr('釘選側邊欄')}\n onClick={pin}\n >\n <Icon name=\"push-pin\" size={22} filled={pinned} />\n </button>\n </Tooltip>\n ) : null}\n {toggle ? (\n <Tooltip label={toggleLabel} side=\"right\" describe={false}>\n <button type=\"button\" className=\"ui-nav-collapse\" aria-label={toggleLabel} onClick={toggle}>\n <Icon name=\"sidebar-simple\" size={22} />\n </button>\n </Tooltip>\n ) : null}\n </div>\n ) : null}\n </aside>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;AAaA,aAAaA,qBAAcC,kBAAWC,sBAAe;AA2BrD,MAAM,WAAW,MACf,CAAC,CAAC,KAAK,OAAO,MAAM,YAAY,CAAC,MAAM,eAAe,CAAC,KAAK,CAAC,MAAM,QAAQ,CAAC,KAAK,OAAQ,EAAe,SAAS;AAEnH,SAAS,YAAY,EAAE,OAAO,aAAwE;CACpG,MAAM,OAAwB,QAAQ,KAAK,IAAI,QAAQ,OAAO,UAAU,WAAW,EAAE,MAAM,MAAM,IAAI;CACrG,QACE,CAAC,QAAQ,WACT,WACA,wLACF;CACA,IAAI,CAAC,MAAM,OAAO,oBAAC,OAAD;EAAK,WAAU;EAAgB,UAAA,YAAY,OAAQ;CAA+B,CAAA;CACpG,MAAM,QACJ,qBAAA,UAAA,EAAA,UAAA,CACE,oBAAC,QAAD;EAAM,WAAU;EAAc,eAAY;EACvC,UAAA,KAAK,QAAQ,oBAAC,QAAD;GAAM,WAAU;GAAmB,UAAA,MAAM,KAAK,KAAK,IAAI,CAAC,CAAC;EAAS,CAAA;CAC5E,CAAA,GACN,oBAAC,QAAD;EAAM,WAAU;EAAqB,UAAA,KAAK;CAAW,CAAA,CACrD,EAAA,CAAA;CAGJ,MAAM,QAAQ,YAAY,KAAK,OAAO,KAAA;CACtC,OACE,oBAAC,OAAD;EAAK,WAAU;EACZ,UAAA,KAAK,OACJ,oBAAC,KAAD;GAAG,WAAU;GAAoB,MAAM,KAAK;GAAM,SAAS,KAAK;GA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O;OAAE,KAAK,IAAI;OAAK,QAAQ,IAAI;OAAQ,WAAW,SAAS,eAAe;OAAQ,YAAY,eAAe,OAAO,UAAU,IAAI,IAAI;MAAE;KAC7I,CAAA,IACC,MACH,SAAS,KAAK,KAAK,MAClB,qBAAC,MAAM,UAAP,EAAA,UAAA,CACG,IAAI,IAAI,oBAAC,OAAD;MAAK,WAAU;MAAiB,MAAK;KAAa,CAAA,IAAI,MAC9D,IAAI,MAAM,SAAS,OAAO,IAAI,IAAI,KAAK,CAAC,CAC3B,EAAA,GAHK,CAGL,CACjB,CACE;;GACF,CAAA;GACJ,UAAU,MACT,qBAAC,OAAD;IAAK,WAAU;IAAf,UAAA,CACG,MACC,oBAAC,SAAD;KAAS,OAAO,SAAS,GAAG,MAAM,IAAI,GAAG,OAAO;KAAG,MAAK;KAAQ,UAAU;KACxE,UAAA,oBAAC,UAAD;MACE,MAAK;MACL,WAAW,GAAG,mBAAmB,cAAc,UAAU,OAAO;MAChE,gBAAc;MACd,cAAY,SAAS,GAAG,MAAM,IAAI,GAAG,OAAO;MAC5C,SAAS;MAET,UAAA,oBAAC,MAAD;OAAM,MAAK;OAAW,MAAM;OAAI,QAAQ;MAAS,CAAA;KAC3C,CAAA;IACD,CAAA,IACP,MACH,SACC,oBAAC,SAAD;KAAS,OAAO;KAAa,MAAK;KAAQ,UAAU;KAClD,UAAA,oBAAC,UAAD;MAAQ,MAAK;MAAS,WAAU;MAAkB,cAAY;MAAa,SAAS;MAClF,UAAA,oBAAC,MAAD;OAAM,MAAK;OAAiB,MAAM;MAAK,CAAA;KACjC,CAAA;IACD,CAAA,IACP,IACD;GACH,CAAA,IAAA;EACC;;AAEX"}
|
|
@@ -19,6 +19,6 @@ export interface SplitPaneProps {
|
|
|
19
19
|
/** Extra class on the root element. */
|
|
20
20
|
className?: string;
|
|
21
21
|
}
|
|
22
|
-
/** A main area and a side panel with a divider between them that can be dragged, moved with the arrow keys
|
|
23
|
-
* with a double-click. Under 960 px (its own width) the two stack, side panel below, without the divider. */
|
|
22
|
+
/** A main area and a side panel with a divider between them that can be dragged, moved with the arrow keys, stepped
|
|
23
|
+
* through preset widths with a click (narrowest, default, 1.5 times the default) and reset with a double-click or Enter. Under 960 px (its own width) the two stack, side panel below, without the divider. */
|
|
24
24
|
export declare function SplitPane({ a, b, defaultB, minA, minB, storageKey, label, onResize, className }: SplitPaneProps): React.JSX.Element;
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
import { tr } from "../../i18n.js";
|
|
3
3
|
import { browserKey, cx, resizeObserver } from "../../internal/util.js";
|
|
4
4
|
import { devWarn } from "../../internal/dev.js";
|
|
5
|
+
import { announce } from "../../announce.js";
|
|
6
|
+
import { nextPreset } from "../../internal/clickSteps.js";
|
|
5
7
|
import * as React from "react";
|
|
6
8
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
9
|
// src/components/SplitPane/SplitPane.tsx
|
|
@@ -26,15 +28,15 @@ const write = (key, v) => {
|
|
|
26
28
|
window.localStorage.setItem("ui-split:" + key, String(Math.round(v)));
|
|
27
29
|
} catch {}
|
|
28
30
|
};
|
|
29
|
-
/** A main area and a side panel with a divider between them that can be dragged, moved with the arrow keys
|
|
30
|
-
* with a double-click. Under 960 px (its own width) the two stack, side panel below, without the divider. */
|
|
31
|
+
/** A main area and a side panel with a divider between them that can be dragged, moved with the arrow keys, stepped
|
|
32
|
+
* through preset widths with a click (narrowest, default, 1.5 times the default) and reset with a double-click or Enter. Under 960 px (its own width) the two stack, side panel below, without the divider. */
|
|
31
33
|
function SplitPane({ a, b, defaultB = 420, minA = 360, minB = 280, storageKey, label, onResize, className }) {
|
|
32
34
|
devWarn(defaultB < minB, "SplitPane", `defaultB (${defaultB} px) is under minB (${minB} px): the panel starts at ${minB} px. Raise defaultB or lower minB.`);
|
|
33
35
|
const root = React.useRef(null);
|
|
34
36
|
const [width, setWidth] = React.useState(0);
|
|
35
37
|
const [side, setSide] = React.useState(() => (typeof window === "undefined" ? null : read(storageKey)) ?? defaultB);
|
|
36
38
|
const [drag, setDrag] = React.useState(false);
|
|
37
|
-
const dragging = React.useRef(
|
|
39
|
+
const dragging = React.useRef(null);
|
|
38
40
|
const id = React.useId();
|
|
39
41
|
React.useLayoutEffect(() => {
|
|
40
42
|
const el = root.current;
|
|
@@ -71,18 +73,35 @@ function SplitPane({ a, b, defaultB = 420, minA = 360, minB = 280, storageKey, l
|
|
|
71
73
|
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
72
74
|
} catch {}
|
|
73
75
|
e.currentTarget.focus({ preventScroll: true });
|
|
74
|
-
dragging.current =
|
|
76
|
+
dragging.current = {
|
|
77
|
+
x: e.clientX,
|
|
78
|
+
moved: false
|
|
79
|
+
};
|
|
75
80
|
setDrag(true);
|
|
76
81
|
};
|
|
77
82
|
const onMove = (e) => {
|
|
78
|
-
|
|
83
|
+
const d = dragging.current;
|
|
84
|
+
if (!d || !root.current) return;
|
|
85
|
+
if (!d.moved && Math.abs(e.clientX - d.x) < 4) return;
|
|
86
|
+
d.moved = true;
|
|
79
87
|
const r = root.current.getBoundingClientRect();
|
|
80
88
|
set(r.right - e.clientX - HANDLE / 2, false);
|
|
81
89
|
};
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
90
|
+
const presets = [
|
|
91
|
+
minB,
|
|
92
|
+
defaultB,
|
|
93
|
+
defaultB * 1.5
|
|
94
|
+
];
|
|
95
|
+
const onUp = (e, cancel = false) => {
|
|
96
|
+
const d = dragging.current;
|
|
97
|
+
if (!d) return;
|
|
98
|
+
dragging.current = null;
|
|
85
99
|
setDrag(false);
|
|
100
|
+
if (!d.moved && !cancel) {
|
|
101
|
+
const next = Math.round(Math.min(maxB, Math.max(minB, nextPreset(shown, presets))));
|
|
102
|
+
set(next);
|
|
103
|
+
announce(tr("面板 {n} px", { n: next }));
|
|
104
|
+
}
|
|
86
105
|
try {
|
|
87
106
|
if (e.currentTarget.hasPointerCapture?.(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);
|
|
88
107
|
} catch {}
|
|
@@ -110,12 +129,13 @@ function SplitPane({ a, b, defaultB = 420, minA = 360, minB = 280, storageKey, l
|
|
|
110
129
|
a: valueA,
|
|
111
130
|
b: shown
|
|
112
131
|
}),
|
|
132
|
+
"aria-description": tr("拖曳或按 ← → 調整;按一下切換預設寬度,按兩下或 Enter 還原"),
|
|
113
133
|
className: "ui-split-handle",
|
|
114
134
|
onKeyDown: onKey,
|
|
115
135
|
onPointerDown: onDown,
|
|
116
136
|
onPointerMove: onMove,
|
|
117
137
|
onPointerUp: onUp,
|
|
118
|
-
onPointerCancel: onUp,
|
|
138
|
+
onPointerCancel: (e) => onUp(e, true),
|
|
119
139
|
onDoubleClick: () => set(defaultB),
|
|
120
140
|
children: /* @__PURE__ */ jsxs("span", {
|
|
121
141
|
className: "ui-split-grip",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SplitPane.js","names":[],"sources":["../../../src/components/SplitPane/SplitPane.tsx"],"sourcesContent":["import * as React from 'react';\nimport { tr } from '../../i18n';\nimport { browserKey, cx, resizeObserver } from '../../internal/util';\nimport { devWarn } from '../../internal/dev';\n\nexport interface SplitPaneProps {\n /** The main area, on the left. */\n a: React.ReactNode;\n /** The side panel, on the right; its width is what the divider changes. */\n b: React.ReactNode;\n /** px. The side panel's width at first and after a double-click on the divider. Default 420. */\n defaultB?: number;\n /** px. The main area never gets narrower than this. Default 360. */\n minA?: number;\n /** px. The side panel never gets narrower than this. Default 280. */\n minB?: number;\n /** Remembers the side panel's width in localStorage under this name. Without it the width lasts until the page reloads. */\n storageKey?: string;\n /** Names the divider for screen readers. Default 「調整面板寬度」. */\n label?: string;\n /** After every change, with the side panel's new width in px. */\n onResize?: (b: number) => void;\n /** Extra class on the root element. */\n className?: string;\n}\n\n/** Width of the divider between the two areas, in px; a 48 px hit area is drawn over it. */\nconst HANDLE = 16;\n/** Narrower than this (px, the component's own width), the two areas stack and the divider goes away. */\nconst STACK = 960;\n/** One arrow key press, in px. */\nconst STEP = 16;\n\nconst read = (key: string | undefined): number | null => {\n if (!key) return null;\n try {\n const v = Number(window.localStorage.getItem('ui-split:' + key));\n return Number.isFinite(v) && v > 0 ? v : null;\n } catch {\n return null;\n }\n};\nconst write = (key: string | undefined, v: number) => {\n if (!key) return;\n try {\n window.localStorage.setItem('ui-split:' + key, String(Math.round(v)));\n } catch {\n // Storage blocked or full: the width still changes, it is just not remembered.\n }\n};\n\n/** A main area and a side panel with a divider between them that can be dragged, moved with the arrow keys and reset\n * with a double-click. Under 960 px (its own width) the two stack, side panel below, without the divider. */\nexport function SplitPane({ a, b, defaultB = 420, minA = 360, minB = 280, storageKey, label, onResize, className }: SplitPaneProps) {\n devWarn(defaultB < minB, 'SplitPane', `defaultB (${defaultB} px) is under minB (${minB} px): the panel starts at ${minB} px. Raise defaultB or lower minB.`);\n const root = React.useRef<HTMLDivElement>(null);\n const [width, setWidth] = React.useState(0);\n // The stored width, read on the first render in the browser. The grid columns are set only once the component has\n // measured itself, so the server's render and the first one in the browser match.\n const [side, setSide] = React.useState(() => (typeof window === 'undefined' ? null : read(storageKey)) ?? defaultB);\n const [drag, setDrag] = React.useState(false);\n // Read by the move and up handlers: a move can come before the render that follows the press.\n const dragging = React.useRef(false);\n const id = React.useId();\n React.useLayoutEffect(() => {\n const el = root.current;\n if (!el) return;\n const measure = () => setWidth(el.clientWidth);\n measure();\n const ro = resizeObserver(measure);\n ro?.observe(el);\n return () => ro?.disconnect();\n }, []);\n const stacked = width > 0 && width < STACK;\n // The side panel's width as it can be now: both areas keep their minimum.\n const maxB = Math.max(minB, width - HANDLE - minA);\n const shown = Math.round(Math.min(maxB, Math.max(minB, side)));\n const valueA = Math.max(0, width - HANDLE - shown);\n // The width after the last move, for the release that stores it (the render with it may not have happened yet).\n const latest = React.useRef(shown);\n const set = (v: number, keep = true) => {\n const next = Math.round(Math.min(maxB, Math.max(minB, v)));\n latest.current = next;\n setSide(next);\n if (keep) write(storageKey, next);\n onResize?.(next);\n };\n const onKey = (e: React.KeyboardEvent) => {\n if (browserKey(e)) return;\n // The value is the main area's width: ← makes it narrower (the panel wider), Home makes it as narrow as it goes.\n const to =\n e.key === 'ArrowLeft'\n ? shown + STEP\n : e.key === 'ArrowRight'\n ? shown - STEP\n : e.key === 'Home'\n ? maxB\n : e.key === 'End'\n ? minB\n : e.key === 'Enter'\n ? defaultB\n : null;\n if (to == null) return;\n e.preventDefault();\n set(to);\n };\n const onDown = (e: React.PointerEvent<HTMLDivElement>) => {\n if (e.button !== 0) return;\n e.preventDefault();\n // Keeps getting the moves when the pointer leaves the divider (a fast drag).\n try {\n e.currentTarget.setPointerCapture?.(e.pointerId);\n } catch {\n // No such pointer (a synthetic event): moves still come while over the divider.\n }\n e.currentTarget.focus({ preventScroll: true });\n dragging.current = true;\n setDrag(true);\n };\n const onMove = (e: React.PointerEvent<HTMLDivElement>) => {\n if (!dragging.current || !root.current) return;\n const r = root.current.getBoundingClientRect();\n set(r.right - e.clientX - HANDLE / 2, false);\n };\n const onUp = (e: React.PointerEvent<HTMLDivElement>) => {\n if (!dragging.current) return;\n dragging.current = false;\n setDrag(false);\n try {\n if (e.currentTarget.hasPointerCapture?.(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);\n } catch {\n // Already released.\n }\n if (root.current) write(storageKey, latest.current);\n };\n return (\n <div\n ref={root}\n className={cx('ui-split', stacked && 'is-stacked', drag && 'is-dragging', className)}\n style={stacked || !width ? undefined : { gridTemplateColumns: `minmax(0, 1fr) ${HANDLE}px ${shown}px` }}\n >\n <div className=\"ui-split-a\">{a}</div>\n {stacked ? null : (\n <div\n role=\"separator\"\n tabIndex={0}\n aria-orientation=\"vertical\"\n aria-label={label || tr('調整面板寬度')}\n aria-controls={id}\n aria-valuenow={valueA}\n aria-valuemin={minA}\n aria-valuemax={Math.max(minA, width - HANDLE - minB)}\n aria-valuetext={tr('主要區域 {a} px,面板 {b} px', { a: valueA, b: shown })}\n className=\"ui-split-handle\"\n onKeyDown={onKey}\n onPointerDown={onDown}\n onPointerMove={onMove}\n onPointerUp={onUp}\n onPointerCancel={onUp}\n onDoubleClick={() => set(defaultB)}\n >\n {/* GB: a small handle in the middle of the line, so the divider reads as something to drag. */}\n <span className=\"ui-split-grip\" aria-hidden=\"true\">\n <i />\n <i />\n <i />\n </span>\n </div>\n )}\n <div id={id} className=\"ui-split-b\">\n {b}\n </div>\n </div>\n );\n}\n"],"mappings":";;;;;;;;AA2BA,MAAM,SAAS;;AAEf,MAAM,QAAQ;;AAEd,MAAM,OAAO;AAEb,MAAM,QAAQ,QAA2C;CACvD,IAAI,CAAC,KAAK,OAAO;CACjB,IAAI;EACF,MAAM,IAAI,OAAO,OAAO,aAAa,QAAQ,cAAc,GAAG,CAAC;EAC/D,OAAO,OAAO,SAAS,CAAC,KAAK,IAAI,IAAI,IAAI;CAC3C,QAAQ;EACN,OAAO;CACT;AACF;AACA,MAAM,SAAS,KAAyB,MAAc;CACpD,IAAI,CAAC,KAAK;CACV,IAAI;EACF,OAAO,aAAa,QAAQ,cAAc,KAAK,OAAO,KAAK,MAAM,CAAC,CAAC,CAAC;CACtE,QAAQ,CAER;AACF;;;AAIA,SAAgB,UAAU,EAAE,GAAG,GAAG,WAAW,KAAK,OAAO,KAAK,OAAO,KAAK,YAAY,OAAO,UAAU,aAA6B;CAClI,QAAQ,WAAW,MAAM,aAAa,aAAa,SAAS,sBAAsB,KAAK,4BAA4B,KAAK,mCAAmC;CAC3J,MAAM,OAAO,MAAM,OAAuB,IAAI;CAC9C,MAAM,CAAC,OAAO,YAAY,MAAM,SAAS,CAAC;CAG1C,MAAM,CAAC,MAAM,WAAW,MAAM,gBAAgB,OAAO,WAAW,cAAc,OAAO,KAAK,UAAU,MAAM,QAAQ;CAClH,MAAM,CAAC,MAAM,WAAW,MAAM,SAAS,KAAK;CAE5C,MAAM,WAAW,MAAM,OAAO,KAAK;CACnC,MAAM,KAAK,MAAM,MAAM;CACvB,MAAM,sBAAsB;EAC1B,MAAM,KAAK,KA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SplitPane.js","names":[],"sources":["../../../src/components/SplitPane/SplitPane.tsx"],"sourcesContent":["import * as React from 'react';\nimport { tr } from '../../i18n';\nimport { browserKey, cx, resizeObserver } from '../../internal/util';\nimport { devWarn } from '../../internal/dev';\nimport { announce } from '../../announce';\nimport { DRAG_MIN, nextPreset } from '../../internal/clickSteps';\n\nexport interface SplitPaneProps {\n /** The main area, on the left. */\n a: React.ReactNode;\n /** The side panel, on the right; its width is what the divider changes. */\n b: React.ReactNode;\n /** px. The side panel's width at first and after a double-click on the divider. Default 420. */\n defaultB?: number;\n /** px. The main area never gets narrower than this. Default 360. */\n minA?: number;\n /** px. The side panel never gets narrower than this. Default 280. */\n minB?: number;\n /** Remembers the side panel's width in localStorage under this name. Without it the width lasts until the page reloads. */\n storageKey?: string;\n /** Names the divider for screen readers. Default 「調整面板寬度」. */\n label?: string;\n /** After every change, with the side panel's new width in px. */\n onResize?: (b: number) => void;\n /** Extra class on the root element. */\n className?: string;\n}\n\n/** Width of the divider between the two areas, in px; a 48 px hit area is drawn over it. */\nconst HANDLE = 16;\n/** Narrower than this (px, the component's own width), the two areas stack and the divider goes away. */\nconst STACK = 960;\n/** One arrow key press, in px. */\nconst STEP = 16;\n\nconst read = (key: string | undefined): number | null => {\n if (!key) return null;\n try {\n const v = Number(window.localStorage.getItem('ui-split:' + key));\n return Number.isFinite(v) && v > 0 ? v : null;\n } catch {\n return null;\n }\n};\nconst write = (key: string | undefined, v: number) => {\n if (!key) return;\n try {\n window.localStorage.setItem('ui-split:' + key, String(Math.round(v)));\n } catch {\n // Storage blocked or full: the width still changes, it is just not remembered.\n }\n};\n\n/** A main area and a side panel with a divider between them that can be dragged, moved with the arrow keys, stepped\n * through preset widths with a click (narrowest, default, 1.5 times the default) and reset with a double-click or Enter. Under 960 px (its own width) the two stack, side panel below, without the divider. */\nexport function SplitPane({ a, b, defaultB = 420, minA = 360, minB = 280, storageKey, label, onResize, className }: SplitPaneProps) {\n devWarn(defaultB < minB, 'SplitPane', `defaultB (${defaultB} px) is under minB (${minB} px): the panel starts at ${minB} px. Raise defaultB or lower minB.`);\n const root = React.useRef<HTMLDivElement>(null);\n const [width, setWidth] = React.useState(0);\n // The stored width, read on the first render in the browser. The grid columns are set only once the component has\n // measured itself, so the server's render and the first one in the browser match.\n const [side, setSide] = React.useState(() => (typeof window === 'undefined' ? null : read(storageKey)) ?? defaultB);\n const [drag, setDrag] = React.useState(false);\n // Read by the move and up handlers: a move can come before the render that follows the press. `moved` turns a press\n // into a drag once the pointer has gone DRAG_MIN px; released before that it was a click.\n const dragging = React.useRef<{ x: number; moved: boolean } | null>(null);\n const id = React.useId();\n React.useLayoutEffect(() => {\n const el = root.current;\n if (!el) return;\n const measure = () => setWidth(el.clientWidth);\n measure();\n const ro = resizeObserver(measure);\n ro?.observe(el);\n return () => ro?.disconnect();\n }, []);\n const stacked = width > 0 && width < STACK;\n // The side panel's width as it can be now: both areas keep their minimum.\n const maxB = Math.max(minB, width - HANDLE - minA);\n const shown = Math.round(Math.min(maxB, Math.max(minB, side)));\n const valueA = Math.max(0, width - HANDLE - shown);\n // The width after the last move, for the release that stores it (the render with it may not have happened yet).\n const latest = React.useRef(shown);\n const set = (v: number, keep = true) => {\n const next = Math.round(Math.min(maxB, Math.max(minB, v)));\n latest.current = next;\n setSide(next);\n if (keep) write(storageKey, next);\n onResize?.(next);\n };\n const onKey = (e: React.KeyboardEvent) => {\n if (browserKey(e)) return;\n // The value is the main area's width: ← makes it narrower (the panel wider), Home makes it as narrow as it goes.\n const to =\n e.key === 'ArrowLeft'\n ? shown + STEP\n : e.key === 'ArrowRight'\n ? shown - STEP\n : e.key === 'Home'\n ? maxB\n : e.key === 'End'\n ? minB\n : e.key === 'Enter'\n ? defaultB\n : null;\n if (to == null) return;\n e.preventDefault();\n set(to);\n };\n const onDown = (e: React.PointerEvent<HTMLDivElement>) => {\n if (e.button !== 0) return;\n e.preventDefault();\n // Keeps getting the moves when the pointer leaves the divider (a fast drag).\n try {\n e.currentTarget.setPointerCapture?.(e.pointerId);\n } catch {\n // No such pointer (a synthetic event): moves still come while over the divider.\n }\n e.currentTarget.focus({ preventScroll: true });\n dragging.current = { x: e.clientX, moved: false };\n setDrag(true);\n };\n const onMove = (e: React.PointerEvent<HTMLDivElement>) => {\n const d = dragging.current;\n if (!d || !root.current) return;\n if (!d.moved && Math.abs(e.clientX - d.x) < DRAG_MIN) return;\n d.moved = true;\n const r = root.current.getBoundingClientRect();\n set(r.right - e.clientX - HANDLE / 2, false);\n };\n // A click without dragging (WCAG 2.5.7): the next preset width of the panel, announced.\n const presets = [minB, defaultB, defaultB * 1.5];\n const onUp = (e: React.PointerEvent<HTMLDivElement>, cancel = false) => {\n const d = dragging.current;\n if (!d) return;\n dragging.current = null;\n setDrag(false);\n if (!d.moved && !cancel) {\n const next = Math.round(Math.min(maxB, Math.max(minB, nextPreset(shown, presets))));\n set(next);\n announce(tr('面板 {n} px', { n: next }));\n }\n try {\n if (e.currentTarget.hasPointerCapture?.(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId);\n } catch {\n // Already released.\n }\n if (root.current) write(storageKey, latest.current);\n };\n return (\n <div\n ref={root}\n className={cx('ui-split', stacked && 'is-stacked', drag && 'is-dragging', className)}\n style={stacked || !width ? undefined : { gridTemplateColumns: `minmax(0, 1fr) ${HANDLE}px ${shown}px` }}\n >\n <div className=\"ui-split-a\">{a}</div>\n {stacked ? null : (\n <div\n role=\"separator\"\n tabIndex={0}\n aria-orientation=\"vertical\"\n aria-label={label || tr('調整面板寬度')}\n aria-controls={id}\n aria-valuenow={valueA}\n aria-valuemin={minA}\n aria-valuemax={Math.max(minA, width - HANDLE - minB)}\n aria-valuetext={tr('主要區域 {a} px,面板 {b} px', { a: valueA, b: shown })}\n aria-description={tr('拖曳或按 ← → 調整;按一下切換預設寬度,按兩下或 Enter 還原')}\n className=\"ui-split-handle\"\n onKeyDown={onKey}\n onPointerDown={onDown}\n onPointerMove={onMove}\n onPointerUp={onUp}\n onPointerCancel={(e) => onUp(e, true)}\n onDoubleClick={() => set(defaultB)}\n >\n {/* GB: a small handle in the middle of the line, so the divider reads as something to drag. */}\n <span className=\"ui-split-grip\" aria-hidden=\"true\">\n <i />\n <i />\n <i />\n </span>\n </div>\n )}\n <div id={id} className=\"ui-split-b\">\n {b}\n </div>\n </div>\n );\n}\n"],"mappings":";;;;;;;;;;AA6BA,MAAM,SAAS;;AAEf,MAAM,QAAQ;;AAEd,MAAM,OAAO;AAEb,MAAM,QAAQ,QAA2C;CACvD,IAAI,CAAC,KAAK,OAAO;CACjB,IAAI;EACF,MAAM,IAAI,OAAO,OAAO,aAAa,QAAQ,cAAc,GAAG,CAAC;EAC/D,OAAO,OAAO,SAAS,CAAC,KAAK,IAAI,IAAI,IAAI;CAC3C,QAAQ;EACN,OAAO;CACT;AACF;AACA,MAAM,SAAS,KAAyB,MAAc;CACpD,IAAI,CAAC,KAAK;CACV,IAAI;EACF,OAAO,aAAa,QAAQ,cAAc,KAAK,OAAO,KAAK,MAAM,CAAC,CAAC,CAAC;CACtE,QAAQ,CAER;AACF;;;AAIA,SAAgB,UAAU,EAAE,GAAG,GAAG,WAAW,KAAK,OAAO,KAAK,OAAO,KAAK,YAAY,OAAO,UAAU,aAA6B;CAClI,QAAQ,WAAW,MAAM,aAAa,aAAa,SAAS,sBAAsB,KAAK,4BAA4B,KAAK,mCAAmC;CAC3J,MAAM,OAAO,MAAM,OAAuB,IAAI;CAC9C,MAAM,CAAC,OAAO,YAAY,MAAM,SAAS,CAAC;CAG1C,MAAM,CAAC,MAAM,WAAW,MAAM,gBAAgB,OAAO,WAAW,cAAc,OAAO,KAAK,UAAU,MAAM,QAAQ;CAClH,MAAM,CAAC,MAAM,WAAW,MAAM,SAAS,KAAK;CAG5C,MAAM,WAAW,MAAM,OAA6C,IAAI;CACxE,MAAM,KAAK,MAAM,MAAM;CACvB,MAAM,sBAAsB;EAC1B,MAAM,KAAK,KAAK;EAChB,IAAI,CAAC,IAAI;EACT,MAAM,gBAAgB,SA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E,GAAG;KAAQ,GAAG;IAAM,CAAC;IACnE,oBAAkB,GAAG,qCAAqC;IAC1D,WAAU;IACV,WAAW;IACX,eAAe;IACf,eAAe;IACf,aAAa;IACb,kBAAkB,MAAM,KAAK,GAAG,IAAI;IACpC,qBAAqB,IAAI,QAAQ;IAGjC,UAAA,qBAAC,QAAD;KAAM,WAAU;KAAgB,eAAY;KAA5C,UAAA;MACE,oBAAC,KAAD,CAAI,CAAA;MACJ,oBAAC,KAAD,CAAI,CAAA;MACJ,oBAAC,KAAD,CAAI,CAAA;KACA;;GACH,CAAA;GAEP,oBAAC,OAAD;IAAS;IAAI,WAAU;IACpB,UAAA;GACE,CAAA;EACF;;AAET"}
|
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import { FieldProps } from "../../internal/Field.js";
|
|
2
2
|
import * as React from "react";
|
|
3
|
-
export interface TextFieldProps extends FieldProps, Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'value' | 'name' | 'required' | 'disabled'> {
|
|
3
|
+
export interface TextFieldProps extends FieldProps, Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'value' | 'name' | 'required' | 'disabled' | 'prefix'> {
|
|
4
4
|
/** The text. */
|
|
5
5
|
value?: string;
|
|
6
|
+
/** Muted text inside the box before the value, e.g. 「NT$」 or 「https://」. */
|
|
7
|
+
prefix?: string;
|
|
8
|
+
/** Muted text inside the box at its end, e.g. 「元」 or 「.example.com」. */
|
|
9
|
+
suffix?: string;
|
|
10
|
+
/** The value's unit, muted, right after the typed text (「12 GB」), or after the placeholder while empty. */
|
|
11
|
+
unit?: string;
|
|
6
12
|
/** Receives the new text, then the native event: onChange={(v) => setName(v)}. */
|
|
7
13
|
onChange?: (value: string, event: React.ChangeEvent<HTMLInputElement>) => void;
|
|
8
14
|
/** The underlying <input>. */
|
|
9
15
|
ref?: React.Ref<HTMLInputElement>;
|
|
10
16
|
}
|
|
11
17
|
/** A labelled one-line input with a hint or an error under it. */
|
|
12
|
-
export declare function TextField({ label, hideLabel, hint, error, id, className, required, disabled, onChange, ...native }: TextFieldProps): React.JSX.Element;
|
|
18
|
+
export declare function TextField({ label, hideLabel, hint, error, id, className, required, disabled, onChange, prefix, suffix, unit, ...native }: TextFieldProps): React.JSX.Element;
|
|
@@ -5,10 +5,32 @@ import * as React from "react";
|
|
|
5
5
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
6
|
// src/components/TextField/TextField.tsx
|
|
7
7
|
/** A labelled one-line input with a hint or an error under it. */
|
|
8
|
-
function TextField({ label, hideLabel, hint, error, id, className, required, disabled, onChange, ...native }) {
|
|
8
|
+
function TextField({ label, hideLabel, hint, error, id, className, required, disabled, onChange, prefix, suffix, unit, ...native }) {
|
|
9
9
|
const auto = React.useId();
|
|
10
10
|
const fieldId = id || auto;
|
|
11
11
|
const msg = error ? fieldId + "-err" : hint ? fieldId + "-hint" : void 0;
|
|
12
|
+
const [typed, setTyped] = React.useState(String(native.defaultValue ?? ""));
|
|
13
|
+
const affixed = !!(prefix || suffix || unit);
|
|
14
|
+
const affixIds = [
|
|
15
|
+
prefix && fieldId + "-pre",
|
|
16
|
+
unit && fieldId + "-unit",
|
|
17
|
+
suffix && fieldId + "-suf"
|
|
18
|
+
];
|
|
19
|
+
const input = /* @__PURE__ */ jsx("input", {
|
|
20
|
+
type: "text",
|
|
21
|
+
...native,
|
|
22
|
+
required,
|
|
23
|
+
disabled,
|
|
24
|
+
id: fieldId,
|
|
25
|
+
className: "ui-input",
|
|
26
|
+
onChange: onChange || unit && native.value === void 0 ? (e) => {
|
|
27
|
+
if (unit && native.value === void 0) setTyped(e.target.value);
|
|
28
|
+
onChange?.(e.target.value, e);
|
|
29
|
+
} : void 0,
|
|
30
|
+
"aria-invalid": error ? true : native["aria-invalid"],
|
|
31
|
+
"aria-describedby": describedBy(native["aria-describedby"], ...affixIds, msg)
|
|
32
|
+
});
|
|
33
|
+
const shown = String(native.value ?? typed) || native.placeholder || "";
|
|
12
34
|
return /* @__PURE__ */ jsxs("div", {
|
|
13
35
|
className: cx("ui-field", error && "has-error", disabled && "is-disabled", className),
|
|
14
36
|
children: [
|
|
@@ -19,18 +41,42 @@ function TextField({ label, hideLabel, hint, error, id, className, required, dis
|
|
|
19
41
|
children: label
|
|
20
42
|
}),
|
|
21
43
|
/* @__PURE__ */ jsxs("div", {
|
|
22
|
-
className: "ui-field-box",
|
|
23
|
-
children: [
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
className: cx("ui-field-box", affixed && "has-affix"),
|
|
45
|
+
children: [
|
|
46
|
+
prefix ? /* @__PURE__ */ jsx("span", {
|
|
47
|
+
id: fieldId + "-pre",
|
|
48
|
+
className: "ui-field-affix",
|
|
49
|
+
children: prefix
|
|
50
|
+
}) : null,
|
|
51
|
+
unit ? /* @__PURE__ */ jsxs("span", {
|
|
52
|
+
className: "ui-field-unitwrap",
|
|
53
|
+
children: [
|
|
54
|
+
input,
|
|
55
|
+
/* @__PURE__ */ jsxs("span", {
|
|
56
|
+
className: "ui-field-unitline",
|
|
57
|
+
"aria-hidden": "true",
|
|
58
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
59
|
+
className: "ui-field-mirror",
|
|
60
|
+
children: shown
|
|
61
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
62
|
+
className: "ui-field-affix",
|
|
63
|
+
children: (shown ? "\xA0" : "") + unit
|
|
64
|
+
})]
|
|
65
|
+
}),
|
|
66
|
+
/* @__PURE__ */ jsx("span", {
|
|
67
|
+
id: fieldId + "-unit",
|
|
68
|
+
className: "ui-visually-hidden",
|
|
69
|
+
children: unit
|
|
70
|
+
})
|
|
71
|
+
]
|
|
72
|
+
}) : input,
|
|
73
|
+
suffix ? /* @__PURE__ */ jsx("span", {
|
|
74
|
+
id: fieldId + "-suf",
|
|
75
|
+
className: "ui-field-affix",
|
|
76
|
+
children: suffix
|
|
77
|
+
}) : null,
|
|
78
|
+
error ? /* @__PURE__ */ jsx(FieldErrorIcon, { inline: affixed }) : null
|
|
79
|
+
]
|
|
34
80
|
}),
|
|
35
81
|
msg ? /* @__PURE__ */ jsx(FieldMessage, {
|
|
36
82
|
id: msg,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TextField.js","names":[],"sources":["../../../src/components/TextField/TextField.tsx"],"sourcesContent":["import * as React from 'react';\nimport { cx } from '../../internal/util';\nimport { describedBy, FieldErrorIcon, FieldLabel, FieldMessage, type FieldProps } from '../../internal/Field';\n\nexport interface TextFieldProps
|
|
1
|
+
{"version":3,"file":"TextField.js","names":[],"sources":["../../../src/components/TextField/TextField.tsx"],"sourcesContent":["import * as React from 'react';\nimport { cx } from '../../internal/util';\nimport { describedBy, FieldErrorIcon, FieldLabel, FieldMessage, type FieldProps } from '../../internal/Field';\n\nexport interface TextFieldProps\n extends FieldProps, Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'value' | 'name' | 'required' | 'disabled' | 'prefix'> {\n /** The text. */\n value?: string;\n /** Muted text inside the box before the value, e.g. 「NT$」 or 「https://」. */\n prefix?: string;\n /** Muted text inside the box at its end, e.g. 「元」 or 「.example.com」. */\n suffix?: string;\n /** The value's unit, muted, right after the typed text (「12 GB」), or after the placeholder while empty. */\n unit?: string;\n /** Receives the new text, then the native event: onChange={(v) => setName(v)}. */\n onChange?: (value: string, event: React.ChangeEvent<HTMLInputElement>) => void;\n /** The underlying <input>. */\n ref?: React.Ref<HTMLInputElement>;\n}\n\n/** A labelled one-line input with a hint or an error under it. */\nexport function TextField({ label, hideLabel, hint, error, id, className, required, disabled, onChange, prefix, suffix, unit, ...native }: TextFieldProps) {\n const auto = React.useId();\n const fieldId = id || auto;\n const msg = error ? fieldId + '-err' : hint ? fieldId + '-hint' : undefined;\n // Uncontrolled with a unit: the typed text, so the unit follows it.\n const [typed, setTyped] = React.useState(String(native.defaultValue ?? ''));\n const affixed = !!(prefix || suffix || unit);\n // Screen readers hear the prefix, suffix and unit with the field's description.\n const affixIds = [prefix && fieldId + '-pre', unit && fieldId + '-unit', suffix && fieldId + '-suf'];\n const input = (\n <input\n type=\"text\"\n {...native}\n required={required}\n disabled={disabled}\n id={fieldId}\n className=\"ui-input\"\n onChange={\n onChange || (unit && native.value === undefined)\n ? (e) => {\n if (unit && native.value === undefined) setTyped(e.target.value);\n onChange?.(e.target.value, e);\n }\n : undefined\n }\n aria-invalid={error ? true : native['aria-invalid']}\n // Your own description first, then the prefix, unit and suffix, then the hint or the error.\n aria-describedby={describedBy(native['aria-describedby'], ...affixIds, msg)}\n />\n );\n const shown = String(native.value ?? typed) || native.placeholder || '';\n return (\n <div className={cx('ui-field', error && 'has-error', disabled && 'is-disabled', className)}>\n <FieldLabel htmlFor={fieldId} required={required} hidden={hideLabel}>\n {label}\n </FieldLabel>\n <div className={cx('ui-field-box', affixed && 'has-affix')}>\n {prefix ? (\n <span id={fieldId + '-pre'} className=\"ui-field-affix\">\n {prefix}\n </span>\n ) : null}\n {unit ? (\n // The unit sits after the text: an invisible copy of the text pushes it there.\n <span className=\"ui-field-unitwrap\">\n {input}\n <span className=\"ui-field-unitline\" aria-hidden=\"true\">\n <span className=\"ui-field-mirror\">{shown}</span>\n <span className=\"ui-field-affix\">{(shown ? '\\u00a0' : '') + unit}</span>\n </span>\n <span id={fieldId + '-unit'} className=\"ui-visually-hidden\">\n {unit}\n </span>\n </span>\n ) : (\n input\n )}\n {suffix ? (\n <span id={fieldId + '-suf'} className=\"ui-field-affix\">\n {suffix}\n </span>\n ) : null}\n {error ? <FieldErrorIcon inline={affixed} /> : null}\n </div>\n {msg ? <FieldMessage id={msg} error={error} hint={hint} /> : null}\n </div>\n );\n}\n"],"mappings":";;;;;;;AAqBA,SAAgB,UAAU,EAAE,OAAO,WAAW,MAAM,OAAO,IAAI,WAAW,UAAU,UAAU,UAAU,QAAQ,QAAQ,MAAM,GAAG,UAA0B;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