DEV Community

Joseph Nelson
Joseph Nelson

Posted on

Advanced Instagram-Style Zoom Sliders with react-instagram-zoom-slider in React

react-instagram-zoom-slider is a React component library for creating Instagram-style image sliders with pinch-to-zoom functionality. It provides smooth touch gestures, zoom interactions, and swipe navigation similar to Instagram's image viewer. This guide walks through advanced usage of react-instagram-zoom-slider with React, including zoom controls, gesture handling, and complex slider configurations. This is part 60 of a series on using react-instagram-zoom-slider with React.

Demo animation

Prerequisites

Before you begin, make sure you have:

  • Node.js version 14.0 or higher installed
  • npm, yarn, or pnpm package manager
  • A React project (version 16.8 or higher) or create-react-app setup
  • Basic knowledge of React hooks (useState, useEffect, useRef)
  • Familiarity with touch events and gestures
  • Understanding of CSS transforms and animations

Installation

Install react-instagram-zoom-slider using your preferred package manager:

npm install react-instagram-zoom-slider
Enter fullscreen mode Exit fullscreen mode

Or with yarn:

yarn add react-instagram-zoom-slider
Enter fullscreen mode Exit fullscreen mode

Or with pnpm:

pnpm add react-instagram-zoom-slider
Enter fullscreen mode Exit fullscreen mode

After installation, your package.json should include:

{
  "dependencies": {
    "react-instagram-zoom-slider": "^1.0.0",
    "react": "^18.0.0",
    "react-dom": "^18.0.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Project Setup

react-instagram-zoom-slider requires minimal setup. Import the component and you're ready to use it.

First Example / Basic Usage

Let's create a simple zoom slider. Create a new file src/ZoomSliderExample.jsx:

// src/ZoomSliderExample.jsx
import React from 'react';
import InstagramZoomSlider from 'react-instagram-zoom-slider';

function ZoomSliderExample() {
  const images = [
    'https://via.placeholder.com/800x600/007bff/ffffff?text=Image+1',
    'https://via.placeholder.com/800x600/28a745/ffffff?text=Image+2',
    'https://via.placeholder.com/800x600/ffc107/000000?text=Image+3',
    'https://via.placeholder.com/800x600/dc3545/ffffff?text=Image+4'
  ];

  return (
    <div style={{ padding: '20px', maxWidth: '800px', margin: '0 auto' }}>
      <h2>Instagram Zoom Slider Example</h2>
      <div style={{ height: '600px', border: '1px solid #ddd', borderRadius: '8px', overflow: 'hidden' }}>
        <InstagramZoomSlider
          images={images}
          width="100%"
          height="100%"
        />
      </div>
    </div>
  );
}

export default ZoomSliderExample;
Enter fullscreen mode Exit fullscreen mode

Update your App.jsx:

// src/App.jsx
import React from 'react';
import ZoomSliderExample from './ZoomSliderExample';
import './App.css';

function App() {
  return (
    <div className="App">
      <ZoomSliderExample />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

This creates a basic Instagram-style slider with zoom functionality.

Understanding the Basics

react-instagram-zoom-slider provides several key features:

  • InstagramZoomSlider component: Main slider component
  • Pinch-to-zoom: Touch gesture support for zooming
  • Swipe navigation: Swipe between images
  • Double-tap zoom: Double-tap to zoom in/out
  • Smooth animations: Smooth transitions and zoom effects
  • Responsive: Works on mobile and desktop

Key concepts for advanced usage:

  • Image array: Pass array of image URLs
  • Zoom controls: Automatic zoom controls on touch/click
  • Gesture handling: Built-in support for pinch and swipe gestures
  • Customization: Configurable zoom levels and behavior
  • Event handling: Callbacks for slide changes and interactions

Here's an example with custom configuration:

// src/AdvancedZoomSliderExample.jsx
import React, { useState } from 'react';
import InstagramZoomSlider from 'react-instagram-zoom-slider';

function AdvancedZoomSliderExample() {
  const [currentIndex, setCurrentIndex] = useState(0);

  const images = [
    'https://via.placeholder.com/800x600/007bff/ffffff?text=Image+1',
    'https://via.placeholder.com/800x600/28a745/ffffff?text=Image+2',
    'https://via.placeholder.com/800x600/ffc107/000000?text=Image+3',
    'https://via.placeholder.com/800x600/dc3545/ffffff?text=Image+4',
    'https://via.placeholder.com/800x600/6c757d/ffffff?text=Image+5'
  ];

  const handleSlideChange = (index) => {
    setCurrentIndex(index);
    console.log(`Slide changed to index: ${index}`);
  };

  return (
    <div style={{ padding: '20px', maxWidth: '900px', margin: '0 auto' }}>
      <h2>Advanced Zoom Slider</h2>
      <div style={{ marginBottom: '20px', textAlign: 'center', color: '#666' }}>
        Image {currentIndex + 1} of {images.length}
      </div>
      <div style={{ height: '600px', border: '1px solid #ddd', borderRadius: '8px', overflow: 'hidden' }}>
        <InstagramZoomSlider
          images={images}
          width="100%"
          height="100%"
          onSlideChange={handleSlideChange}
          showDots={true}
          showArrows={true}
        />
      </div>
    </div>
  );
}

export default AdvancedZoomSliderExample;
Enter fullscreen mode Exit fullscreen mode

Practical Example / Building Something Real

Let's build a comprehensive image gallery with zoom functionality:

// src/ImageGalleryZoomSlider.jsx
import React, { useState } from 'react';
import InstagramZoomSlider from 'react-instagram-zoom-slider';

function ImageGalleryZoomSlider() {
  const [currentIndex, setCurrentIndex] = useState(0);
  const [isFullscreen, setIsFullscreen] = useState(false);

  const images = [
    {
      url: 'https://via.placeholder.com/1200x800/007bff/ffffff?text=Image+1',
      title: 'Image 1',
      description: 'Description for image 1'
    },
    {
      url: 'https://via.placeholder.com/1200x800/28a745/ffffff?text=Image+2',
      title: 'Image 2',
      description: 'Description for image 2'
    },
    {
      url: 'https://via.placeholder.com/1200x800/ffc107/000000?text=Image+3',
      title: 'Image 3',
      description: 'Description for image 3'
    },
    {
      url: 'https://via.placeholder.com/1200x800/dc3545/ffffff?text=Image+4',
      title: 'Image 4',
      description: 'Description for image 4'
    },
    {
      url: 'https://via.placeholder.com/1200x800/6c757d/ffffff?text=Image+5',
      title: 'Image 5',
      description: 'Description for image 5'
    }
  ];

  const imageUrls = images.map(img => img.url);

  const handleSlideChange = (index) => {
    setCurrentIndex(index);
  };

  const handleFullscreen = () => {
    setIsFullscreen(!isFullscreen);
  };

  return (
    <div style={{ padding: '20px', maxWidth: '1000px', margin: '0 auto' }}>
      <h1>Image Gallery with Zoom</h1>

      <div style={{
        position: isFullscreen ? 'fixed' : 'relative',
        top: isFullscreen ? 0 : 'auto',
        left: isFullscreen ? 0 : 'auto',
        right: isFullscreen ? 0 : 'auto',
        bottom: isFullscreen ? 0 : 'auto',
        width: isFullscreen ? '100vw' : '100%',
        height: isFullscreen ? '100vh' : '700px',
        backgroundColor: isFullscreen ? '#000' : 'transparent',
        zIndex: isFullscreen ? 9999 : 1,
        display: 'flex',
        flexDirection: 'column',
        justifyContent: 'center',
        alignItems: 'center'
      }}>
        <div style={{
          width: '100%',
          height: isFullscreen ? '100%' : '100%',
          border: isFullscreen ? 'none' : '1px solid #ddd',
          borderRadius: isFullscreen ? 0 : '8px',
          overflow: 'hidden',
          position: 'relative'
        }}>
          <InstagramZoomSlider
            images={imageUrls}
            width="100%"
            height="100%"
            onSlideChange={handleSlideChange}
            showDots={true}
            showArrows={true}
          />
          {isFullscreen && (
            <button
              onClick={handleFullscreen}
              style={{
                position: 'absolute',
                top: '20px',
                right: '20px',
                padding: '10px 20px',
                backgroundColor: 'rgba(255,255,255,0.9)',
                color: '#333',
                border: 'none',
                borderRadius: '4px',
                cursor: 'pointer',
                zIndex: 10000
              }}
            >
              Exit Fullscreen
            </button>
          )}
        </div>
        {!isFullscreen && (
          <div style={{ marginTop: '20px', textAlign: 'center' }}>
            <div style={{ marginBottom: '10px' }}>
              <h3>{images[currentIndex].title}</h3>
              <p style={{ color: '#666' }}>{images[currentIndex].description}</p>
            </div>
            <button
              onClick={handleFullscreen}
              style={{
                padding: '10px 20px',
                backgroundColor: '#007bff',
                color: 'white',
                border: 'none',
                borderRadius: '4px',
                cursor: 'pointer'
              }}
            >
              View Fullscreen
            </button>
          </div>
        )}
      </div>

      <div style={{ marginTop: '20px', display: 'flex', justifyContent: 'center', gap: '10px' }}>
        {images.map((image, index) => (
          <img
            key={index}
            src={image.url}
            alt={image.title}
            onClick={() => setCurrentIndex(index)}
            style={{
              width: '100px',
              height: '100px',
              objectFit: 'cover',
              borderRadius: '4px',
              cursor: 'pointer',
              border: currentIndex === index ? '3px solid #007bff' : '1px solid #ddd',
              opacity: currentIndex === index ? 1 : 0.7
            }}
          />
        ))}
      </div>
    </div>
  );
}

export default ImageGalleryZoomSlider;
Enter fullscreen mode Exit fullscreen mode

Now create a product image viewer:

// src/ProductImageViewer.jsx
import React, { useState } from 'react';
import InstagramZoomSlider from 'react-instagram-zoom-slider';

function ProductImageViewer() {
  const [selectedImageIndex, setSelectedImageIndex] = useState(0);

  const productImages = [
    'https://via.placeholder.com/800x800/007bff/ffffff?text=Product+View+1',
    'https://via.placeholder.com/800x800/28a745/ffffff?text=Product+View+2',
    'https://via.placeholder.com/800x800/ffc107/000000?text=Product+View+3',
    'https://via.placeholder.com/800x800/dc3545/ffffff?text=Product+View+4'
  ];

  return (
    <div style={{ padding: '20px', maxWidth: '1000px', margin: '0 auto' }}>
      <h2>Product Image Viewer</h2>
      <div style={{ display: 'flex', gap: '20px' }}>
        <div style={{ flex: 1 }}>
          <div style={{ height: '600px', border: '1px solid #ddd', borderRadius: '8px', overflow: 'hidden' }}>
            <InstagramZoomSlider
              images={productImages}
              width="100%"
              height="100%"
              onSlideChange={setSelectedImageIndex}
              showDots={true}
              showArrows={true}
            />
          </div>
        </div>
        <div style={{ flex: 1 }}>
          <h3>Product Details</h3>
          <p>Use pinch-to-zoom or double-tap to zoom in on product images.</p>
          <p>Swipe left or right to navigate between product views.</p>
          <div style={{ marginTop: '20px' }}>
            <p>Current view: {selectedImageIndex + 1} of {productImages.length}</p>
          </div>
        </div>
      </div>
    </div>
  );
}

export default ProductImageViewer;
Enter fullscreen mode Exit fullscreen mode

Update your App.jsx:

// src/App.jsx
import React from 'react';
import ImageGalleryZoomSlider from './ImageGalleryZoomSlider';
import ProductImageViewer from './ProductImageViewer';
import './App.css';

function App() {
  return (
    <div className="App">
      <ImageGalleryZoomSlider />
      <ProductImageViewer />
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

This example demonstrates:

  • Image gallery with zoom functionality
  • Fullscreen mode
  • Thumbnail navigation
  • Product image viewer
  • Slide change callbacks
  • Custom controls

Common Issues / Troubleshooting

  1. Zoom not working: Ensure touch events are properly handled. The library requires touch support for pinch-to-zoom. On desktop, use mouse wheel or double-click.

  2. Images not loading: Check that image URLs are valid and accessible. Use absolute URLs or ensure relative paths are correct.

  3. Slider not responding: Make sure the container has proper dimensions (width and height). The slider needs explicit dimensions to function correctly.

  4. Gestures not working: Touch gestures require proper event handling. Ensure there are no CSS conflicts preventing touch events.

  5. Fullscreen issues: When implementing fullscreen, ensure proper z-index and positioning. The slider container needs to be properly sized for fullscreen mode.

  6. Performance: For large image sets, consider lazy loading or image optimization. Large images can impact performance, especially on mobile devices.

Next Steps

Now that you have an advanced understanding of react-instagram-zoom-slider:

  • Explore advanced zoom configurations
  • Learn about custom gesture handling
  • Implement image lazy loading
  • Add custom navigation controls
  • Create custom zoom animations
  • Integrate with image optimization libraries
  • Learn about other zoom slider libraries
  • Check the official repository: https://github.com/skozer/react-instagram-zoom-slider
  • Look for part 61 of this series for more advanced topics

Summary

You've successfully integrated react-instagram-zoom-slider into your React application with advanced features including zoom functionality, fullscreen mode, thumbnail navigation, and product image viewing. react-instagram-zoom-slider provides an Instagram-like experience for image viewing with smooth gestures and zoom interactions.

SEO Keywords

react-instagram-zoom-slider
React Instagram slider
react-instagram-zoom-slider tutorial
React zoom slider
react-instagram-zoom-slider installation
React pinch to zoom
react-instagram-zoom-slider example
React image zoom
react-instagram-zoom-slider setup
React touch slider
react-instagram-zoom-slider customization
React image gallery zoom
react-instagram-zoom-slider gestures
React zoom library
react-instagram-zoom-slider getting started

Top comments (0)