Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-image-turntable

Package Overview
Dependencies
Maintainers
0
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-image-turntable

Display a set of images as a draggable 360 degree turntable.

  • 4.0.0-0
  • beta
  • next
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.7K
increased by184.16%
Maintainers
0
Weekly downloads
 
Created
Source

React Image Turntable

Display a set of images as a draggable 360 degree turntable.

React Image Turntable with rotating car

Open in StackBlitz

NPM package License MIT GitHub CI status

Features

  • Accessible & screen-reader friendly
  • Responsive & intrinsically sized
  • Built-in keyboard navigation
  • Programmatically controllable
  • Teeny Tiny
  • Zero dependencies
  • Type safe

Install

npm i react-image-turntable
# Or
yarn add react-image-turntable
# Or
pnpm i react-image-turntable

Usage

Example

import { ReactImageTurntable, useReactImageTurntable } from 'react-image-turntable';

const images = [
  'https://via.placeholder.com/1200x800?text=1',
  'https://via.placeholder.com/1200x800?text=2',
  'https://via.placeholder.com/1200x800?text=3',
];

export const App = () => {
  const turntableProps = useReactImageTurntable({ images });

  return <ReactImageTurntable {...turntableProps} />;
};

See the example code for full demo.

useReactImageTurntable Props

Input Props

The useReactImageTurntable hook accepts the following properties.

PropsTypeRequiredDefault ValueDescription
autoRotate.enabledbooleanfalseWhether to automatically rotate the turntable.
autoRotate.intervalnumber200The interval between autorotations in ms.
imagesstring[]undefinedList of image src attributes.
initialImageIndexnumber0Index of image to show first.
movementSensitivitynumber20The amount a "drag" has to move before an image changes to next or previous.
onIndexChange(index: number) => voidundefinedCallback to trigger whenever the active index changes.
Output Props

The useReactImageTurntable hook returns the following properties.

PropsTypeDescription
activeImageIndexnumberThe index of the image currently being shown.
setActiveImageIndex(index: number) => voidFunction to set the active index.
imagesstring[]The images passed into the hook.
refRefObject<HTMLDivElement>The ref of the root turntable element.

Note that there is no need for a setImages function. images is not stored in state. If you want to change the images simply change the images prop passed into the hook.

Example Usage
View example
import { ReactImageTurntable, useReactImageTurntable } from 'react-image-turntable';

const images = [
  'https://via.placeholder.com/1200x800?text=1',
  'https://via.placeholder.com/1200x800?text=2',
  'https://via.placeholder.com/1200x800?text=3',
];

export const App = () => {
  const turntableProps = useReactImageTurntable({
    autoRotate: { disabled: true, interval: 75 },
    images,
    initialImageIndex: 1, // Start on the second image.
    movementSensitivity: 50, // Increase the amount of drag needed to change images.
    onIndexChange: (index) => console.log(`Active image index changed to ${index}`),
  });

  const handleSelectFirstImage = () => {
    turntableProps.setActiveIndex(0);
  };

  return (
    <>
      <button type="button" onClick={handleSelectFirstImage}>
        Select first image
      </button>
      <ReactImageTurntable {...turntableProps} />
    </>
  );
};

Custom Styling

The library uses the first image in images[] to intrinsically size the component, it also exports the following classNames allowing you to apply custom styles.

classNamePurpose
CLASS_NAME_IMGBase class for all images.
CLASS_NAME_IMG_PRIMARYClass of first image in images[] (sets the size of the main component).
CLASS_NAME_IMG_SECONDARYClass of subsequent images.

Contributing

See the contributing guide to get started.


Browser Support

The library is built for ES2021.

Notes

  • It's recommended you use a minimum of 24-36 for a smooth experience
  • Legacy version by @andrewmcoupe

Keywords

FAQs

Package last updated on 17 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc