仕組み
A 56px bar with four slots: start, title, a middle area and actions, then the avatar and its menu. Icon buttons are 36px filled circles and the person is a filled pill, so the bar needs no outlines; one soft hairline sits under it. Width tiers come from the bar's own width through a ResizeObserver, not the viewport, because it usually sits beside a side navigation; under 768px it keeps only the avatar. The title shrinks first, and a breadcrumb node shrinks itself so its collapsed menu can still extend below the bar. The avatar menu is absolutely positioned, not a portal.
向いている場面
- Dashboards and workspaces with a side navigation: the strip at the top right holding the page name or breadcrumb, global actions and the signed-in person.
- A fixed place for entries unrelated to a page: notifications, help, switch store, sign out.
- Narrow screens where the side navigation becomes a drawer: put the expand-menu button in start.
向かない場面
- The page's own title and main button: put them in a page header inside the content area.
- A marketing site header with a row of links: the top bar does not handle navigation between pages.
- Inside an ancestor with overflow: hidden that is only as tall as the bar: the avatar menu would be clipped.
- A mobile app title bar with a back arrow and centered title: that is a different pattern.
使い方
import { TopBar, TopBarAction } from './TopBar.jsx'
const userMenu = [
{ key: 'profile', label: 'Profile settings', icon: <UserIcon /> },
{ key: 'store', label: 'Switch store', icon: <SwapIcon /> },
{ key: 'logout', label: 'Sign out', icon: <ExitIcon />, danger: true },
]
export function AppHeader({ unread, onOpenNotices, onLogout }) {
return (
<TopBar
title="Inventory"
actions={
<>
<TopBarAction label="Notifications" badge={unread} onClick={onOpenNotices}>
<BellIcon />
</TopBarAction>
<TopBarAction label="Help" onClick={() => window.open('/help')}>
<HelpIcon />
</TopBarAction>
</>
}
user={{ name: 'Maya Lopez', description: 'Market Street store · Manager', avatarUrl: '/avatars/maya.png' }}
userMenu={userMenu}
onUserMenuSelect={(key) => {
if (key === 'logout') onLogout()
}}
/>
)
}
// A breadcrumb as the title; on narrow screens the page shell passes an expand-menu button into start
<TopBar
start={
<TopBarAction label="Expand menu" onClick={openNav}>
<MenuIcon />
</TopBarAction>
}
title={<Breadcrumb items={[{ label: 'Products', href: '/products' }, { label: 'Inventory' }]} />}
user={{ name: 'Maya Lopez' }}
/>プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
start | ReactNode | — | Leftmost slot, for something like an expand-menu button; 8px from the title. |
title | ReactNode | — | Page name or breadcrumb. A string shows at 16px / 600 and truncates; a node (breadcrumb) shrinks and truncates itself. |
children | ReactNode | — | Middle area, such as a global search, filling the width between the title and the actions. On narrow screens the caller hides it. |
actions | ReactNode | — | A group of actions on the right, usually a few TopBarAction, 8px apart. |
user | { name, avatarUrl?, description? } | — | The signed-in person: a filled pill with a 32px avatar and the name on the far right; the menu head repeats the avatar with the name and description (store, role). Omit for no avatar area. |
userMenu | { key, label, icon?, danger?, disabled? }[] | — | Menu opened from the avatar; danger items use the danger color and get a separator. Without it the avatar is display only. |
onUserMenuSelect | (key: string) => void | — | Called when a menu item is chosen; the menu closes and focus returns to the avatar first. |
compact | boolean | — | Show only the avatar, no name. When omitted it follows the bar's own width: under 768px only the avatar. |
...rest | HTMLAttributes<HTMLElement> | — | Other attributes go to the root <header>. Use --ds-topbar-pad for side padding and --ds-topbar-z for the dropdown layer (default 30). |
TopBarAction · label | string | — | Required. The button name: used as aria-label and shown as a tip below the button after 400ms of hover or on keyboard focus. Keep it to 1–3 words. |
TopBarAction · children | ReactNode | — | The icon: a 20px SVG using currentColor. |
TopBarAction · badge | number | — | Number badge: above 99 shows "99+"; 0 or omitted shows nothing. It pops in with a small overshoot when the number changes (not on first paint). The count is added to the accessible name. |
TopBarAction · dot | boolean | false | Red dot for new activity without a number; ignored when badge is above 0. |
TopBarAction · labels | object | — | Replaces built-in text (withCount, withDot), both functions that build the accessible name. Shallow-merged over the English defaults. |
TopBarAction · ...rest | ButtonHTMLAttributes<HTMLButtonElement> | — | Other attributes (onClick, disabled, className…) go to the <button>; ref is supported. |
アクセシビリティ
- The root is a <header>; the avatar button has aria-haspopup="menu", aria-expanded and aria-controls, and the menu uses role="menu", "menuitem" and "separator".
- On the avatar button Enter, Space and Down open the menu on the first item, Up on the last; inside, Up and Down cycle (skipping disabled), Home and End jump, Enter or Space selects.
- Esc closes only the menu and returns focus to the avatar; Tab closes and moves on; clicking outside or moving focus out closes it.
- With only the avatar shown the name is visually hidden, not removed, so the button keeps the person's name; TopBarAction needs a label, and badge counts or "new activity" are appended to it.
- The text tip shows after 400ms of hover or at once on keyboard focus, and closes on press or Esc; none on touch.
- Buttons have a focus ring (outline in forced-colors mode); targets are 44px on touch, and reduced motion fades without movement and does not pop badges.
モーション
The avatar menu grows from the avatar in 200ms ease-out (scale 0.96 and 6px) and leaves in 140ms ease-in; the tip (an ink pill with a caret) scales in over 160ms; a changed badge pops from 0.4 with a spring overshoot in about 400ms; hover colours take 160ms and a press scales buttons to 0.94; menu highlights have no transition.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
TopBar.jsxtop-bar.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。