Skip to content

fix: clean up isPressing state after parent is dragged #3152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions packages/framer-motion/src/gestures/__tests__/press.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,35 @@ describe("press", () => {
expect(press).toBeCalledTimes(1)
})

test("press event listeners do fire after drag gesture on parent element", async () => {
const press = jest.fn()
const Component = () => (
<MockDrag>
<motion.div drag data-testid="parent">
<motion.div onTap={() => press()} data-testid="child" />
</motion.div>
</MockDrag>
)

const { getByTestId, rerender } = render(<Component />)
rerender(<Component />)

// First, perform a drag gesture on the parent element
const childElement = getByTestId("child")
const parentElement = getByTestId("parent")
const pointer = await drag(parentElement, childElement).to(100, 100)
pointer.end()
await nextFrame()

// Now try to tap the child element
pointerDown(childElement)
pointerUp(childElement)
await nextFrame()

// The tap event should fire
expect(press).toBeCalledTimes(1)
})

test("press event listeners unset", async () => {
const press = jest.fn()
const Component = () => <motion.div onTap={() => press()} />
Expand Down
8 changes: 5 additions & 3 deletions packages/motion-dom/src/gestures/press/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ export function press(
window.removeEventListener("pointerup", onPointerUp)
window.removeEventListener("pointercancel", onPointerCancel)

if (!isValidPressEvent(endEvent) || !isPressing.has(target)) {
return
if (isPressing.has(target)) {
isPressing.delete(target)
}

isPressing.delete(target)
if (!isValidPressEvent(endEvent)) {
return
}

if (typeof onPressEnd === "function") {
onPressEnd(endEvent, { success })
Expand Down