Banner for Astro
Astro announcement containers with optional dismissal and stored visibility.
Usage
import { Banner, BannerContainer } from "@/components/ui/banner"
<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.
<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
Installation
bunx --bun shadcn@latest add @gnostic/bannernpx shadcn@latest add @gnostic/bannerpnpm dlx shadcn@latest add @gnostic/bannerComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
No extra packages are required.
Copy the files
src/components/ui/banner/banner-container.astro
---
import type { HTMLAttributes } from "astro/types"
import XIcon from "@lucide/astro/icons/x"
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
data-slot="banner-close"
type="button"
class="focus-visible:ring-ring/50 absolute top-1/2 right-4 z-20 inline-flex shrink-0 -translate-y-1/2 items-center justify-center rounded-sm text-current/65 transition-colors outline-none hover:text-current focus-visible:text-current focus-visible:ring-2 disabled:pointer-events-none disabled:opacity-50 in-data-[variant=floating]:right-5"
aria-label="Close"
>
<XIcon class="size-4" />
<span class="sr-only">Close</span>
</button>
)}
</div>
src/components/ui/banner/banner.astro
---
import type { HTMLAttributes } from "astro/types"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
type Props = HTMLAttributes<"aside"> &
VariantProps<typeof variants> & {
storageKey?: string
}
const variants = 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,1280px)-2rem)] overflow-hidden rounded-lg border shadow-sm",
},
},
defaultVariants: {
variant: "default",
},
})
const { class: className, variant, storageKey, ...props } = Astro.props
---
<aside
class={cn(variants({ variant }), className)}
data-slot="banner"
data-banner-storage-key={storageKey}
data-variant={variant}
{...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/index.ts
export { default as Banner } from "./banner.astro"
export { default as BannerContainer } from "./banner-container.astro"
Notes
- The default banner spans its available width.
- Set
variant="floating"for a bar with an inset surface.
API Reference
Read the installed source 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.