Card flip
A tall two-sided event pass card that flips in 3D via an explicit button to reveal a check-in code and venue access notes.
pnpm dlx shadcn@latest add @haze/card-flip---
import { RotateCcw, RotateCw } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
type BadgeTone = "warning" | "brand" | "success" | "neutral";
interface MetaField {
label: string;
value: string;
span?: 1 | 2;
}
interface AccessNote {
label: string;
value: string;
}
interface Props {
/** Unique identifier used for face landmarks and aria-controls links. */
id: string;
eyebrow?: string;
badge?: string;
badgeTone?: BadgeTone;
edition?: string;
title?: string;
metadata?: MetaField[];
holder?: string;
serial?: string;
backEyebrow?: string;
backTitle?: string;
accessNotes?: AccessNote[];
frontActionLabel?: string;
backActionLabel?: string;
defaultFlipped?: boolean;
headingLevel?: 2 | 3 | 4;
class?: string;
}
const {
id,
eyebrow = "Admit one",
badge = "VIP",
badgeTone = "neutral",
edition = "Vol. 04",
title = "Fieldwork Sessions",
metadata = [
{ label: "Date", value: "Fri, Oct 24" },
{ label: "Doors", value: "19:30" },
{ label: "Venue", value: "Studio 09, North Pier", span: 2 },
],
holder = "Jordan Vale",
serial = "No. 004182",
backEyebrow = "Check-in pass",
backTitle = "Entry & access",
accessNotes = [
{ label: "Doors", value: "West Gate scan opens at 19:30 sharp." },
{ label: "Transit", value: "Harbor Line to North Pier, Exit B." },
{ label: "Seating", value: "Unreserved floor & balcony access." },
],
frontActionLabel = "Flip for check-in",
backActionLabel = "Show front",
defaultFlipped = false,
headingLevel = 3,
class: className,
} = Astro.props;
const Heading = ({ 2: "h2", 3: "h3", 4: "h4" } as const)[headingLevel];
const hasFrontSlot = Astro.slots.has("default");
const hasBackSlot = Astro.slots.has("back");
const hasBadgeSlot = Astro.slots.has("badge");
const badgeStyles: Record<BadgeTone, string> = {
warning: "bg-warning text-warning-foreground",
brand: "bg-brand text-brand-foreground",
success: "bg-success text-success-foreground",
neutral: "bg-primary text-primary-foreground",
};
const editionStyles: Record<BadgeTone, string> = {
warning: "text-warning",
brand: "text-brand",
success: "text-success",
neutral: "text-muted-foreground",
};
const barcodeBars = [
[0, 3], [5, 1.5], [8, 4], [14, 1.5], [17, 2.5], [22, 1.5], [25, 3.5], [31, 2],
[35, 1.5], [38, 4], [44, 2], [48, 1.5], [52, 3], [57, 1.5], [61, 2.5], [66, 4],
[72, 1.5], [76, 3], [81, 1.5], [85, 2], [89, 3.5], [95, 1.5], [99, 2.5], [104, 1.5],
[108, 4], [114, 2], [118, 1.5], [122, 3], [127, 2], [131, 1.5], [135, 3.5],
];
const triggerBase =
"min-h-8 w-full cursor-pointer items-center justify-between gap-2 rounded-lg border border-frame bg-control px-3 py-1.5 text-xs/4 font-medium text-muted-foreground transition-colors duration-150 ease-snappy hover:border-frame-hover hover:bg-accent hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring";
const fallbackFocus =
"[[data-haze-card-flip]:has(>[data-flip-fallback]:focus-visible)_&]:outline-2 [[data-haze-card-flip]:has(>[data-flip-fallback]:focus-visible)_&]:outline-offset-1 [[data-haze-card-flip]:has(>[data-flip-fallback]:focus-visible)_&]:outline-ring";
---
<div
id={id}
data-haze-card-flip
data-flipped={defaultFlipped ? "" : undefined}
class={cn("group/flip relative w-full max-w-[300px] min-w-0 perspective-distant", className)}
>
<input
id={`${id}-toggle`}
type="checkbox"
data-flip-fallback
checked={defaultFlipped}
aria-label={frontActionLabel}
class="sr-only [[data-enhanced]_&]:hidden"
/>
<div
data-flip-stage
class="grid min-w-0 motion-safe:transform-3d motion-safe:transition-transform motion-safe:duration-500 motion-safe:ease-smooth motion-safe:group-data-[flipped]/flip:rotate-y-180 motion-safe:[[data-haze-card-flip]:has(>[data-flip-fallback]:checked)_&]:rotate-y-180"
>
<section
id={`${id}-front`}
data-flip-front
aria-labelledby={`${id}-front-title`}
class="col-start-1 row-start-1 flex min-h-[340px] min-w-0 flex-col justify-between gap-5 rounded-2xl border border-border bg-card p-5 text-card-foreground shadow-lift inset-shadow-highlight motion-safe:backface-hidden group-data-[flipped]/flip:pointer-events-none [[data-haze-card-flip]:has(>[data-flip-fallback]:checked)_&]:pointer-events-none motion-reduce:group-data-[flipped]/flip:invisible motion-reduce:[[data-haze-card-flip]:has(>[data-flip-fallback]:checked)_&]:invisible"
>
<div class="grid min-w-0 gap-5">
<div class="flex min-w-0 items-center justify-between gap-3">
{eyebrow && (
<p class="text-[0.6875rem]/4 font-medium tracking-wider text-muted-foreground uppercase">
{eyebrow}
</p>
)}
{hasBadgeSlot ? (
<div class="shrink-0">
<slot name="badge" />
</div>
) : (
badge && (
<span
class={cn(
"inline-flex shrink-0 items-center rounded-md px-2 py-0.5 text-xs/4 font-semibold tracking-wide",
badgeStyles[badgeTone],
)}
>
{badge}
</span>
)
)}
</div>
<div class="grid min-w-0 gap-1.5">
{edition && (
<p class={cn("text-xs/4 font-medium tracking-wide", editionStyles[badgeTone])}>{edition}</p>
)}
<Heading
id={`${id}-front-title`}
class="text-2xl/7 font-semibold tracking-tight text-card-foreground wrap-anywhere"
>
{title}
</Heading>
</div>
<div class="min-w-0">
{hasFrontSlot ? (
<slot />
) : (
metadata.length > 0 && (
<dl class="grid grid-cols-2 gap-x-4 gap-y-3">
{metadata.map((item) => (
<div class={cn("grid min-w-0 gap-0.5", item.span === 2 && "col-span-2")}>
<dt class="text-[0.6875rem]/4 tracking-wider text-muted-foreground uppercase">
{item.label}
</dt>
<dd class="text-[0.8125rem]/5 font-medium text-foreground tabular-nums wrap-anywhere">
{item.value}
</dd>
</div>
))}
</dl>
)
)}
</div>
</div>
<div class="grid min-w-0 gap-3 border-t border-border pt-3.5">
<div class="flex min-w-0 items-end justify-between gap-3">
{holder && (
<div class="grid min-w-0 gap-0.5">
<span class="text-[0.6875rem]/4 tracking-wider text-muted-foreground uppercase">Holder</span>
<span class="truncate text-[0.8125rem]/5 font-medium text-foreground">{holder}</span>
</div>
)}
{serial && (
<span class="shrink-0 font-mono text-xs/5 text-muted-foreground tabular-nums">{serial}</span>
)}
</div>
<div>
<label for={`${id}-toggle`} class={cn("inline-flex [[data-enhanced]_&]:hidden", triggerBase, fallbackFocus)}>
<span class="truncate">{frontActionLabel}</span>
<RotateCw aria-hidden="true" class="size-3.5 shrink-0" />
</label>
<button
type="button"
data-flip-trigger="back"
aria-pressed={defaultFlipped ? "true" : "false"}
aria-controls={`${id}-back`}
class={cn("hidden [[data-enhanced]_&]:inline-flex", triggerBase)}
>
<span class="truncate">{frontActionLabel}</span>
<RotateCw aria-hidden="true" class="size-3.5 shrink-0" />
</button>
</div>
</div>
</section>
<section
id={`${id}-back`}
data-flip-back
aria-labelledby={`${id}-back-title`}
class="pointer-events-none col-start-1 row-start-1 flex min-h-[340px] min-w-0 flex-col justify-between gap-4 rounded-2xl border border-border bg-card p-5 text-card-foreground shadow-lift inset-shadow-highlight motion-safe:rotate-y-180 motion-safe:backface-hidden group-data-[flipped]/flip:pointer-events-auto [[data-haze-card-flip]:has(>[data-flip-fallback]:checked)_&]:pointer-events-auto motion-reduce:invisible motion-reduce:group-data-[flipped]/flip:visible motion-reduce:[[data-haze-card-flip]:has(>[data-flip-fallback]:checked)_&]:visible"
>
<div class="grid min-w-0 gap-4">
<div class="flex min-w-0 items-center justify-between gap-2">
<div class="grid min-w-0 gap-0.5">
{backEyebrow && (
<p class="text-[0.6875rem]/4 font-medium tracking-wider text-muted-foreground uppercase">
{backEyebrow}
</p>
)}
<Heading id={`${id}-back-title`} class="text-sm/5 font-semibold tracking-tight text-card-foreground">
{backTitle}
</Heading>
</div>
{serial && (
<span class="shrink-0 font-mono text-[0.6875rem]/4 text-muted-foreground tabular-nums">{serial}</span>
)}
</div>
<div class="min-w-0">
{hasBackSlot ? (
<slot name="back" />
) : (
<div class="grid min-w-0 gap-4">
<div class="flex items-center justify-between gap-3 rounded-xl border border-border bg-muted/60 px-3.5 py-3">
<svg
aria-hidden="true"
viewBox="0 0 140 42"
class="h-10 w-full max-w-[164px] text-foreground"
>
{barcodeBars.map(([x, w]) => (
<rect x={x} y="2" width={w} height="38" rx="0.5" fill="currentColor" />
))}
</svg>
<svg
aria-hidden="true"
viewBox="0 0 36 36"
class="size-10 shrink-0 text-foreground"
>
<rect x="2" y="2" width="11" height="11" rx="1.5" fill="none" stroke="currentColor" stroke-width="2" />
<rect x="5.5" y="5.5" width="4" height="4" fill="currentColor" />
<rect x="23" y="2" width="11" height="11" rx="1.5" fill="none" stroke="currentColor" stroke-width="2" />
<rect x="26.5" y="5.5" width="4" height="4" fill="currentColor" />
<rect x="2" y="23" width="11" height="11" rx="1.5" fill="none" stroke="currentColor" stroke-width="2" />
<rect x="5.5" y="26.5" width="4" height="4" fill="currentColor" />
<rect x="17" y="17" width="4" height="4" fill="currentColor" />
<rect x="23" y="17" width="4" height="4" fill="currentColor" />
<rect x="29" y="17" width="5" height="4" fill="currentColor" />
<rect x="17" y="23" width="4" height="5" fill="currentColor" />
<rect x="24" y="24" width="4" height="4" fill="currentColor" />
<rect x="30" y="24" width="4" height="10" fill="currentColor" />
<rect x="17" y="30" width="9" height="4" fill="currentColor" />
</svg>
</div>
{accessNotes.length > 0 && (
<dl class="grid gap-2.5 text-xs/4">
{accessNotes.map((note) => (
<div class="grid min-w-0 gap-0.5">
<dt class="text-[0.6875rem]/4 font-medium tracking-wider text-muted-foreground uppercase">
{note.label}
</dt>
<dd class="text-card-foreground wrap-anywhere">{note.value}</dd>
</div>
))}
</dl>
)}
</div>
)}
</div>
</div>
<div class="border-t border-border pt-3">
<label for={`${id}-toggle`} class={cn("inline-flex [[data-enhanced]_&]:hidden", triggerBase, fallbackFocus)}>
<span class="truncate">{backActionLabel}</span>
<RotateCcw aria-hidden="true" class="size-3.5 shrink-0" />
</label>
<button
type="button"
data-flip-trigger="front"
aria-pressed={defaultFlipped ? "true" : "false"}
aria-controls={`${id}-front`}
class={cn("hidden [[data-enhanced]_&]:inline-flex", triggerBase)}
>
<span class="truncate">{backActionLabel}</span>
<RotateCcw aria-hidden="true" class="size-3.5 shrink-0" />
</button>
</div>
</section>
</div>
</div>
<script>
function enhance(root: HTMLElement) {
if (root.hasAttribute("data-enhanced")) return;
const front = root.querySelector<HTMLElement>("[data-flip-front]");
const back = root.querySelector<HTMLElement>("[data-flip-back]");
const toBack = root.querySelector<HTMLButtonElement>('[data-flip-trigger="back"]');
const toFront = root.querySelector<HTMLButtonElement>('[data-flip-trigger="front"]');
if (!front || !back || !toBack || !toFront) return;
const fallback = root.querySelector<HTMLInputElement>("[data-flip-fallback]");
const initial = Boolean(fallback?.checked || root.hasAttribute("data-flipped"));
if (fallback) {
fallback.checked = false;
fallback.disabled = true;
}
const sync = (flipped: boolean, moveFocus = false) => {
root.toggleAttribute("data-flipped", flipped);
front.inert = flipped;
back.inert = !flipped;
toBack.setAttribute("aria-pressed", String(flipped));
toFront.setAttribute("aria-pressed", String(flipped));
if (moveFocus) {
(flipped ? toFront : toBack).focus({ preventScroll: true });
}
};
toBack.addEventListener("click", () => sync(true, true));
toFront.addEventListener("click", () => sync(false, true));
back.addEventListener("keydown", (event) => {
if (event.key === "Escape") {
event.preventDefault();
sync(false, true);
}
});
root.setAttribute("data-enhanced", "");
sync(initial, false);
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-card-flip]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-card-flip-listener")) {
document.documentElement.setAttribute("data-haze-card-flip-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- A compact vertical pass, credential, or summary card has a focused reverse side such as a check-in code or access details
- Users benefit from toggling between a display face and structured back metadata within the same footprint
When not to use
- Both views need to be read or compared at the same time. Use card or expandable-card.
- The back face contains long multi-section content or forms. Use expandable-card.
Installation
Add the Haze registry to components.json once, then install Card flip with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/card-flipnpx shadcn@latest add @haze/card-flipyarn dlx shadcn@latest add @haze/card-flipbunx --bun shadcn@latest add @haze/card-flipFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique identifier for face landmarks and aria-controls links. |
| eyebrow | string | "Admit one" | Micro uppercase label in the top-left corner of the front face. |
| badge | string | "VIP" | Compact pass tier badge rendered in the top-right corner. |
| badgeTone | "warning" | "brand" | "success" | "neutral" | "neutral" | Semantic token tone for the pass badge and edition label. |
| edition | string | "Vol. 04" | Accent edition or series label above the display title. |
| title | string | "Fieldwork Sessions" | Primary display heading shown on the front face. |
| metadata | { label: string; value: string; span?: 1 | 2 }[] | – | Two-column metadata fields rendered when no default slot is provided. |
| holder | string | "Jordan Vale" | Pass holder name displayed in the front footer. |
| serial | string | "No. 004182" | Monospace serial number shown on both faces. |
| backEyebrow | string | "Check-in pass" | Micro uppercase label above the back face heading. |
| backTitle | string | "Entry & access" | Heading shown on the back face. |
| accessNotes | { label: string; value: string }[] | – | Venue or access notes rendered beneath the check-in barcode when no back slot is provided. |
| frontActionLabel | string | "Flip for check-in" | Label for the button that flips to the back face. |
| backActionLabel | string | "Show front" | Label for the button that flips back to the front face. |
| defaultFlipped | boolean | false | Start with the back face visible. |
| headingLevel | 2 | 3 | 4 | 3 | Heading level for both face titles. |
| class | string | – | Additional classes on the outer perspective wrapper. |
Keyboard interactions
- Enter or Space
- Flip the card when focused on the face toggle button.
- Escape
- Return from the back face to the front face.
Accessibility
- Flipping is triggered by explicit accessible buttons with aria-pressed and aria-controls rather than hover, so touch and keyboard users can operate both faces.
- The hidden face is marked inert when enhanced so keyboard focus and screen readers never enter the unseen side, and prefers-reduced-motion swaps faces without a 3D spin.
- Decorative barcode and QR SVG graphics carry aria-hidden, and without JavaScript a visually hidden checkbox and paired labels preserve full toggle access.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Card flip" with `npx shadcn@latest add @haze/card-flip`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import CardFlip from "@/components/haze/ui/card-flip.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/card-flip.md
Customize with real content
Adapt the Haze card-flip ui for our product. Replace the demo values of these props with our real content: id, eyebrow, badge, badgeTone, edition, title, metadata, holder, serial, backEyebrow, backTitle, accessNotes, frontActionLabel, backActionLabel, defaultFlipped, headingLevel, class. Keep the layout, the heading level rules and these accessibility details: Flipping is triggered by explicit accessible buttons with aria-pressed and aria-controls rather than hover, so touch and keyboard users can operate both faces. The hidden face is marked inert when enhanced so keyboard focus and screen readers never enter the unseen side, and prefers-reduced-motion swaps faces without a 3D spin. Decorative barcode and QR SVG graphics carry aria-hidden, and without JavaScript a visually hidden checkbox and paired labels preserve full toggle access. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using card-flip, check it fits: use it when a compact vertical pass, credential, or summary card has a focused reverse side such as a check-in code or access details; users benefit from toggling between a display face and structured back metadata within the same footprint. Avoid it when both views need to be read or compared at the same time. use card or expandable-card.; the back face contains long multi-section content or forms. use expandable-card.. Alternatives: @haze/card, @haze/expandable-card, @haze/boarding-pass.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.