Flick Card
A physically flippable card — drag to turn, flick to spin, or click either half to push it away from you. Lifts toward the viewer at 90°.
Flick Card
A physically flippable card — drag to turn, flick to spin, or click either half to push it away from you. Lifts toward the viewer at 90°.
The card responds to drag, flick, and click gestures with physics-based motion. Drag follows your finger one-to-one, flicking spins the card with momentum that lands on a face, and clicking either half pushes that side away from you. The card lifts 36px toward the viewer at 90° and casts a narrowing floor shadow as it turns edge-on. A flat band of light sweeps across whichever face is showing. Hovering pre-tilts the card toward your cursor, and the edge reveals stacked slices of thickness between the two faces.
Installation
Install the component with the Craft-UI Blocks CLI — it drops flick-card.tsx into your project and wires up its runtime dependencies:
npx mvpblocks add flick-cardpnpm dlx mvpblocks add flick-cardyarn dlx mvpblocks add flick-cardbunx mvpblocks add flick-cardFile Structure
The CLI saves the component to your project's components/ui folder:
Import the default export from @/components/ui/flick-card. The props type is exported as CardFlipProps.
Usage
CardFlip is a client component and every prop is optional — with no props it renders a demo event ticket. Drop it into any section:
"use client";
import CardFlip from "@/components/ui/flick-card";
export function TicketCard() {
return <CardFlip />;
}Pass your own faces with front and back props. Each can be any React node:
<CardFlip
front={
<div className="flex h-full flex-col items-center justify-center p-8">
<h2 className="text-2xl font-bold">Front Side</h2>
<p className="mt-2 text-center">Your content here</p>
</div>
}
back={
<div className="flex h-full flex-col items-center justify-center p-8">
<h2 className="text-2xl font-bold">Back Side</h2>
<p className="mt-2 text-center">More content</p>
</div>
}
/>Control which side shows with flipped and listen for changes with onFlippedChange:
const [isFlipped, setIsFlipped] = useState(false);
<CardFlip
front={<div>Front content</div>}
back={<div>Back content</div>}
flipped={isFlipped}
onFlippedChange={setIsFlipped}
/>Set the initial side when uncontrolled with defaultFlipped:
<CardFlip
front={<div>Front content</div>}
back={<div>Back content</div>}
defaultFlipped={true}
/>Change the flip axis to tumble top-over-bottom instead of spinning left-right:
<CardFlip
front={<div>Front content</div>}
back={<div>Back content</div>}
axis="x"
/>Provide an accessible label for the card:
<CardFlip
front={<div>Ticket front</div>}
back={<div>Ticket back</div>}
label="Event ticket for Fieldwork Sessions"
/>Customize the card and face styling:
<CardFlip
front={<div>Front</div>}
back={<div>Back</div>}
className="w-[320px]"
faceClassName="rounded-3xl bg-gradient-to-br from-purple-500 to-pink-500"
/>Theming
The card container is w-[260px] aspect-[7/10] rounded-2xl with a 1100px perspective. The faces default to rounded-2xl border-zinc-200 bg-white text-zinc-900 in light mode, inverting to dark:border-zinc-800 dark:bg-zinc-900 dark:text-zinc-50.
The edge between faces consists of 7 stacked slices in bg-zinc-300 (dark:bg-zinc-700), creating a visible thickness when the card is at an angle.
The floor shadow is bg-zinc-900/25 (dark:bg-black/70) with a blur, narrowing and fading as the card turns edge-on.
A flat band of light sweeps across the visible face, rendered as bg-white with dynamic opacity and transform. The face dims with a bg-black overlay as it turns away.
Focus shows a ring-2 ring-orange-500 ring-offset-4 ring.
Override face styling completely with faceClassName:
<CardFlip
faceClassName="rounded-3xl border-2 border-blue-500 bg-gradient-to-br from-blue-50 to-blue-100 dark:from-blue-950 dark:to-blue-900"
/>Animations
- Drag: The card follows your finger or mouse one-to-one at
0.6degrees per pixel. Movement below4pxis treated as a click instead of a drag. - Flick: Release velocity is sampled from the last
80msof drag movement. The card projects ahead0.22sand lands on the nearest face. A flick faster than450 deg/salways turns at least one full rotation, even if the projection would land on the current side. Maximum spin is capped at6half-turns per flick. - Spring: The card settles on a face with a spring (stiffness
170, damping20) that runs at up to240substeps per frame for smooth motion. When settled within0.05degrees and0.5 deg/svelocity, the angle parks exactly on0or180to avoid number growth. - Lift: As the card turns, it translates toward the viewer by
sin(angle) × 36px, reaching maximum lift at 90°. - Shadow: The floor shadow scales and fades based on how edge-on the card is. At 90°, it narrows to
16%width (or height for x-axis flips) and dims to55%opacity. - Light sweep: A flat band of light follows the card's rotation angle, translating
sin(angle) × 140%across the face with an18degrotation for y-axis or72degfor x-axis. Opacity reaches12%at 90°. - Face dimming: A black overlay on each face reaches
14%opacity at 90°, making the card appear to turn away from the light. - Hover tilt: When hovering with a mouse, the card pre-tilts up to
6degtoward the cursor based on pointer position relative to the card bounds. The tilt is a CSS transform on a wrapper, separate from the physics simulation. - Click flip: Clicking either half pushes that side away. The right or top half pushes forward (positive rotation), the left or bottom half pulls toward you (negative rotation).
- Face swap: The visible face changes at 90° (when
sin(angle)crosses zero). The invisible face is markedinertandaria-hidden, removing it from tab order and screen readers. - Reduced motion: All spring animation is disabled. The card snaps instantly to the target angle when clicked or released. Hover tilt is also disabled.
How it works
The card uses a physics simulation that runs on requestAnimationFrame when the spring has energy. The simulation tracks angle, velocity, and target, integrating a spring-damper force over small substeps for numerical stability.
Pointer events track one finger at a time. pointerdown captures the pointer, records the starting angle and position, and initializes the velocity sample buffer. pointermove checks for drag threshold (4px hypotenuse), then directly sets the angle from pointer displacement, bypassing the spring. The sample buffer keeps up to 8 timestamped angle snapshots.
On release, velocity is calculated as (last.angle - first.angle) / (last.time - first.time) over the most recent 80ms window. The target face is computed by projecting angle + velocity × 0.22s and rounding to the nearest 180° increment. If velocity exceeds the flick threshold and the projection would land on the current face, the target is pushed one full rotation in the velocity's direction. The target is clamped to ±6 half-turns from the starting face to prevent infinite spins.
The spring runs an explicit Euler integration with adaptive substeps. Each frame measures real elapsed time, caps it at 50ms to prevent spiral-of-death, and divides it into substeps of at most 1/240s. Each substep computes force from position and velocity, updates velocity, then updates angle.
The card settles when both |angle - target| < 0.05 and |velocity| < 0.5. At that point, the angle is snapped to exactly 0 or 180 (whichever the target is on) to prevent floating-point drift over many flips.
The render function updates four sets of DOM elements directly via refs: the card's 3D transform, the shadow's scale and opacity, and the light bands' transform and opacity on both faces. This bypasses React's render cycle for 60fps motion.
The visible face flips at 90° by comparing Math.round(angle / 180) % 2. When this value changes, React state updates to swap inert and aria-hidden attributes, keeping only the visible face in the accessibility tree.
For controlled mode, a useEffect watches the flipped prop and a request counter. When flipped disagrees with the spring's target, the effect calculates the nearest face on the requested side and aims the spring there. If a parent ignores onFlippedChange and keeps flipped unchanged, the card swings back to its controlled side after each attempted flip.
The hover tilt reads pointer position relative to the card's bounding rect, normalizes to [-1, 1] on each axis, and applies rotateX and rotateY transforms scaled by 6deg. This tilt is CSS-only and does not interact with the physics simulation.
The edge is rendered as 7 separate <div> elements, each translateZ shifted by a fraction of the total thickness. This creates a visible stacked effect when the card is at an angle.
The component includes demo content (an event ticket) with a procedurally generated QR-like code built from a seeded random module grid plus three finder patterns.
Props
Prop
Type
Accessibility
- The card is a focusable
role="group"witharia-roledescription="flip card"to describe its interactive nature. aria-labelprovides the card's name, defaulting to "Event ticket".- A visually hidden hint (linked via
aria-describedby) announces "Showing the front. Press Enter or Space, or drag, to turn it over" (or "back" when flipped). - Enter and Space keys flip the card when focused, providing keyboard access.
- Only the visible face is in the tab order and exposed to screen readers. The hidden face is marked
inertandaria-hidden. - Interactive elements inside faces (buttons, links, inputs) can be excluded from flip gestures by adding
data-no-flipattribute or matching theINTERACTIVEselector. - Focus ring is visible and has sufficient offset for clarity.
Registry Bundle
Install the exact registry entry shown on the right when you want the component file and its declared runtime dependencies together. Dependencies: none — the card uses CSS transforms and a vanilla JavaScript physics simulation.
Contact
If you find any bug or issue, feel free to raise an issue.
Last updated on