Skip to content

Commit

Permalink
Merge pull request #443 from greenpeace/planet-5728
Browse files Browse the repository at this point in the history
PLANET-5728: Carousel Gallery: Slider not working
  • Loading branch information
comzeradd authored Nov 26, 2020
2 parents 07f7aa5 + 09442a4 commit 38c764a
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions assets/src/blocks/Gallery/GalleryCarousel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState, useEffect, useRef } from '@wordpress/element';
import _uniqueId from 'lodash/uniqueId';

const { __ } = wp.i18n;

Expand All @@ -24,8 +23,8 @@ export const GalleryCarousel = ({ images }) => {
}

const goToSlide = newSlide => {
const nextElement = document.getElementById(`${idRef.current}-slide${newSlide}`);
const activeElement = document.getElementById(`${idRef.current}-slide${currentSlide}`);
const nextElement = slidesRef.current[newSlide];
const activeElement = slidesRef.current[currentSlide];
if (newSlide !== currentSlide && nextElement && activeElement && !sliding) {
setSliding(true);
const order = getOrder(newSlide);
Expand Down Expand Up @@ -53,7 +52,7 @@ export const GalleryCarousel = ({ images }) => {
const goToPrevSlide = () => goToSlide(currentSlide === 0 ? lastSlide : currentSlide - 1);

const timerRef = useRef(null);
const idRef = useRef(null);
const slidesRef = useRef([]);

// Set up the autoplay for the slides
useEffect(() => {
Expand All @@ -66,13 +65,6 @@ export const GalleryCarousel = ({ images }) => {
}
}, [currentSlide, images]);

// Set up the unique id for the carousel
useEffect(() => {
if (!idRef.current) {
idRef.current = `gallery-${_uniqueId()}`;
}
}, []);

return (
<div className="carousel slide">
{images.length > 1 &&
Expand All @@ -97,7 +89,7 @@ export const GalleryCarousel = ({ images }) => {
<div
key={image.image_src}
className={`carousel-item ${index === currentSlide ? 'active' : ''}`}
id={`${idRef.current}-slide${index}`}
ref={element => slidesRef.current[index] = element}
>
<img
src={image.image_src}
Expand Down

0 comments on commit 38c764a

Please sign in to comment.