import { useEffect, useRef, useState } from "react";
import {
ActorName, ActorPicture, AppBar, AppCrumbs, AppFrame, AppPanel, AppSidebar,
AppSwitcher, Input, ListRow, PortalProvider, ScrollArea, SessionMenu,
SettingsGroup, SettingsPage, SettingsRow, Sheet, characterFromId,
} from "@arcadia/ui";
function Activity() {
const viewport = useRef<HTMLDivElement>(null);
const [shown, setShown] = useState(20);
useEffect(() => {
const el = viewport.current;
if (!el) return;
const near = () => { if (el.scrollHeight - el.scrollTop - el.clientHeight < el.clientHeight) setShown((n) => Math.min(EVENTS, n + 20)); };
el.addEventListener("scroll", near, { passive: true });
return () => el.removeEventListener("scroll", near);
}, []);
return (
<AppFrame rootStyle={page.frame} bar={<AppBar product="Product" home={HERE} switcher={<Switcher />} session={<Person />} />}>
<AppPanel title="Activity" readout={`${shown} of ${EVENTS}`} viewportRef={viewport}>
{Array.from({ length: shown }, (_, i) => (
<ListRow key={i} leading="history" label={`Card ${i + 1} moved to Done`} meta={`${i + 1}h`} interactive={false} />
))}
</AppPanel>
</AppFrame>
);
}
const ADA = { name: "Ada Lovelace" };
const MEMBERS: readonly { actor: Actor; seen: string }[] = [
{ actor: { kind: "person", ...ADA }, seen: "Now" },
{ actor: { kind: "agent", name: "Scout", avatar: characterFromId("agt_scout_ada"), person: ADA }, seen: "2 min ago" },
{ actor: { kind: "agent", name: "Fable", avatar: characterFromId("agt_fable_ada"), person: ADA }, seen: "3 h ago" },
];
function Members() {
return (
<AppFrame rootStyle={page.frame} bar={<AppBar product="Product" home={HERE} switcher={<Switcher />} session={<Person />} />}>
<AppPanel title="Members" readout={`${MEMBERS.length} in the team`}>
<div {...sx("app-page-column", page.pageColumn)}>
{MEMBERS.map(({ actor, seen }) => (
<ListRow key={actor.name} interactive={false} leading={<ActorPicture actor={actor} alone aria-hidden />}
label={<ActorName actor={actor} />} meta={seen} metaYields={false} />
))}
</div>
</AppPanel>
</AppFrame>
);
}
function Narrow({ open }: { open?: boolean }) {
const [view, setView] = useState<View>("Settings");
return (
<AppFrame
rootStyle={page.frame}
bar={<AppBar product="Product" home={HERE} crumbs={<AppCrumbs items={[{ label: "Workspace", href: HERE }, { label: view }]} />} switcher={<Switcher />} session={<Person />} />}
sidebar={<AppSidebar aria-label="Workspace" head={<Workspace />} groups={groupsFor(view, setView)} />}
>
<AppPanel title={view} viewKey={view}>
{view === "Settings" ? <SettingsSample /> : <Placeholder title={view} />}
</AppPanel>
{open && (
<Sheet open modal={false} initialFocus={false} aria-label="Workspace">
<AppSidebar aria-label="Workspace" head={<Workspace />} groups={groupsFor(view, setView)} />
</Sheet>
)}
</AppFrame>
);
}
function LongNames() {
const [view, setView] = useState("A page whose name runs on longer than any bar has room for");
const many = Array.from({ length: 24 }, (_, i) => `Project number ${i + 1} with a name that is long`);
return (
<AppFrame
rootStyle={page.frame}
bar={(
<AppBar
product="A product with a long name" home={HERE}
crumbs={<AppCrumbs items={[{ label: "A workspace with a long name", href: HERE }, { label: "Projects", href: HERE }, { label: view }]} />}
switcher={<Switcher />} session={<Person />}
/>
)}
sidebar={<AppSidebar aria-label="Workspace" groups={[{ label: "Projects", items: many.map((label) => ({ label, current: view === label, onSelect: () => setView(label) })) }]} />}
>
<AppPanel title={view} viewKey={view}><Placeholder title="Project" /></AppPanel>
</AppFrame>
);
}
function SwitcherOpen({ container }: { container: HTMLElement }) {
return (
<PortalProvider container={container}>
<div {...sx("app-bar-sample", page.bar)}>
<AppBar product="Product" home={HERE} switcher={<AppSwitcher products={PRODUCTS} current="Product" open modal={false} />} session={<Person />} />
</div>
</PortalProvider>
);
}
function PersonOpen({ container }: { container: HTMLElement }) {
return (
<PortalProvider container={container}>
<div {...sx("app-bar-sample", page.bar)}>
<AppBar product="Product" home={HERE} switcher={<Switcher />} session={<Person open />} />
</div>
</PortalProvider>
);
}
function Alex() {
const [theme, setTheme] = useState<ThemePreference>("system");
return <SessionMenu name="alex" theme={theme} onThemeChange={setTheme} onSignOut={() => {}} />;
}
function PersonNoAddress({ container }: { container: HTMLElement }) {
const [theme, setTheme] = useState<ThemePreference>("system");
return (
<PortalProvider container={container}>
<div {...sx("app-bar-sample", page.bar)}>
<AppBar
product="Product" home={HERE} switcher={<Switcher />}
session={<SessionMenu name="alex" theme={theme} onThemeChange={setTheme} onSignOut={() => {}} open modal={false} />}
/>
</div>
</PortalProvider>
);
}
function Usage() {
return (
<AppFrame
bar={(
<AppBar
product="Product" home="/"
crumbs={<AppCrumbs items={[{ label: "Workspace", href: "/" }, { label: "Settings" }]} />}
switcher={<AppSwitcher products={products} current="Product" />}
session={<SessionMenu name={person.name} address={person.address} account={account} theme={theme} onThemeChange={setTheme} onSignOut={signOut} />}
/>
)}
sidebar={(
<AppSidebar aria-label="Workspace" groups={[{ items: [{ label: "Overview", href: "/" }, { label: "Settings", href: "/settings", current: true }] }]} />
)}
>
<AppPanel title="Settings" viewKey={path}>
<SettingsPage>
<SettingsGroup label="Workspace">
<SettingsRow label="Name" control={<Input aria-label="Name" defaultValue="Workspace" />} />
</SettingsGroup>
</SettingsPage>
</AppPanel>
</AppFrame>
);
}
const products = PRODUCTS;
const person = { name: "Alex Morgan", address: "[email protected]" };
const account = HERE;
const theme: ThemePreference = "system";
const setTheme = (_: ThemePreference) => {};
const signOut = () => {};
const path = HERE;
const MEASURES = [
["--app-bar-height", "The bar: one line, the same in every product."],
["--app-sidebar-width", "The sidebar beside the panel, and its sheet on a narrow window."],
["--app-panel-inset", "The panel from the window's end and bottom edges, and its start with no sidebar beside it."],
["--radius-frame", "The panel's corner: a surface's frame, 12."],
["--app-settings-width", "A settings page's one column."],
["--app-page-width", "Any other page's column: members, a record, a table. A settings page keeps its own."],
["--app-sheet-breakpoint", "Under this width of the frame the sidebar becomes a sheet."],
] as const;
export default function AppFramePage() {
const live = useFndMeasure();
return (
<>
<Specimen code={SRC.WithSidebar} layout="fill" caption="At 960 wide, drawn to fit. The rows choose the page as a product's links would; the title takes focus when it changes.">
<Window size="wide" render={() => <WithSidebar />} />
</Specimen>
<Section title="Usage">
<CodeBlock code={displaySnippet(SRC.Usage)} />
<P>
One frame for every signed-in surface, so the account, a product's dashboard and a product read as one
application. A public page keeps the website's frame. The bar and the sidebar sit on the ground and never
scroll; the panel is the one raised surface, a step of surface above them with no line round it, since the step
parts them already. The page scrolls inside it under a head that stays, with no rule under the head: the page
fades into it. The head holds the page's title, at most one quiet readout, and at most one action.
</P>
<P>
The bar opens with the Arcadia mark and the product's short name, which link to its home, then the crumbs. It
ends with one optional slot, the switcher, the bell (`AppBell`, on its own page under Notifications) and the
person's avatar. Its height and the places of the mark, the name, the switcher and the avatar come from the
frame's tokens and from nothing a product passes, so moving between products the bar does not move by a pixel. A bar whose mark leads to Arcadia's home instead, as the
account's does, passes `arcadiaHome`: the mark and the name become two links where the one stood.
</P>
<P>
The frame answers to its own width. Under `--app-sheet-breakpoint` the sidebar leaves the ground: the bar's start
holds the control that opens it as a `Sheet`, and choosing a row closes it. A product may hold the sheet itself
with `sheetOpen`, so an empty page's action can open it. The panel then stands inset on both sides, so its head
and its page step in from its edge by one step less.
</P>
</Section>
<Section title="Examples">
<Sub title="Without a sidebar" lead="A product of one page leaves the sidebar out and the panel keeps the inset at the window's start too. This page fills the panel: its stream scrolls on its own and the field under it stays.">
<Specimen code={SRC.WithoutSidebar} layout="fill">
<Window size="wide" render={() => <WithoutSidebar />} />
</Specimen>
</Sub>
<Sub title="Narrow, the sheet open" lead="Under the breakpoint the sheet slides from the start over a scrim, on the dialog's times. It keeps focus inside, opens on the row you are on, closes on Escape, on a press outside, on its close button where the toggle stood and on a row chosen, and gives focus back to the toggle.">
<Specimen code={SRC.Narrow} cols={2}>
<Cell caption="Open">
<Window size="narrow" inert render={(c) => <PortalProvider container={c}><Narrow open /></PortalProvider>} />
</Cell>
<Cell caption="Try it">
<Window size="narrow" render={(c) => <PortalProvider container={c}><Narrow /></PortalProvider>} />
</Cell>
</Specimen>
</Sub>
<Sub title="A page that fills the panel" lead="With `fill` the panel gives the page its whole body and the page brings its own scroller, here one that scrolls both ways and keeps its bar clear of the panel's corner with `--scroll-radius` at the panel's `--radius-frame`. The head holds a `readout` before its one action: the keys the board listens to.">
<Specimen code={SRC.Board} layout="fill">
<Window size="wide" render={() => <Board />} />
</Specimen>
<P>
Its head's `bar` is a second row under the title's, at the head's inset and 8 above the page: the
board's filters, those in force as chips and one Clear before the filter button. Given a function, it is
told whether the panel is 640 wide; under that the chips stand in the filter menu's head, and with none in
force the bar holds the button alone.
</P>
</Sub>
<Sub title="The column's scroller" lead="Without `fill` the panel scrolls the page itself and hands its scroller out through `viewportRef`, as `ScrollArea` does: here older events come as the page nears its end, and the head's `readout` counts what is shown. A readout is quiet and never a control: a key, a count, a legend.">
<Specimen code={SRC.Activity} layout="fill">
<Window size="wide" render={() => <Activity />} />
</Specimen>
</Sub>
<Sub title="The switcher" lead="The grid at the bar's end opens Arcadia's apps from the kit's own list: Home first, then the products by short name. The one you are in carries a check and is not a link.">
<Specimen code={SRC.SwitcherOpen} size="tall" stage={(c) => <SwitcherOpen container={c} />} stageAlign="end">
<AppSwitcher products={PRODUCTS} current="Product" />
</Specimen>
</Sub>
<Sub title="The person's menu" lead="The avatar alone, a picture or initials. Its menu starts with a 40px picture, the name in Body emphasis and an optional Caption address. It holds the product's own rows; the Arcadia account, left out on the account itself; the theme; and Sign out, which runs `onSignOut`, a request the product sends itself and never an address to visit.">
<Specimen code={SRC.PersonOpen} size="tall" stage={(c) => <PersonOpen container={c} />} stageAlign="end">
<Person />
</Specimen>
</Sub>
<Sub title="Without an address" lead="A product that holds no address leaves `address` out, and its line is not drawn: the name alone heads the menu.">
<Specimen code={SRC.PersonNoAddress} size="tall" stage={(c) => <PersonNoAddress container={c} />} stageAlign="end">
<Alex />
</Specimen>
</Sub>
<Sub title="Long names" lead="Crumbs never wrap: a parent says itself shorter, then fades, then gives way whole, before the page you are on is cut, and the page fades last. A sidebar longer than the window scrolls under its head with its edge faded.">
<Specimen code={SRC.LongNames} layout="fill">
<Window size="wide" render={() => <LongNames />} />
</Specimen>
</Sub>
<Sub title="Settings page" lead="One column at `--app-settings-width`, each row's label at its start with at most one quiet line under it and its value or control at its end. A text field takes one width in every row, a text area the whole row under its label, a readout is mono, and a row that opens something is pressable whole and ends in a chevron.">
<Specimen code={SRC.SettingsSample} surface="pane" layout="fill">
<SettingsSample />
</Specimen>
</Sub>
<Sub title="Any other page" lead="A page that is not settings (members, a record, a table) takes one column at `--app-page-width`, wider than the settings measure, so its columns have room; on a narrower panel it is the panel's width.">
<Specimen code={SRC.Members} layout="fill">
<Window size="wide" render={() => <Members />} />
</Specimen>
</Sub>
</Section>
<Section title="Measures" lead="Each measure is one token, named for the frame; a product never types its number.">
<div ref={live}>
<Table
label="App frame tokens"
cols={[{ label: "Token", kind: "token" }, { label: "Use" }, { label: "Value", kind: "num" }]}
rows={MEASURES.map(([token, use]) => [<Tok name={token} />, use, <span data-token={token} />])}
/>
</div>
</Section>
<Section title="Sheet" lead="The narrow form of a pane: from its edge over a scrim, on the dialog's times on the strong ease-out, back out to its edge on close; under reduced motion it fades where it stands. Its width is the sidebar's, capped so `--drawer-gutter` of the page stays in sight.">
<Knobs type="SheetKnobs" />
</Section>
</>
);
}
export function Notes() {
return (
<Section title="Accessibility">
<P>
The bar is the `banner`, the sidebar a `navigation` named by its `aria-label`, the crumbs a `navigation` named
Breadcrumb, and the panel sits in `main`. The first stop is a skip link that moves focus to the panel's title.
Tab then reads the frame in order: the toggle on a narrow window, the home link, the crumbs, the bar's slot, the
switcher, the bell, the avatar, the sidebar's head and rows, then the page. With `arcadiaHome` the mark is a stop of its
own before the name, named Arcadia. The panel's head reads its title, its readout as plain text, then its action.
</P>
<P>
The current row and the crumbs' last level carry `aria-current="page"`, and the product you are in is marked the
same way in the switcher. The switcher is named Arcadia apps and the avatar by the person's name; both open
the kit's menu, which takes the arrow keys and Escape. When `viewKey` changes the title takes focus, so a reader
starts in the new view.
</P>
<P>
The sheet is a modal dialog named like the sidebar: the page behind goes inert, focus starts on the row you are on
and stays inside, Escape and a press on the scrim close it, and focus goes back to the toggle, which reports
`aria-expanded`. Every stop draws the kit's ring.
</P>
</Section>
);
}