Header for Astro
Astro header parts for a site logo, navigation, and actions.
Usage
import { Button } from "@/components/ui/button"
import { Header, HeaderContainer, HeaderGroup } from "@/components/ui/header"
import { Logo, LogoText } from "@/components/ui/logo"
Examples
Installation
bunx --bun shadcn@latest add @gnostic/headernpx shadcn@latest add @gnostic/headerpnpm dlx shadcn@latest add @gnostic/headerComponent 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/header/header-container.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="header-container"
class={cn("relative mx-auto flex w-full max-w-7xl gap-4 px-4", className)}
{...props}
>
<slot />
</div>
src/components/ui/header/header-group.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="header-group"
class={cn("flex min-w-0 items-center gap-2", className)}
{...props}
>
<slot />
</div>
src/components/ui/header/header.astro
---
import type { HTMLAttributes } from "astro/types"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
type Props = HTMLAttributes<"header"> & VariantProps<typeof variants>
const variants = cva("@container/header relative z-50 flex w-full items-center gap-4 py-2", {
variants: {
variant: {
default: "",
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, ...props } = Astro.props
---
<header
class={cn(variants({ variant }), className)}
data-slot="header"
data-variant={variant}
{...props}
>
<slot />
</header>
src/components/ui/header/index.ts
export { default as Header } from "./header.astro"
export { default as HeaderContainer } from "./header-container.astro"
export { default as HeaderGroup } from "./header-group.astro"
Notes
Headercontrols the layout —defaultis full-width,floatingis a constrained rounded card.HeaderGroupis an unopinionated flex row. Use utility classes likeml-autoorjustify-endon the group when you need alignment.HeaderContaineris an optional middle slot when you need a flex region between left and right groups.- For a mobile menu, use
SheetandSheetTriggerfrom@/components/ui/sheetdirectly inside aHeaderGroup.
API Reference
Read the installed source for the complete prop types.
When to use Header
Use Header as the page-level header container, then add the logo, navigation, and actions through its parts. The default and floating variants change the shell; they do not choose navigation links for you.
Keyboard and behavior
Keep a clear navigation landmark and label icon controls. A compact header still needs a working route to all primary links. Add a mobile menu composition when the full navigation does not fit.
Theme and layout
Check the logo width, long link labels, and action spacing at narrow widths. Floating surfaces need enough contrast over the page beneath them. Sticky positioning is a page layout choice.