react-next-tilt
v0.4.3
Published
A Performant Customizable Tilt Component for React
Downloads
545
Maintainers
Readme
Table of Contents
Features
- Easy to use
- Zero dependencies
- Highly customizable
- Touch and Gyroscope support
- Two customizable glare effects (spot/line)
- Parallax ready
"Scale on Hover/Touch"
support"Shadow on Hover/Touch"
support"Disable Scroll on Touch"
support"Full-Page Listening"
support"Control Element"
support- No jittery movement around the edges
- Built with performance in mind (
requestAnimationFrame()
,will-change
, and other optimizations) - Built from the ground up using React Hooks/TypeScript (is not a port of another library)
- Minimum amount of component re-renders
- Typed props with JSDoc descriptions
- Tested extensively using Cypress/Storybook
Installation
$ npm install react-next-tilt
Once the package is installed, you can import
the component:
import { Tilt } from 'react-next-tilt';
Usage
Basic Usage
Place the element/component you want the tilt effect to be applied to inside of <Tilt></Tilt>
.
<Tilt>
<img src="path/to/image.jpg" />
</Tilt>
You can place any element/component inside of
<Tilt></Tilt>
(doesn't have to be an image)
Parallax Effect
This component is "parallax ready", meaning you don't need to change any settings for it to work.
You just need to set up your parallax effect in JSX/CSS and place it inside of <Tilt></Tilt>
You can read this article to learn more about how to set up the 3D parallax effect.
⚠️ Setting
lineGlareMixBlendMode
and/orspotGlareMixBlendMode
properties to anything other than"normal"
will break the parallax effect.
Props
All props are optional.
In addition to these props, you can use any valid
HTMLDivElement
props likeclassName=''
,data-...='...'
,onMouseMove={...}
etc. they will be applied to the container element.
While you can tilt the component to a given angle by adjusting the initial angles, it will cause the component to re-render. It is advised to use the
tilt()
function exposed by the component's ref instead.
Events/Callbacks
Ref
The component's ref object contains these properties:
Ref functions don't re-render the component.
Ref Usage (ref function)
import { Tilt } from 'react-next-tilt';
const MyComponent = () => {
return (
<Tilt
ref={(ref) => {
if (ref) {
//do something with the ref
}
}}
>
...
</Tilt>
);
};
Ref Usage (useEffect)
import { useRef, useEffect } from 'react';
import { Tilt, TiltRef } from 'react-next-tilt';
const MyComponent = () => {
const ref = useRef<TiltRef>(null);
useEffect(() => {
if (ref.current) {
//do something with the ref
}
}, []);
return <Tilt ref={ref}>...</Tilt>;
};
Tilt on Mount
import { useRef, useEffect } from 'react';
import { Tilt, TiltRef } from 'react-next-tilt';
const MyComponent = () => {
const ref = useRef<TiltRef | null>(null);
useEffect(()=>{
if (ref.current) {
//do something else with the ref
}
},[]);
return (
<Tilt
ref={(r) => {
if (r) {
console.log(`angle = ${JSON.stringify(r.angle())}`);
r.tilt({ angleX: 10, angleY: 10 });
console.log(`angle = ${JSON.stringify(r.angle())}`);
ref.current = r;
}
}}
...
>
...
</Tilt>
);
};
Author
Rashid Shamloo (github.com/rashidshamloo)