rashidshamloo / react-flip-tilt

A Performant Customizable React Component Providing the Flip-Tilt Effect
MIT License
0 stars 0 forks source link

React Flip Tilt

A Performant Customizable React Component Providing the Flip-Tilt Effect

Main DemoMain Demo (Parallax)Flip Direction DemoControl Element DemoStorybook

[![install size](https://img.shields.io/badge/dynamic/json?url=https://packagephobia.com/v2/api.json?p=react-flip-tilt&query=$.install.pretty&label=install%20size&style=flat-square)](https://packagephobia.now.sh/result?p=react-flip-tilt) [![npm bundle size](https://img.shields.io/bundlephobia/minzip/react-flip-tilt?style=flat-square)](https://bundlephobia.com/package/react-flip-tilt@latest) [![npm downloads](https://img.shields.io/npm/dm/react-flip-tilt.svg?style=flat-square)](https://npm-stat.com/charts.html?package=react-flip-tilt) [![Known Vulnerabilities](https://snyk.io/test/npm/react-flip-tilt/badge.svg)](https://snyk.io/test/npm/react-flip-tilt)

Table of Contents

Features

Installation

$ npm install framer-motion react-flip-tilt

Once the package is installed, you can import the component:

import { FlipTilt } from 'react-flip-tilt';

Usage

You can set the front/back properties to either an image source as string or an element/component

Image Source

<FlipTilt front="/front.webp" back="/back.webp" />

Element/Component

<FlipTilt front={<div>...</div>} back={<MyComponent />} />

Mixture of Both

<FlipTilt front="/front.webp" back={<MyComponent />} />

Parallax Effect

Using react-next-tilt (default)

Using react-next-parallax (type='parallax')

Props

All props are optional

In addition to these props, you can use:

  • Any valid HTMLDivElement props like className='', data-...='...', onMouseMove={...} etc. they will be applied to the container element.
  • Any of the react-next-tilt props like scale, disableScrollOnTouch, controlElement, etc.
  • Any of the react-next-parallax props like animationMode, animationReverse, offsetMultiplier, etc. (when type is set to 'parallax')

While you can flip the component by changing the flipped prop, it will cause the component to re-render and interrupts the animation. It is advised to use the flip() function exposed by the component's ref instead.

NameDescriptionDefault
front
Front image/element
note: You can pass either an image source as string or an element/component
example: 'path/to/image.jpg', <div>...</div>, <Component />
ReactNode
<></>
back
Back image/element
note: You can pass either an image source as string or an element/component
example: 'path/to/image.jpg', <div>...</div>, <Component />
ReactNode
<></>
direction
Direction of the flip animation
"horizontal" | "vertical"
"horizontal"
flipReverse
Reverses the rotation of the flip animation
boolean
false
flipBackReverse
Reverses the rotation of the flip back animation
boolean
false
borderWidth
Border width applied to the back image/element
example: '4px', '1em', '2rem'
string
"2px"
borderColor
Border color applied to the back image/element
example: 'lightblue', '#445566AA', 'rgba(50,150,250,0.5)', 'hsla(100,50%,50%,0.2)'
string(Property.BorderColor)
white
borderStyle
Border style applied to the back image/element
example: 'solid', 'dashed', 'dotted'
string (Property.BorderStyle)
solid
mass
Mass of the element in framer-motion's spring animation. Higher values will result in more lethargic movement.
number
0.5
stiffness
Stiffness of the spring in framer-motion's spring animation. Higher values will create more sudden movement.
number
120
flipped

Locks the component to one side

true: back side

false: front side

boolean
-
type

Determines the type of the component

'tilt': react-next-tilt

'parallax': react-next-parallax

"tilt" | "parallax"
"tilt"

Events/Callbacks

The component has the following events/callbacks in addition to react-next-tilt events/callbacks:

NameDescriptionParameters
onFlip
Callback function that is called with the container element when the component flips and the back side is visible
(element: HTMLDivElement) => void
element: The component's container element
onFlipBack
Callback function that is called with the container element when the component flips back and the front side is visible
(element: HTMLDivElement) => void
element: The component's container element

Ref

The component's ref object contains the following properties in addition to react-next-tilt ref properties:

NameDescriptionParameters
isFlipped
Returns whether the element is flipped or not
() => boolean
-
flip
Animates/Flips the component without re-rendering it.
() => Promise<void>
-

Ref Usage (ref function)

import { FlipTilt } from 'react-flip-tilt';

const MyComponent = () => {
  return (
    <FlipTilt
      ref={(ref) => {
        if (ref) {
          //do something with the ref
        }
      }}
      ...
    />
  );
};

Ref Usage (useEffect)

import { useRef, useEffect } from 'react';
import { FlipTilt, FlipTiltRef } from 'react-flip-tilt';

const MyComponent = () => {
  const ref = useRef<FlipTiltRef>(null);

  useEffect(() => {
    if (ref.current) {
      //do something with the ref
    }
  }, []);

  return <FlipTilt ref={ref} ... />;
};

Flip on Mount

import { useRef, useEffect } from 'react';
import { FlipTilt, FlipTiltRef } from 'react-flip-tilt';

const MyComponent = () => {
  const ref = useRef<FlipTiltRef | null>(null);

  useEffect(()=>{
    if (ref.current) {
    //do something else with the ref
    }
  },[]);

  return (
    <FlipTilt
      ref={async (r) => {
        if (r) {
          console.log(`isFlipped = ${r.isFlipped()}`);
          await r.flip();
          console.log(`isFlipped = ${r.isFlipped()}`);
          ref.current = r;
        }
      }}
      ...
    />
  );
};

Credits

Animated using framer-motion

Inspired by evolany.com

Author

Rashid Shamloo (github.com/rashidshamloo)

License

MIT