---
description: Draggable overlay — wrap any children into a draggable block with unified pointer dragging (a 4px threshold still counts as a click), boundary clamping (viewport or selector), axis restriction, optional inertia and position memory, plus arrow-key nudging and aria-live position announcements.
---

# 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.

<DemoBlock title="boundary=&quot;#dg-stage&quot;">
  <div
    id="dg-stage"
    style="position: relative; width: 100%; height: 240px; border: 1px dashed var(--oas-color-border); border-radius: var(--oas-radius-md); background: var(--oas-color-bg-hover)"
  >
    <oas-drag
      id="dg-button"
      boundary="#dg-stage"
      remember
      storage-key="oas-drag-demo-button"
      style="position: absolute; inset-block-end: 16px; inset-inline-end: 16px"
    >
      <oas-button type="primary">Support</oas-button>
    </oas-drag>
  </div>
  <span id="dg-button-out" style="display: block; color: var(--oas-color-text-secondary); font-size: var(--oas-font-size-sm)">Drag the support button (or Tab to it and nudge with the arrow keys)</span>
</DemoBlock>

## 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.

<DemoBlock title="handle slot">
  <div
    id="dg-stage-handle"
    style="position: relative; width: 100%; height: 220px; border: 1px dashed var(--oas-color-border); border-radius: var(--oas-radius-md)"
  >
    <oas-drag
      id="dg-panel"
      boundary="#dg-stage-handle"
      axis="both"
      style="position: absolute; inset-block-start: 12px; inset-inline-start: 12px; max-inline-size: 260px"
    >
      <div slot="handle" style="cursor: grab; padding: var(--oas-space-2); background: var(--oas-color-bg-hover); border-radius: var(--oas-radius-md) 0 0 var(--oas-radius-md)">⠿ Drag</div>
      <div style="padding: var(--oas-space-3); background: var(--oas-color-bg-elevated); border: 1px solid var(--oas-color-border); border-radius: var(--oas-radius-md)">
        <strong>Quick tools</strong>
        <p style="margin: var(--oas-space-1) 0 0; color: var(--oas-color-text-secondary); font-size: var(--oas-font-size-sm)">Only the handle on the left drags; the panel content stays clickable.</p>
      </div>
    </oas-drag>
  </div>
</DemoBlock>

## 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`.

<DemoBlock title="axis=&quot;x&quot; + inertia + announce">
  <div
    id="dg-stage-axis"
    style="position: relative; width: 100%; height: 120px; border: 1px dashed var(--oas-color-border); border-radius: var(--oas-radius-md); overflow: hidden"
  >
    <oas-drag
      id="dg-chip"
      boundary="#dg-stage-axis"
      axis="x"
      inertia
      announce
      style="position: absolute; inset-block-start: 36px; inset-inline-start: 12px"
    >
      <span style="display: inline-block; padding: var(--oas-space-2) var(--oas-space-4); background: var(--oas-color-primary); color: var(--oas-color-text-on-primary); border-radius: 999px">Drag horizontally</span>
    </oas-drag>
  </div>
  <span id="dg-chip-out" style="display: block; color: var(--oas-color-text-secondary); font-size: var(--oas-font-size-sm)"></span>
</DemoBlock>

## 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).

<DemoBlock title="Grid snap=20 and snap points">
  <div
    id="dg-stage-snap-en"
    style="position: relative; width: 100%; height: 220px; border: 1px dashed var(--oas-color-border); border-radius: var(--oas-radius-md); overflow: hidden"
  >
    <oas-drag
      id="dg-snap-grid-en"
      boundary="#dg-stage-snap-en"
      snap="20"
      style="position: absolute; inset-block-start: 16px; inset-inline-start: 16px"
    >
      <span style="display: inline-block; padding: var(--oas-space-2) var(--oas-space-3); background: var(--oas-color-bg-hover); border-radius: var(--oas-radius-md)">Grid 20px</span>
    </oas-drag>
    <oas-drag
      id="dg-snap-target-en"
      boundary="#dg-stage-snap-en"
      snap-targets='[{"x":200,"y":30},{"x":60,"y":60}]'
      snap-threshold="16"
      style="position: absolute; inset-block-start: 120px; inset-inline-start: 16px"
    >
      <span style="display: inline-block; padding: var(--oas-space-2) var(--oas-space-3); background: var(--oas-color-primary); color: var(--oas-color-text-on-primary); border-radius: var(--oas-radius-md)">Snap point (drag near it)</span>
    </oas-drag>
  </div>
  <span id="dg-snap-out-en" style="display: block; color: var(--oas-color-text-secondary); font-size: var(--oas-font-size-sm)"></span>
</DemoBlock>

## 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

<script setup>
import { onMounted } from 'vue'
import { onUpgraded } from '../../.vitepress/theme/when-defined'

onMounted(() => {
  const text = (id, value) => {
    const el = document.getElementById(id)
    if (el) el.textContent = value
  }
  const report = (id, label) => {
    const el = document.getElementById(id)
    if (!el) return
    onUpgraded(el, () => {
      text(`${id}-out`, `${label}: x ${el.position.x}, y ${el.position.y}`)
      el.addEventListener('oas-drag-end', (event) => {
        text(`${id}-out`, `${label} ended: x ${event.detail.x}, y ${event.detail.y} (${event.detail.source})`)
      })
      el.addEventListener('oas-drag', (event) => {
        if (event.detail.source !== 'keyboard') return
        text(`${id}-out`, `${label}: x ${event.detail.x}, y ${event.detail.y} (keyboard)`)
      })
    })
  }
  report('dg-button', 'Support button position')
  report('dg-chip', 'Horizontal position')
  // Snapping: report the released position (grid / snap point end value)
  const snapOut = document.getElementById('dg-snap-out-en')
  for (const [id, label] of [
    ['dg-snap-grid-en', 'Grid snap position'],
    ['dg-snap-target-en', 'Snap point position'],
  ]) {
    const el = document.getElementById(id)
    if (!el) continue
    onUpgraded(el, () => {
      el.addEventListener('oas-drag-end', (event) => {
        snapOut.textContent = `${label}: x ${event.detail.x}, y ${event.detail.y}`
      })
    })
  }
})
</script>

## 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 |
