react-parallax-tilt

repository·main·Indexed 22 days ago

https://github.com/mkosir/react-parallax-tilt

A 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.

Tokens
3K
Snippets
10
Records
14
Agent score
78%

What's inside react-parallax-tilt

  1. Use the gyroscope for device orientation

    main

    You can enable device orientation detection by setting the gyroscope prop to true.

    Important Considerations:

    • Device orientation detection is experimental; check browser compatibility.
    • Always use secure origins (https).
    • It may not work within cross-origin <iframe> elements.
    • iOS 13+ Support: Apple requires a permission dialog. This dialog cannot be triggered automatically on page load; it must be triggered by a user interaction (e.g., a button tap).
    <Tilt gyroscope={true}>
      <h1>React Parallax Tilt 👀</h1>
    </Tilt>
  2. Basic usage of the Tilt component

    main

    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>
      );
    };
  3. Reference the Tilt component props

    main

    The Tilt component accepts several optional props to customize the tilt, glare, scale, and event behavior.

    Tilt Configuration

    • 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.

    Glare Effect

    • 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).

    Visual & Animation Settings

    • 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.

    Interaction & Device Settings

    • trackOnWindow (boolean, default: false): Tracks mouse and touch events across the entire window.
    • gyroscope (boolean, default: false): Enables/disables device orientation detection.

    Event Callbacks

    • 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.
  4. Use the ReactParallaxTilt component

    main

    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).

    Key Features

    • Tilt Effect: Applies 3D rotation to the wrapped element.
    • Glare Effect: Adds a reflective glare overlay that moves with the tilt.
    • Gyroscope Support: Enables tilt effects using mobile device orientation.
    • Event Callbacks: Provides hooks for onEnter, onMove, and onLeave events.
    • Customization: Highly configurable via props for perspective, scale, transition speed, and more.
    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>
    );
  5. Configure the glare effect via GlareProps

    main

    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.

    Available Props

    PropTypeDescription
    glareEnablebooleanEnables or disables the glare effect.
    glareMaxOpacitynumberThe maximum opacity of the glare. Range: 0 to 1 (e.g., 0.5 for 50%).
    glareColorstringThe color of the glare effect (CSS color value).
    glarePositionGlarePositionThe position of the glare effect.
    glareReversebooleanIf true, reverses the glare direction.
    glareBorderRadiusstringThe border radius of the glare. Accepts any standard CSS border radius value.
  6. Configure TiltProps for the Tilt component

    main

    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
    };
  7. Configure ReactParallaxTilt props

    main

    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>
  8. Reference TiltProps configuration options

    main

    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;
    };
  9. Use the onMove callback to track tilt data

    main

    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;
  10. Use the onEnter and onLeave callbacks

    main

    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;