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
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets).

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md).
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/config@3.1.4/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/seven-badgers-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wethegit/react-gallery": patch
---

Fix gallery item getting stuck
1 change: 1 addition & 0 deletions package-lock.json

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

79 changes: 49 additions & 30 deletions src/lib/components/gallery-main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,25 @@ export const GalleryMain = ({ renderGalleryItem, className, ...props }) => {
swipeThreshold,
} = useGallery()

const handlePointerDown = useCallback(() => {
if (!draggable) return
const resetTouchState = useCallback(() => {
setTouchState((prevState) => ({
...prevState,
isDragging: false,
xOffset: 0,
start: 0,
offsetting: false,
scrolling: false,
}))
}, [setTouchState])

setTouchState((prevState) => ({ ...prevState, isDragging: true }))
}, [draggable, setTouchState])
const handlePointerDown = useCallback(
(event) => {
if (!draggable) return
event.currentTarget.setPointerCapture?.(event.pointerId)
setTouchState((prevState) => ({ ...prevState, isDragging: true }))
},
[draggable, setTouchState]
)

const handlePointerMove = useCallback(
(event) => {
Expand Down Expand Up @@ -85,46 +99,51 @@ export const GalleryMain = ({ renderGalleryItem, className, ...props }) => {
]
)

const handlePointerUp = useCallback(() => {
if (!draggable) return
const handlePointerUp = useCallback(
(event) => {
if (!draggable) return

if (touchState.isDragging) {
/*
event.currentTarget.releasePointerCapture?.(event.pointerId)
if (touchState.isDragging) {
/*
check if the offset value is more than the swipeThreshold.
if it is then we'll move to the next or prev item in the gallery,
otherwise it'll just spring back to the current position.
*/
if (Math.abs(touchState.xOffset) > swipeThreshold) {
if (touchState.xOffset < 0) next()
else previous()
if (Math.abs(touchState.xOffset) > swipeThreshold) {
if (touchState.xOffset < 0) next()
else previous()
}

resetTouchState()
}
},
[
draggable,
touchState.isDragging,
touchState.xOffset,
swipeThreshold,
next,
previous,
resetTouchState,
]
)

// reset all the touchState values.
setTouchState((prevState) => ({
...prevState,
isDragging: false,
xOffset: 0,
start: 0,
offsetting: false,
scrolling: false,
}))
}
}, [
draggable,
touchState.isDragging,
touchState.xOffset,
swipeThreshold,
setTouchState,
next,
previous,
])
const handlePointerCancel = useCallback(
(event) => {
event.currentTarget.releasePointerCapture?.(event.pointerId)
resetTouchState()
},
[resetTouchState]
)

return (
<ul
className={classnames([styles.gallery__main, className])}
onPointerDown={draggable ? handlePointerDown : null}
onPointerMove={draggable ? handlePointerMove : null}
onPointerUp={draggable ? handlePointerUp : null}
onPointerCancel={draggable ? handlePointerCancel : null}
style={{ "--selected": activeIndex, "--total": galleryItems.length }}
{...props}
>
Expand Down
Loading