Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
22 changes: 17 additions & 5 deletions src/lib/components/gallery-context.jsx
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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,
Expand Down