Files
CatoandClaude f1c5c7c16c feat(menu): rows shown only below 768px (shown: "narrow") (#95)
An AccountMenu item or group may carry shown: "always" | "narrow"
(default "always"), and extra has the same choice as extraShown. A narrow
row is in the menu below 768px only: hidden by the package stylesheet in a
media query (no width read in script, right on the server render and the
first paint), skipped by the arrow keys, and a hidden group takes its
divider with it.

For a row that repeats a control the header carries from md up, such as
OrangeCat's theme toggle, which its menu also showed as "Appearance".


Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-13 11:53:47 +01:00

144 lines
5.6 KiB
TypeScript

import { type ReactNode } from "react";
import { type AccountUser } from "./identity.js";
/**
* At which widths a row is shown. `"always"` (the default) at every width;
* `"narrow"` only below 768px, for a row that repeats a control the header
* itself shows from that width up (a theme toggle, a search button). Hidden
* by the package's stylesheet, in a media query, so the row is right on the
* first paint and on the server: no width read in script, no flash.
*/
export type AccountRowShown = "always" | "narrow";
/** A place to go. Rendered through `renderLink` so a Next app keeps <Link>. */
export type AccountLinkItem = {
id: string;
label: string;
href: string;
icon?: ReactNode;
shown?: AccountRowShown;
};
/** Something to do. Closes the menu, then runs. */
export type AccountActionItem = {
id: string;
label: string;
onSelect: () => void | Promise<unknown>;
icon?: ReactNode;
shown?: AccountRowShown;
};
export type AccountItem = AccountLinkItem | AccountActionItem;
/**
* Items are grouped; a divider separates groups. A group whose `shown` is
* `"narrow"`, or whose every item's is, goes from 768px up with its divider.
*/
export type AccountGroup = {
id: string;
items: AccountItem[];
shown?: AccountRowShown;
};
/** A group goes wide when it says so, or when every one of its items does. */
export declare function groupShown(group: AccountGroup): AccountRowShown;
/**
* Another account the signed-in person can become: a second identity, a work
* account. The app decides which ones exist; the menu shows them and switches.
*/
export type AccountSwitchTarget = {
id: string;
user: AccountUser;
/**
* What this account is to the person, in a word or two ("Main account").
* Leads the second line, so it is what survives a long handle.
*/
note?: string;
/**
* How many things wait for the person in this account (unread messages,
* notifications). Shown as a count on its row, and as a dot on the trigger
* so it is known without opening the menu. Zero, absent or not a positive
* number: nothing is shown.
*/
waiting?: number;
};
export type AccountMenuLabels = {
/** Accessible name of the trigger; "{name}" is replaced. */
trigger: string;
menu: string;
signOut: string;
signingOut: string;
signOutFailed: string;
/** Heading over the other accounts. */
switchAccount: string;
switching: string;
switchFailed: string;
/** Read out for an account's count; "{count}" is replaced. */
waiting: string;
/** Added to the trigger's name when another account has something waiting. */
waitingElsewhere: string;
/**
* The signed-out link of `AccountControl`. Optional, so a full set of
* labels written before it existed still type-checks.
*/
signIn?: string;
};
export declare const DEFAULT_LABELS: Required<AccountMenuLabels>;
export type RenderLinkProps = {
href: string;
className: string;
role: "menuitem";
tabIndex: -1;
onClick: () => void;
children: ReactNode;
};
export type AccountMenuProps = {
user: AccountUser;
/**
* Called when the person signs out. May return a promise; while it is
* pending the row says so, and if it rejects the menu stays open and says
* the sign-out did not complete — a dead sign-out is the one failure this
* menu must never hide.
*/
onSignOut: () => void | Promise<unknown>;
groups?: AccountGroup[];
/**
* The other accounts this person can become, listed under who is signed in.
* Omit or pass none and the menu is unchanged. Their email is never shown:
* the second line is the note and the handle.
*/
accounts?: AccountSwitchTarget[];
/**
* Called with the account to become. May return a promise; while it is
* pending the row says so, and if it rejects the menu stays open and says
* the switch did not complete, exactly as sign-out does. On success the app
* usually reloads as the other account.
*/
onSwitchAccount?: (id: string) => void | Promise<unknown>;
/**
* Rows that are not menu items — a theme switch, a status line. Rendered
* between the groups and Sign out, not navigable by arrow keys.
*/
extra?: ReactNode;
/**
* At which widths `extra` is shown. `"narrow"` for a theme switch the
* header itself carries from 768px up. Default `"always"`.
*/
extraShown?: AccountRowShown;
/** Defaults to a plain <a>. Pass `(p) => <Link {...p} />` in a Next app. */
renderLink?: (props: RenderLinkProps) => ReactNode;
labels?: Partial<AccountMenuLabels>;
/** Which edge of the trigger the panel aligns to. */
align?: "end" | "start";
className?: string;
};
/**
* The account menu: who is signed in, where to go, how to sign out.
*
* Identity, not navigation: the app's sidebar is for places in the product;
* this is for the person. Nothing should appear in both.
*
* Behaviour that is shared and NOT configurable, because each item shipped
* broken in at least one app:
* - a 44px trigger (Loki's was 36px, under the touch-target floor);
* - a photo that fails to load falls back to initials, then a glyph;
* - Escape closes and returns focus to the trigger; arrows, Home and End
* move between items; a click outside closes;
* - sign-out shows that it is happening, and says so when it fails.
*/
export declare function AccountMenu({ user, onSignOut, groups, accounts, onSwitchAccount, extra, extraShown, renderLink, labels: labelOverrides, align, className, }: AccountMenuProps): import("react").JSX.Element;
//# sourceMappingURL=AccountMenu.d.ts.map