# Banner

A slim bar across the top of a page for announcements and site-wide notices.

Source: [banner.tsx](https://github.com/jakejarvis/jarvis-ui/blob/main/src/components/ui/banner.tsx)

```tsx
import {
  CircleCheckIcon,
  InfoIcon,
  MegaphoneIcon,
  OctagonXIcon,
  TriangleAlertIcon,
} from "lucide-react";

import { Banner, BannerContent } from "@/components/ui/banner";

export default function BannerVariants() {
  return (
    <div className="w-full overflow-hidden rounded-lg border-[0.5px] border-border">
      <Banner variant="primary">
        <MegaphoneIcon />
        <BannerContent>
          Shared views are here. <a href="#shared-views">Read the announcement</a>
        </BannerContent>
      </Banner>
      <Banner>
        <BannerContent>You’re viewing the docs for version 1.</BannerContent>
      </Banner>
      <Banner variant="info">
        <InfoIcon />
        <BannerContent>
          Projects will be read-only on Saturday from 02:00 to 03:00 UTC.
        </BannerContent>
      </Banner>
      <Banner variant="success">
        <CircleCheckIcon />
        <BannerContent>Your account is verified.</BannerContent>
      </Banner>
      <Banner variant="warning">
        <TriangleAlertIcon />
        <BannerContent>You’ve used 9.2 of 10 GB of storage.</BannerContent>
      </Banner>
      <Banner variant="destructive">
        <OctagonXIcon />
        <BannerContent>
          Deployments are failing. <a href="#status">Check the status page</a>
        </BannerContent>
      </Banner>
    </div>
  );
}
```

## Installation

Add the registry, once per project:

```bash
npx shadcn@latest registry add @jarvis=https://ui.jarv.is/r/{name}.json
```

Then add the component:

```bash
npx shadcn@latest add @jarvis/banner
```

## Usage

```tsx
import { MegaphoneIcon } from "lucide-react";

import { Banner, BannerContent } from "@/components/ui/banner";

export function LaunchBanner() {
  return (
    <Banner variant="primary">
      <MegaphoneIcon />
      <BannerContent>
        Shared views are here. <a href="/blog/shared-views">Read the announcement</a>
      </BannerContent>
    </Banner>
  );
}
```

Render the banner first in the page, above the site's header, so it spans the full width. It
scrolls away with the page, and a [Scroll Header](https://ui.jarv.is/components/scroll-header.md) after it sticks to the
top once the banner is out of view. For a message inside the page's content, use an
[Alert](https://ui.jarv.is/components/alert.md) instead.

An icon placed before `BannerContent` sits beside the text, and the text stays centered. Links in
`BannerContent` are underlined to match the banner's colors.

Banner renders a `<section>` labeled "Notice", so screen reader users can find it among the page's
landmarks. Pass an `aria-label` that describes your banner, such as "Announcement". Screen readers
don't announce the banner when it appears. For a message that responds to something someone did,
use a [Toast](https://ui.jarv.is/components/toast.md).

## Composition

- `Banner`
  - `BannerContent`
  - `BannerClose`

## Examples

### Dismissible

`BannerClose` renders an icon button labeled "Dismiss" at the banner's end. It doesn't hide the
banner by itself, so you decide what dismissing does. Pass `aria-label` to change its label.

```tsx
"use client";

import { MegaphoneIcon } from "lucide-react";
import { useState } from "react";

import { Banner, BannerClose, BannerContent } from "@/components/ui/banner";
import { Button } from "@/components/ui/button";

export default function BannerDismissible() {
  const [open, setOpen] = useState(true);

  if (!open) {
    return <Button onClick={() => setOpen(true)}>Show banner</Button>;
  }

  return (
    <Banner variant="primary">
      <MegaphoneIcon />
      <BannerContent>
        Shared views are here. <a href="#shared-views">Read the announcement</a>
      </BannerContent>
      <BannerClose onClick={() => setOpen(false)} />
    </Banner>
  );
}
```

To keep a banner hidden after someone dismisses it, save the choice in local storage or on your
server. Read the saved choice in an effect so server-rendered pages don't flash the banner.

```tsx
"use client";

import * as React from "react";

import { Banner, BannerClose, BannerContent } from "@/components/ui/banner";

export function LaunchBanner() {
  const [open, setOpen] = React.useState(false);

  React.useEffect(() => {
    setOpen(localStorage.getItem("launch-banner") === null);
  }, []);

  if (!open) return null;

  return (
    <Banner variant="primary" aria-label="Announcement">
      <BannerContent>
        Shared views are here. <a href="/blog/shared-views">Read the announcement</a>
      </BannerContent>
      <BannerClose
        onClick={() => {
          localStorage.setItem("launch-banner", "dismissed");
          setOpen(false);
        }}
      />
    </Banner>
  );
}
```

### With a button

Place a small [Button](https://ui.jarv.is/components/button.md) after `BannerContent` for the banner's main action.

```tsx
import { TriangleAlertIcon } from "lucide-react";

import { Banner, BannerContent } from "@/components/ui/banner";
import { Button } from "@/components/ui/button";

export default function BannerWithButton() {
  return (
    <Banner variant="warning">
      <TriangleAlertIcon />
      <BannerContent>Your trial ends in 3 days.</BannerContent>
      <Button size="xs">Upgrade</Button>
    </Banner>
  );
}
```

## Props

| Part | Extends |
| --- | --- |
| `Banner` | `<section>` |
| `BannerContent` | `<p>` |
| `BannerClose` | [`Button`](https://ui.jarv.is/components/button.md) |

Base UI's [styling](https://base-ui.com/react/handbook/styling) and [composition](https://base-ui.com/react/handbook/composition) guides say more about `className`, `style` and `render`.

### Banner

| Prop         | Type                                                                          | Default     | Description                                                                                                                                                                               |
| ------------ | ----------------------------------------------------------------------------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `variant?`   | `"default" \| "destructive" \| "info" \| "primary" \| "success" \| "warning"` | `"default"` |                                                                                                                                                                                           |
| `className?` | `string`                                                                      | —           | CSS class applied to the element.                                                                                                                                                         |
| `style?`     | `React.CSSProperties`                                                         | —           | Style applied to the element.                                                                                                                                                             |
| `render?`    | `ComponentRenderFn<HTMLProps, {}> \| React.ReactElement`                      | —           | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

### BannerContent

| Prop         | Type                                                     | Default | Description                                                                                                                                                                               |
| ------------ | -------------------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `className?` | `string`                                                 | —       | CSS class applied to the element.                                                                                                                                                         |
| `style?`     | `React.CSSProperties`                                    | —       | Style applied to the element.                                                                                                                                                             |
| `render?`    | `ComponentRenderFn<HTMLProps, {}> \| React.ReactElement` | —       | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |

### BannerClose

| Prop                     | Type                                                                                | Default   | Description                                                                                                                                                                               |
| ------------------------ | ----------------------------------------------------------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `nativeButton?`          | `boolean`                                                                           | `true`    | Whether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).         |
| `focusableWhenDisabled?` | `boolean`                                                                           | `false`   | Whether the button should be focusable when disabled.                                                                                                                                     |
| `variant?`               | `"default" \| "destructive" \| "ghost" \| "link" \| "outline" \| "primary"`         | `"ghost"` |                                                                                                                                                                                           |
| `className?`             | `string \| ((state: ButtonState) => string \| undefined)`                           | —         | CSS class applied to the element, or a function that returns a class based on the component's state.                                                                                      |
| `style?`                 | `React.CSSProperties \| ((state: ButtonState) => React.CSSProperties \| undefined)` | —         | Style applied to the element, or a function that returns a style object based on the component's state.                                                                                   |
| `render?`                | `ComponentRenderFn<HTMLProps, ButtonState> \| React.ReactElement`                   | —         | Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render. |
