# Sticky Header + Megamenu Fix

**Date:** 2026-03-30
**Status:** Approved

## Problem

The megamenu (overlay + backdrop) breaks when scrolling while it's active:
1. **Gap** appears between the navbar and the megamenu backdrop
2. **Left shift** — the megamenu overlay moves horizontally when compact view toggles off

Root cause: the desktop header uses `position: absolute` on `.headerbar__main`, switching to `position: fixed` on scroll. This changes the containing block for positioned descendants and triggers JS recalculations of overlay positioning during CSS transitions, causing instability.

Additionally, the backdrop position is set once on `pointerenter` and never updated during compact transitions, so it goes stale as the nav items change height.

## Solution: Approach A — Switch to `position: sticky`

Replace the `absolute → fixed` positioning dance with `position: sticky` on `.site-header`. This eliminates containing block changes, simplifies the JS, and aligns desktop behavior with mobile (which already uses sticky).

## Changes

### 1. CSS — header.css

**Base styles:**
- `.site-header`: change `position: relative` → `position: sticky; top: 0`
- Add `.site-header.is-header-compact { min-height: 64px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }`
- Add `min-height` to the `is-header-transitioning` transition rule on `.site-header`
- Add `.site-header` to the `is-header-transitioning` will-change rule

**Desktop media query (`min-width: 1025px`):**
- Remove `.site-header .headerbar__main { position: absolute; left: 0; right: 0; overflow-x: clip; z-index: 350; }`
- Remove `.headerbar__main--animate` rule
- Remove `.headerbar__main--fixed` rule
- Remove `.headerbar__main--sticky` rule

**Mobile media query (`max-width: 1024px`):**
- Remove `position: sticky; top: 0` from `.site-header` (now in base styles)
- Remove `transition: none` from `.site-header` mobile (now handled by base)

### 2. CSS — megamenu.css

- Backdrop: change `width: 100vw; left: 0` → `left: 0; right: 0` (avoids scrollbar overflow, containing block is now full-width `.site-header`)

### 3. JS — custom.js

**Remove constants:**
- `MAIN_FIXED_CLASS`
- `MAIN_STICKY_CLASS`
- `MAIN_ANIMATE_CLASS`

**Simplify `bindCompactHeader()`:**
- Remove `headerMain` variable and all `headerMain.classList` operations
- `setCompactState()` becomes: toggle `is-header-compact` + `is-header-transitioning` on header
- `clearCompactState()` becomes: remove `is-header-compact` + clear transition

### 4. JS — navigation.js

**Overlay left shift fix:**
- In `bindOverlay()`, skip `syncOverlay()` when `is-header-transitioning` class is present
- Add `transitionend` handler on the header for a final sync after transition completes

**Backdrop gap fix:**
- Track the currently-hovered nav item (`activeItem` + `activeOverlay`)
- On scroll events, if an item is active, re-run `positionBackdrop()` + `expandBackdrop()`
- Clear active tracking on `pointerleave` and when hovering non-child items

### 5. CSS Cleanup

- Merge duplicate `.site-header__branding` rules (header.css lines 31-37 and 83-85)
- Remove commented-out `border-bottom` (header.css line 6)
- Remove commented-out gradient divider block (megamenu.css lines 467-483)

## Layout Shift Note

When compact triggers (scrollY > 1), the header shrinks from 97px to 64px. Content shifts up 33px. This is absorbed by scroll momentum and transitions over 320ms. No scroll-loop risk — scrollY doesn't change when header height changes. This is the standard approach used by modern sticky headers (Apple, Stripe, etc.).

## Trade-offs

- **Lost:** The slide-in-from-top animation when the header becomes compact. Replaced by a smooth height reduction, which is visually clean.
- **Gained:** Stable megamenu positioning, simpler JS (~40 lines removed), consistent desktop/mobile architecture, elimination of the containing block change bug class.

## Files Modified

- `web/themes/custom/kaindl_theme/css/header.css`
- `web/themes/custom/kaindl_theme/css/megamenu.css`
- `web/themes/custom/kaindl_theme/js/custom.js`
- `web/themes/custom/kaindl_theme/js/navigation.js`
