From 5321787fffa85bc2e36042ab808982d201efcb2a Mon Sep 17 00:00:00 2001 From: Nicholas Lancey Date: Tue, 29 Aug 2023 13:35:31 -0700 Subject: [PATCH 1/3] fix(active-index): resets index if activeIndex doesn't exist --- src/lib/components/gallery-main.jsx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/components/gallery-main.jsx b/src/lib/components/gallery-main.jsx index 9422592..1f8d284 100644 --- a/src/lib/components/gallery-main.jsx +++ b/src/lib/components/gallery-main.jsx @@ -1,6 +1,6 @@ // packages import PropTypes from "prop-types" -import { useCallback } from "react" +import { useCallback, useEffect } from "react" // hooks import { useGallery } from "../hooks/use-gallery" @@ -18,8 +18,13 @@ export const GalleryMain = ({ renderGalleryItem, className, ...props }) => { touchState, setTouchState, swipeThreshold, + goToIndex, } = useGallery() + useEffect(() => { + if (!galleryItems[activeIndex]) goToIndex(0) + }, [activeIndex, galleryItems, goToIndex]) + const handlePointerDown = useCallback(() => { if (!draggable) return From ea2dae124ad598ec44fe4cbc048c0df7a4f42bf5 Mon Sep 17 00:00:00 2001 From: Nicholas Lancey Date: Thu, 21 Sep 2023 16:17:34 -0700 Subject: [PATCH 2/3] feat(goToIndex): validates index, moves useEffect to context --- src/lib/components/gallery-context.jsx | 22 +++++++++++++++++----- src/lib/components/gallery-main.jsx | 7 +------ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/lib/components/gallery-context.jsx b/src/lib/components/gallery-context.jsx index 283b1b0..ad556fc 100644 --- a/src/lib/components/gallery-context.jsx +++ b/src/lib/components/gallery-context.jsx @@ -1,6 +1,6 @@ // packages import PropTypes from "prop-types" -import { createContext, useCallback, useRef, useState } from "react" +import { createContext, useCallback, useRef, useState, useEffect } from "react" // utils import classnames from "../utils/classnames" @@ -74,15 +74,27 @@ export const Gallery = ({ const goToIndex = useCallback( (index) => { + const validatedIndex = !items[index] ? 0 : index + setPreviouslyActiveIndex(activeIndex) - setActiveIndex(index) - const direction = index - activeIndex > 0 ? 1 : 0 - if (onChange) onChange({ oldIndex: activeIndex, newIndex: index, direction }) + setActiveIndex(validatedIndex) + const direction = validatedIndex - activeIndex > 0 ? 1 : 0 + if (onChange) + onChange({ oldIndex: activeIndex, newIndex: validatedIndex, direction }) }, - [activeIndex, onChange] + [activeIndex, onChange, items] ) + // This useEffect checks to see if the gallery item (index) exists, and if it + // doesn't it resets the gallery to the first slide (0). The edge case here + // becomes apparent when or if the length of gallery items changes, for instance + // separating/grouping items based on screen size. + + useEffect(() => { + if (!items[activeIndex]) goToIndex() + }, [activeIndex, items, goToIndex]) + const value = { galleryItems: items, itemNodes, diff --git a/src/lib/components/gallery-main.jsx b/src/lib/components/gallery-main.jsx index 1f8d284..9422592 100644 --- a/src/lib/components/gallery-main.jsx +++ b/src/lib/components/gallery-main.jsx @@ -1,6 +1,6 @@ // packages import PropTypes from "prop-types" -import { useCallback, useEffect } from "react" +import { useCallback } from "react" // hooks import { useGallery } from "../hooks/use-gallery" @@ -18,13 +18,8 @@ export const GalleryMain = ({ renderGalleryItem, className, ...props }) => { touchState, setTouchState, swipeThreshold, - goToIndex, } = useGallery() - useEffect(() => { - if (!galleryItems[activeIndex]) goToIndex(0) - }, [activeIndex, galleryItems, goToIndex]) - const handlePointerDown = useCallback(() => { if (!draggable) return From 99d24a2f8df6dbb2f5b6c91e9b0ecd3563801cbe Mon Sep 17 00:00:00 2001 From: Andrew Rubin Date: Fri, 22 Sep 2023 14:00:31 -0700 Subject: [PATCH 3/3] 2.0.1 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0c401a1..a298a13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@wethegit/react-gallery", - "version": "1.0.2", + "version": "2.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@wethegit/react-gallery", - "version": "1.0.2", + "version": "2.0.1", "license": "MIT", "dependencies": { "prop-types": "~15.8.1", diff --git a/package.json b/package.json index 3a6e737..b4fc68b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@wethegit/react-gallery", - "version": "2.0.0", + "version": "2.0.1", "description": "A customizable, accessible gallery component for React projects.", "files": [ "dist"