diff --git a/.changeset/README.md b/.changeset/README.md new file mode 100644 index 0000000..654c6d4 --- /dev/null +++ b/.changeset/README.md @@ -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). diff --git a/.changeset/config.json b/.changeset/config.json new file mode 100644 index 0000000..5c58ec9 --- /dev/null +++ b/.changeset/config.json @@ -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": [] +} diff --git a/.changeset/seven-badgers-type.md b/.changeset/seven-badgers-type.md new file mode 100644 index 0000000..05d4949 --- /dev/null +++ b/.changeset/seven-badgers-type.md @@ -0,0 +1,5 @@ +--- +"@wethegit/react-gallery": patch +--- + +Fix gallery item getting stuck diff --git a/package-lock.json b/package-lock.json index 912b98b..1c7d72a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4372,6 +4372,7 @@ "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.1.tgz", "integrity": "sha512-qQKT4zQxXl8lLwBtHMWwaTcGfFOZviOJet3Oy/xmGk2gZH677CJM9EvtfdSkgWcATZhj/55JZ0rmy3myCT5lsA==", "dev": true, + "license": "MIT", "dependencies": { "argparse": "^2.0.1" }, diff --git a/src/lib/components/gallery-main.jsx b/src/lib/components/gallery-main.jsx index d0fa8b6..4cc2f16 100644 --- a/src/lib/components/gallery-main.jsx +++ b/src/lib/components/gallery-main.jsx @@ -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) => { @@ -85,39 +99,43 @@ 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 (