Draggable Elements PURE HYPERSCRIPT

Reusable behavior with parameters and custom events
This example shows how to create a reusable "Draggable" behavior that can be installed on any element, with support for custom drag handles, event hooks, and smooth positioning.
behavior install measure custom events halt event
Instructions: Drag the boxes by their title bars. Notice the shadow effect during dragging!
Draggable Item 1
Drag me by the title bar!
Draggable Item 2
I'm draggable too!
Draggable Item 3
Move me around!

The Behavior Definition

behavior Draggable(dragHandle) init if no dragHandle set the dragHandle to me end on pointerdown(clientX, clientY) from dragHandle halt the event -- Prevent text selection trigger draggable:start -- Custom event hook measure x set startX to it measure y set startY to it set xoff to clientX - startX set yoff to clientY - startY repeat until event pointerup from document wait for pointermove(clientX, clientY) or pointerup(clientX, clientY) from document add { left: ${clientX - xoff}px; top: ${clientY - yoff}px; } trigger draggable:move -- During drag end trigger draggable:end -- Drag complete end end

Usage Example

<div class="draggable-item" _="install Draggable(dragHandle: .titlebar in me) on draggable:start add .dragging on draggable:end remove .dragging"> <div class="titlebar">Draggable Item</div> <div class="content">Drag me!</div> </div>

How it works:

  1. Install behavior: install Draggable(dragHandle: .titlebar in me)
  2. Initialize: If no drag handle specified, use the element itself
  3. Capture start: On pointerdown, measure current position and calculate offset
  4. Track movement: Loop until pointerup from document, updating position on each pointermove
  5. Custom events: Trigger draggable:start, draggable:move, draggable:end for hooks
  6. CSS injection: Use template strings to add position styles directly

Advanced Concepts:

Custom Event Hooks:

Try it yourself:

Key insight about event flow: The behavior listens for pointer events from document rather than just from dragHandle. This ensures the drag continues smoothly even when the pointer moves quickly and temporarily leaves the drag handle's bounds. The halt the event on pointerdown prevents text selection during drag.
Note about behaviors: The behavior feature requires full hyperscript implementation. If dragging doesn't work yet, it indicates the behavior system needs implementation. The behavior code above is defined in a <script type="text/hyperscript"> tag at the bottom of this page.