Install react-parallax-tilt via npm
mainTo use the tilt effects in your React project, install the package using npm:
npm install react-parallax-tiltrepository·main·Indexed 22 days ago
https://github.com/mkosir/react-parallax-tiltA lightweight, zero-dependency React library for applying 3D parallax tilt and glare effects to components. It supports mouse, touch, and gyroscope input, providing highly configurable props for tilt angles, perspective, scale, and glare positioning, along with event callbacks for onEnter, onMove, and onLeave.
To use the tilt effects in your React project, install the package using npm:
npm install react-parallax-tiltYou can enable device orientation detection by setting the gyroscope prop to true.
Important Considerations:
https).<iframe> elements.<Tilt gyroscope={true}>
<h1>React Parallax Tilt 👀</h1>
</Tilt>Wrap any React component with the Tilt component to apply hover-based parallax effects. The Tilt component acts as a wrapper for the content you want to tilt.
import Tilt from 'react-parallax-tilt';
const App = () => {
return (
<Tilt>
<div style={{ height: '300px', backgroundColor: 'darkgreen' }}>
<h1>React Parallax Tilt 👀</h1>
</div>
</Tilt>
);
};The Tilt component accepts several optional props to customize the tilt, glare, scale, and event behavior.
tiltEnable (boolean, default: true): Enables/disables the tilt effect.tiltReverse (boolean, default: false): Reverses the tilt direction.tiltAngleXInitial (number, default: 0): Initial tilt angle (in degrees) on the x-axis.tiltAngleYInitial (number, default: 0): Initial tilt angle (in degrees) on the y-axis.tiltMaxAngleX (number, default: 20): Maximum tilt rotation (in degrees) on the x-axis. Range: 0°-90°.tiltMaxAngleY (number, default: 20): Maximum tilt rotation (in degrees) on the y-axis. Range: 0°-90°.tiltAxis ('x' | 'y', default: undefined): Enables tilt on a single axis only.tiltAngleXManual (number | null, default: null): Manual tilt rotation (in degrees) on the x-axis.tiltAngleYManual (number | null, default: null): Manual tilt rotation (in degrees) on the y-axis.glareEnable (boolean, default: false): Enables/disables the glare effect.glareMaxOpacity (number, default: 0.7): Maximum glare opacity (0.5 = 50%, 1 = 100%). Range: 0-1.glareColor (string, default: #ffffff): Sets the color of the glare effect.glarePosition ('top' | 'right' | 'bottom' | 'left' | 'all', default: bottom): Sets the position of the glare effect.glareReverse (boolean, default: false): Reverses the glare direction.glareBorderRadius (string, default: 0): Sets the border radius of the glare (standard CSS value).scale (number, default: 1): Scale of the component (1.5 = 150%, 2 = 200%).perspective (number, default: 1000): Defines how far the tilt component appears from the user. Lower values create more extreme tilt effects.flipVertically (boolean, default: false): Enables/disables vertical flipping.flipHorizontally (boolean, default: false): Enables/disables horizontal flipping.reset (boolean, default: true): Determines if effects should reset on onLeave event.transitionEasing (string, default: cubic-bezier(.03,.98,.52,.99)): Easing function for the transition.transitionSpeed (number, default: 400): Speed of the transition.trackOnWindow (boolean, default: false): Tracks mouse and touch events across the entire window.gyroscope (boolean, default: false): Enables/disables device orientation detection.onMove (({ tiltAngleX: number, tiltAngleY: number, tiltAngleXPercentage: number, tiltAngleYPercentage: number, glareAngle: number, glareOpacity: number, event: Event }) => void): Triggered when user moves on the component.onEnter ((event: Event) => void): Triggered when user enters the component.onLeave ((event: Event) => void): Triggered when user leaves the component.The ReactParallaxTilt component is the primary entry point for creating parallax tilt effects in React applications. It wraps any children elements and applies a 3D tilt effect based on mouse movement, touch interaction, or device orientation (gyroscope).
onEnter, onMove, and onLeave events.import React from 'react';
import { ReactParallaxTilt } from 'react-parallax-tilt';
const MyComponent = () => (
<ReactParallaxTilt>
<div style={{ width: '300px', height: '200px', background: 'blue' }}>
Tilt Me!
</div>
</ReactParallaxTilt>
);The GlareProps type defines the configuration for the glare visual effect. You can use these props to control the visibility, opacity, color, position, and shape of the glare overlay on the tilted element.
| Prop | Type | Description |
|---|---|---|
glareEnable | boolean | Enables or disables the glare effect. |
glareMaxOpacity | number | The maximum opacity of the glare. Range: 0 to 1 (e.g., 0.5 for 50%). |
glareColor | string | The color of the glare effect (CSS color value). |
glarePosition | GlarePosition | The position of the glare effect. |
glareReverse | boolean | If true, reverses the glare direction. |
glareBorderRadius | string | The border radius of the glare. Accepts any standard CSS border radius value. |
The TiltProps type defines the configuration options for the tilt effect. You can control whether the effect is enabled, reverse the direction, set initial angles, define maximum rotation limits, restrict tilt to a single axis, or provide manual rotation values.
// Example usage of TiltProps
const props: TiltProps = {
tiltEnable: true,
tiltReverse: false,
tiltMaxAngleX: 25,
tiltMaxAngleY: 25,
tiltAxis: 'x' // Restricts tilt to the x-axis
};The ReactParallaxTilt component accepts several props to control the tilt effect, glare effect, and general styling. It extends TiltProps and GlareProps and includes standard HTML attributes for div elements like className and style.
<ReactParallaxTilt
scale={1.2}
perspective={500}
flipVertically={false}
flipHorizontally={false}
reset={true}
transitionEasing="ease-in-out"
transitionSpeed={250}
trackOnWindow={false}
gyroscope={false}
onMove={({ tiltAngleX, tiltAngleY }) => console.log(tiltAngleX, tiltAngleY)}
onEnter={({ event }) => console.log('Entered', event)}
onLeave={({ event }) => console.log('Left', event)}
>
<div>Your Content</div>
</ReactParallaxTilt>The following properties are available in the TiltProps object:
type TiltProps = {
/** Enables/disables the tilt effect. */
tiltEnable?: boolean;
/** Reverses the tilt direction. */
tiltReverse?: boolean;
/** Initial tilt angle (in degrees) on the x-axis. */
tiltAngleXInitial?: number;
/** Initial tilt angle (in degrees) on the y-axis. */
tiltAngleYInitial?: number;
/** Maximum tilt rotation (in degrees) on the x-axis (range: `0°-90°`). */
tiltMaxAngleX?: number;
/** Maximum tilt rotation (in degrees) on the y-axis (range: `0°-90°`). */
tiltMaxAngleY?: number;
/** Enables tilt on a single axis only. */
tiltAxis?: Axis;
/** Manual tilt rotation (in degrees) on the x-axis. */
tiltAngleXManual?: number | null;
/** Manual tilt rotation (in degrees) on the y-axis. */
tiltAngleYManual?: number | null;
};The onMove prop is a callback function triggered whenever the user moves over the component. It provides detailed information about the current tilt angles and glare state.
type OnMoveParams = {
tiltAngleX: number;
tiltAngleY: number;
tiltAngleXPercentage: number;
tiltAngleYPercentage: number;
glareAngle: number;
glareOpacity: number;
event: SupportedEvent;
};
type OnMove = (onMoveParams: OnMoveParams) => void;The Axis type can be used with the tiltAxis prop to restrict the tilt effect to a single dimension.
type Axis = 'x' | 'y';The onEnter and onLeave props are callbacks triggered when the user's pointer (mouse or touch) enters or leaves the component's area. Both provide an event object containing the original MouseEvent, React.MouseEvent, TouchEvent, or React.TouchEvent.
type OnEnterParams = {
event: MouseEvent | React.MouseEvent | TouchEvent | React.TouchEvent;
};
type OnEnter = (onEnterParams: OnEnterParams) => void;
type OnLeaveParams = {
event: MouseEvent | React.MouseEvent | TouchEvent | React.TouchEvent;
};
type OnLeave = (onLeaveParams: OnLeaveParams) => void;