@phx-hook/drag-drop-detector
Version:
Change element classes on drag and drop events
54 lines (34 loc) • 1.18 kB
Markdown
Change element classes on drag and drop events.
[](https://phx-hook.elixir-saas.com/drag-drop-detector)
```js
import DragDropDetectorHook from "@phx-hook/drag-drop-detector";
const hooks = {
DragDropDetector: DragDropDetectorHook({ activeClass: "drag-active" }),
};
let liveSocket = new LiveSocket("/live", Socket, { hooks, ... });
```
```heex
<div id="drop_area" phx-hook="DragDropDetector">
...
</div>
```
* `activeClass`: Class to be added to the element when it is dragged over. Defaults to `"drag-active"`.
* `data-drag-target`: Set to `"window"` to detect dragging anywhere in the window, instead of just over the hook element. Useful for creating a global drag & drop upload experience.
You might find it helpful to add Tailwind variants for various drop states.
Using Tailwind v4:
```css
/* In assets/css/app.css */
@custom-variant drag-active (.drag-active&, .drag-active &);
```
Using Tailwind v3:
```js
// In assets/tailwind.config.js
plugins: [
plugin(({addVariant}) => addVariant("drag-active", [".drag-active&", ".drag-active &"])),
]
```