Floating & Fullscreen
Switch between a centered rounded header and a full-width edge-to-edge layout with a single prop.
A responsive, typed header component for Astro projects. Floating and fullscreen layouts, multi-level dropdowns, dark-mode support — all with inline SVG icons and no CDN required.
Switch between a centered rounded header and a full-width edge-to-edge layout with a single prop.
Detects the .dark class on <html> automatically, or force a theme with preset.
Supports top-level, secondary, and tertiary menu items — both on desktop hover and mobile accordion.
Arrow icons are inline SVG — no CDN script, no extra network request, no flash of unstyled icons.
All props and config objects are fully typed. Import types directly for IntelliSense and compile-time safety.
Add any content (buttons, search, badges) to the actions slot — shown in both header and mobile panel.
npm i @sofidevo/astro-dynamic-headerPass any utility class through classNames. The component's styles live in @layer components and yours in@layer utilities, so yours always wins — no!important, no twMerge(), and since v5 no inline styles either.
<Header
preset="dark"
classNames={{ header: "bg-red-500 shadow-xl" }}
/>Prefer plain CSS? Define your class inside @layer utilities — the same layer Tailwind uses. Hover states, transitions, and everything else work the same way.
<Header classNames={{ header: "custom-header-bg" }} />
<style is:inline>
@layer utilities {
.custom-header-bg {
background-color: red;
}
.custom-header-bg:hover {
background-color: darkred;
}
}
</style>A dynamic, responsive header component for Astro projects. Supports floating and fullscreen layouts, multi-level dropdown navigation, native CSS variable customization, dark mode, and TypeScript — all with zero external icon dependencies.
As of v4.0.0 the component styles are shipped inside CSS cascade layers, so any utility class you pass to the component wins over the built-in styles without !important. As of v5.0.0 theming is CSS-variables-only (--l-*, --d-*, --header-z-index): the theme prop was removed and the component renders no inline styles.
.dark on <html>, or forces a state with preset.@layer components, so your utility classes override them without !important and without twMerge().https://base-astro-psi.vercel.app/fullscreen-demo
# npm
npm i @sofidevo/astro-dynamic-header
# pnpm
pnpm add @sofidevo/astro-dynamic-header
# yarn
yarn add @sofidevo/astro-dynamic-header
astro ^7.0.0.---
import Header from '@sofidevo/astro-dynamic-header/Header';
const menuItems = [
{ link: '/about', text: 'About' },
{ link: '/contact', text: 'Contact' },
];
const navigation = { menuItems };
---
<Header navigation={navigation}>
<a slot="logo" href="/">MyLogo</a>
</Header>
[!WARNING] v5.0.0 removes the JavaScript theming API. The component now has a single theming mechanism: CSS variables.
theme prop and defaultThemes were removedColors, blur, and z-index are plain CSS variables — one mechanism instead of two, and no inline styles at all, so layered CSS and utilities can always win.
Before (v4):
---
import { defaultThemes } from '@sofidevo/astro-dynamic-header';
const theme = { light: { ...defaultThemes.light, accentColor: "#7c3aed", zIndex: 60 } };
---
<Header theme={theme} />
After (v5):
:root {
--l-accent: #7c3aed;
--header-z-index: 60;
}
The DualThemeConfig and ThemeConfig types and the ./defaults export are gone; passing theme now fails type-checking.
| v4.x | v5.0.0 | |
|---|---|---|
| Theming API | theme prop + CSS variables |
CSS variables only |
| Container z-index | inline style="z-index: 10" (no CSS could beat it) |
z-index: var(--header-z-index, 200) in @layer components (utilities win) |
defaultThemes |
exported from ./defaults |
removed |
z-index utilities passed as classNames.container (for example "z-50") now work as expected.
console.warn with the exact fix when your overrides cannot win (wrong layer order) or when an old v3.x install is detected. The check is stripped from production builds.<style is:inline> with @layer theme, base, components, utilities;, so styles injected at runtime are ordered correctly too.classNames and navigation.menu__link__class.- [ ] Update the package: npm i -U @sofidevo/astro-dynamic-header
- [ ] Replace every `theme={{ light: {...}, dark: {...} }}` with the matching CSS variables (see "CSS variable reference").
- [ ] Replace `theme.zIndex` with `--header-z-index` (or pass a z-index utility via `classNames.container`).
- [ ] Remove `defaultThemes` imports; the defaults live in the CSS variable table below.
[!WARNING] v4.0.0 changes how the component’s CSS participates in the cascade. Read this section before upgrading from
3.x.
classNames.logo and classNames.logoText were removedHeaderClassNames no longer accepts logo or logoText. Those keys never matched any rendered markup (the logo has always been a slot), but passing them now fails type-checking.
Before (v3) — never actually styled anything:
<Header classNames={{ logo: "hover:opacity-80", logoText: "font-bold" }} />
After (v4) — style the markup you put in the slot:
<Header>
<a slot="logo" href="/" class="logo-link hover:opacity-80">
<span class="font-bold">MyLogo</span>
</a>
</Header>
| v3.x | v4.0.0 | |
|---|---|---|
| Style block | <style is:inline> (global) |
<style> (scoped to the component) |
| Layer | none (unlayered) | @layer components |
| Order statement | none | @layer theme, base, components, utilities; |
| Override your header with utilities | required !important |
works out of the box |
| Your unlayered CSS vs. component | component usually won (specificity + document order) | your unlayered CSS wins |
Two practical consequences:
classNames, Tailwind classes, and any rule you put in @layer utilities beat the built-in styles even at equal or lower specificity. You can delete the !importants you may have added.header { ... }, a { ... }), and Tailwind v3 Preflight are emitted outside any layer, and unlayered rules beat every layered rule. If your reset should sit under the header, wrap it in @layer base (see the Styling Guide and the FAQ).!important. .header.header--force-light / .header.header--force-dark rely on specificity instead, so they can be overridden by your own layered CSS if you ever need to..active rule no longer uses !important, so active-link styles are overridable like everything else.MobileNav defaults to type="floating" when rendered on its own (previously it produced an undefined modifier class).rgba(255, 255, 255, 0.9) for --l-bg, see CSS variable reference).- [ ] Update the package: npm i -U @sofidevo/astro-dynamic-header
- [ ] Remove `logo` / `logoText` from `classNames` (style the `logo` slot instead).
- [ ] Remove the `!important` declarations you added to override the header (they are no longer needed).
- [ ] Move your global resets / element selectors into `@layer base`.
- [ ] Using Tailwind v3? Read "Preflight changed my nav link colors" in the FAQ.
<Header>| Prop | Type | Default | Description |
|---|---|---|---|
headerType |
"floating" | "fullscreen" |
"floating" |
Layout style |
preset |
"light" | "dark" | "auto" |
"auto" |
Theme mode. "auto" follows .dark on <html>. |
navigation |
NavConfig |
{} |
Menu items, home link, and custom CSS classes |
classNames |
HeaderClassNames |
{} |
Inject CSS classes into structural elements |
[!NOTE] There is no
themeprop — it was removed in v5. Colors, blur, and z-index are CSS variables (see CSS variable reference).
NavConfig| Property | Type | Default | Description |
|---|---|---|---|
menuItems |
MenuItem[] |
[] |
Top-level navigation items |
homeUrl |
string |
"/" |
URL for the home link |
header__menu__class |
string |
— | Extra CSS class(es) for the desktop <nav> element |
header__item__class |
string |
— | Extra CSS class(es) for each top-level <li> menu item |
menu__link__class |
string |
— | Extra CSS class(es) for each top-level <a> link |
MenuItem| Property | Type | Required | Description |
|---|---|---|---|
link |
string |
Yes | URL the item points to |
text |
string |
Yes | Display label |
submenu |
SecondaryMenuItem[] |
No | Optional nested items (2nd level) |
SecondaryMenuItem| Property | Type | Required | Description |
|---|---|---|---|
link |
string |
Yes | URL the item points to |
text |
string |
Yes | Display label |
submenu |
TertiaryMenuItem[] |
No | Optional nested items (3rd level) |
TertiaryMenuItem| Property | Type | Required | Description |
|---|---|---|---|
link |
string |
Yes | URL the item points to |
text |
string |
Yes | Display label |
| Slot name | Visible on | Description |
|---|---|---|
logo |
Header | Render your logo exactly as you need (native HTML/widgets) |
actions |
Desktop + mobile | Add buttons, links, or utility widgets |
<Header navigation={{ menuItems }}>
<a slot="logo" href="/" class="logo-link">
<img src="/logo.svg" alt="Branding" width="40" />
<span>MyBrand</span>
</a>
<div slot="actions">
<a href="/login" class="btn">Log in</a>
</div>
</Header>
Slotted markup is rendered by your page, so your page’s CSS applies to it normally. Style the logo with your own classes (this replaces the removed classNames.logo / classNames.logoText).
The classNames prop injects CSS classes into specific structural elements.
HeaderClassNames| Property | Target element | Common use cases |
|---|---|---|
container |
Outer <div> wrapping the header |
Positioning, padding, outer gutters |
header |
Inner <header> element |
Shadows, borders, transitions, radius, backdrop |
nav |
<div> wrapping the desktop nav items |
Spacing, alignment, responsive visibility |
mobileNav |
Root <nav> of the mobile panel |
Backdrop blur, slide-in overrides, panel width |
<!-- Tailwind example -->
<Header
classNames={{
header: "shadow-xl border-b border-black/5 dark:border-white/10",
container: "top-4 px-6",
mobileNav: "backdrop-blur-md",
}}
/>
Everything below assumes you want to change how the header looks or behaves. Pick the lightest tool that solves your case:
| Goal | Use |
|---|---|
| One-off tweaks (shadow, border, padding, radius) | classNames with utilities |
| A different overall design (square, full-width, compact) | Your own CSS in @layer utilities |
| Restyle nav links, dropdowns, or the mobile panel | Selectors targeting the internal class hooks |
| Brand colors, blur, background | CSS variables (--l-* / --d-*) |
| Per-instance tokens or z-index | Scoped CSS variables (--header-z-index, --l-* on a wrapper) |
| Logo / buttons markup | Slots |
The component ships this statement ahead of its own rules:
@layer theme, base, components, utilities;
Priority, lowest to highest:
base < components (this header) < utilities (your classNames/overrides) < unlayered CSS
Rules of thumb:
@layer base.@layer utilities (or pass them as classNames).@layer theme, base, components, utilities;).Square, full-width, no glass effect — the classic “make it look like a different component” case:
<Header
classNames={{
container: "header-wide",
header: "header-square",
}}
/>
@layer utilities {
.header-wide {
padding: 0;
}
.header-square {
max-width: 100%;
border-radius: 0;
box-shadow: none;
backdrop-filter: none;
background-color: #ffffff;
border-bottom: 1px solid #e5e7eb;
padding: 0.75rem 2rem;
}
}
No !important, and the same classes work whether you are on Tailwind or not.
The container is position: fixed by default. To make the header participate in normal flow (or stick to the top while scrolling):
<Header classNames={{ container: "nav-sticky", header: "nav-compact" }} />
@layer utilities {
.nav-sticky {
position: sticky;
top: 0;
}
.nav-compact {
padding-top: 0.25rem;
padding-bottom: 0.25rem;
border-radius: 0;
}
}
The header renders regular DOM (no shadow root), so any selector reaches the internals. Wrap the header in a class to scope your rules to one page:
<div class="docs-header">
<Header navigation={{ menuItems }} />
</div>
@layer utilities {
/* Typography of the desktop links */
.docs-header .menu__link {
text-transform: uppercase;
letter-spacing: 0.02em;
font-size: 0.875rem;
}
/* Dropdown surfaces */
.docs-header .submenu,
.docs-header .subsubmenu {
border-radius: 12px;
padding: 0.75rem;
box-shadow: 0 12px 32px rgb(0 0 0 / 0.18);
}
/* Hover underline color */
.docs-header .menu__link::after {
background: #7c3aed;
height: 3px;
top: 30px;
}
/* Active link in the mobile panel */
.docs-header .mobile-menu__link.active {
color: #7c3aed;
}
}
Because these live in utilities, they beat the component’s components layer regardless of specificity.
The desktop nav hides below 768px. To keep it visible down to 640px:
@layer utilities {
@media (width >= 640px) and (width < 768px) {
.docs-header .header__menu {
display: flex;
}
.docs-header .hamburger {
display: none;
}
}
}
Media queries live inside the rule, so you re-declare the property at the breakpoints you care about; the layer decides which declaration wins.
@layer utilities {
:root.dark .docs-header .header {
--d-bg: rgb(17 24 39 / 0.85);
box-shadow: 0 8px 30px rgb(0 0 0 / 0.35);
}
}
See Customization & Theme Config for the full variable reference. Global (whole site):
:root {
--l-accent: #7c3aed;
--l-bg: rgb(255 255 255 / 0.85);
--d-accent: #a78bfa;
--d-bg: rgb(10 10 10 / 0.85);
}
Scoped to one section (marketing page gets a purple tint, the docs stay neutral):
.marketing-hero {
--l-bg: rgb(124 58 237 / 0.14);
--l-blur: blur(28px);
}
<section class="marketing-hero">
<Header navigation={{ menuItems }} />
</section>
Variables are inherited, so defining them on any ancestor of the header works.
The container resolves its stacking level as z-index: var(--header-z-index, 200) inside @layer components, and there is no inline style — so z-index utilities passed as classNames.container win:
<Header classNames={{ container: "z-50" }} />
Or set it once, globally or on a wrapper (variables are inherited):
:root {
--header-z-index: 60;
}
Per-instance tokens work the same way:
<section class="marketing-hero">
<Header navigation={{ menuItems }} />
</section>
.marketing-hero {
--l-bg: rgb(124 58 237 / 0.14);
--header-z-index: 60;
}
<Header classNames={{ mobileNav: "mobile-wide" }} />
@layer utilities {
/* Full-bleed panel instead of the default over-wide slide-in */
.mobile-wide {
width: 100vw;
}
.mobile-wide.is-active {
transform: translateX(0);
}
/* Softer surface + accent links */
.mobile-wide .mobile-menu li a {
border-color: rgb(124 58 237 / 0.35);
font-size: 1.1rem;
}
}
If you prefer the fullscreen slide behavior everywhere, render with headerType="fullscreen" — the panel then uses translate(0) when open.
The hamburger lines follow the header text color (--l-text / --d-text), so recoloring the text recolors the lines:
:root {
--l-text: #111827;
--d-text: #f9fafb;
}
Shape and size of the button itself:
<Header classNames={{ header: "header-round-btn" }} />
@layer utilities {
#hamburger-btn {
border-radius: 999px;
padding: 0.6rem 0.9rem;
}
}
The sun and chevron icons use currentColor, so they follow the surrounding text color automatically. The moon glyph uses var(--svg-color--fff, #fff) instead, so it stays white unless you override that variable.
| Element | Classes / id |
|---|---|
| Outer wrapper | .header__container, .header__container--floating, .header__container--fullscreen |
| Header bar | .header, .header--floating, .header--fullscreen, .header--force-light, .header--force-dark |
| Desktop nav wrapper | .nav-menu-wrapper |
| Desktop nav | #header-menu, .header__menu, .menu, .menu__item, .header__item, .menu__link |
| Dropdowns | .submenu, .subsubmenu, .submenu__item--secondary, .submenu__item--tertiary |
| Hamburger | #hamburger-btn, .hamburger, .hamburger-box, .hamburger-inner |
| Mobile panel | #mobile-header-menu, .mobile-header__menu, .mobile-header__menu--floating, .mobile-header__menu--fullscreen |
| Mobile list | .mobile-menu, .mobile-menu__link, .mobile-details, .menu__summary, .mobile-submenu, .mobile-subsubmenu |
| Slot wrappers | .actions-desktop, .actions-mobile |
@layer utilities, put @layer theme, base, components, utilities; at the very top of your global stylesheet — the dev console warning points it out.var(--header-z-index, 200) in @layer components, so utilities beat it.!important in a layer still works (important declarations reverse layer order), but you should not need it.@layer base.data-astro-cid-* attributes. Your selectors do not need them; plain class selectors work.You can fully customize the color scheme using CSS Custom Properties — the only theming mechanism since v5.
Input variables (set them wherever the header lives — :root, a wrapper, or a per-instance scope):
| Variable | Used for | Light default | Dark default |
|---|---|---|---|
--l-bg / --d-bg |
Header background (translucent) | rgba(255, 255, 255, 0.9) |
#0d0d0dcc |
--l-bg-opaque / --d-bg-opaque |
Solid background of dropdowns and the mobile panel | rgb(255, 255, 255) |
#0d0d0d |
--l-text / --d-text |
Text, hamburger lines, icons | #1a1a1a |
#ffffff |
--l-accent / --d-accent |
Hover underline, active links, dashed borders | #3e1c71 |
#00ffff |
--l-blur / --d-blur |
backdrop-filter value |
blur(20px) |
blur(20px) |
--header-z-index |
Stacking level of the fixed container | 200 |
200 |
Derived variables (resolved by the component per theme state; override them only if you need to target internals directly):
| Variable | Resolves to |
|---|---|
--bg-color |
--l-bg or --d-bg |
--bg-color-opaque |
--l-bg-opaque or --d-bg-opaque |
--text-color |
--l-text or --d-text |
--accent-color |
--l-accent or --d-accent |
--backdrop-blur |
--l-blur or --d-blur |
:root {
/* Light theme overrides */
--l-accent: #7c3aed;
--l-bg: rgba(255, 255, 255, 0.85);
--l-bg-opaque: #ffffff;
--l-text: #1a1a1a;
--l-blur: blur(20px);
/* Dark theme overrides */
--d-accent: #a78bfa;
--d-bg: rgba(10, 10, 10, 0.85);
--d-bg-opaque: #0a0a0a;
--d-text: #f5f5f5;
--d-blur: blur(20px);
/* Stacking */
--header-z-index: 60;
}
The hamburger lines and the sun/chevron icons follow --l-text / --d-text, so text color drives them too. The moon glyph keeps its own white fill (--svg-color--fff, default #fff).
Because there are no inline styles, your own layered CSS always beats these defaults — override any variable on a wrapper to scope it to one instance (see z-index and per-instance tokens).
[!IMPORTANT] When using transparent backgrounds, always supply a solid fallback in
--l-bg-opaque/--d-bg-opaque. Submenus and mobile panels utilize this solid color to prevent visual glitches with nested blur effects.
---
import Header from '@sofidevo/astro-dynamic-header/Header';
import type {
MenuItem,
NavConfig,
HeaderClassNames,
HeaderProps,
} from '@sofidevo/astro-dynamic-header';
const menuItems: MenuItem[] = [
{ link: '/about', text: 'About' }
];
const navigation: NavConfig = {
menuItems,
homeUrl: "/",
header__menu__class: "flex gap-4"
};
const classNames: HeaderClassNames = {
header: "shadow-lg"
};
---
<Header
navigation={navigation}
classNames={classNames}
/>
pnpm install
pnpm dev # demo at localhost:4321
pnpm check # astro check (0 errors expected)
pnpm test # vitest: unit + rendering + built-CSS tests
pnpm build # builds the demo site
The test suite runs in two Vitest projects: dom (jsdom, exercises the extracted behaviour scripts) and astro (Node, renders Header.astro through Astro’s Container API and asserts the built CSS contract — layer order, rules inside @layer components, no !important). See tests/README.md.
Import using the direct subpath:
import Header from '@sofidevo/astro-dynamic-header/Header';
Other components are exported similarly:
import NavMenu from '@sofidevo/astro-dynamic-header/NavMenu';
import MobileNav from '@sofidevo/astro-dynamic-header/MobileNav';
import ChevronIcon from '@sofidevo/astro-dynamic-header/ChevronIcon';
Check these in order:
npm ls @sofidevo/astro-dynamic-header (or the pnpm/yarn equivalent). v3.x ships unlayered styles that beat every layered override, so no amount of @layer utilities or Tailwind classes can win. Update to the latest version — in dev, the component logs a console warning when it detects this.@layer utilities, or in a class passed through classNames. Rules in @layer base (and your resets) lose to the component by design.utilities before the layer order statement ran? Layers are ordered by their first appearance in the document; a later statement cannot reorder them. If your global CSS uses @layer but no statement comes first, add @layer theme, base, components, utilities; as its very first line (Tailwind v4 already emits this). In dev, the component logs a console warning with the exact fix when it detects this ordering.data-astro-cid-*).@import "tailwindcss" comes first in your entry CSS.Since v5 the component renders no inline styles at all — CSS variables (--l-*, --d-*, --header-z-index) and @layer utilities always win.
Tailwind v3 emits Preflight and utilities outside native cascade layers (v3 hijacks @layer as its own directive), and CSS outside a layer beats CSS inside a layer. Preflight’s a { color: inherit; ... } therefore overrides the component’s layered link colors.
Options:
theme, base, components, utilities, so everything lines up.module.exports = { corePlugins: { preflight: false } }..header__menu a,
.mobile-menu a {
color: var(--accent-color, #3e1c71);
}
Note that v3 utilities still work as expected, because unlayered utilities also beat the component.
Icons are rendered as inline SVG components. If you are upgrading from v1.x or v2.0 and have the old ChevronIcon CDN <script> tag in your layout <head>, you can safely remove it.
Classic View Transitions symptom. With <ClientRouter /> Astro swaps the whole <body> on every in-site navigation without re-executing the hoisted module scripts, so the listeners bound to #hamburger-btn on the first load end up attached to nodes that are no longer in the document, and the freshly swapped button stays inert until a hard reload.
Since 5.0.10 the component does not depend on scripts re-running:
document, the one node no router replaces, so the hamburger works no matter what swapped the markup — transition:persist, Swup, Turbo, or a plain reload.astro:page-load, the lifecycle event Astro fires on the initial load and after every client-side navigation. Every init is idempotent, so nothing gets wired twice.No data-astro-rerun, no manual init call, no prop: update the package and it works.
Adjust it with --header-z-index (globally, on a wrapper, or via classNames.container="z-50" — utilities win since there is no inline z-index):
:root {
/* default is 200; raise it further if your content stacks higher */
--header-z-index: 300;
}
document and re-bound on Astro’s astro:page-load, so the hamburger and the submenus keep working with plain MPA reloads, <ClientRouter /> view transitions, transition:persist, SSR/SSG/hybrid output, and DOM-swapping tools such as Swup or Turbo — with nothing to configure.<script> modules, so Astro emits them as deduplicated type="module" assets that a script-src 'self' policy allows. <script is:inline> was deliberately avoided: it is always inline, would need 'unsafe-inline' or a hash, and cannot be TypeScript.MIT License.