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

position property is not biding when the options object is in the script (svelte version) #122

Open
amccampos opened this issue Feb 23, 2023 · 1 comment

Comments

@amccampos
Copy link

The example in the docs explaining the position property works fine (link).
However, when the options object is passed, the dragged element doesn't update programmatically.

For instance, the code below does not make the dragged element to get back to its original position when released.

<script>
  import { draggable } from '@neodrag/svelte'
  let position = { x: 0, y: 0 };
  let options = {
    position,
    onDrag: ({ offsetX, offsetY }) => {
      position = { x: offsetX, y: offsetY };
    },
    onDragEnd: ({ offsetX, offsetY }) => {
      position = { x: 0, y: 0 };
    }
  }
</script>

<div class="drag" use:draggable={options}>
  I can be moved with the slider too
</div>

X: <input type="range" min="0" max="300" bind:value={position.x} />
Y: <input type="range" min="0" max="300" bind:value={position.y} />

However, the code below does.

<script>
  import { draggable } from '@neodrag/svelte'
  let position = { x: 0, y: 0 }
</script>

<div class="drag" use:draggable={{
  position,
  onDrag: ({ offsetX, offsetY }) => {
    position = { x: offsetX, y: offsetY };
  },
  onDragEnd: ({ offsetX, offsetY }) => {
    position = { x: 0, y: 0 }
  }
}}>
  I can be moved with the slider too
</div>

X: <input type="range" min="0" max="300" bind:value={position.x} />
Y: <input type="range" min="0" max="300" bind:value={position.y} />

Version: 2.0.3
Framework: Svelte

@amccampos amccampos changed the title position property is not biding when the options object is on script (svelte version) position property is not biding when the options object is in the script (svelte version) Feb 23, 2023
@PuruVJ
Copy link
Owner

PuruVJ commented Mar 20, 2023

It is because your options variable isn't watching position. Instead of let options, use $: options.

Let me know if that helps :)

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