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

Examples in plain Javascript please #10

Open
nevf opened this issue Jul 20, 2018 · 6 comments
Open

Examples in plain Javascript please #10

nevf opened this issue Jul 20, 2018 · 6 comments

Comments

@nevf
Copy link

nevf commented Jul 20, 2018

All the examples, which are great by the way, use React and I am not a React person. Further my attempt at getting smooth-dnd to work in a plain JS app didn't get very far. The entire containerElement dragged.

It appears as though the React etc. libraries also include various CSS which isn't discussed in the docs. Also smooth-dnd inserts its own wrapper element, which isn't mentioned and can beak existing markup.

So is it possible to get a complete example or two in plain JS without using React, Vue etc.

Thanks.

@nevf
Copy link
Author

nevf commented Jul 21, 2018

I've made some progress today, however I've hit a stumbling block. In my use case the draggable elements inside a container are dynamically added and removed. This doesn't work as smooth-dnd expects a static set if draggable elements which are defined before it is called.

I've tried various things including using .dispose() but without success.

To resolve this it would be great to see and API exposed for dynamically updating the list of draggable elements. I would think this is a reasonably common requirement.

Thanks.

@dgvinay
Copy link

dgvinay commented Oct 22, 2018

@nevf Can you guide me on how to use this library in plain JavaScript? Few lines of sample code would be helpful. Thanks.

@nevf
Copy link
Author

nevf commented Oct 22, 2018

This should help:

import SmoothDnD from 'smooth-dnd';

    SmoothDnD( document.querySelector( some_container_element ),
               // options
               { dragHandleSelector: ...
                dragClass: ...
                dropClass: ...
               }
    )

I'm using SmoothDND to enable Tabs to be rearranged in our Web App Clibu and plan to use it for other UI elements. You can see a gif of it on our Blog.

@stuta
Copy link

stuta commented Apr 19, 2019

Every draggable element must have smooth-dnd-draggable-wrapper class in it. It took me a long time to figure it out.

Html page below works in new browsers that support dynamic import (Chrome, Safari). But moving items will cause an error:
TypeError: Argument 1 ('node') to Node.appendChild must be an instance of Node.

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<meta name="viewport" content="width=device-width,initial-scale=1.0" />
	<base href="" />
	<title>smooth-dnd test</title>
</head>

<body>
	<div id="container">
		<div class='smooth-dnd-draggable-wrapper'>Draggable 1</div>
		<div class='smooth-dnd-draggable-wrapper'>Draggable 2</div>
		<div class='smooth-dnd-draggable-wrapper'>Draggable 3</div>
	</div>
	<script type="module">
		import('./node_modules/smooth-dnd/dist/index.js').then(() => {
			var options = {}
			var containerElement = document.getElementById('container');
			var container = SmoothDnD.default(containerElement, options);
			console.debug(container)
		}).catch((err) => {
			alert(`smooth-dnd dynamic import failed with error: ${err}`)
		})
	</script>
</body>

</html>

@stuta
Copy link

stuta commented Apr 19, 2019

This works also in Firefox:

	<script type="module">
		import './node_modules/smooth-dnd/dist/index.js'
		var options = {}
		var containerElement = document.getElementById('container');
		var container = SmoothDnD.smoothDnD(containerElement, options);
		console.debug('SmoothDnD container:', container)
	</script>

@David-Carty
Copy link

Looks great, but totally painful if you just want to keep things simple and use vanilla JS without the module syntax, how hard is this to make available via a CDN?

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

4 participants