feat(frontend): simplify the candidate cabinet header

This commit is contained in:
shira committed 2026-10-10 15:34:42 +03:00
1 parent 90ff71735a
commit 89932b8714
28 files changed
+1154 -179

No files matched your search

+3 -1
View File
@@ -4,15 +4,17 @@ export { Card } from "./card/card";
export { CircuitTrace } from "./decor/circuit-trace";
export { HeaderPlate } from "./header-plate/header-plate";
export { Page } from "./layout/page";
export { PageNavProvider } from "./layout/page-nav";
export { Section } from "./layout/section";
export { Stack } from "./layout/stack";
export { NavItem, TextLink, type NavItemLayout } from "./link/text-link";
export { NavItem, TextLink, type NavItemCurrent, type NavItemLayout } from "./link/text-link";
export { MainRegion, mainContentId } from "./main-region/main-region";
export { MenuButton } from "./menu-button/menu-button";
export { useMenuToggle } from "./menu-button/use-menu-toggle";
export { Mono } from "./mono/mono";
export { Notice, type NoticeTone } from "./notice/notice";
export { SegmentedControl, type SegmentedOption } from "./segmented-control/segmented-control";
export { SectionSwitch, type SectionSwitchItem } from "./section-switch/section-switch";
export { Skeleton } from "./skeleton/skeleton";
export { SkipLink } from "./skip-link/skip-link";
export { describeError, isAccessDenied, type ErrorDescription } from "./status-view/describe-error";
@@ -12,6 +12,11 @@
max-width: calc(var(--content-width-wide) + 2 * var(--gutter));
}
/* Secondary navigation of the shell's section, above the heading. */
.section-nav {
margin-bottom: var(--space-5);
}
/* The header closes with a hairline trace that starts at a node. */
.page-header {
position: relative;
+16
View File
@@ -0,0 +1,16 @@
import { createContext, useContext, type ReactNode } from "react";
const PageNavContext = createContext<ReactNode>(null);
/**
* Lets a shell hand a secondary navigation (for example a switch between the
* sub-sections of a cabinet section) to the pages inside it. Every Page shows
* it above its heading, so it lines up with the page content at any width
* without each screen repeating it.
*/
export const PageNavProvider = PageNavContext.Provider;
/** The secondary navigation provided by the shell, or null. */
export function usePageNav(): ReactNode {
return useContext(PageNavContext);
}
+5 -1
View File
@@ -1,5 +1,6 @@
import type { ReactNode } from "react";
import styles from "./layout.module.css";
import { usePageNav } from "./page-nav";
const productName = "Lincodex";
@@ -15,12 +16,15 @@ type PageProps = {
/**
* Page content inside a shell's main landmark. It owns the only h1 of the
* page and sets the document title (React hoists <title> into <head>).
* page and sets the document title (React hoists <title> into <head>). A
* secondary navigation from the shell (see PageNavProvider) sits above it.
*/
export function Page({ title, eyebrow, lead, width = "narrow", children }: PageProps) {
const sectionNav = usePageNav();
return (
<div className={`${styles.page} ${styles[width]}`}>
<title>{`${title} - ${productName}`}</title>
{sectionNav ? <div className={styles["section-nav"]}>{sectionNav}</div> : null}
<header className={styles["page-header"]}>
{eyebrow ? <p className={styles.eyebrow}>{eyebrow}</p> : null}
<h1 className={styles.title}>{title}</h1>
@@ -43,7 +43,7 @@
background-color: var(--color-surface-tint-hover);
}
.nav[aria-current="page"] {
.nav[aria-current] {
color: var(--color-on-inverse);
background-color: var(--color-inverse);
}
+32 -4
View File
@@ -1,4 +1,5 @@
import { Link, NavLink, type LinkProps, type NavLinkProps } from "react-router-dom";
import { Link, NavLink, type NavLinkProps } from "react-router-dom";
import type { LinkProps } from "react-router-dom";
import styles from "./text-link.module.css";
type TextLinkProps = Omit<LinkProps, "className"> & {
@@ -12,19 +13,46 @@ export function TextLink({ className, ...rest }: TextLinkProps) {
export type NavItemLayout = "row" | "rail";
type NavItemProps = Omit<NavLinkProps, "className" | "children"> & {
/**
* Current state set by the shell for a section that owns several routes:
* "page" when the link points at the open page, "true" when the open page is
* a sub-page of the section, "none" when the section is not open.
*/
export type NavItemCurrent = "page" | "true" | "none";
type NavItemProps = Omit<NavLinkProps, "className" | "children" | "style"> & {
/** "rail" stretches the item to the width of a vertical list. */
layout?: NavItemLayout;
/**
* Overrides the route match of NavLink, for a section that stays active on
* the routes of its sub-sections.
*/
current?: NavItemCurrent;
children: string;
};
/**
* Navigation link of a shell. NavLink sets aria-current="page" on the active
* item; the active item is an inverted capsule, a change of fill rather than
* of hue alone.
* of hue alone. A shell can state the current state itself with `current`.
*/
export function NavItem({ layout = "row", children, ...rest }: NavItemProps) {
export function NavItem({ layout = "row", current, children, ...rest }: NavItemProps) {
const className = layout === "rail" ? `${styles.nav} ${styles.rail}` : (styles.nav ?? "");
if (current !== undefined) {
// The route match options belong to NavLink and are not valid on Link.
const { end, caseSensitive, ...linkProps } = rest;
void end;
void caseSensitive;
return (
<Link
{...linkProps}
className={className}
aria-current={current === "none" ? undefined : current}
>
{children}
</Link>
);
}
return (
<NavLink {...rest} className={className}>
{children}
@@ -56,3 +56,19 @@
.icon[data-open] span:last-child {
transform: translateY(-0.28rem) rotate(-45deg);
}
/* Caret of a menu that drops from a control; it turns up while open. */
.chevron {
flex: none;
transition: transform var(--duration-base) var(--easing-emphasized);
}
.root[aria-expanded="true"] .chevron {
transform: rotate(180deg);
}
/* The open page is one of the menu's destinations: a hairline ring in the
* accent color, a change of outline rather than of fill alone. */
.root[data-current]:not([aria-expanded="true"]) {
border-color: var(--color-accent);
}
@@ -7,13 +7,32 @@ type MenuButtonProps = {
onToggle: () => void;
buttonRef: RefObject<HTMLButtonElement | null>;
className?: string | undefined;
/** Visible label; "Меню" unless the menu has a more specific name. */
label?: string;
/**
* "bars" is the burger that turns into a cross, for a menu that replaces a
* navigation. "chevron" is a caret, for a menu that drops from a control.
* "none" is the label alone, for a button that sits among text tabs.
*/
icon?: "bars" | "chevron" | "none";
/** Marks the button when the open page is one of the menu's destinations. */
current?: boolean;
};
/**
* Button that shows or hides a collapsed navigation menu. The visible label
* stays "Меню"; aria-expanded tells whether the menu is open.
* Button that shows or hides a collapsed navigation menu. aria-expanded tells
* whether the menu is open.
*/
export function MenuButton({ menuId, isOpen, onToggle, buttonRef, className }: MenuButtonProps) {
export function MenuButton({
menuId,
isOpen,
onToggle,
buttonRef,
className,
label = "Меню",
icon = "bars",
current = false,
}: MenuButtonProps) {
return (
<button
ref={buttonRef}
@@ -21,13 +40,35 @@ export function MenuButton({ menuId, isOpen, onToggle, buttonRef, className }: M
className={className ? `${styles.root} ${className}` : styles.root}
aria-expanded={isOpen}
aria-controls={menuId}
data-current={current || undefined}
onClick={onToggle}
>
<span className={styles.icon} aria-hidden="true" data-open={isOpen || undefined}>
<span />
<span />
</span>
Меню
{icon === "bars" ? (
<span className={styles.icon} aria-hidden="true" data-open={isOpen || undefined}>
<span />
<span />
</span>
) : null}
{label}
{icon === "chevron" ? (
<svg
className={styles.chevron}
viewBox="0 0 12 12"
width="12"
height="12"
aria-hidden="true"
focusable="false"
>
<path
d="M2.5 4.5 6 8l3.5-3.5"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
) : null}
</button>
);
}
@@ -1,14 +1,25 @@
import { useCallback, useEffect, useId, useRef, useState } from "react";
import { useLocation } from "react-router-dom";
type MenuToggleOptions = {
/**
* Also closes the menu when a pointer press or keyboard focus lands outside
* the element that holds the button and the menu (`rootRef`). For a menu
* that floats over the page, where leaving it by Tab or by a press on the
* page should dismiss it.
*/
dismissOutside?: boolean;
};
/**
* State of a navigation menu that collapses behind a button on narrow
* screens. The menu closes after a navigation and on Escape; Escape returns
* focus to the button so keyboard users keep their place.
*/
export function useMenuToggle() {
export function useMenuToggle({ dismissOutside = false }: MenuToggleOptions = {}) {
const menuId = useId();
const buttonRef = useRef<HTMLButtonElement>(null);
const rootRef = useRef<HTMLDivElement>(null);
const [isOpen, setIsOpen] = useState(false);
const { pathname } = useLocation();
const [openedAt, setOpenedAt] = useState(pathname);
@@ -26,14 +37,30 @@ export function useMenuToggle() {
setIsOpen(false);
buttonRef.current?.focus();
}
function handleOutside(event: Event) {
const root = rootRef.current;
if (root && event.target instanceof Node && !root.contains(event.target)) {
setIsOpen(false);
}
}
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, [isOpen]);
if (dismissOutside) {
document.addEventListener("pointerdown", handleOutside);
document.addEventListener("focusin", handleOutside);
}
return () => {
document.removeEventListener("keydown", handleKeyDown);
document.removeEventListener("pointerdown", handleOutside);
document.removeEventListener("focusin", handleOutside);
};
}, [isOpen, dismissOutside]);
const toggle = useCallback(() => {
setOpenedAt(pathname);
setIsOpen((open) => !open);
}, [pathname]);
return { menuId, buttonRef, isOpen, toggle };
const close = useCallback(() => setIsOpen(false), []);
return { menuId, buttonRef, rootRef, isOpen, toggle, close };
}
@@ -0,0 +1,50 @@
.root {
min-width: 0;
}
.track {
display: flex;
gap: var(--space-1);
width: fit-content;
max-width: 100%;
margin: 0;
padding: var(--space-1);
border: var(--line-thin) solid var(--color-divider);
border-radius: var(--radius-pill);
list-style: none;
background: var(--color-surface);
}
.segment {
display: flex;
min-width: 0;
}
/* 44px tall: the segment is a touch target. */
.link {
display: inline-flex;
align-items: center;
min-height: var(--control-height);
padding: var(--space-2) var(--space-4);
border-radius: var(--radius-pill);
color: var(--color-text-secondary);
font-family: var(--font-mono);
font-size: var(--font-size-s);
font-weight: var(--font-weight-medium);
text-decoration: none;
white-space: nowrap;
transition:
background-color var(--duration-fast) var(--easing-standard),
color var(--duration-fast) var(--easing-standard);
}
.link:hover {
color: var(--color-text);
background: var(--color-surface-tint-hover);
}
/* The open segment: graphite on paper 14.53:1. */
.link[aria-current] {
color: var(--color-on-inverse);
background: var(--color-inverse);
}
@@ -0,0 +1,42 @@
import { Link } from "react-router-dom";
import type { NavItemCurrent } from "../link/text-link";
import styles from "./section-switch.module.css";
export type SectionSwitchItem = {
to: string;
label: string;
current: NavItemCurrent;
};
type SectionSwitchProps = {
/** Accessible name of the navigation landmark. */
label: string;
items: readonly SectionSwitchItem[];
};
/**
* Switch between the sub-sections of one section, drawn as a capsule track
* like Tabs. Each segment is a link to its own route, so the browser history,
* the address, and aria-current work as for any navigation; the open segment
* is inverted to paper. Tabs would be wrong here: there is no panel to
* control, only another page.
*/
export function SectionSwitch({ label, items }: SectionSwitchProps) {
return (
<nav aria-label={label} className={styles.root}>
<ul className={styles.track}>
{items.map((item) => (
<li key={item.to} className={styles.segment}>
<Link
to={item.to}
className={styles.link}
aria-current={item.current === "none" ? undefined : item.current}
>
{item.label}
</Link>
</li>
))}
</ul>
</nav>
);
}