Tilt card
A card that leans towards the pointer.
Demo
Usage
import { TiltCard } from '@tuttn/slant'
<TiltCard>
<TiltCard.Layer depth={32}><Logo /></TiltCard.Layer>
<TiltCard.Layer depth={12}><h3>Slant 0.5</h3></TiltCard.Layer>
<p>Content without a layer stays on the surface.</p>
</TiltCard>
// A smaller lean, no growth, no light.
<TiltCard max={4} scale={1} glare={false}>…</TiltCard>
// Flat and still, for a card in a dense grid or while it is being edited.
<TiltCard disabled>…</TiltCard>Props
| Prop | Type | Default | Description |
|---|---|---|---|
max | number | 8 | How far the card leans at its edges, in degrees, on each axis. |
glare | boolean | true | A soft light on the surface that follows the pointer. |
scale | number | 1.02 | How much the card grows while it leans; 1 keeps its size. |
disabled | boolean | false | Keeps the card flat and still, whatever the pointer does. |
TiltCard.Layer depth | number | 20 | How far the layer lifts off the card while it leans, in pixels. About 6 to 36 reads well; text is best at 12 or under. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on each part's own div: the card, and each TiltCard.Layer. |
The card is a raised surface (the theme's surface, a hairline border, the large radius and the medium shadow) with your content inside. While a mouse or trackpad pointer is over it, the side under the pointer lifts toward the viewer, up to max degrees at the edges, the card grows by scale, a soft light follows the pointer, and each TiltCard.Layer rises depth pixels off the surface, shifting a little against the lean. When the pointer leaves it eases back flat; at rest everything sits flat, so text stays crisp.
The pointer sets four custom properties on the card, once a frame at most: --sl-tilt-x and --sl-tilt-y (the turn about each axis, in degrees) and --sl-glare-x and --sl-glare-y (the light's position, in percent), and the card carries data-active while it leans. Your own styles can read them too. Put layers inside the card directly or inside other layers: an element between them without transform-style: preserve-3d flattens the depth (the shift against the lean still shows). The card doesn't clip its content, because clipping would flatten it too, so leave layers room to move.
Accessibility and motion
The effect is decoration, so nothing depends on it. On a touch screen or a pen without hover ((hover: hover) and (pointer: fine) false), under prefers-reduced-motion, and with disabled, the pointer does nothing: no properties, no data-active, a flat card. Keyboard focus inside never moves it, and the card adds no role or tab stop of its own; the content keeps its own semantics. It runs only while hovered, so there is nothing to pause off screen. For a still surface, see Card.