svelte-dnd-action: Transitions not working in combination with drag and drop

When you have a draggable list that has transitions, as soon as you drag an item, it is removed from the list. You can still drag it around, but it’s impossible to get it back into the list. I’d like to have transitions for adding items to and deleting items from the draggable list.

How to reproduce

Go to the Svelte REPL and enter the following code:

<style>
	div {
		height: 1.5em;
		width: 10em;
		text-align: center;
		border: 1px solid black;
		margin: 0.2em;
		padding: 0.3em;
	}
</style>
<script>
	import {dndzone} from 'svelte-dnd-action';
	import {fade} from 'svelte/transition';
	function handleSort(e) {
		items = e.detail.items;
	}
	let items = [
		{id:1, title: 'I'},
		{id:2, title: 'Am'},
		{id:3, title: 'Yoda'}
	];
</script>
<section use:dndzone={{items}} on:consider={handleSort} on:finalize={handleSort}>
	{#each items as item(item.id)}
		<div transition:fade>
			{item.title}	
		</div>
	{/each}
</section>

(Code based on this article.)

See that the item disappears from the list as soon as you start dragging and that it is impossible to put it back in the list.

When starting to drag, this error appears in the console:

action.js:360 Uncaught (in promise) TypeError: Cannot read property 'hasOwnProperty' of undefined
    at configure (action.js:360)
    at update (action.js:380)
    at Object.update [as p] (PlayerList.svelte:13)
    at update (index.mjs:730)
    at flush (index.mjs:699)

(The error is from my personal project, not from the REPL, because the REPL console doesn’t show the source files and line numbers as clearly.)

When you remove transition:fade from the item div, everything is working perfectly again.

My (totally uneducated) guess is that the transition removes the element from the DOM, while svelte-dnd-action does not know about this. Could this be the case? Is it possible to make this work?

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 1
  • Comments: 16 (7 by maintainers)

Most upvoted comments

I was able to wrap both the in and out (send and receive from crossfade) transitions in conditional functions that only run if the data source change was remote, and not local (i.e. not from svelte-dnd-action). Wrapping just the out/send transition didn’t seem to be enough to fix the errors, so I did both transitions. Something along the lines of this:

let boardOrigin: 'local' | 'remote' = 'remote';

const conditionalSend = (node: any, params: CrossfadeParams & { key: any }): () => TransitionConfig => {
	if (boardOrigin === 'remote') {
		return send(node, params);
	}

	return () => { return {} };
};

There are still 2 remaining issues I can see to deal with

  1. Disabling new DnD events while a transition is running (should be easy enough)
  2. Handling remote data source updates while mid-DnD. I think the easiest option would be to simply cancel the current dragging (and don’t run finalize), but I’m not sure if that is currently a possibility with this library? Any thoughts?

Edit: Actually the second issue isn’t really related to transitions. I’ll see if this topic of updating the data source mid-drag has been discussed elsewhere.

Thanks

@EmielH it gets hairy when state changes are involved. I was referring mainly to this: https://github.com/sveltejs/svelte/pull/4999 see this tweetstorm which is also linked from the PR for more details