Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

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…

Read the release notes

draggable

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 draggable attribute 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).

Values

ValueMeaning
trueThe element can be dragged.
falseThe element can't be dragged. Use it to stop images and links being dragged.
Absent, empty or any other valueauto (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

EventFires onWhen
dragstartDragged elementThe drag begins. Put data in dataTransfer here.
dragDragged elementRepeatedly while dragging.
dragenterElement under the pointerThe drag enters it.
dragoverElement under the pointerRepeatedly while over it. Call preventDefault() to allow a drop.
dragleaveElement under the pointerThe drag leaves it.
dropDrop targetThe user drops on an element that allowed it. Read the data here.
dragendDragged elementThe 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.

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.

Stopping default drags

In Vanilla Breeze

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:

SelectorStyleSet by
[draggable="true"]cursor: grab (grabbing while pressed), user-select: none, a 2px --color-interactive :focus-visible outlineYou
[data-dragging]Faded to 40% and scaled down slightlyThe item being dragged
[data-drag-over]Dashed --color-interactive outline and a light tintA drop target under the pointer
[data-drop-target="before|after"]A 3px insertion bar on that edgeThe item next to the drop position
[data-just-dropped]A brief ring flashThe item that just landed
[data-reorder-mode] [data-grabbed]Outline and shadowThe 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.
  • tabindex — make items reachable by keyboard.

Which to choose

  • <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.
↑