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

Stop dragging #500

Open
AnteroRamos opened this issue Jul 20, 2021 · 1 comment
Open

Stop dragging #500

AnteroRamos opened this issue Jul 20, 2021 · 1 comment

Comments

@AnteroRamos
Copy link

Hi,

I've noticed that stop dragging on ESC key up was implemented in the new version, but since that isn't usable yet, is there any other way to force stop drag on JS?

Thank you!

@zjffun
Copy link
Contributor

zjffun commented Jul 21, 2021

Dispatch drag:stop event can do this. (Notes: elements that have been moved can't be automatically reset.) For example:

<style>
  .container {
    display: flex;
  }
  .item {
    width: 100px;
    height: 100px;
    margin: 10px;
    background: violet;
  }
</style>

<div class="container">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/@shopify/draggable@1.0.0-beta.12/lib/draggable.bundle.js"></script>
<script>
  const draggable = new Draggable.Draggable(
    document.querySelectorAll(".container"),
    {
      draggable: ".item",
    }
  );

  const triggerMouseUpOnESC = (evt) => {
    if (evt.key === "Escape") {
      document.dispatchEvent(new Event("drag:stop"));
    }
  };

  draggable.on("drag:start", function (e) {
    document.addEventListener("keyup", triggerMouseUpOnESC);
  });

  draggable.on("drag:stop", function (e) {
    document.removeEventListener("keyup", triggerMouseUpOnESC);
  });
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants