An open API service indexing awesome lists of open source software.

https://github.com/gfazioli/react-flip

A React component that wraps any two faces and animates a 3D rotation between them. Themeable via CSS variables, zero runtime dependencies, ~2 KB gzipped, TypeScript-first.
https://github.com/gfazioli/react-flip

3d animation card css-modules flip react react-component transition typescript

Last synced: 17 days ago
JSON representation

A React component that wraps any two faces and animates a 3D rotation between them. Themeable via CSS variables, zero runtime dependencies, ~2 KB gzipped, TypeScript-first.

Awesome Lists containing this project

README

          

# @gfazioli/react-flip

**Two faces, one component. A 3D-rotation flip for React.**

[![npm version](https://img.shields.io/npm/v/@gfazioli/react-flip?style=flat-square&color=8b5cf6)](https://www.npmjs.com/package/@gfazioli/react-flip)
[![bundle size](https://img.shields.io/bundlephobia/minzip/@gfazioli/react-flip?style=flat-square&label=gzip&color=8b5cf6)](https://bundlephobia.com/package/@gfazioli/react-flip)
[![types](https://img.shields.io/npm/types/@gfazioli/react-flip?style=flat-square&color=8b5cf6)](https://www.npmjs.com/package/@gfazioli/react-flip)
[![downloads](https://img.shields.io/npm/dm/@gfazioli/react-flip?style=flat-square&color=8b5cf6)](https://www.npmjs.com/package/@gfazioli/react-flip)
[![license](https://img.shields.io/npm/l/@gfazioli/react-flip?style=flat-square&color=8b5cf6)](./LICENSE)

[**Live playground โ†’**](https://gfazioli.github.io/react-flip/) ใƒป
[**Changelog**](./CHANGELOG.md)


> Wrap any two faces and animate a 3D rotation between them. Themeable via CSS variables, controlled or uncontrolled, zero runtime dependencies, ~2 KB gzipped.

- ๐Ÿƒ **Two faces, one prop** โ€” drop any two children inside ``. Use `` to wire any element as a click trigger.
- ๐ŸŽ›๏ธ **Tunable** โ€” `perspective`, `duration`, `easing`, `direction`, plus separate `directionFlipIn` / `directionFlipOut` for one-way coin-flip rotations.
- ๐ŸŽจ **Themeable via CSS variables** โ€” every prop is mirrored as a `--rfp-*` custom property; override on any selector.
- ๐Ÿ“ฆ **Zero runtime dependencies** โ€” only React as a peer dependency.
- ๐Ÿชถ **Tiny** โ€” ~2 KB ESM gzipped, tree-shakeable, dual ESM + CJS.
- โŒจ๏ธ **TypeScript-first** โ€” full type declarations included.
- โ™ฟ **Reduced-motion aware** โ€” respects `prefers-reduced-motion`.

## Install

```bash
npm install @gfazioli/react-flip
# or
pnpm add @gfazioli/react-flip
# or
yarn add @gfazioli/react-flip
```

Requires React 18 or newer.

## Usage

```tsx
import { Flip } from "@gfazioli/react-flip";
import "@gfazioli/react-flip/styles.css";

export function Card() {
return (




Done



);
}
```

The stylesheet must be imported once in your app (root layout, entry file, or wherever you prefer). The component expects **exactly two children** โ€” the first is the front, the second is the back.

### Controlled

```tsx
const [open, setOpen] = useState(false);



;
```

### Uncontrolled with Flip.Target

`Flip.Target` clones its single child element and wires `onClick` to toggle the flip โ€” preserving any existing `onClick` you passed.

```tsx
console.log("flipped:", next)}>

Front



Show specs



Back



Back

```

### Vertical flip

```tsx


```

### Coin flip (one-way rotation)

By default the component reverses the rotation direction when flipping back. Set `directionFlipIn` and `directionFlipOut` to the same value to keep spinning the same way:

```tsx


```

## Theming

Three properties are mirrored as CSS custom properties on the root element. Override them on any selector to theme one boundary, a section, or the whole app:

```css
.my-card {
--rfp-perspective: 1400px;
--rfp-duration: 1.2s;
--rfp-easing: cubic-bezier(0.5, -0.25, 0.5, 1.4);
}
```

| Variable | Default | Equivalent prop |
|---|---|---|
| `--rfp-perspective` | `1000px` | `perspective` |
| `--rfp-duration` | `0.8s` | `duration` (number, in seconds) |
| `--rfp-easing` | `ease-in-out` | `easing` |

`direction` and the `directionFlipIn`/`directionFlipOut` pair are **not** exposed as CSS variables โ€” they drive the rotation math directly because the back face must be pre-rotated on the inverse axis.

## Props

### ``

| Prop | Type | Default | Description |
|---|---|---|---|
| `children` | `[ReactNode, ReactNode]` | โ€” | Exactly two children. First is front, second is back. |
| `flipped` | `boolean` | โ€” | Controlled flipped state. |
| `defaultFlipped` | `boolean` | `false` | Uncontrolled initial state. |
| `onChange` | `(flipped: boolean) => void` | โ€” | Fires with the new boolean value on each flip. |
| `onBack` | `() => void` | โ€” | Fires when the back face becomes visible. |
| `onFront` | `() => void` | โ€” | Fires when the front face becomes visible. |
| `perspective` | `string` | `"1000px"` | CSS perspective for the 3D rotation. |
| `duration` | `number` | `0.8` | Animation duration, in **seconds**. |
| `easing` | `CSS transitionTimingFunction` | `"ease-in-out"` | Easing function. |
| `direction` | `"horizontal" \| "vertical"` | `"horizontal"` | Rotation axis (Y for horizontal, X for vertical). |
| `directionFlipIn` | `"positive" \| "negative"` | `"negative"` | Rotation sign when showing the back. |
| `directionFlipOut` | `"positive" \| "negative"` | `"positive"` | Rotation sign when showing the front. |
| `className`, `style` | โ€” | โ€” | Applied to the root wrapper. |

`forwardRef` returns the underlying `HTMLDivElement`.

### ``

| Prop | Type | Description |
|---|---|---|
| `children` | `ReactElement` | A single React element. Strings, numbers, fragments, and arrays are not supported. |

The clone receives a `data-flipped` attribute mirroring the current state (useful for CSS styling), and an `onClick` that toggles the flip while preserving the original handler.

## Other Undolog components

Small, accessible React components โ€” same philosophy, same toolchain, zero runtime dependencies:

- **[react-toggle-component](https://gfazioli.github.io/react-toggle/)** โ€” an accessible toggle/switch with CSS-variable theming. ([npm](https://www.npmjs.com/package/react-toggle-component) ยท [GitHub](https://github.com/gfazioli/react-toggle))
- **[react-amiga-guru-meditation](https://gfazioli.github.io/react-amiga-guru-meditation/)** โ€” an error boundary that renders the iconic Amiga Guru Meditation screen. ([npm](https://www.npmjs.com/package/react-amiga-guru-meditation) ยท [GitHub](https://github.com/gfazioli/react-amiga-guru-meditation))
- **[@gfazioli/react-tilt](https://gfazioli.github.io/react-tilt/)** โ€” interactive 3D tilt cards with parallax, glare, light, shadow, gyroscope, and spring physics. ([npm](https://www.npmjs.com/package/@gfazioli/react-tilt) ยท [GitHub](https://github.com/gfazioli/react-tilt))

## License

MIT โ€” ยฉ Giovambattista Fazioli

## Sponsor

If this project saves you time, consider [sponsoring on GitHub](https://github.com/sponsors/gfazioli) โ€” it directly supports continued maintenance and new releases.



Sponsor on GitHub