# Navbar

The top bar of a public site: brand, up to five links, actions and a language choice, collapsing into a menu on phones.

- Family: navigation
- Import: `import { Navbar, NavbarBrand, NavbarItem, NavbarActions } from "@senvenli/react/navigation";`
- Page: https://design.senvenli.com/docs/components/navbar
- JSON: https://design.senvenli.com/docs/components/navbar.json

## Example: A public site's top bar

Brand, links and actions; under 768px the links and actions move into a menu panel.

```tsx
import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Navbar } from "@senvenli/react/navigation";

export default function Example() {
  return (
    <div className="overflow-hidden rounded-ds-large border border-ds-border">
      <Navbar
        brand="Acme"
        brandHref="#"
        brandLabel="Acme home"
        currentKey="pricing"
        links={[
          { id: "features", label: "Features", href: "#" },
          { id: "pricing", label: "Pricing", href: "#" },
          { id: "docs", label: "Docs", href: "#" },
        ]}
        actions={
          <>
            <Button variant="subtle">Sign in</Button>
            <Button variant="primary">Start free trial</Button>
          </>
        }
      />
      <div className="p-ds-300">
        <Text tone="subtle">The page starts here.</Text>
      </div>
    </div>
  );
}
```

## Examples

### Composed from parts

`NavbarBrand`, `NavbarItem` and `NavbarActions` as children: a logo with an icon, a link with a badge.

```tsx
import { Button } from "@senvenli/react/action";
import { Badge, Icon, Text } from "@senvenli/react/display";
import { Navbar, NavbarActions, NavbarBrand, NavbarItem } from "@senvenli/react/navigation";

export default function Example() {
  return (
    <div className="overflow-hidden rounded-ds-large border border-ds-border">
      <Navbar brandHref="#" brandLabel="Acme home" currentKey="docs">
        <NavbarBrand>
          <Icon name="hexagon" />
          Acme
        </NavbarBrand>
        <NavbarItem id="features" href="#">
          Features
        </NavbarItem>
        <NavbarItem id="changelog" href="#">
          Changelog
          <Badge variant="subtle" tone="information">
            New
          </Badge>
        </NavbarItem>
        <NavbarItem id="docs" href="#">
          Docs
        </NavbarItem>
        <NavbarActions>
          <Button variant="subtle">Sign in</Button>
          <Button variant="primary">Start free trial</Button>
        </NavbarActions>
      </Navbar>
      <div className="p-ds-300">
        <Text tone="subtle">The page starts here.</Text>
      </div>
    </div>
  );
}
```

### Compact, with a language switch

The 48px bar with a LanguageSwitcher among the end actions.

```tsx
import { Button } from "@senvenli/react/action";
import { Navbar } from "@senvenli/react/navigation";
import { LanguageSwitcher } from "@senvenli/react/action";
import { useState } from "react";

// The language switcher is one of the end actions, before the primary action; on phones it moves into the panel.
export default function Example() {
  const [locale, setLocale] = useState<"en" | "de" | "fr">("en");
  return (
    <div className="overflow-hidden rounded-ds-large border border-ds-border">
      <Navbar
        density="compact"
        brand="Acme"
        brandHref="#"
        brandLabel="Acme home"
        links={[
          { id: "features", label: "Features", href: "#" },
          { id: "pricing", label: "Pricing", href: "#" },
        ]}
        actions={
          <>
            <LanguageSwitcher locales={["en", "de", "fr"]} value={locale} onChange={setLocale} size="small" />
            <Button variant="primary" size="small">
              Start free trial
            </Button>
          </>
        }
      />
    </div>
  );
}
```

## Guidelines

### When to use it

- The main navigation of a marketing or public site.

### Use something else when

- [sidebar](https://design.senvenli.com/docs/components/sidebar.md): An app with many sections.

## Accessibility

Built in:

- A navigation landmark named "Main menu".
- The brand link is named for the home page.

You provide:

- A `brandLabel` when the brand alone does not say where it goes.
- With children: an id on every NavbarItem (currentKey matches it), or isCurrent on the page's link.

## API reference

### Navbar

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| brand | `React.ReactNode` |  | no | The logo and wordmark (the site's logo). It links to `brandHref`. Or a `NavbarBrand` child. |
| brandHref | `string` | `"/"` | no | Where the brand goes: the home page. |
| brandLabel | `string` |  | no | The brand link's name when the brand alone does not say where it goes ("Acme home"). |
| links | `readonly NavbarLink[]` |  | no | Up to five destinations. Leave it out to compose `NavbarItem`s as children instead. |
| children | `React.ReactNode` |  | no | With no `links`: `NavbarItem`s (the destinations), plus an optional `NavbarBrand` (instead of `brand`) and `NavbarActions` (instead of `actions`). |
| currentKey | `string` |  | no | The link of the page (or section) in view: marked `aria-current="page"`. |
| actions | `React.ReactNode` |  | no | At the end: a language switcher, sign-in (subtle), then the one primary action, the same on every page. On phones they move into the menu panel, under the links. Or a `NavbarActions` child. |
| isSticky | `boolean` |  | no | Pinned to the top while the page scrolls. |
| maxWidth | `string \| number` |  | no | The width of the bar's content ("1280px", 1280), centred, so the brand and the actions line up with the page's own container; the bar's background and border still run the full width. Left out, the content spans it. |
| density | `Density` | `"comfortable"` | no | `compact`: 48px instead of 56px, the bar after the hero. |
| aria-label | `string` |  | no | Names the navigation landmark; "Main menu" by default. |

### NavbarBrand

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | yes | The logo and wordmark; it links to the Navbar's `brandHref`. |

### NavbarItem

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `string` |  | yes | Matches the Navbar's `currentKey`. |
| href | `string` |  | yes | A page or an in-page anchor ("/#pricing"); it goes through the host router. |
| children | `React.ReactNode` |  | yes | A noun for the place ("Pricing"), never a verb. |
| isCurrent | `boolean` |  | no | Marks this link as the page in view, instead of the Navbar's `currentKey`. |

### NavbarActions

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | yes | A language switcher, sign-in (subtle), then the one primary action. |

### Tokens it reads

`--ds-background-neutral`, `--ds-background-neutral-pressed`, `--ds-background-neutral-subtle-hovered`, `--ds-blanket`, `--ds-border`, `--ds-border-focused`, `--ds-easing-enter`, `--ds-easing-exit`, `--ds-radius-control-medium`, `--ds-radius-medium`, `--ds-shadow-overlay`, `--ds-space-025`, `--ds-space-075`, `--ds-space-100`, `--ds-space-200`, `--ds-space-300`, `--ds-surface-overlay`, `--ds-surface-raised`, `--ds-text`, `--ds-text-subtle`

## Related

- [sidebar](https://design.senvenli.com/docs/components/sidebar.md)
