Navbar
Navbar lays out a site's top bar, with a logo at the start, links in the centre and actions at the end.Navbar is the bar across the top of a website. It places a logo at the start, the main links in the centre and actions such as "Sign in" at the end, and it stays at the top of the window as the page scrolls. The top row of an app with panes is Shell.Header in a Shell.
Playground
Ship your next release faster
Scroll this page to see how the bar behaves.Usage
import { Button, Navbar, Text } from '@kushagradhawan/kookie-ui';
import Link from 'next/link';
export function SiteHeader() {
return (
<Navbar.Root position="sticky">
<Navbar.Logo asChild>
<Link href="/" aria-label="Acme home">
<Text weight="medium">Acme</Text>
</Link>
</Navbar.Logo>
<Navbar.Navigation aria-label="Main">
<Button asChild emphasis="quiet"><Link href="/pricing">Pricing</Link></Button>
<Button asChild emphasis="quiet"><Link href="/docs">Docs</Link></Button>
</Navbar.Navigation>
<Navbar.Actions>
<Button asChild emphasis="loud"><Link href="/signup">Get started</Link></Button>
</Navbar.Actions>
</Navbar.Root>
);
}Anatomy
<Navbar.Root>
<Navbar.Logo />
<Navbar.Navigation />
<Navbar.Actions />
</Navbar.Root>- Root: The bar, a
headerelement. Its three columns keep the navigation centred on the page however wide the logo and actions are. - Logo: The start column, for the brand. It's a link to the home page in most sites.
- Navigation: The centre column, for the main links. It has
role="navigation", and stays centred even when there is no Logo. - Actions: The end column, for buttons such as "Sign in" or a profile menu. It stays in the end column even when there is no Navigation.
Best practices
Keep the main links to a few. Navigation sits in the centre column, so a long list crowds the logo and actions. Put the rest in a footer or a Dropdown Menu.
Make links links. Render your router's link inside a Button with asChild. People can then open a page in a new tab and see where it goes.
Give one action the loud emphasis. The action you most want people to take, such as "Get started", is loud, and "Sign in" beside it is quiet.
Leave room for a fixed bar. A fixed Navbar sits over the page and takes no space. Pad the top of the page by the bar's height, or use position="sticky", which keeps its place in the flow.
Use Shell.Header in an app. Navbar is for pages that scroll. A frame with navigation panes, an inspector or a bottom panel is a Shell.
Position
position sets how the bar stays on screen.
| Position | What it does | Use it for |
|---|---|---|
fixed | Pins to the top of the window and takes no space. This is the default | Pages that leave room for it at the top |
sticky | Scrolls with the page until it reaches the top, then stays | Most sites. The bar keeps its place in the flow |
static | Scrolls away with the page | A bar at the top of a short page or a preview |
Fixed and sticky bars sit above the page with a z-index of 50 and paint the page's background, so content scrolls behind them.
Size
size sets the bar's padding, from '1' (8px by 12px) to '4' (24px by 32px). Size 2 (12px by 16px) is the default, or the Theme's size when one is set. The bar's height comes from its content, so match the buttons inside to it.
Examples
Profile menu in the actions
Actions is a Flex, so put the controls in it side by side.
<Navbar.Actions>
<Button iconOnly emphasis="quiet" aria-label="Notifications" tooltip="Notifications">
<HugeiconsIcon icon={Notification01Icon} strokeWidth={1.75} />
</Button>
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Button emphasis="quiet">Account</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>{/* Profile, settings, sign out */}</DropdownMenu.Content>
</DropdownMenu.Root>
</Navbar.Actions>Small screens
Navbar doesn't change at narrow widths, so plan for them. Hide the links below md with the Flex display prop, and offer them from a menu button in the actions.
<Navbar.Navigation aria-label="Main" display={{ initial: 'none', md: 'flex' }}>
{/* links */}
</Navbar.Navigation>
<Navbar.Actions>
<DropdownMenu.Root>
<DropdownMenu.Trigger>
<Button iconOnly emphasis="quiet" aria-label="Menu">
<HugeiconsIcon icon={Menu01Icon} strokeWidth={1.75} />
</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Content>{/* the same links */}</DropdownMenu.Content>
</DropdownMenu.Root>
</Navbar.Actions>Props
Root
| Prop | Type | Default | Description |
|---|---|---|---|
position | 'fixed' | 'sticky' | 'static' | 'fixed' | How the bar stays on screen |
size | '1' | '2' | '3' | '4' | Theme size, else '2' | The padding step |
Root accepts the attributes of the HTML header element.
Logo, Navigation and Actions
Each slot is a Flex and takes its props except the margin props, since the bar spaces its columns.
| Prop | Type | Default | Description |
|---|---|---|---|
asChild | boolean | false | Renders the child element instead of a Flex |
gap | Flex gap | Logo '2', others '4' | The space between the slot's children |
align | Flex align | 'center' | The children's vertical alignment |
justify | Flex justify | Logo 'start', Navigation 'center', Actions 'end' | Where the children sit in the column |
With asChild, your element takes the slot's class and its Flex layout, including the default gap, align and justify.
Accessibility
- Landmark: Root is a
header, which is the page's banner landmark when it isn't inside anarticle,aside,main,navorsection. - Navigation: It has
role="navigation"and the name "Main". Pass your ownaria-labeloraria-labelledbywhen the page has other navigation. - Logo: A logo that is only an image needs a name. Set
aria-labelon the link, oralton the image. - Focus order: The slots render in the order you write them, which is the order Tab moves through them. Keep it logo, navigation, actions.