Show HN: A cool Drag-and-Drop implementation for Svelte
github.com
github.com
Drag and drop still seems like a mess. Browser support and touch support varies so you can't just use native APIs. Every popular library sits with tons of open issues and open PRs. Corporate solutions that have been open sourced appear abandoned. All the libraries I saw clocked in over 100kb.
Am I wrong? I want the "preact" or "day.js" drag and drop library. 10kb or less poly fill that gives me the basics. Does that exist?
To the library authors's credit, I think creating a general DnD library that can address most use cases is tough.
I would love to see your 100-200 lines if you are willing to share. Not asking you to ship it and support it and end up the N+1'th unsupported DND library though ;)
My implementation doesn't generalize at all really and I only need to support desktop browsers. If I ever own source this app I'll let you know. But basically I have one component listening to mouse events and firing callbacks with the rectangle the user has dragged (mouse down point to most recent mouse point), and with that my state manager updates things accordingly and drops upon mouse release.
I wrote my own as well, not in 100-200 lines granted. Dependency free ~300-400sloc using basic mouse events and element calculations were enough to implement advanced behaviors such as snapping, collision detection, free movement, packing, swapping, containerization. I had lots of freedom on how it behaves and was able to tweak it to what I wanted, something probably impossible with general dnd libraries. It works the same in most modern browsers I've tested, since it's only using DOM apis for basic events and style modification.
Libraries are great if you need established algorithms and data structures. Building out custom UX interactions, presumably like interacting with DOM objects, DIY was easier since there's actually very little work to do.
Edit: I have implementation for React and Vue but not for Svelet. So if anyone is interested let me know.
And congrats to Isaac for this nice work dedicated to Svelte community.
Its core + plugin model will likely be the closest you can get to bundle size minimization.
Until the situation improves, for production, I use react-beautiful-dnd.
Long term, I think this might actually be a good problem to be solved in web components (e.g., using something like Stencil), then output targets for React, Svelte, Angular, etc can be generated.
I wouldn't say "all" the major frameworks, though (just Angular[JS] and React).
There are also a couple more transitive dependencies.
But the 9.8kB figure does include all the dependencies.
I actively hate spending bytes supporting IE (and especially such ancient versions as IE7), and if necessary maintain forks of any libraries I use to avoid doing so; so I tried ripping out IE support along with crossvent and a few obsolete and/or bad abstractions, and quickly got it down to 7kB minified, and I expect one so inclined could shrink it further. (The crossvent part can be done in two minutes, it’s that simple.)
I should really make a feature of this kind of thing on my blog, I enjoy doing it so much.
https://github.com/isaacHagoel/svelte-dnd-action#accessibili...
- You need to wait a bit for a drop target to become active, so when you try to move items quickly it's not registered and snaps back to the start location, is it something that is intended ?
- The animation is smooth with Edge but a bit janky with Firefox, as if it was animated at 10fps or so. It's probably Firefox's fault honestly. Also the border on dragged items is not shown with Firefox.
IIRC there is no other option if you want to include animation in a GitHub readme, as it will only include image formats inline that way. Otherwise you could use a video, which for this content would likely be larger but would have the advantage of being able to not auto-play.
Of course "don't include animation at all" would be a valid opinion, though not one likely to meet with universal agreement.
Naive conversion will result in something much larger than the gif so not address the original complaint of "large chunk of auto-download/play content on the readme page".
> IIRC there is no other option if you want to include animation in a GitHub readme
Not the parent.
In any case, I wonder if it can be done with an embedded `video` tag. Maybe if the video is data URI encoded it could work?
It works with stateful components too.