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"
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
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
Snapping (grid and snap points)
snap: grid snapping — a positive integer sets the grid size in px, the literalgridmeans 8px. On release (and when an inertia glide settles) the offset is aligned to the nearest grid point, followed by one extraoas-dragcarrying 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 withinsnap-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
positionwrites stay exact, and theaxisconstraint still holds (snap-then-clamp never bypasses it). - Invalid
snap/snap-targets/snap-thresholdproduce onedevWarneach and disable snapping (dragging itself keeps working).
Grid snap=20 and snap points
Accessibility and interaction contract
- In whole-host mode the host is the focus stop (
tabindex="0", accessible name from i18ndrag.label, hint wired througharia-describedby); arrow keys nudge 1px, Shift+arrow 10px, constrained byaxisand the boundary - With
announceon, position changes are announced througharia-live="polite";disabledturns 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
clickthe 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
inertiaon, a single flick emits twooas-drag-endevents: one at pointerup, one when the glide settles (sourceispointerfor 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
| Attribute | Description | Type | Default |
|---|---|---|---|
announce | Announce position changes through aria-live="polite" (off by default) | boolean | — |
aria-label | Accessible name (defaults to the i18n drag.label) | string | — |
axis | Drag axis: both (default) / x horizontal only / y vertical only; invalid values fall back to both with a warning | string | both |
boundary | Drag boundary: viewport (default) or a CSS selector whose element rect is the boundary (a miss warns once and falls back to the viewport) | string | viewport |
disabled | Turn dragging off: pointer and keyboard are ignored and the focus stop is removed | boolean | — |
inertia | Decaying 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: reduce | boolean | — |
keyboard | Keyboard nudging: on by default (tabindex="0", 1px with arrow keys, 10px with Shift); write false to disable | string | — |
remember | Persist the position to localStorage and restore it on mount; silently degrades when unavailable (SSR / private mode / quota) | boolean | — |
snap | Snap 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 unaffected | string | — |
snap-targets | Snap 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 one | string | — |
snap-threshold | Snap point distance threshold (px, default 12); invalid values warn once and fall back to the default | string | — |
storage-key | Storage key for position memory (default oas-drag) | string | oas-drag |
Property (JS property; see each row for attribute reflection)
| Property | Description | Type | Default |
|---|---|---|---|
position | Current offset { x, y } relative to the initial layout position: get+set channel; assignment is clamped immediately and emits no events | DragPosition | string | — |
Methods
| Method | Signature | Description |
|---|---|---|
reset | reset(): void | Return to the initial position (offset zero) and clear the stored position |
Events
| Event | Description |
|---|---|
oas-drag | Dispatched on every position change (pointer drag / keyboard nudge / inertia), detail: { x, y, source } |
oas-drag-end | Dispatched 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-start | Dispatched once when a pointer drag passes the 4px threshold and captures the pointer, detail: { x, y, source } |
Slots
| Name | Description |
|---|---|
| default | Default slot: the wrapped floating content (also the drag surface when the whole host drags) |
handle | Drag handle: once it has content, only the handle starts a drag |