---
title: Banner for Astro
description: "Astro announcement containers with optional dismissal and stored visibility."
seo:
    title: Banner for Astro | Shadcn & Tailwind CSS
related:
    - /components/alert
    - /components/header
sidebar:
    label: "Banner"
---

## Usage

```ts
import { Banner, BannerContainer } from "@/components/ui/banner"
```

```astro
<Banner>
  <BannerContainer>
    <p class="text-sm font-medium">Banner Title</p>
    <p class="text-sm opacity-90">Banner Description</p>
  </BannerContainer>
</Banner>
```

`BannerContainer` renders a close button by default. Set `showClose={false}` to
hide it.

```astro
<Banner>
  <BannerContainer showClose={false}>
    <p class="text-sm font-medium">Banner Title</p>
    <p class="text-sm opacity-90">Banner Description</p>
  </BannerContainer>
</Banner>
```

## Examples

```astro
---
import { Banner, BannerContainer } from "@/components/ui/banner"
---

<Banner>
  <BannerContainer>
    <p class="text-sm font-medium">New gnostic AI field notes</p>
    <p class="text-sm opacity-90">
      Explore three examples of ticket routing, reply review, and invoice
      intake.
    </p>
  </BannerContainer>
</Banner>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/banner
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/banner/banner-container.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import XIcon from "@lucide/astro/icons/x"
import { Button } from "@/components/ui/button"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & {
  showClose?: boolean
}

const { class: className, showClose = true, ...props } = Astro.props
---

<div
  class={cn(
    "relative mx-auto flex w-full max-w-7xl gap-4 px-4",
    showClose && "pr-10",
    className,
  )}
  {...props}
>
  <slot />
  {showClose && (
    <Button
      variant="ghost"
      size="icon-xs"
      data-slot="banner-close"
      aria-label="Close"
    >
      <XIcon class="size-4" />
    </Button>
  )}
</div>
```

#### src/components/ui/banner/banner.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import { bannerVariants, type BannerProps } from "./types"

import { cn } from "cn"

type Props = HTMLAttributes<"aside"> & BannerProps

const { class: className, variant, mode, storageKey, ...props } = Astro.props
---

<aside
  class={cn(bannerVariants({ variant, mode }), className)}
  data-slot="banner"
  data-banner-storage-key={storageKey}
  data-variant={variant}
  data-mode={mode}
  {...props}
>
  <slot />
</aside>

<script>
  const getStorageToken = (key: string) => `banner:${key}`

  const syncBanners = () => {
    document.querySelectorAll("[data-slot='banner']").forEach((banner) => {
      if (!(banner instanceof HTMLElement)) return
      const storageKey = banner.dataset.bannerStorageKey
      if (
        storageKey &&
        sessionStorage.getItem(getStorageToken(storageKey)) === "hidden"
      ) {
        banner.hidden = true
      }
    })
  }

  syncBanners()
  document.addEventListener("astro:page-load", syncBanners)

  document.addEventListener("click", (event) => {
    if (!(event.target instanceof Element)) return
    const closeButton = event.target.closest("[data-slot='banner-close']")
    const banner = closeButton?.closest("[data-slot='banner']")
    if (!(banner instanceof HTMLElement)) return
    banner.hidden = true
    const storageKey = banner.dataset.bannerStorageKey
    if (storageKey)
      sessionStorage.setItem(getStorageToken(storageKey), "hidden")
  })
</script>
```

#### src/components/ui/banner/types.ts

```ts
import { cva, type VariantProps } from 'class-variance-authority'

export const bannerVariants = cva("@container relative flex items-center py-2", {
    variants: {
        variant: {
            default: "bg-foreground text-background w-full",
            floating: "bg-background mx-auto my-2 w-[calc(100%-2rem)] max-w-[calc(var(--container,var(--container-7xl))-2rem)] overflow-hidden rounded-lg border shadow-sm",
        },
        mode: {
            inverted: "inverted bg-background text-foreground",
            default: '',
        },
    },
    defaultVariants: {
        variant: 'default',
        mode: 'default',
    },
})

export type BannerProps = VariantProps<typeof bannerVariants> & {
    /**
     * The key used to store whether the banner has been dismissed.
     *
     * @default `banner-${variant}`
     */
    storageKey?: string
    class?: string

    showClose?: boolean
}
```

#### src/components/ui/banner/index.ts

```ts
export { default as Banner } from './banner.astro'
export { default as BannerContainer } from './banner-container.astro'
export type { BannerProps } from './types'
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


```astro
---
import { Banner, BannerContainer } from "@/components/ui/banner"
---

<Banner variant="floating" storageKey="docs-banner-invoice-review">
  <BannerContainer>
    <p class="text-sm font-medium">gnostic AI pilot update</p>
    <p class="text-sm opacity-90">A sample invoice is ready for review.</p>
  </BannerContainer>
</Banner>
```

Set `mode="inverted"` to swap the banner colors. Controls inside the banner
inherit those colors.

```astro
---
import { Banner, BannerContainer } from "@/components/ui/banner"
---

<Banner mode="inverted" storageKey="docs-banner-invoice-review">
  <BannerContainer>
    <p class="text-sm font-medium">gnostic AI pilot update</p>
    <p class="text-sm opacity-90">A sample invoice is ready for review.</p>
  </BannerContainer>
</Banner>
```

## Notes

- The default banner spans its available width.
- Set `variant="floating"` for a bar with an inset surface.

## API Reference

Read the [installed source](/ui/r/banner.json) for the complete prop types.

## When to use Banner

Place a Banner near the top of the page for a service notice or current announcement. Use the default or floating layout to suit the page shell, then add message and action parts through slots.

## Keyboard and behavior

A dismissal control needs a clear accessible name. If you use `storageKey`, the client behavior can retain the dismissed state; change the key when the notice itself changes. Important information must also remain available elsewhere after dismissal.

## Theme and layout

Check long text beside the action at a narrow width. Shared background and border tokens keep the banner consistent with the page, but a persistent banner must not cover a focused control or heading.
