Skip to content

Drag Draggable overlay ​

Wrap any children into a draggable floating block: pointer dragging (mouse, touch and pen alike), boundary clamping, optional inertia and position memory, and arrow-key nudging. Typical use cases are a floating support button or a floating tool block.

Basic usage (floating support button) ​

The whole host is the drag surface by default; boundary narrows the allowed area (the demo stage here — a real page can use viewport). Moves under 4px count as a click, so the button still works.

boundary="#dg-stage"

Support
Drag the support button (or Tab to it and nudge with the arrow keys)

Handle mode (tool block) ​

With a handle slot only the handle starts a drag; the rest of the block keeps its own interactions. The handle content carries focus, so the host no longer takes a Tab stop.

handle slot

⠿ Drag
Quick tools

Only the handle on the left drags; the panel content stays clickable.

Axis, inertia and announcements ​

axis="x" restricts dragging to the horizontal axis; inertia glides after release (velocity decays 0.95 per frame for up to 60 frames, stopping instantly at the boundary; a last velocity sample older than 100ms counts as stopped, so no glide; instant stop under prefers-reduced-motion: reduce); announce reports position changes through aria-live.

axis="x" + inertia + announce

Drag horizontally

Snapping (grid and snap points) ​

  • snap: grid snapping — a positive integer sets the grid size in px, the literal grid means 8px. On release (and when an inertia glide settles) the offset is aligned to the nearest grid point, followed by one extra oas-drag carrying the snapped value.
  • snap-targets: a JSON array of snap points [{ x, y }] (offset coordinates relative to the initial layout position). On release, if the nearest point is within snap-threshold (default 12px) the offset snaps exactly to it; while dragging near a point a hint ring appears (part="snap-hint", dashed primary circle, purely visual and hit-test transparent).
  • Snapping only applies to pointer release points: keyboard nudges (1px / Shift 10px) and position writes stay exact, and the axis constraint still holds (snap-then-clamp never bypasses it).
  • Invalid snap / snap-targets / snap-threshold produce one devWarn each and disable snapping (dragging itself keeps working).

Grid snap=20 and snap points

Grid 20pxSnap point (drag near it)

Accessibility and interaction contract ​

  • In whole-host mode the host is the focus stop (tabindex="0", accessible name from i18n drag.label, hint wired through aria-describedby); arrow keys nudge 1px, Shift+arrow 10px, constrained by axis and the boundary
  • With announce on, position changes are announced through aria-live="polite"; disabled turns dragging off and removes the focus stop
  • Pointer moves under 4px are treated as clicks (no pointer capture, no default prevention), so buttons and inputs inside the content keep working
  • When a drag passes the threshold, the click the browser fires afterwards is swallowed (releasing over a child button does not trigger it); the next normal click works as usual. pointercancel (scroll / gesture takeover) only ends the drag — no position is saved and no inertia starts
  • With inertia on, a single flick emits two oas-drag-end events: one at pointerup, one when the glide settles (source is pointer for both); a glide preempted by a new pointerdown has no glide-tail emission. Scenarios that need a single "ended" signal should act on the second arrival, or deduplicate within a short time window
  • In handle mode only the handle starts a drag; inputs inside the content keep their own arrow-key semantics

API ​

oas-drag ​

Attributes ​

AttributeDescriptionTypeDefault
announceAnnounce position changes through aria-live="polite" (off by default)boolean—
aria-labelAccessible name (defaults to the i18n drag.label)string—
axisDrag axis: both (default) / x horizontal only / y vertical only; invalid values fall back to both with a warningstringboth
boundaryDrag boundary: viewport (default) or a CSS selector whose element rect is the boundary (a miss warns once and falls back to the viewport)stringviewport
disabledTurn dragging off: pointer and keyboard are ignored and the focus stop is removedboolean—
inertiaDecaying glide after release (velocity decays 0.95 per frame for up to 800ms of real time — consistent feel on 60/120Hz screens, stopping at the boundary or when speed falls below the threshold; a last velocity sample older than 100ms counts as stopped, so no glide); instant stop under prefers-reduced-motion: reduceboolean—
keyboardKeyboard nudging: on by default (tabindex="0", 1px with arrow keys, 10px with Shift); write false to disablestring—
rememberPersist the position to localStorage and restore it on mount; silently degrades when unavailable (SSR / private mode / quota)boolean—
snapSnap grid size: a positive integer is the grid in px, the literal grid means 8px; on release (including the inertia tail) the offset aligns to the nearest grid point; keyboard nudges are unaffectedstring—
snap-targetsSnap points JSON array [{ x, y }] (offsets relative to the initial layout position): releasing within snap-threshold of the nearest point snaps exactly to it; a hint ring (part="snap-hint") appears while dragging near onestring—
snap-thresholdSnap point distance threshold (px, default 12); invalid values warn once and fall back to the defaultstring—
storage-keyStorage key for position memory (default oas-drag)stringoas-drag

Property (JS property; see each row for attribute reflection) ​

PropertyDescriptionTypeDefault
positionCurrent offset { x, y } relative to the initial layout position: get+set channel; assignment is clamped immediately and emits no eventsDragPosition | string—

Methods ​

MethodSignatureDescription
resetreset(): voidReturn to the initial position (offset zero) and clear the stored position

Events ​

EventDescription
oas-dragDispatched on every position change (pointer drag / keyboard nudge / inertia), detail: { x, y, source }
oas-drag-endDispatched when a pointer drag ends, detail: { x, y, source, phase }; with inertia a single flick emits twice — phase: 'release' at pointerup and phase: 'coast-end' when the glide settles; a coast preempted by a new pointerdown saves the current position without a glide-tail emission
oas-drag-startDispatched once when a pointer drag passes the 4px threshold and captures the pointer, detail: { x, y, source }

Slots ​

NameDescription
defaultDefault slot: the wrapped floating content (also the drag surface when the whole host drags)
handleDrag handle: once it has content, only the handle starts a drag

Released under the MIT OR Apache-2.0 License.