Drag

drag makes an element follow the pointer. drag="x" or drag="y" keeps it to one axis. On release it carries on at the speed it was let go, a Fling, and slows to a stop.

Live demo
drag/solid2.tsx
import { domAnimation, drag } from "@wardic/solid"
import { elements, makeWard } from "@wardic/solid/hooks"

// This page drags: its own Wardic instance brings `drag`, so pages that don't never load it.
const { w } = makeWard({ features: [domAnimation, drag], elements })

/** A ball held inside its arena, and one that drags across and springs back. */
export default function Drag() {
  let arena!: HTMLDivElement
  return (
    <div class="sample-arena" ref={arena}>
      <w.div class="sample-ball" drag dragConstraints={() => arena} dragElastic={0.2} whileDrag={{ scale: 1.15 }} />
      <w.div class="sample-ball sample-ball-alt" drag="x" dragSnapToOrigin whileDrag={{ scale: 1.15 }} />
    </div>
  )
}
drag/solid1.tsx
import { createWardic, domAnimation, drag } from "@wardic/solid/v1"

// This page drags: its own Wardic instance brings `drag`, so pages that don't never load it.
const { w } = createWardic([domAnimation, drag])

/** A ball held inside its arena, and one that drags across and springs back. */
export default function Drag() {
  let arena!: HTMLDivElement
  return (
    <div class="sample-arena" ref={arena}>
      <w.div class="sample-ball" drag dragConstraints={() => arena} dragElastic={0.2} whileDrag={{ scale: 1.15 }} />
      <w.div class="sample-ball sample-ball-alt" drag="x" dragSnapToOrigin whileDrag={{ scale: 1.15 }} />
    </div>
  )
}
drag/foldkit.ts
import { DomLive } from "@wardic/dom"
import type { Html, HtmlBuilder } from "foldkit/html"
import { Features, Wardic } from "@wardic/foldkit"

// This app drags: its Handle names `drag` among its Features. An app has one Handle, so its Features
// are the app's; one that doesn't drag leaves `drag` out and never loads it.
const wardic = Wardic.make({ env: DomLive, features: [Features.domAnimation, Features.drag] })

/** A ball held inside its arena, and one that drags across and springs back. */
export const view = <Message,>(h: HtmlBuilder<Message>): Html => {
  const w = wardic.html(h)
  return h.div([h.Class("sample-arena")], [
    w.div([h.Class("sample-ball"), w.Drag(), w.DragConstraints("parent"), w.DragElastic(0.2), w.WhileDrag({ scale: 1.15 })]),
    w.div([h.Class("sample-ball sample-ball-alt"), w.Drag("x"), w.DragSnapToOrigin(), w.WhileDrag({ scale: 1.15 })]),
  ])
}
drag/motion.tsx
import { domMax, LazyMotion } from "motion/react"
import * as m from "motion/react-m"
import { useRef } from "react"

/** A ball held inside its arena, and one that drags across and springs back. */
export default function Drag() {
  const arena = useRef<HTMLDivElement>(null)
  return (
    // This page drags, so it brings `domMax`, Motion's drag and layout, itself: pages that don't never load it.
    <LazyMotion features={domMax}>
      <div className="sample-arena" ref={arena}>
        <m.div className="sample-ball" drag dragConstraints={arena} dragElastic={0.2} whileDrag={{ scale: 1.15 }} />
        <m.div className="sample-ball sample-ball-alt" drag="x" dragSnapToOrigin whileDrag={{ scale: 1.15 }} />
      </div>
    </LazyMotion>
  )
}

The Feature

Dragging is the drag Feature. A page that drags makes a Wardic instance with it, makeWard({ features: [domAnimation, drag], elements }) (on Solid 1, createWardic([domAnimation, drag])), as the demo above does, so the pages that don't never load its code.

Constraints

dragConstraints keeps the element inside a box, { left, right, top, bottom } in pixels from where it started, or inside another element, given as an accessor (() => arena). dragElastic, 0 to 1, is how far it can be pulled past them; it springs back on release.

Release

  • dragMomentum={false} stops it where it's released.
  • dragSnapToOrigin springs it back to where it started.
  • dragTransition tunes the glide (power, timeConstant, bounceStiffness, bounceDamping).

While dragging

whileDrag applies while it's dragged, as whileTap does while it's pressed.

One axis, decided by the gesture

dragDirectionLock waits for the pointer to move 10 px along an axis, then drags on that one alone. The drag starts at that moment, so a card that drags sideways inside a sheet that drags down doesn't start on a vertical swipe.

Drags inside drags

A drag claims its axes as it starts, so a draggable inside a draggable drags on its own, and its ancestor stays put for that gesture. dragPropagation claims nothing, so the ancestor drags with it.

Starting from somewhere else

createDragControls() starts a drag from another element, such as a handle:

const controls = createDragControls()
<div class="handle" onPointerDown={(event) => controls.start(event)} />
<w.div drag="y" dragControls={controls} dragListener={false} />

Inside a zoomed parent

An element inside a scaled or rotated parent moves in the parent's pixels, so it runs ahead of the pointer or lags behind it. <WardicConfig transformPagePoint={correctParentTransform(() => plan)}> maps the pointer through the parent's transform, so it stays under the pointer at any zoom. Arrange a room shows it.

In the storefront

The storefront's examples that show it, live and with their code:

  • Notifications you swipe away: Throw a notification aside and it flies off; for four seconds you can undo it, then it's gone and the rest close the gap. (on Foldkit)
  • Arrange a room, at any zoom: Zoom the floor plan in or out, then drag the sofa, the table and the lamp about: each stays under your pointer, and inside the room. (on Foldkit)
  • The collection on a dial that turns: All twelve pieces ride a wheel turning under the page: drag it and throw it, and it settles on a piece, its name and number rolling over like a counter, its price counting to the next. Press the piece at the front and its photo is tossed into its details along an arc, and from there into the cart.