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

react-lensview

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-lensview

A versatile React component for viewing and interacting with images, offering intuitive zoom and drag functionalities for detailed exploration.

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

LensView

LensView is a React component that allows users to view images with zoom and drag functionalities. This component enhances image viewing experiences by providing intuitive controls for navigation and manipulation.

Features

  • Zoom in and out on images using mouse scroll
  • Drag images around when zoomed in
  • Navigate between images using next and previous buttons
  • Close the image view with a simple button click

Installation

To install the LensView component, use npm or yarn:

npm install react-lensview

or

yarn add react-lensview

Usage

Here’s a simple example of how to use the LensView component in your React application:

TypeScript Example (.ts or .tsx)

  • Step 1: Import Packages
// Import React and required hooks
import React, { useState } from 'react';
// Import LensView package
import LensView from 'react-lensview';
  • Step 2: Create the Image Array
// Image array with src, alt and other attributes
const imageArray = [
  { src: 'image-1.jpg', alt: 'Image 1', name: "name-1" },
  { src: 'image-2.jpg', alt: 'Image 2', name: "name-2" },
  { src: 'image-3.jpg', alt: 'Image 3', name: "name-3" },
];
  • Step 3: Create the Lensview Component
const App: React.FC = () => {
  const [isImageOpen, setIsImageOpen] = useState<boolean>(false);
  const [currentImageIndex, setCurrentImageIndex] = useState<number>(0);

  const openImage = (index: number) => {
    setCurrentImageIndex(index);
    setIsImageOpen(true); 
  };

  const closeImage = () => {
    setIsImageOpen(false);
  };

  return (
    <LensView
      images={imageArray}
      isImageOpen={isImageOpen} 
      currentImageIndex={currentImageIndex}
      openImage={openImage}  
      closeImage={closeImage}   
      controls={{ enableZoom: true, enableDrag: true }} 
    >
      {imageArray.map((image, index) => (
        <div key={index} onClick={() => openImage(index)}>
          <img src={image.src} alt={image.alt} />
        </div>
      ))}
    </LensView>
  );
};

export default App;

Props

LensView Component Props

PropTypeRequiredDefault ValueDescription
images{ src: string; alt: string }[]YesN/AAn array of image objects containing src (URL of the image) and alt (alternative text).
childrenReact.ReactNodeYesN/AChild components or elements to be displayed alongside the image viewer.
isImageOpenbooleanYesN/AA flag indicating whether the image viewer is open or closed.
currentImageIndexnumberYesN/AThe index of the currently displayed image in the images array.
openImage(index: number) => voidYesN/AFunction to open an image at a specified index.
closeImage() => voidYesN/AFunction to close the image viewer.
controlsIControlsNo{ enableZoom: false, enableDrag: false }An object to configure controls; includes enableZoom and enableDrag flags.

Explanation of the Props

images: This prop accepts an array of image objects. Each object must contain a src string for the image URL and an alt string for alternative text, which is important for accessibility.

children: The children prop allows you to pass any valid React elements that will be displayed alongside the image viewer, such as thumbnails or additional content.

isImageOpen: A boolean that determines if the image viewer is currently displayed. It should be true when an image is selected.

currentImageIndex: The index of the currently displayed image. This is necessary for navigating between images.

openImage: A function that is called with an index to open a specific image in the viewer.

closeImage: A function that closes the image viewer when called.

controls: An optional prop that configures whether zoom and drag functionalities are enabled. If not provided, both features are disabled by default.

License

Feel free to submit issues or pull requests for improvements and bug fixes!

This Markdown document provides a comprehensive overview of your LensView component, guiding users on how to install, use, and customize it in their projects. You can modify the sections as needed to suit your specific requirements or add any additional information.

Keywords

FAQs

Package last updated on 03 Oct 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