0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Make an element draggable with the HTML Drag and Drop API. Covers the default auto state, the event lifecycle, VB's drag state styling hooks, and the keyboard and single-pointer alternatives every drag needs.
Overview
The draggableattribute controls whether the user can pick an element up with the pointer and drag it, using the browser's Drag and Drop API. It only starts the drag: you write the event listeners that accept a drop and do something with it.
Dragging is a pointer gesture. Every drag-and-drop interface also needs a keyboard way and a single-click way to do the same thing. For reordering lists, VB's <drag-surface> provides all three.
Applies to: any HTML element (it is a global attribute).
<ulid="todo"><lidraggable="true"id="task-1">Write release notes <buttontype="button"class="ghost"data-done="task-1">Mark done</button></li><lidraggable="true"id="task-2">Update screenshots <buttontype="button"class="ghost"data-done="task-2">Mark done</button></li></ul><ulid="done"></ul><paria-live="polite"id="status"></p><scripttype="module">const done = document.getElementById('done');const status = document.getElementById('status');functioncomplete(item) { item.removeAttribute('draggable'); item.querySelector('button')?.remove(); done.append(item); status.textContent = `${item.textContent.trim()} moved to Done.`; }document.addEventListener('dragstart', (e) => {const item = e.target.closest?.('[draggable="true"]');if (!item) return; e.dataTransfer.setData('text/plain', item.id); e.dataTransfer.effectAllowed = 'move'; item.toggleAttribute('data-dragging', true); // VB styles this });document.addEventListener('dragend', (e) => { e.target.removeAttribute?.('data-dragging'); }); done.addEventListener('dragover', (e) => { e.preventDefault(); // allow the drop e.dataTransfer.dropEffect = 'move'; done.toggleAttribute('data-drag-over', true); // VB styles this }); done.addEventListener('dragleave', () => done.removeAttribute('data-drag-over')); done.addEventListener('drop', (e) => { e.preventDefault(); done.removeAttribute('data-drag-over');const item = document.getElementById(e.dataTransfer.getData('text/plain'));if (item) complete(item); });// The same result without draggingdocument.addEventListener('click', (e) => {const button = e.target.closest('[data-done]');if (button) complete(document.getElementById(button.dataset.done)); });</script>
Values
Value
Meaning
true
The element can be dragged.
false
The element can't be dragged. Use it to stop images and links being dragged.
Absent, empty or any other value
auto (the default): the browser decides. Images, links with an href and selected text are draggable; other elements are not.
It isn't inherited: an element inside a draggable="true" parent moves with the parent but isn't draggable on its own. It is an enumerated attribute, so write draggable="true"; a bare draggable means auto.
The event lifecycle
Event
Fires on
When
dragstart
Dragged element
The drag begins. Put data in dataTransfer here.
drag
Dragged element
Repeatedly while dragging.
dragenter
Element under the pointer
The drag enters it.
dragover
Element under the pointer
Repeatedly while over it. Call preventDefault() to allow a drop.
dragleave
Element under the pointer
The drag leaves it.
drop
Drop target
The user drops on an element that allowed it. Read the data here.
dragend
Dragged element
The drag ends, by a drop or a cancel (Escape). Clean up here.
The browser refuses drops by default. Without preventDefault() in dragover, drop never fires.
Examples
Reorder with <drag-surface>
Children with draggable="true" inside a <drag-surface> can be dragged, moved with the keyboard (Space or Enter to grab, arrow keys to move, again to drop), and with data-move-controls, moved with per-item buttons. Every move is announced in a live region.
drag-surface
123456
<h3id="col-backlog">Backlog</h3><drag-surfacegroup="sprint"aria-labelledby="col-backlog"data-move-controlsdata-layout="stack"data-layout-gap="xs"><articledraggable="true"data-id="story-1">Password reset flow</article><articledraggable="true"data-id="story-2">Export to CSV</article><articledraggable="true"data-id="story-3">Dark mode audit</article></drag-surface>
Carrying data
dataTransfer can hold several formats at once; a target reads the one it understands. Files dragged in from the operating system arrive in dataTransfer.files.
src/native-elements/draggable/styles.css styles drag state for every page, not only inside <drag-surface>. Set the same attributes from your own drag code to get the same look:
Selector
Style
Set by
[draggable="true"]
cursor: grab (grabbing while pressed), user-select: none, a 2px --color-interactive:focus-visible outline
You
[data-dragging]
Faded to 40% and scaled down slightly
The item being dragged
[data-drag-over]
Dashed --color-interactive outline and a light tint
A drop target under the pointer
[data-drop-target="before|after"]
A 3px insertion bar on that edge
The item next to the drop position
[data-just-dropped]
A brief ring flash
The item that just landed
[data-reorder-mode] [data-grabbed]
Outline and shadow
The item grabbed with the keyboard
Under reduced motion the scale and the flash are switched off. <drag-surface> sets all of these itself, makes each item focusable (tabindex="0"), supports drag handles (data-drag-handle), horizontal lists and transfers between surfaces that share a group, and fires drag-surface:reorder and drag-surface:transfer events.
Accessibility
Drag and drop is pointer-only. The API has no keyboard support, so provide another way to do the same thing (WCAG 2.1.1 Keyboard). It doesn't have to be dragging: a "Move to" button or menu is fine.
Offer a single-pointer alternative too, for people who can click but can't hold and drag (WCAG 2.5.7 Dragging Movements). <drag-surface data-move-controls> renders one per item.
draggable doesn't make an element focusable. If items take keyboard commands, add tabindex="0" (drag-surface does) or put a button inside each.
Announce the result in an aria-live="polite" region: what moved and where it ended up ("Export to CSV, moved to position 2 of 3").
Don't use aria-grabbed or aria-dropeffect. Both were deprecated in ARIA 1.1 and screen readers don't support them. Use the live region instead.
Gotchas
Protected drag data. Outside dragstart and drop (so during dragenter and dragover too) getData() returns nothing, for every drag. Only dataTransfer.types is available, so decide whether to accept a drop from the types. The data itself is readable in drop.
dragleave fires when entering a child. Moving from a drop zone onto an element inside it fires dragleave on the zone. Check e.relatedTarget or count enters and leaves before removing your highlight.
Touch is unreliable. Mobile browsers differ in whether and how a long press starts an HTML drag. Don't let touch users depend on it; the button alternative covers them.
The drag image is a snapshot. CSS can't style it while it moves. Use dataTransfer.setDragImage() for a custom one.
user-select: none on [draggable="true"] means the text in a draggable item can't be selected. Put selectable text outside it, or use a drag handle.
Related
<drag-surface> — accessible reordering and transfer between lists.
data-upload — drop files from the desktop onto a file input.
data-sortable — sort a list or table by a field instead of by hand.
<drag-surface> — any list or board the user rearranges. Keyboard, move buttons and announcements are built in.
draggable="true" with your own listeners — other drag interactions (drag to a bin, to a folder, onto a canvas). Reuse VB's state attributes for styling, and build the keyboard and button paths yourself.
data-upload — files dragged in from outside the page.
data-sortable — when the order follows a field (date, name, price), let users pick the field rather than drag.