{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"headline-switch-hero","type":"registry:component","title":"Headline Switch Hero","description":"The second line of the headline is the navigation: KITCHENS / BATHROOMS / LOFTS at headline size, and choosing one swaps the treated photo plate, the copy and the price.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/headline-switch-hero.tsx","content":"\"use client\";\n\nimport { useId, useRef, useState } from \"react\";\n\n/**\n * HeadlineSwitchHero — the trade you do is the headline, and the headline is\n * the navigation.\n *\n * \"Fitted for\" and then, at the same size, KITCHENS / BATHROOMS / LOFTS. The\n * chosen word is solid ink; the others are hollow outlines. Choosing one\n * swaps the photo plate on the right, the line of copy and the \"from\" price in\n * the plate's corner. Nothing changes on a timer: the visitor decides what\n * they are looking at, and the page answers.\n *\n * IT IS A TABLIST. The words are real tabs (role=\"tab\", aria-selected, roving\n * tabindex) inside a tablist, and arrow keys move between them, so a keyboard\n * user gets exactly what a mouse user gets. The plate is a tabpanel.\n *\n * ONE SATURATED FIELD. The plate colour is the only large area of colour on\n * the page. Photographs are greyscaled and multiplied into it, so any three\n * photos from the van read as one series. The button is ink, not accent — one\n * field per screen.\n *\n * Needs Tailwind v4 (container queries are built in).\n */\n\nexport type Trade = {\n  /** The headline word. Keep it to one word; it is set very large. */\n  word: string;\n  /** One line under the headline. */\n  copy: string;\n  /** Photo for the plate. http(s), root-relative or data:image only. */\n  image?: string;\n  /** Mono microcopy in the plate's corner: \"from £8,500 · 3–5 weeks\". */\n  meta?: string;\n  /** Where the button goes for this trade. Falls back to ctaHref. */\n  href?: string;\n};\n\nconst EASE = \"cubic-bezier(0.22, 1, 0.36, 1)\";\nconst HEX = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;\n\nfunction hex(v: string, fallback: string) {\n  return HEX.test(v) ? v : fallback;\n}\nfunction rgba(colour: string, alpha: number) {\n  let h = colour.slice(1);\n  if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];\n  const n = parseInt(h, 16);\n  return \"rgba(\" + ((n >> 16) & 255) + \",\" + ((n >> 8) & 255) + \",\" + (n & 255) + \",\" + alpha + \")\";\n}\n\n/**\n * Relative paths, fragments, http(s), mailto and tel; anything else becomes\n * \"#\". Judged on the URL the browser will see: a URL parser deletes tabs and\n * newlines anywhere and reads \"\\\" as \"/\", so \"/\\t/evil.com\" and \"/\\evil.com\"\n * are both \"//evil.com\" — another site dressed as a local path.\n */\nfunction safeHref(href: string) {\n  const h = href.replace(/[\\t\\n\\r]/g, \"\").trim();\n  if (h.includes(\"\\\\\")) return \"#\";\n  if (/^(\\/(?!\\/)|#|\\?|\\.{1,2}\\/)/.test(h)) return h;\n  if (/^(https?:|mailto:|tel:)/i.test(h)) return h;\n  return \"#\";\n}\n\n/** Images: http(s), root-relative or data:image only — judged like safeHref. */\nfunction safeSrc(src?: string) {\n  if (!src) return \"\";\n  const s = src.replace(/[\\t\\n\\r]/g, \"\").trim();\n  if (s.includes(\"\\\\\")) return \"\";\n  return /^(https?:\\/\\/|\\/(?!\\/)|data:image\\/)/i.test(s) ? s : \"\";\n}\n\n// Static text only; no prop ever reaches this style element.\nconst CSS =\n  \"@keyframes hsh-in{from{opacity:0}to{opacity:1}}\" +\n  \"@media (prefers-reduced-motion:reduce){[data-hsh-motion]{animation:none!important;transition:none!important}}\";\n\nexport function HeadlineSwitchHero({\n  trades,\n  lead = \"Fitted for\",\n  field = \"#c8452c\",\n  paper = \"#f4f2ee\",\n  ink = \"#111111\",\n  ctaLabel = \"Get a quote\",\n  ctaHref = \"#quote\",\n  phone,\n  tag,\n  defaultIndex = 0,\n  onChange,\n  className = \"\",\n}: {\n  trades: Trade[];\n  /** The first line of the headline. */\n  lead?: string;\n  /** The plate colour. Hex only. */\n  field?: string;\n  paper?: string;\n  ink?: string;\n  ctaLabel?: string;\n  ctaHref?: string;\n  /** Shown beside the button as a tel: link. */\n  phone?: string;\n  /** Mono microcopy top-left: \"Est. 2011 · Bristol\". */\n  tag?: string;\n  defaultIndex?: number;\n  onChange?: (trade: Trade, index: number) => void;\n  className?: string;\n}) {\n  const id = useId().replace(/[^a-zA-Z0-9]/g, \"\");\n  const [active, setActive] = useState(Math.min(defaultIndex, Math.max(0, trades.length - 1)));\n  const tabs = useRef<(HTMLButtonElement | null)[]>([]);\n\n  // Plate layers: each choice adds a layer that fades in over the last one.\n  // Only the last two are kept. Render-time adjustment, not an effect.\n  const [layers, setLayers] = useState([{ index: active, n: 0 }]);\n  const [seen, setSeen] = useState(active);\n  if (active !== seen) {\n    setSeen(active);\n    setLayers((l) => [...l.slice(-1), { index: active, n: (l[l.length - 1]?.n ?? 0) + 1 }]);\n  }\n\n  const plate = hex(field, \"#c8452c\");\n  const pa = hex(paper, \"#f4f2ee\");\n  const ik = hex(ink, \"#111111\");\n  const trade = trades[active];\n\n  function choose(i: number) {\n    if (i === active || !trades[i]) return;\n    setActive(i);\n    onChange?.(trades[i], i);\n  }\n\n  function onKeyDown(e: React.KeyboardEvent) {\n    const n = trades.length;\n    let j = active;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowDown\") j = (active + 1) % n;\n    else if (e.key === \"ArrowLeft\" || e.key === \"ArrowUp\") j = (active - 1 + n) % n;\n    else if (e.key === \"Home\") j = 0;\n    else if (e.key === \"End\") j = n - 1;\n    else return;\n    e.preventDefault();\n    choose(j);\n    tabs.current[j]?.focus();\n  }\n\n  return (\n    <section\n      className={\"@container relative h-full overflow-hidden font-sans \" + className}\n      style={{ background: pa, color: ik }}\n    >\n      <style>{CSS}</style>\n      {/* A container query answers to an ANCESTOR container, so the layout that\n          switches at @3xl lives on this inner box, never on the section itself. */}\n      <div className=\"flex h-full flex-col @3xl:flex-row\">\n\n      {/* The plate. On a phone it is a band across the top; wide, it is the right third. */}\n      <div\n        id={id + \"-panel\"}\n        role=\"tabpanel\"\n        aria-labelledby={id + \"-tab-\" + active}\n        className=\"relative order-first h-[38%] shrink-0 @3xl:order-last @3xl:h-auto @3xl:w-[42%]\"\n        style={{ background: plate }}\n      >\n        {layers.map((layer) => {\n          const t = trades[layer.index];\n          const src = safeSrc(t?.image);\n          return (\n            <div key={layer.n} data-hsh-motion=\"\" className=\"absolute inset-0\" style={{ animation: \"hsh-in 700ms \" + EASE }}>\n              {src ? (\n                // eslint-disable-next-line @next/next/no-img-element\n                <img\n                  src={src}\n                  alt=\"\"\n                  loading=\"lazy\"\n                  decoding=\"async\"\n                  className=\"h-full w-full object-cover\"\n                  style={{ filter: \"grayscale(1) contrast(1.25) brightness(1.05)\", mixBlendMode: \"multiply\", opacity: 0.92 }}\n                />\n              ) : (\n                <div className=\"h-full w-full\" style={{ background: rgba(ik, 0.18) }} />\n              )}\n            </div>\n          );\n        })}\n        {/* Plate corners: index top-right, the price and lead time bottom-left. */}\n        <span\n          aria-hidden=\"true\"\n          className=\"absolute top-5 right-5 font-mono text-[11px] tracking-[0.16em] tabular-nums uppercase\"\n          style={{ color: pa }}\n        >\n          {String(active + 1).padStart(2, \"0\")} / {String(trades.length).padStart(2, \"0\")}\n        </span>\n        {trade?.meta && (\n          <span\n            key={active}\n            data-hsh-motion=\"\"\n            className=\"absolute bottom-5 left-5 font-mono text-[11px] tracking-[0.16em] uppercase\"\n            style={{ color: pa, animation: \"hsh-in 700ms \" + EASE }}\n          >\n            {trade.meta}\n          </span>\n        )}\n      </div>\n\n      {/* The copy. */}\n      <div className=\"relative flex min-h-0 flex-1 flex-col justify-between px-6 pt-6 pb-6 @3xl:px-14 @3xl:pt-10 @3xl:pb-12\">\n        <p className=\"font-mono text-[11px] tracking-[0.18em] uppercase\" style={{ color: rgba(ik, 0.6) }}>\n          {tag}\n        </p>\n\n        <div className=\"my-4 min-w-0 @3xl:my-0\">\n          <h1 className=\"font-black tracking-[-0.05em] uppercase\">\n            <span className=\"block text-[44px] leading-[0.86] @3xl:text-[112px]\">{lead}</span>\n            {/* The second line is the navigation. */}\n            <span\n              role=\"tablist\"\n              aria-label=\"What we do\"\n              onKeyDown={onKeyDown}\n              className=\"mt-1 flex flex-wrap gap-x-[0.28em] text-[44px] leading-[0.86] @3xl:mt-2 @3xl:text-[112px]\"\n            >\n              {trades.map((t, i) => {\n                const on = i === active;\n                return (\n                  <button\n                    key={t.word + i}\n                    ref={(el) => {\n                      tabs.current[i] = el;\n                    }}\n                    id={id + \"-tab-\" + i}\n                    type=\"button\"\n                    role=\"tab\"\n                    aria-selected={on}\n                    aria-controls={id + \"-panel\"}\n                    tabIndex={on ? 0 : -1}\n                    onClick={() => choose(i)}\n                    data-hsh-motion=\"\"\n                    className=\"cursor-pointer rounded-[2px] font-black uppercase outline-none focus-visible:ring-2 focus-visible:ring-offset-4\"\n                    style={{\n                      // Hollow when not chosen: the same word, drawn as an outline.\n                      color: on ? ik : \"transparent\",\n                      WebkitTextStroke: on ? \"0\" : \"1.5px \" + rgba(ik, 0.7),\n                      transition: \"color 500ms \" + EASE + \", -webkit-text-stroke-color 500ms \" + EASE,\n                    }}\n                  >\n                    {t.word}\n                  </button>\n                );\n              })}\n            </span>\n          </h1>\n        </div>\n\n        <div className=\"flex flex-col gap-5 @3xl:flex-row @3xl:items-end @3xl:justify-between @3xl:gap-8\">\n          <p\n            key={active}\n            data-hsh-motion=\"\"\n            className=\"max-w-[36ch] text-[15px] leading-[1.45] @3xl:text-[18px]\"\n            style={{ animation: \"hsh-in 700ms \" + EASE, color: rgba(ik, 0.82) }}\n          >\n            {trade?.copy}\n          </p>\n          <div className=\"flex shrink-0 items-center gap-5\">\n            <a\n              href={safeHref(trade?.href ?? ctaHref)}\n              className=\"inline-flex h-11 items-center px-5 font-mono text-[12px] tracking-[0.14em] uppercase transition-transform duration-300 hover:-translate-y-px focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none\"\n              style={{ background: ik, color: pa }}\n            >\n              {ctaLabel}\n            </a>\n            {phone && (\n              <a\n                href={\"tel:\" + phone.replace(/[^+\\d]/g, \"\")}\n                className=\"font-mono text-[12px] tracking-[0.14em] uppercase underline-offset-4 hover:underline\"\n              >\n                {phone}\n              </a>\n            )}\n          </div>\n        </div>\n      </div>\n      </div>\n    </section>\n  );\n}\n","type":"registry:component","target":"components/viberdy/headline-switch-hero.tsx"}],"meta":{"tier":"free","tags":["hero","tabs","local-business","typography","keyboard"],"href":"https://viberdy.dev/library/headline-switch-hero","aiPrompt":"Create a React + Tailwind (v4) component named HeadlineSwitchHero: a hero for a trade or salon where the second line of the headline is a row of the things the business does, set at headline size, and choosing one swaps the photo plate, the line of copy and the price in the plate's corner. Nothing changes on a timer.\n\nData: `trades`, an array of { word, copy, image?, meta?, href? }. Props: trades, lead (first headline line, default \"Fitted for\"), field (plate colour, hex), paper, ink (hex; validate all three with a regex and default), ctaLabel, ctaHref, phone (tel: link), tag (mono microcopy top-left), defaultIndex, onChange(trade, index), className.\n\nLayout (the section is a `@container`; wide layout at @3xl):\n- Wide: copy on the left, the plate as the right 42% at full height. Narrow: the plate is a band across the top at 38% of the height, copy below.\n- Copy column: `tag` top-left in 11px uppercase monospace, 0.18em tracking, ink at 60%. The headline is an h1: line one is `lead` at 44px (112px wide), font-black, uppercase, tracking -0.05em, leading 0.86; line two is the tablist of trade words at the same size. The chosen word is solid ink; the others are hollow — colour transparent with -webkit-text-stroke 1.5px in ink at 70% — and the swap transitions over 500ms cubic-bezier(0.22, 1, 0.36, 1).\n- Below: the chosen trade's `copy` at 15px/18px, max 36ch, ink at 82%, fading in over 700ms with a keyframe (re-keyed on change); a button filled INK with paper text (not the accent — one saturated field per screen), 44px tall, 12px uppercase monospace; and the phone as a tel: link beside it.\n- Plate: background is `field`. Photographs are rendered with filter grayscale(1) contrast(1.25) brightness(1.05), mix-blend-mode multiply, opacity 0.92, object-fit cover, so any photo belongs to the plate colour. Each change of trade adds a new layer that fades in over 700ms on top of the previous one; keep only the last two layers (a render-time state adjustment, not an effect). Corners in paper-coloured 11px mono: \"01 / 03\" top-right, the trade's `meta` bottom-left.\n- Only accept http(s), root-relative or data:image image sources, after stripping tabs/newlines and rejecting any backslash (a URL parser reads \"\\\" as \"/\", so \"/\\evil.com/x.png\" is another origin); sanitise hrefs the same way (strip tabs/newlines, reject backslashes, allow relative paths, fragments, http(s), mailto and tel; otherwise \"#\").\n\nBehaviour and accessibility:\n- The word row is role=\"tablist\" with aria-label; each word is a button with role=\"tab\", aria-selected, aria-controls pointing at the plate (role=\"tabpanel\", aria-labelledby the active tab), and a roving tabindex. Arrow Left/Right/Up/Down move and select (wrapping), Home and End jump; focus follows the selection. Generate ids from useId, stripped to alphanumerics.\n- A visible focus ring on the tabs and the button.\n- A style element with static text only: the fade keyframe and a prefers-reduced-motion rule that removes animation and transitions from elements carrying a data attribute.\n\nOutput one self-contained \"use client\" component with no dependencies beyond React, exporting the Trade type.","license":"https://viberdy.dev/license"}}