Navigation
App bar
The app bar is the strip at the top of every screen of a product: on the web it holds the product, search, Add, notifications and the signed-in person, and on phones it becomes the top bar above the page title.
In kanso-ui as AppBar
Web and native
AppBar in kanso-ui 0.59.0, for React.
import { AppBar } from '@kanso-labs/kanso-ui'The bar at the top of a page that carries its title, a way back and one or two actions.
It follows Material Design's app bar in its three recommended sizes: sm is the
small bar, and md and lg are the medium and large flexible bars, whose
heights are minimums so a subtitle or a wrapping headline grows the bar instead
of being cut off. Centre alignment is a setting on every size rather than a
variant of its own, as in the current spec.
What you provide
headline: the page's title. It is the page's<h1>unlessheadingLevelsays otherwise.leading: usually a back or menuIconButton, with anaria-label.trailing: actions at the far end, usuallyIconButtons.- The bar renders a
<header>and paints its own surface. Apinnedbar needs a scrolling ancestor to stick inside: the window, or an element with its own overflow.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
align | 'center' | 'start' | 'start' | Which of Material Design's alignments the text takes. `center` is the configuration that replaced the old center-aligned variant, and centres the text on the bar rather than between the slots, so a bar with a leading icon and no actions still has its headline in the middle. The text moves off the middle only as far as a slot wider than the other needs, rather than running under it. Beside the slots, which is `sm` and a collapsed flexible bar, each line of centred text takes one line and is cut short with an ellipsis rather than wrapping. |
collapsed | boolean | false | Collapses a flexible bar to the height and headline of the small one, for a bar `pinned` over content that has been scrolled. Material Design specifies it for the flexible sizes, and without it a pinned `lg` costs 152px of the viewport for as long as the page is open. Controlled, and for the same reason `scrolled` is: only the app knows which element scrolls, and how far into it the bar should have finished collapsing. `useAppBarScroll` derives both from that element's scroll, and is the way to set them unless the app needs a rule of its own. Ignored on `sm`, which is already the height this collapses to. The headline and the subtitle move up into the row the slots sit in and take the small bar's type roles, which fit both lines in its 64px, and the slots never leave that row: they stay where they are while the bar shrinks under them. A kicker goes, and fades back in on expanding. Collapsing hands the page back the height the bar gives up, and a bar whose state is read off the scroll offset can then undo itself in two ways. Both move the offset back under the threshold, so the bar expands, the page regains its height, and the two take turns for as long as the reader stays there. - **Scroll anchoring.** Browsers answer a change in height above the viewport by moving the offset by the same amount, to keep the content in place. Give the scroll container `overflow-anchor: none`, which is also the movement you want: the content follows the bar's bottom edge up rather than standing still while the bar shrinks behind it. - **The shorter page.** A page that overflows by less than the threshold plus the height the bar gives back cannot scroll that far once the bar has collapsed, so the browser pulls the offset back inside it. No threshold avoids this, since the band moves with it; what does is not collapsing on such a page, which is the rule `useAppBarScroll` applies. |
contentInset | string | spacing.lg | Where the bar's content starts on each edge: the headline where nothing comes before it, and the leading icon's glyph where something does, with the headline after the icon button's target. The trailing edge is the same mirrored. Any length works, `0` included, for a bar drawn flush. The default is Material Design's own margin for a top app bar, the spacing scale's `lg` step, which is also the margin it gives body content, so a bar and the page beneath line up without either being told about the other. Set it to the page's gutter where that differs. |
contentMaxInlineSize | string | — | How wide the bar's contents may run before they stop growing, with the row centred in whatever is left. The bar's own surface still paints edge to edge. This is what lines a full-bleed bar up with a page whose content sits in a measure: give it the page's measure, and `contentInset` the page's gutter. Unset, the row is as wide as the bar. |
headingLevel | 1 | 2 | 3 | 4 | 5 | 6 | 1 | Where the headline sits in the page's outline, as one of the six heading levels. A screen reader moves between headings, so a bar drawn beside another bar, or under a page heading of its own, should lower it. A value outside the six that reaches it untyped falls back to 1. |
headline | ReactNode | — | The page's title. Wraps rather than truncating, and grows the bar with it, except beside the slots of a centred bar, where it takes one line and is cut short with an ellipsis — see `align`. |
kicker | ReactNode | — | A line over the headline in `md` and `lg`, in label medium on on surface variant, naming where the screen sits — the parent a detail screen opened from, say, which it may hold as a link back there. Makes the bar taller by its line, and goes as the bar collapses, leaving the headline and any subtitle. `sm` draws none. |
leading | ReactNode | — | Usually a back or menu icon button. Sits before the text, and a standard icon button here draws its icon in on surface, the colour the page gives a bar's leading icon, rather than its own on surface variant. |
pinned | boolean | false | Holds the bar at the top of its scroll container while the content scrolls beneath it, and keeps it painted over that content. The bar still needs a scrolling ancestor to stick inside: the window, or an element with its own overflow. A pinned bar is the one `collapsed` and `scrolled` are for. |
scrolled | boolean | false | Whether the content beneath has been scrolled. Material Design separates a bar from scrolled content with a fill colour rather than a shadow, and this is what applies it. Controlled, with no listener of its own: only the app knows which element scrolls, and a bar that watched the window would be wrong inside every pane and dialog that scrolls independently. |
size | 'lg' | 'md' | 'sm' | 'sm' | Height and headline size, as steps of the library's size scale. `sm` is Material Design's small bar, a fixed 64px row; `md` and `lg` are its medium and large flexible bars, which put the text in a row of its own under the slots and hug it. A kicker adds its line to `md` and `lg`. |
subtitle | ReactNode | — | A second line under the headline. Makes `md` and `lg` taller. |
trailing | ReactNode | — | Actions, at the far end. Usually icon buttons. |
Also takes the props of Omit<RenderComponentProps<'header'>, 'children'>.
Example
import { AppBar, IconButton } from '@kanso-labs/kanso-ui'
<AppBar
headline="Headline"
leading={<IconButton aria-label="Back"><BackIcon /></IconButton>}
trailing={<IconButton aria-label="More"><MoreIcon /></IconButton>}
/>
<AppBar size="lg" headline="Headline" subtitle="Supporting line" />The design’s classes
k-appbar, k-appbar__product, k-appbar__dot, k-appbar__spacer, k-search, k-topbar, k-topbar--back, k-pagehead, k-pagehead__date and k-pagehead__title, with k-btn, k-iconbtn and k-avatar. In kanso-ui and react-native-kanso-ui it is AppBar.