@alulore/kumiko 0.18.2 → 0.19.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/dialog/ask.d.ts +7 -0
- package/dist/dialog/ask.js +8 -0
- package/dist/dialog/dialog-content.d.ts +23 -0
- package/dist/dialog/dialog-content.js +15 -0
- package/dist/dialog/dialog-entry.d.ts +14 -0
- package/dist/dialog/dialog-entry.js +111 -0
- package/dist/dialog/dialog-labels.d.ts +23 -0
- package/dist/dialog/dialog-labels.js +16 -0
- package/dist/dialog/dialog-store.d.ts +139 -0
- package/dist/dialog/dialog-store.js +296 -0
- package/dist/dialog/dialog.css +39 -1
- package/dist/dialog/dialog.d.ts +0 -3
- package/dist/dialog/dialog.js +0 -6
- package/dist/dialog/index.d.ts +8 -2
- package/dist/dialog/index.js +7 -2
- package/dist/dialog/leave-guard.d.ts +24 -0
- package/dist/dialog/leave-guard.js +98 -0
- package/dist/dialog/nest.d.ts +14 -0
- package/dist/dialog/nest.js +26 -0
- package/dist/dialog/open-dialog.d.ts +16 -0
- package/dist/dialog/open-dialog.js +13 -0
- package/dist/dialog/overlay-host.d.ts +27 -0
- package/dist/dialog/overlay-host.js +47 -0
- package/dist/dialog/question-dialog.d.ts +21 -0
- package/dist/dialog/question-dialog.js +78 -0
- package/dist/drawer/drawer.css +18 -16
- package/dist/menu/menu.css +21 -17
- package/dist/otp-input/otp-input.css +38 -37
- package/dist/page/index.d.ts +8 -0
- package/dist/page/index.js +8 -0
- package/dist/page/page-action.d.ts +26 -0
- package/dist/page/page-action.js +23 -0
- package/dist/page/page-actions.d.ts +10 -0
- package/dist/page/page-actions.js +66 -0
- package/dist/page/page-back.d.ts +11 -0
- package/dist/page/page-back.js +17 -0
- package/dist/page/page-breadcrumbs.d.ts +21 -0
- package/dist/page/page-breadcrumbs.js +60 -0
- package/dist/page/page-context.d.ts +5 -0
- package/dist/page/page-context.js +13 -0
- package/dist/page/page-fit.d.ts +26 -0
- package/dist/page/page-fit.js +36 -0
- package/dist/page/page-header.d.ts +26 -0
- package/dist/page/page-header.js +16 -0
- package/dist/page/page-icons.d.ts +4 -0
- package/dist/page/page-icons.js +13 -0
- package/dist/page/page-labels.d.ts +12 -0
- package/dist/page/page-labels.js +6 -0
- package/dist/page/page-section.d.ts +14 -0
- package/dist/page/page-section.js +9 -0
- package/dist/page/page.css +422 -0
- package/dist/page/page.d.ts +16 -0
- package/dist/page/page.js +12 -0
- package/dist/page/use-measured-fit.d.ts +22 -0
- package/dist/page/use-measured-fit.js +75 -0
- package/dist/page/use-stuck.d.ts +7 -0
- package/dist/page/use-stuck.js +31 -0
- package/dist/pagination/index.d.ts +3 -0
- package/dist/pagination/index.js +3 -0
- package/dist/pagination/pagination-extras.d.ts +28 -0
- package/dist/pagination/pagination-extras.js +42 -0
- package/dist/pagination/pagination-fit.d.ts +15 -0
- package/dist/pagination/pagination-fit.js +52 -0
- package/dist/pagination/pagination-icons.d.ts +4 -0
- package/dist/pagination/pagination-icons.js +16 -0
- package/dist/pagination/pagination-labels.d.ts +23 -0
- package/dist/pagination/pagination-labels.js +14 -0
- package/dist/pagination/pagination-math.d.ts +7 -0
- package/dist/pagination/pagination-math.js +24 -0
- package/dist/pagination/pagination-motion.d.ts +6 -0
- package/dist/pagination/pagination-motion.js +71 -0
- package/dist/pagination/pagination.css +455 -0
- package/dist/pagination/pagination.d.ts +59 -0
- package/dist/pagination/pagination.js +77 -0
- package/dist/select/select.css +6 -10
- package/dist/server-render.test.js +49 -0
- package/dist/sidebar/app-shell.d.ts +54 -0
- package/dist/sidebar/app-shell.js +28 -0
- package/dist/sidebar/index.d.ts +9 -0
- package/dist/sidebar/index.js +8 -0
- package/dist/sidebar/shell-context.d.ts +30 -0
- package/dist/sidebar/shell-context.js +20 -0
- package/dist/sidebar/shell-sheet.d.ts +7 -0
- package/dist/sidebar/shell-sheet.js +12 -0
- package/dist/sidebar/shell-top-bar.d.ts +8 -0
- package/dist/sidebar/shell-top-bar.js +25 -0
- package/dist/sidebar/sidebar-body.d.ts +5 -0
- package/dist/sidebar/sidebar-body.js +37 -0
- package/dist/sidebar/sidebar-icons.d.ts +4 -0
- package/dist/sidebar/sidebar-icons.js +13 -0
- package/dist/sidebar/sidebar-item.d.ts +26 -0
- package/dist/sidebar/sidebar-item.js +68 -0
- package/dist/sidebar/sidebar-labels.d.ts +14 -0
- package/dist/sidebar/sidebar-labels.js +7 -0
- package/dist/sidebar/sidebar-marker.d.ts +12 -0
- package/dist/sidebar/sidebar-marker.js +77 -0
- package/dist/sidebar/sidebar-storage.d.ts +18 -0
- package/dist/sidebar/sidebar-storage.js +27 -0
- package/dist/sidebar/sidebar-toggle.d.ts +7 -0
- package/dist/sidebar/sidebar-toggle.js +18 -0
- package/dist/sidebar/sidebar.css +587 -0
- package/dist/sidebar/sidebar.d.ts +19 -0
- package/dist/sidebar/sidebar.js +31 -0
- package/dist/sidebar/use-collapsed.d.ts +13 -0
- package/dist/sidebar/use-collapsed.js +20 -0
- package/dist/sidebar/use-narrow.d.ts +4 -0
- package/dist/sidebar/use-narrow.js +18 -0
- package/dist/sidebar/use-shell-focus.d.ts +8 -0
- package/dist/sidebar/use-shell-focus.js +30 -0
- package/dist/sidebar/use-shell-shortcut.d.ts +4 -0
- package/dist/sidebar/use-shell-shortcut.js +32 -0
- package/dist/sidebar/use-shell-state.d.ts +18 -0
- package/dist/sidebar/use-shell-state.js +47 -0
- package/dist/slider/slider.css +9 -6
- package/dist/smooth-caret/caret-geometry.js +10 -4
- package/dist/table/data-table-props.d.ts +96 -0
- package/dist/table/data-table-props.js +1 -0
- package/dist/table/data-table.d.ts +7 -0
- package/dist/table/data-table.js +59 -0
- package/dist/table/index.d.ts +7 -0
- package/dist/table/index.js +5 -0
- package/dist/table/table-actions.d.ts +9 -0
- package/dist/table/table-actions.js +24 -0
- package/dist/table/table-builder.d.ts +60 -0
- package/dist/table/table-builder.js +99 -0
- package/dist/table/table-cells.d.ts +19 -0
- package/dist/table/table-cells.js +67 -0
- package/dist/table/table-columns.d.ts +38 -0
- package/dist/table/table-columns.js +119 -0
- package/dist/table/table-icons.d.ts +3 -0
- package/dist/table/table-icons.js +8 -0
- package/dist/table/table-labels.d.ts +24 -0
- package/dist/table/table-labels.js +17 -0
- package/dist/table/table-model.d.ts +28 -0
- package/dist/table/table-model.js +69 -0
- package/dist/table/table-sections.d.ts +25 -0
- package/dist/table/table-sections.js +79 -0
- package/dist/table/table-state.d.ts +42 -0
- package/dist/table/table-state.js +81 -0
- package/dist/table/table-types.d.ts +86 -0
- package/dist/table/table-types.js +1 -0
- package/dist/table/table.css +919 -0
- package/dist/table/use-table-view.d.ts +30 -0
- package/dist/table/use-table-view.js +147 -0
- package/dist/toast/index.d.ts +1 -2
- package/dist/toast/index.js +1 -2
- package/package.json +44 -1
|
@@ -5,7 +5,11 @@ import { Avatar } from "./avatar";
|
|
|
5
5
|
import { Drawer, DrawerContent, DrawerTitle, DrawerTrigger } from "./drawer";
|
|
6
6
|
import { FileDropzone, FilePreview } from "./file-input";
|
|
7
7
|
import { OTPInput } from "./otp-input";
|
|
8
|
+
import { Page, PageAction, PageActions, PageBreadcrumbs, PageCrumb, PageHeader } from "./page";
|
|
9
|
+
import { Pagination } from "./pagination";
|
|
10
|
+
import { AppShell, cookieStorage, readSidebarCookie, Sidebar, SidebarBody, SidebarGroup, SidebarItem, SidebarToggle } from "./sidebar";
|
|
8
11
|
import { Skeleton, SkeletonGroup, SkeletonText } from "./skeleton";
|
|
12
|
+
import { column, DataTable, numberColumn } from "./table";
|
|
9
13
|
import { Tabs, TabsList, TabsPanel, TabsTab } from "./tabs";
|
|
10
14
|
/*
|
|
11
15
|
* Bun runs these with no DOM, as a server does: a component that reads `window` or `document`, or makes a
|
|
@@ -46,6 +50,47 @@ function aPictureIsItsBadge() {
|
|
|
46
50
|
expect(html).toContain(">PNG<");
|
|
47
51
|
expect(html).not.toContain("blob:");
|
|
48
52
|
}
|
|
53
|
+
function aPaginationIsALandmarkOfPageLinks() {
|
|
54
|
+
const html = renderToString(_jsx(Pagination, { pageCount: 20, defaultPage: 5, firstLast: true, jump: true }));
|
|
55
|
+
expect(html).toMatch(/<nav aria-label="Pagination"/u);
|
|
56
|
+
expect(html).toMatch(/aria-current="page"[^>]*>5</u);
|
|
57
|
+
expect(html).toContain(">Go to page<");
|
|
58
|
+
}
|
|
59
|
+
const shops = [
|
|
60
|
+
{ id: "s1", name: "Mercado Central", screens: 1234 },
|
|
61
|
+
{ id: "s2", name: "Café Tropical", screens: 3 }
|
|
62
|
+
];
|
|
63
|
+
function aTableIsItsRowsAndPaging() {
|
|
64
|
+
const html = renderToString(_jsx(DataTable, { label: "Shops", data: shops, columns: [
|
|
65
|
+
column("name", "Name", (shop) => shop.name, { sortable: true }),
|
|
66
|
+
numberColumn("screens", "Screens", (shop) => shop.screens)
|
|
67
|
+
], rowLink: (shop, children) => _jsx("a", { href: `/shops/${shop.id}`, children: children }), search: true, selection: true, locale: "en-GB" }));
|
|
68
|
+
expect(html).toMatch(/<caption[^>]*>Shops</u);
|
|
69
|
+
expect(html).toMatch(/<a href="\/shops\/s1" data-slot="table-row-link">Mercado Central</u);
|
|
70
|
+
expect(html).toContain(">1,234<");
|
|
71
|
+
expect(html).toMatch(/<nav aria-label="Pagination"/u);
|
|
72
|
+
}
|
|
73
|
+
function anAppShellIsItsSidebarInPlace() {
|
|
74
|
+
const html = renderToString(_jsx(AppShell, { defaultCollapsed: readSidebarCookie("kumiko-sidebar=collapsed"), storage: cookieStorage(), sidebar: _jsxs(Sidebar, { label: "Admin", children: [_jsx(SidebarBody, { children: _jsxs(SidebarGroup, { label: "Accounts", children: [_jsx(SidebarItem, { href: "/customers", current: true, children: "Customers" }), _jsx(SidebarItem, { badge: 4, children: "Moderation" })] }) }), _jsx(SidebarToggle, {})] }), children: _jsx("h1", { children: "Customers" }) }));
|
|
75
|
+
expect(html.match(/<nav /gu)).toHaveLength(1);
|
|
76
|
+
expect(html).toMatch(/<nav [^>]*data-rail="true"[^>]*aria-label="Admin"/u);
|
|
77
|
+
expect(html).toMatch(/<a [^>]*href="\/customers"[^>]*aria-current="page"/u);
|
|
78
|
+
expect(html).toMatch(/aria-label="Moderation, 4"/u);
|
|
79
|
+
const skipTo = /<a data-slot="app-shell-skip" href="#(?<target>[^"]+)"[^>]*>Skip to content</u.exec(html);
|
|
80
|
+
expect(html).toContain(`<main id="${skipTo?.groups?.target ?? "none"}"`);
|
|
81
|
+
expect(html).toContain('data-slot="app-shell-top-bar"');
|
|
82
|
+
}
|
|
83
|
+
function aPageHeaderIsEveryActionInline() {
|
|
84
|
+
const html = renderToString(_jsx(Page, { children: _jsx(PageHeader, { title: "Acme Ltd", back: _jsxs(PageBreadcrumbs, { children: [_jsx(PageCrumb, { href: "/customers", children: "Customers" }), _jsx(PageCrumb, { current: true, children: "Acme Ltd" })] }), actions: _jsxs(PageActions, { children: [_jsx(PageAction, { primary: true, children: "New invoice" }), _jsx(PageAction, { href: "/dashboard", children: "Open dashboard" }), _jsx(PageAction, { tone: "danger", children: "Suspend" })] }), sticky: true }) }));
|
|
85
|
+
expect(html.match(/<h1[ >]/gu)).toHaveLength(1);
|
|
86
|
+
expect(html).toMatch(/<nav [^>]*aria-label="Breadcrumbs"/u);
|
|
87
|
+
expect(html).toMatch(/aria-current="page"[^>]*>.*Acme Ltd/u);
|
|
88
|
+
const header = html.slice(html.indexOf('data-slot="page-header"'));
|
|
89
|
+
for (const label of ["New invoice", "Open dashboard", "Suspend"]) {
|
|
90
|
+
expect(header).toContain(label);
|
|
91
|
+
}
|
|
92
|
+
expect(header).not.toContain('aria-label="More actions"');
|
|
93
|
+
}
|
|
49
94
|
describe("server render", () => {
|
|
50
95
|
it("renders tabs with the chosen tab selected and its panel", tabsShowTheChosenTab);
|
|
51
96
|
it("renders a closed drawer as its trigger alone", aClosedDrawerIsItsTrigger);
|
|
@@ -54,4 +99,8 @@ describe("server render", () => {
|
|
|
54
99
|
it("renders a loading group with its status and placeholders", aLoadingGroupHasItsStatus);
|
|
55
100
|
it("renders a drop zone with its words and button", aDropZoneHasItsWordsAndButton);
|
|
56
101
|
it("renders a picture's preview as its badge, with no object URL", aPictureIsItsBadge);
|
|
102
|
+
it("renders a pagination as a landmark with its current page", aPaginationIsALandmarkOfPageLinks);
|
|
103
|
+
it("renders a table with its rows, links and paging", aTableIsItsRowsAndPaging);
|
|
104
|
+
it("renders an app shell with its sidebar in place, as the cookie left it", anAppShellIsItsSidebarInPlace);
|
|
105
|
+
it("renders a page header with every action inline and no menu", aPageHeaderIsEveryActionInline);
|
|
57
106
|
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type SidebarCollapse } from "./shell-context";
|
|
2
|
+
import { type SidebarLabels } from "./sidebar-labels";
|
|
3
|
+
import type { SidebarStorage } from "./sidebar-storage";
|
|
4
|
+
import type { SidebarNarrowAt } from "./use-narrow";
|
|
5
|
+
/** How the sidebar and the page meet: a divider between them, or the page raised on a surface of its own. */
|
|
6
|
+
export type SidebarLook = "line" | "inset";
|
|
7
|
+
export interface AppShellProps extends Omit<React.ComponentProps<"div">, "children"> {
|
|
8
|
+
/** The sidebar: a `Sidebar` with its parts. */
|
|
9
|
+
sidebar: React.ReactNode;
|
|
10
|
+
/** The page, which goes in the shell's `<main>`. */
|
|
11
|
+
children?: React.ReactNode;
|
|
12
|
+
/**
|
|
13
|
+
* What collapsing does on a wide screen: shrink to an icon rail, or slide out completely, leaving a top bar with a
|
|
14
|
+
* menu button. On a phone the sidebar is a drawer either way.
|
|
15
|
+
* @defaultValue "rail"
|
|
16
|
+
*/
|
|
17
|
+
collapse?: SidebarCollapse;
|
|
18
|
+
/** Controls whether the sidebar is collapsed. Pair it with `onCollapsedChange`. The `storage` is then left alone. */
|
|
19
|
+
collapsed?: boolean;
|
|
20
|
+
/** Starts the shell collapsed or not, when it holds the state. It wins over the `storage`. @defaultValue false */
|
|
21
|
+
defaultCollapsed?: boolean;
|
|
22
|
+
onCollapsedChange?: (collapsed: boolean) => void;
|
|
23
|
+
/**
|
|
24
|
+
* Where to keep the choice, such as `cookieStorage()`. It is read once to start and written only when the shell
|
|
25
|
+
* holds the state. On a server, pass `defaultCollapsed` from `readSidebarCookie` too, so the first render matches.
|
|
26
|
+
*/
|
|
27
|
+
storage?: SidebarStorage;
|
|
28
|
+
/**
|
|
29
|
+
* The key that collapses and expands the sidebar, or opens the menu on a phone. It does nothing while a field has
|
|
30
|
+
* the focus. `false` turns it off.
|
|
31
|
+
* @defaultValue "mod+b"
|
|
32
|
+
*/
|
|
33
|
+
shortcut?: string | false;
|
|
34
|
+
/**
|
|
35
|
+
* The window width below which the sidebar moves into a drawer: `"sm"`, `"md"` or `"lg"` is 40, 56 or 64rem.
|
|
36
|
+
* @defaultValue "md"
|
|
37
|
+
*/
|
|
38
|
+
narrowAt?: SidebarNarrowAt;
|
|
39
|
+
/** Shown beside the menu button, whenever the sidebar isn't in place. */
|
|
40
|
+
topBar?: React.ReactNode;
|
|
41
|
+
/** The shell's words, over the English ones. */
|
|
42
|
+
labels?: Partial<SidebarLabels>;
|
|
43
|
+
/**
|
|
44
|
+
* How the sidebar and the page meet. Without it, `data-kumiko-sidebar="inset"` on any container picks.
|
|
45
|
+
* @defaultValue "line"
|
|
46
|
+
*/
|
|
47
|
+
look?: SidebarLook;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The frame of an app: a sidebar beside the page, which collapses to a rail or slides out, and on a phone becomes a
|
|
51
|
+
* drawer behind a menu button. It fills its container, at least `--kumiko-shell-min-block-size` tall (the window by
|
|
52
|
+
* default). Only one `<nav>` is ever in the page.
|
|
53
|
+
*/
|
|
54
|
+
export declare function AppShell({ sidebar, children, collapse, collapsed, defaultCollapsed, onCollapsedChange, storage, shortcut, narrowAt, topBar, labels, look, ...props }: AppShellProps): React.JSX.Element;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useId, useMemo, useRef } from "react";
|
|
3
|
+
import { AppShellProvider, InternalsProvider } from "./shell-context";
|
|
4
|
+
import { ShellSheet } from "./shell-sheet";
|
|
5
|
+
import { ShellTopBar } from "./shell-top-bar";
|
|
6
|
+
import { sidebarLabels } from "./sidebar-labels";
|
|
7
|
+
import { useShellState } from "./use-shell-state";
|
|
8
|
+
/** Goes to the page by moving the focus there, so the app's own hash routing never sees it. */
|
|
9
|
+
function SkipLink({ target, children }) {
|
|
10
|
+
return (_jsx("a", { "data-slot": "app-shell-skip", href: `#${target}`, onClick: (event) => {
|
|
11
|
+
event.preventDefault();
|
|
12
|
+
document.getElementById(target)?.focus();
|
|
13
|
+
}, children: children }));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The frame of an app: a sidebar beside the page, which collapses to a rail or slides out, and on a phone becomes a
|
|
17
|
+
* drawer behind a menu button. It fills its container, at least `--kumiko-shell-min-block-size` tall (the window by
|
|
18
|
+
* default). Only one `<nav>` is ever in the page.
|
|
19
|
+
*/
|
|
20
|
+
export function AppShell({ sidebar, children, collapse = "rail", collapsed, defaultCollapsed, onCollapsedChange, storage, shortcut = "mod+b", narrowAt = "md", topBar, labels, look, ...props }) {
|
|
21
|
+
const root = useRef(null);
|
|
22
|
+
const mainId = useId();
|
|
23
|
+
const state = useShellState({ collapse, collapsed, defaultCollapsed, onCollapsedChange, storage, shortcut, narrowAt }, root);
|
|
24
|
+
const { narrow } = state;
|
|
25
|
+
const words = useMemo(() => ({ ...sidebarLabels, ...labels }), [labels]);
|
|
26
|
+
const internals = { labels: words, collapse, placement: "page" };
|
|
27
|
+
return (_jsx(AppShellProvider, { value: state, children: _jsx(InternalsProvider, { value: internals, children: _jsxs("div", { ref: root, "data-slot": "app-shell", "data-collapse": collapse, "data-collapsed": state.collapsed || undefined, "data-narrow-at": narrowAt, "data-look": look, ...props, children: [_jsx(SkipLink, { target: mainId, children: words.skip }), narrow ? null : sidebar, _jsxs("div", { "data-slot": "app-shell-page", children: [_jsx(ShellTopBar, { children: topBar }), _jsx("main", { id: mainId, tabIndex: -1, "data-slot": "app-shell-main", children: children })] }), narrow ? _jsx(ShellSheet, { children: sidebar }) : null] }) }) }));
|
|
28
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { AppShell, type AppShellProps, type SidebarLook } from "./app-shell";
|
|
2
|
+
export { useAppShell, type AppShellState, type SidebarCollapse } from "./shell-context";
|
|
3
|
+
export { Sidebar, SidebarFooter, SidebarGroup, SidebarHeader, type SidebarGroupProps, type SidebarProps } from "./sidebar";
|
|
4
|
+
export { SidebarBody } from "./sidebar-body";
|
|
5
|
+
export { SidebarItem, type SidebarItemProps } from "./sidebar-item";
|
|
6
|
+
export { sidebarLabels, type SidebarLabels } from "./sidebar-labels";
|
|
7
|
+
export { cookieStorage, readSidebarCookie, type SidebarStorage } from "./sidebar-storage";
|
|
8
|
+
export { SidebarToggle, type SidebarToggleProps } from "./sidebar-toggle";
|
|
9
|
+
export type { SidebarNarrowAt } from "./use-narrow";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { AppShell } from "./app-shell";
|
|
2
|
+
export { useAppShell } from "./shell-context";
|
|
3
|
+
export { Sidebar, SidebarFooter, SidebarGroup, SidebarHeader } from "./sidebar";
|
|
4
|
+
export { SidebarBody } from "./sidebar-body";
|
|
5
|
+
export { SidebarItem } from "./sidebar-item";
|
|
6
|
+
export { sidebarLabels } from "./sidebar-labels";
|
|
7
|
+
export { cookieStorage, readSidebarCookie } from "./sidebar-storage";
|
|
8
|
+
export { SidebarToggle } from "./sidebar-toggle";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { SidebarLabels } from "./sidebar-labels";
|
|
2
|
+
/** How the sidebar gives way on a wide screen: shrinks to icons, or slides out completely. */
|
|
3
|
+
export type SidebarCollapse = "rail" | "hide";
|
|
4
|
+
/** What the shell's parts and an app's own parts can read and change. */
|
|
5
|
+
export interface AppShellState {
|
|
6
|
+
/** Whether the sidebar is collapsed. On a phone it is the remembered choice for wide screens. */
|
|
7
|
+
collapsed: boolean;
|
|
8
|
+
setCollapsed: (collapsed: boolean) => void;
|
|
9
|
+
toggle: () => void;
|
|
10
|
+
/** Whether the sidebar shows as an icon rail right now: collapsed, in rail mode, on a wide screen. */
|
|
11
|
+
rail: boolean;
|
|
12
|
+
/** Whether the window is narrower than `narrowAt`, so the sidebar lives in a drawer. */
|
|
13
|
+
narrow: boolean;
|
|
14
|
+
/** Whether the phone drawer is open. Always `false` on a wide screen. */
|
|
15
|
+
menuOpen: boolean;
|
|
16
|
+
setMenuOpen: (open: boolean) => void;
|
|
17
|
+
}
|
|
18
|
+
export declare const AppShellProvider: import("react").Provider<AppShellState | null>;
|
|
19
|
+
/** The shell's state, for the parts an app writes inside it: a wordmark that goes mark-only in the rail, a user menu. */
|
|
20
|
+
export declare function useAppShell(): AppShellState;
|
|
21
|
+
/** Where a sidebar is drawn: beside the page, or inside the phone drawer. */
|
|
22
|
+
export type SidebarPlacement = "page" | "sheet";
|
|
23
|
+
/** What the shell's own parts need from it. */
|
|
24
|
+
export interface ShellInternals {
|
|
25
|
+
labels: SidebarLabels;
|
|
26
|
+
collapse: SidebarCollapse;
|
|
27
|
+
placement: SidebarPlacement;
|
|
28
|
+
}
|
|
29
|
+
export declare const InternalsProvider: import("react").Provider<ShellInternals | null>;
|
|
30
|
+
export declare function useShellInternals(): ShellInternals;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createContext, useContext } from "react";
|
|
2
|
+
const AppShellContext = createContext(null);
|
|
3
|
+
export const AppShellProvider = AppShellContext.Provider;
|
|
4
|
+
/** The shell's state, for the parts an app writes inside it: a wordmark that goes mark-only in the rail, a user menu. */
|
|
5
|
+
export function useAppShell() {
|
|
6
|
+
const state = useContext(AppShellContext);
|
|
7
|
+
if (!state) {
|
|
8
|
+
throw new Error("useAppShell must be used inside an AppShell.");
|
|
9
|
+
}
|
|
10
|
+
return state;
|
|
11
|
+
}
|
|
12
|
+
const InternalsContext = createContext(null);
|
|
13
|
+
export const InternalsProvider = InternalsContext.Provider;
|
|
14
|
+
export function useShellInternals() {
|
|
15
|
+
const internals = useContext(InternalsContext);
|
|
16
|
+
if (!internals) {
|
|
17
|
+
throw new Error("Sidebar parts must be used inside an AppShell.");
|
|
18
|
+
}
|
|
19
|
+
return internals;
|
|
20
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sidebar on a phone: a drawer from the start edge. Inside it the sidebar is never a rail, and it draws itself
|
|
3
|
+
* as the sheet's content, so the app's own `Sidebar` needs no second version.
|
|
4
|
+
*/
|
|
5
|
+
export declare function ShellSheet({ children }: {
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
}): React.JSX.Element;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Drawer, DrawerBackdrop } from "../drawer";
|
|
3
|
+
import { AppShellProvider, InternalsProvider, useAppShell, useShellInternals } from "./shell-context";
|
|
4
|
+
/**
|
|
5
|
+
* The sidebar on a phone: a drawer from the start edge. Inside it the sidebar is never a rail, and it draws itself
|
|
6
|
+
* as the sheet's content, so the app's own `Sidebar` needs no second version.
|
|
7
|
+
*/
|
|
8
|
+
export function ShellSheet({ children }) {
|
|
9
|
+
const state = useAppShell();
|
|
10
|
+
const internals = useShellInternals();
|
|
11
|
+
return (_jsxs(Drawer, { side: "start", open: state.menuOpen, onOpenChange: state.setMenuOpen, children: [_jsx(DrawerBackdrop, {}), _jsx(AppShellProvider, { value: { ...state, rail: false }, children: _jsx(InternalsProvider, { value: { ...internals, placement: "sheet" }, children: children }) })] }));
|
|
12
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bar that shows while the sidebar isn't in place: on a phone, and in hide mode while it is collapsed. The
|
|
3
|
+
* stylesheet decides when it shows, so a server's first paint is right. Its button opens the drawer on a phone and
|
|
4
|
+
* brings the sidebar back in place on a wide screen.
|
|
5
|
+
*/
|
|
6
|
+
export declare function ShellTopBar({ children }: {
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
}): React.JSX.Element;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Button } from "../button";
|
|
3
|
+
import { useAppShell, useShellInternals } from "./shell-context";
|
|
4
|
+
import { MenuIcon } from "./sidebar-icons";
|
|
5
|
+
/**
|
|
6
|
+
* The bar that shows while the sidebar isn't in place: on a phone, and in hide mode while it is collapsed. The
|
|
7
|
+
* stylesheet decides when it shows, so a server's first paint is right. Its button opens the drawer on a phone and
|
|
8
|
+
* brings the sidebar back in place on a wide screen.
|
|
9
|
+
*/
|
|
10
|
+
export function ShellTopBar({ children }) {
|
|
11
|
+
const { narrow, menuOpen, setMenuOpen, setCollapsed } = useAppShell();
|
|
12
|
+
const { labels } = useShellInternals();
|
|
13
|
+
let label = labels.expand;
|
|
14
|
+
if (narrow) {
|
|
15
|
+
label = menuOpen ? labels.closeMenu : labels.openMenu;
|
|
16
|
+
}
|
|
17
|
+
return (_jsxs("div", { "data-slot": "app-shell-top-bar", children: [_jsx(Button, { variant: "ghost", icon: true, "data-sidebar-menu": "", "aria-label": label, "aria-expanded": narrow ? menuOpen : false, onClick: () => {
|
|
18
|
+
if (narrow) {
|
|
19
|
+
setMenuOpen(!menuOpen);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
setCollapsed(false);
|
|
23
|
+
}
|
|
24
|
+
}, children: _jsx(MenuIcon, {}) }), children] }));
|
|
25
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The middle of the sidebar: the groups and items. It scrolls when they are taller than the room, with fading edges
|
|
3
|
+
* where there is more to see, and holds the marker that glides to the current item.
|
|
4
|
+
*/
|
|
5
|
+
export declare function SidebarBody({ children, ...props }: React.ComponentProps<"div">): React.JSX.Element;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { useSidebarMarker } from "./sidebar-marker";
|
|
4
|
+
/** Notes on the scroller whether there is more to see above and below, so the stylesheet can fade the edges. */
|
|
5
|
+
function useScrollEdges(scroller, content) {
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const element = scroller.current;
|
|
8
|
+
const inside = content.current;
|
|
9
|
+
if (!element || !inside) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
const update = () => {
|
|
13
|
+
element.toggleAttribute("data-more-above", element.scrollTop > 1);
|
|
14
|
+
element.toggleAttribute("data-more-below", element.scrollTop + element.clientHeight < element.scrollHeight - 1);
|
|
15
|
+
};
|
|
16
|
+
update();
|
|
17
|
+
element.addEventListener("scroll", update, { passive: true });
|
|
18
|
+
const observer = new ResizeObserver(update);
|
|
19
|
+
observer.observe(element);
|
|
20
|
+
observer.observe(inside);
|
|
21
|
+
return () => {
|
|
22
|
+
element.removeEventListener("scroll", update);
|
|
23
|
+
observer.disconnect();
|
|
24
|
+
};
|
|
25
|
+
}, [scroller, content]);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The middle of the sidebar: the groups and items. It scrolls when they are taller than the room, with fading edges
|
|
29
|
+
* where there is more to see, and holds the marker that glides to the current item.
|
|
30
|
+
*/
|
|
31
|
+
export function SidebarBody({ children, ...props }) {
|
|
32
|
+
const scroller = useRef(null);
|
|
33
|
+
const content = useRef(null);
|
|
34
|
+
useScrollEdges(scroller, content);
|
|
35
|
+
useSidebarMarker(content);
|
|
36
|
+
return (_jsx("div", { "data-slot": "sidebar-body", ref: scroller, ...props, children: _jsxs("div", { "data-slot": "sidebar-content", ref: content, children: [_jsx("span", { "data-slot": "sidebar-marker", "aria-hidden": "true" }), _jsx("ul", { "data-slot": "sidebar-list", children: children })] }) }));
|
|
37
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/** Icons the shell draws itself, so it needs no icon library. They follow the text colour and mirror in right-to-left. */
|
|
3
|
+
function Icon({ mirror, children }) {
|
|
4
|
+
return (_jsx("svg", { "data-slot": "sidebar-icon", "data-mirror": mirror || undefined, viewBox: "0 0 20 20", fill: "none", stroke: "currentColor", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round", "aria-hidden": "true", children: children }));
|
|
5
|
+
}
|
|
6
|
+
/** A window with its sidebar marked, for collapsing and expanding it. */
|
|
7
|
+
export function PanelIcon() {
|
|
8
|
+
return (_jsxs(Icon, { mirror: true, children: [_jsx("rect", { x: "3", y: "4", width: "14", height: "12", rx: "2.5" }), _jsx("path", { d: "M8 4.5v11" })] }));
|
|
9
|
+
}
|
|
10
|
+
/** The menu button of the top bar. */
|
|
11
|
+
export function MenuIcon() {
|
|
12
|
+
return (_jsx(Icon, { children: _jsx("path", { d: "M3.5 6h13M3.5 10h13M3.5 14h13" }) }));
|
|
13
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type ReactElement, type ReactNode } from "react";
|
|
2
|
+
export interface SidebarItemProps extends Omit<React.HTMLAttributes<HTMLElement>, "children"> {
|
|
3
|
+
/** What the item says. A string also names it for a screen reader and for the rail's tooltip. */
|
|
4
|
+
children: ReactNode;
|
|
5
|
+
/** The item's icon, which stays put as the sidebar collapses. */
|
|
6
|
+
icon?: ReactNode;
|
|
7
|
+
/**
|
|
8
|
+
* A count or a mark beside the label. A number becomes a dot in the rail, and the item's name carries the count
|
|
9
|
+
* ("Moderation, 4").
|
|
10
|
+
*/
|
|
11
|
+
badge?: ReactNode;
|
|
12
|
+
/** Whether this is the page the app is on. The marker glides to it. Only the app knows its routes. */
|
|
13
|
+
current?: boolean;
|
|
14
|
+
/** Makes the item a link to `href`. Without `href` or `render`, it is a button. */
|
|
15
|
+
href?: string;
|
|
16
|
+
/**
|
|
17
|
+
* The app's own link, such as `<Link to="/billing" />`. The item's props and content go into it, as the
|
|
18
|
+
* table's `rowLink` does.
|
|
19
|
+
*/
|
|
20
|
+
render?: ReactElement;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* One destination. It is a link with `href`, the app's own link with `render`, or a button. Choosing one in the
|
|
24
|
+
* phone drawer closes it. In the rail the label becomes a tooltip.
|
|
25
|
+
*/
|
|
26
|
+
export declare function SidebarItem({ icon, badge, current, href, render, children, onClick, ...props }: SidebarItemProps): React.JSX.Element;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { cloneElement } from "react";
|
|
3
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from "../tooltip";
|
|
4
|
+
import { useAppShell, useShellInternals } from "./shell-context";
|
|
5
|
+
/** Any element accepts these props: they land on whatever the app's link renders. */
|
|
6
|
+
function takesProps(_element) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
function spokenName(label, badge) {
|
|
10
|
+
return typeof label === "string" && typeof badge === "number"
|
|
11
|
+
? `${label}, ${badge}`
|
|
12
|
+
: undefined;
|
|
13
|
+
}
|
|
14
|
+
/** What the item is called: its label, with a count or a mark after it. The tooltip shows it, and it names the item. */
|
|
15
|
+
function fullName(label, badge) {
|
|
16
|
+
const spoken = spokenName(label, badge);
|
|
17
|
+
if (spoken !== undefined) {
|
|
18
|
+
return spoken;
|
|
19
|
+
}
|
|
20
|
+
if (badge === undefined || badge === null || typeof badge === "number") {
|
|
21
|
+
return label;
|
|
22
|
+
}
|
|
23
|
+
return (_jsxs(_Fragment, { children: [label, ", ", badge] }));
|
|
24
|
+
}
|
|
25
|
+
function Content({ icon, badge, children }) {
|
|
26
|
+
return (_jsxs(_Fragment, { children: [_jsx("span", { "data-slot": "sidebar-item-icon", "aria-hidden": icon ? true : undefined, children: icon }), _jsx("span", { "data-slot": "sidebar-item-label", children: children }), badge === undefined || badge === null ? null : (_jsx("span", { "data-slot": "sidebar-item-badge", "data-kind": typeof badge === "number" ? "count" : "mark", "aria-hidden": typeof badge === "number" ? true : undefined, children: badge }))] }));
|
|
27
|
+
}
|
|
28
|
+
/** The item's element: the app's own link with the props and content poured in, a link, or a button. */
|
|
29
|
+
function itemElement(parts) {
|
|
30
|
+
const { render, href, own, content } = parts;
|
|
31
|
+
if (render) {
|
|
32
|
+
return cloneElement(render, own, content);
|
|
33
|
+
}
|
|
34
|
+
if (href === undefined) {
|
|
35
|
+
return (_jsx("button", { type: "button", ...own, children: content }));
|
|
36
|
+
}
|
|
37
|
+
return (_jsx("a", { href: href, ...own, children: content }));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* One destination. It is a link with `href`, the app's own link with `render`, or a button. Choosing one in the
|
|
41
|
+
* phone drawer closes it. In the rail the label becomes a tooltip.
|
|
42
|
+
*/
|
|
43
|
+
export function SidebarItem({ icon, badge, current, href, render, children, onClick, ...props }) {
|
|
44
|
+
const { rail, setMenuOpen } = useAppShell();
|
|
45
|
+
const { placement } = useShellInternals();
|
|
46
|
+
const name = spokenName(children, badge);
|
|
47
|
+
const own = {
|
|
48
|
+
...props,
|
|
49
|
+
"data-slot": "sidebar-item",
|
|
50
|
+
"data-current": current ? "" : undefined,
|
|
51
|
+
"aria-label": name,
|
|
52
|
+
onClick: (event) => {
|
|
53
|
+
if (render && takesProps(render)) {
|
|
54
|
+
render.props.onClick?.(event);
|
|
55
|
+
}
|
|
56
|
+
onClick?.(event);
|
|
57
|
+
if (placement === "sheet") {
|
|
58
|
+
setMenuOpen(false);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
if (current !== undefined) {
|
|
63
|
+
own["aria-current"] = current ? "page" : undefined;
|
|
64
|
+
}
|
|
65
|
+
const content = (_jsx(Content, { icon: icon, badge: badge, children: children }));
|
|
66
|
+
const element = itemElement({ render, href, own, content });
|
|
67
|
+
return (_jsx("li", { "data-slot": "sidebar-entry", children: _jsxs(Tooltip, { disabled: !rail, type: "label", children: [_jsx(TooltipTrigger, { render: element }), _jsx(TooltipContent, { side: "end", children: fullName(children, badge) })] }) }));
|
|
68
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Every word the shell shows or reads out. */
|
|
2
|
+
export interface SidebarLabels {
|
|
3
|
+
/** The skip link, the first thing Tab reaches. */
|
|
4
|
+
skip: string;
|
|
5
|
+
/** The toggle while the sidebar is open. */
|
|
6
|
+
collapse: string;
|
|
7
|
+
/** The toggle, and the top bar's menu button in hide mode, while the sidebar is collapsed. */
|
|
8
|
+
expand: string;
|
|
9
|
+
/** The top bar's menu button, while the phone drawer is closed. */
|
|
10
|
+
openMenu: string;
|
|
11
|
+
/** The drawer's close button, and the menu button while the drawer is open. */
|
|
12
|
+
closeMenu: string;
|
|
13
|
+
}
|
|
14
|
+
export declare const sidebarLabels: SidebarLabels;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type RefObject } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Puts the marker over the current item, measured within the body's content. The marker keeps its place under the
|
|
4
|
+
* item's own fill, and shows it only when there is a current item in sight. `instant` skips the glide, for a resize.
|
|
5
|
+
*/
|
|
6
|
+
export declare function placeMarker(content: HTMLElement, instant: boolean): void;
|
|
7
|
+
/**
|
|
8
|
+
* Keeps the marker over the current item. It watches for the item that is current changing (the app sets
|
|
9
|
+
* `aria-current`, so the DOM is the truth) and glides there. Anything that moves or resizes the items places it
|
|
10
|
+
* again at once, such as the sidebar settling between a rail and its full width.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useSidebarMarker(content: RefObject<HTMLElement | null>): void;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { useEffect, useLayoutEffect } from "react";
|
|
2
|
+
const markerSelector = ':scope > [data-slot="sidebar-marker"]';
|
|
3
|
+
const currentSelector = '[data-slot="sidebar-item"][aria-current="page"]';
|
|
4
|
+
/**
|
|
5
|
+
* Where `element` sits inside `root`, summed up the offset parents. Layout offsets ignore the scale a pressed item
|
|
6
|
+
* carries, and the walk doesn't care how deep the item is. It is `undefined` when the element isn't laid out.
|
|
7
|
+
*/
|
|
8
|
+
function offsetWithin(element, root) {
|
|
9
|
+
let x = 0;
|
|
10
|
+
let y = 0;
|
|
11
|
+
let node = element;
|
|
12
|
+
while (node && node !== root) {
|
|
13
|
+
x += node.offsetLeft;
|
|
14
|
+
y += node.offsetTop;
|
|
15
|
+
node = node.offsetParent instanceof HTMLElement ? node.offsetParent : null;
|
|
16
|
+
}
|
|
17
|
+
return node === root && element.offsetParent !== null ? { x, y } : undefined;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Puts the marker over the current item, measured within the body's content. The marker keeps its place under the
|
|
21
|
+
* item's own fill, and shows it only when there is a current item in sight. `instant` skips the glide, for a resize.
|
|
22
|
+
*/
|
|
23
|
+
export function placeMarker(content, instant) {
|
|
24
|
+
const marker = content.querySelector(markerSelector);
|
|
25
|
+
if (!marker) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const current = content.querySelector(currentSelector);
|
|
29
|
+
const spot = current ? offsetWithin(current, content) : undefined;
|
|
30
|
+
marker.toggleAttribute("data-hidden", !spot);
|
|
31
|
+
content.toggleAttribute("data-marker", Boolean(spot));
|
|
32
|
+
if (!current || !spot) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
marker.toggleAttribute("data-instant", instant || !Object.hasOwn(marker.dataset, "placed"));
|
|
36
|
+
marker.style.setProperty("--_x", `${spot.x}px`);
|
|
37
|
+
marker.style.setProperty("--_y", `${spot.y}px`);
|
|
38
|
+
marker.style.setProperty("--_w", `${current.offsetWidth}px`);
|
|
39
|
+
marker.style.setProperty("--_h", `${current.offsetHeight}px`);
|
|
40
|
+
marker.dataset.placed = "";
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Keeps the marker over the current item. It watches for the item that is current changing (the app sets
|
|
44
|
+
* `aria-current`, so the DOM is the truth) and glides there. Anything that moves or resizes the items places it
|
|
45
|
+
* again at once, such as the sidebar settling between a rail and its full width.
|
|
46
|
+
*/
|
|
47
|
+
export function useSidebarMarker(content) {
|
|
48
|
+
useLayoutEffect(() => {
|
|
49
|
+
const element = content.current;
|
|
50
|
+
if (element) {
|
|
51
|
+
placeMarker(element, true);
|
|
52
|
+
}
|
|
53
|
+
}, [content]);
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
const element = content.current;
|
|
56
|
+
if (!element) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
const glide = new MutationObserver(() => {
|
|
60
|
+
placeMarker(element, false);
|
|
61
|
+
});
|
|
62
|
+
glide.observe(element, {
|
|
63
|
+
subtree: true,
|
|
64
|
+
childList: true,
|
|
65
|
+
attributes: true,
|
|
66
|
+
attributeFilter: ["aria-current"]
|
|
67
|
+
});
|
|
68
|
+
const jump = new ResizeObserver(() => {
|
|
69
|
+
placeMarker(element, true);
|
|
70
|
+
});
|
|
71
|
+
jump.observe(element);
|
|
72
|
+
return () => {
|
|
73
|
+
glide.disconnect();
|
|
74
|
+
jump.disconnect();
|
|
75
|
+
};
|
|
76
|
+
}, [content]);
|
|
77
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Where the shell keeps whether the sidebar is collapsed. The shell reads it once to start and writes it only when it owns the state. */
|
|
2
|
+
export interface SidebarStorage {
|
|
3
|
+
get: () => string | null;
|
|
4
|
+
set: (value: string) => unknown;
|
|
5
|
+
}
|
|
6
|
+
/** What the storage holds. */
|
|
7
|
+
export declare const collapsedWord = "collapsed";
|
|
8
|
+
export declare const expandedWord = "expanded";
|
|
9
|
+
/**
|
|
10
|
+
* A cookie over `document.cookie` that lasts a year, so a server can read it too. It is safe to create where there
|
|
11
|
+
* is no document: it reads nothing and writes nothing.
|
|
12
|
+
*/
|
|
13
|
+
export declare function cookieStorage(name?: string): SidebarStorage;
|
|
14
|
+
/**
|
|
15
|
+
* Reads the cookie from a request's `Cookie` header on a server, as the `defaultCollapsed` of the first render.
|
|
16
|
+
* It is `undefined` when the visitor never chose.
|
|
17
|
+
*/
|
|
18
|
+
export declare function readSidebarCookie(cookieHeader: string | null | undefined, name?: string): boolean | undefined;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createCookieStorage } from "@ui-sdk/core";
|
|
2
|
+
const defaultName = "kumiko-sidebar";
|
|
3
|
+
/** What the storage holds. */
|
|
4
|
+
export const collapsedWord = "collapsed";
|
|
5
|
+
export const expandedWord = "expanded";
|
|
6
|
+
/**
|
|
7
|
+
* A cookie over `document.cookie` that lasts a year, so a server can read it too. It is safe to create where there
|
|
8
|
+
* is no document: it reads nothing and writes nothing.
|
|
9
|
+
*/
|
|
10
|
+
export function cookieStorage(name = defaultName) {
|
|
11
|
+
const cookie = createCookieStorage(name);
|
|
12
|
+
return {
|
|
13
|
+
get: () => cookie.get(),
|
|
14
|
+
set: (value) => cookie.set(value)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Reads the cookie from a request's `Cookie` header on a server, as the `defaultCollapsed` of the first render.
|
|
19
|
+
* It is `undefined` when the visitor never chose.
|
|
20
|
+
*/
|
|
21
|
+
export function readSidebarCookie(cookieHeader, name = defaultName) {
|
|
22
|
+
const value = createCookieStorage(name, { cookie: cookieHeader ?? null }).get();
|
|
23
|
+
if (value === collapsedWord) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return value === expandedWord ? false : undefined;
|
|
27
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ButtonProps } from "../button";
|
|
2
|
+
export type SidebarToggleProps = Omit<ButtonProps, "children" | "icon" | "onClick" | "aria-label">;
|
|
3
|
+
/**
|
|
4
|
+
* The button that collapses and expands the sidebar, named by the shell's collapse and expand words. Put it in the
|
|
5
|
+
* `SidebarFooter`. On a phone, where the sidebar is a drawer, it isn't there.
|
|
6
|
+
*/
|
|
7
|
+
export declare function SidebarToggle(props: SidebarToggleProps): React.JSX.Element | null;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Button } from "../button";
|
|
3
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from "../tooltip";
|
|
4
|
+
import { useAppShell, useShellInternals } from "./shell-context";
|
|
5
|
+
import { PanelIcon } from "./sidebar-icons";
|
|
6
|
+
/**
|
|
7
|
+
* The button that collapses and expands the sidebar, named by the shell's collapse and expand words. Put it in the
|
|
8
|
+
* `SidebarFooter`. On a phone, where the sidebar is a drawer, it isn't there.
|
|
9
|
+
*/
|
|
10
|
+
export function SidebarToggle(props) {
|
|
11
|
+
const { collapsed, toggle } = useAppShell();
|
|
12
|
+
const { labels, placement } = useShellInternals();
|
|
13
|
+
if (placement === "sheet") {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const label = collapsed ? labels.expand : labels.collapse;
|
|
17
|
+
return (_jsxs(Tooltip, { type: "label", children: [_jsx(TooltipTrigger, { render: _jsx(Button, { variant: "ghost", icon: true, "data-sidebar-toggle": "", "aria-label": label, "aria-expanded": !collapsed, onClick: toggle, ...props, children: _jsx(PanelIcon, {}) }) }), _jsx(TooltipContent, { side: "end", children: label })] }));
|
|
18
|
+
}
|