Skip to content
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

fix: prevent stop or pause when drag constraints #2758

Open
wants to merge 5 commits 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
3 changes: 2 additions & 1 deletion dev/react/src/tests/drag-ref-constraints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const App = () => {
const [dragging, setDragging] = useState(false)
const params = new URLSearchParams(window.location.search)
const layout = params.get("layout") || undefined
const elastic = Number(params.get("elastic")) || 0

// We do this to test when scroll position isn't 0/0
useLayoutEffect(() => {
Expand All @@ -27,7 +28,7 @@ export const App = () => {
id="box"
data-testid="draggable"
drag
dragElastic={0}
dragElastic={elastic}
dragMomentum={false}
style={{
width: 50,
Expand Down
25 changes: 23 additions & 2 deletions packages/framer-motion/cypress/integration/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,27 @@ describe("Drag", () => {
})
})

it("reset drag constraints (ref-based), when click after dragging", () => {
cy.visit("?test=drag-ref-constraints&elastic=1")
.wait(200)
.get("[data-testid='draggable']")
.trigger("pointerdown", 10, 10)
.wait(200)
.trigger("pointermove", 300, 300, { force: true })
.wait(200)
.trigger("pointerup", { force: true })
.trigger("pointerdown", { force: true })
.trigger("pointerup", { force: true })
.wait(1000)
.should(($draggable: any) => {
const draggable = $draggable[0] as HTMLDivElement
const { left, top } = draggable.getBoundingClientRect()

expect(left).to.equal(150)
expect(top).to.equal(150)
})
})

Comment on lines +231 to +251
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I append E2E test for issue #2697

I'm wondering if it's appropriate to set wait for 1000ms 🤔

it("doesn't reset drag constraints (ref-based), while dragging, on unrelated parent component updates", () => {
cy.visit("?test=drag-ref-constraints")
.wait(200)
Expand All @@ -242,8 +263,8 @@ describe("Drag", () => {
const draggable = $draggable[0] as HTMLDivElement
const { left, top } = draggable.getBoundingClientRect()

expect(left).to.equal(150)
expect(top).to.equal(150)
expect(left).to.equal(200)
expect(top).to.equal(200)
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ export class VisualElementDragControls {
if (presenceContext && presenceContext.isPresent === false) return

const onSessionStart = (event: PointerEvent) => {
const { dragSnapToOrigin } = this.getProps()
const { dragSnapToOrigin, dragConstraints } = this.getProps()

// Stop or pause any animations on both axis values immediately. This allows the user to throw and catch
// the component.
dragSnapToOrigin ? this.pauseAnimation() : this.stopAnimation()
// Stop or pause any animations on both axis values immediately when not using dragConstraints.
// This allows the user to throw and catch the component.
if (!dragConstraints) {
dragSnapToOrigin ? this.pauseAnimation() : this.stopAnimation()
}

if (snapToCursor) {
this.snapToCursor(extractEventInfo(event, "page").point)
Expand Down