Todae

Todae

useLaneDrag

useLaneDrag({ range, onDrop }) lets a person drag items along lanes that share one axis, such as talks on a conference's stages or shifts on a rota. A drag places an item from a queue, moves it along its lane or to another lane, resizes either end, or takes it back to the queue. Pass onCreate and a press on empty lane space drags out a new place. check says whether a place conflicts with other items, and a drop calls onDrop(change).

On this page
import { useLaneDrag } from '@tounsoo/todae';
import { GripVertical } from 'lucide-react';

const lanes = useLaneDrag({
  range: { start: 0, end: 480 },
  grid: quarterHours,
  check: (id, to) => conflicts(talks, id, to),
  onDrop: ({ id, to }) => schedule(id, to),
});

<div {...lanes.getBoardProps()}>
  {stages.map((stage) => (
    <div key={stage.id} {...lanes.getLaneProps(stage.id)}>
      {talksOn(stage).map((talk) => (
        <div key={talk.id} {...lanes.getItemProps(talk.id, talk.at)}>
          <span {...lanes.getHandleProps(talk.id, 'start')} aria-hidden style={{ touchAction: 'none' }} />
          <span {...lanes.getHandleProps(talk.id, 'move')} aria-hidden style={{ cursor: 'grab', touchAction: 'none' }}>
            <GripVertical />
          </span>
          {talk.title}
          <span {...lanes.getHandleProps(talk.id, 'end')} aria-hidden style={{ touchAction: 'none' }} />
        </div>
      ))}
    </div>
  ))}
</div>
<ul {...lanes.getQueueProps()}>
  {unscheduled.map((talk) => (
    <li key={talk.id} {...lanes.getItemProps(talk.id, { span: talk.length })}>
      <span {...lanes.getHandleProps(talk.id, 'move')} aria-hidden style={{ cursor: 'grab', touchAction: 'none' }}>
        <GripVertical />
      </span>
      {talk.title}
    </li>
  ))}
</ul>

Drag a talk by its grip to another time or stage, or an end of it to change its length. Each talk's menu does the same from the keyboard. The Scheduler recipe builds a multi-day board with conflicts across lanes on the hook.

import {
  Button,
  Menu,
  Text,
  useAnnouncer,
  useFocusHandoff,
  useLaneDrag,
  useLaneDragState,
  type LanePlace,
  type LaneVerdict,
  type UseLaneDragResult,
} from '@tounsoo/todae';
import { Ban, EllipsisVertical, GripVertical, TriangleAlert } from 'lucide-react';
import { memo, useId, useRef, useState, type RefObject } from 'react';
import styles from './Schedule.module.css';

// One day from 9:00 to 17:00, counted in minutes from 9:00. Talks snap to quarter hours.
const DAY = { start: 0, end: 480 };
const GRID = Array.from({ length: DAY.end / 15 + 1 }, (_, i) => i * 15);
const CHANGEOVER = 15;
/* The board scrolls sideways: each stage's lane is at least 120rem wide, at
   least 4px a minute at the default font size, so a half-hour talk has room
   for its grip, title and menu button. The stage names stay in view as it scrolls. */
.schedule {
  display: flex;
  flex-direction: column;
  gap: var(--todae-space-4);
  inline-size: 100%;
}

/* The board is one grid: a column for the stage names and one for the day.
   Each row spans both on a subgrid, so it is as wide as the whole day, not only
   the visible board. A sticky name can't leave its row, so a row as narrow as
   the board would carry its name away once the board scrolled past it. A talk
   scrolled into view stops clear of the names. */
.board {
  --schedule-name: 7rem;
  --schedule-day: 120rem;
  display: grid;
  grid-template-columns: var(--schedule-name) minmax(var(--schedule-day), 1fr);

Main hall

Opening keynote

9:00–10:00 · Ana Ruiz

Room A

Tokens that travel

10:15–11:00 · Kenji Sato

Motion with purpose

12:00–12:45 · Ana Ruiz

Room B

Forms people finish

10:30–11:00 · Mira Patel

Unscheduled

  • Auditing a design system

    30 min · Leo Brandt

  • Panel: shipping 1.0

    60 min · Kenji Sato

  • Dark mode, done right

    30 min · Mira Patel

Places

Every lane covers the same range along the axis, in units you choose, such as minutes from the start of the first day. A place is { lane, start, end } in those units, with the lane's key from getLaneProps(lane). The hook maps the pointer to a value from the lane element's own box, so that box covers range exactly: give it no padding or border along the axis. With axis="inline", the default, the range runs in the text direction, so it starts at the right in a right-to-left page; with axis="block", it runs down, as in a day view's columns.

grid lists the points a start or end snaps to, in ascending order, such as every quarter hour. The hook finds the nearest one by binary search, so a long grid costs little per move, and a day you count in your own units, such as one with the nights left out, snaps the same way. Without grid, places don't snap.

getItemProps(id, at) takes a placed item's place, or { span } for an item in the queue, the length it takes when it is placed. The hook reads them from the item's element when a drag starts.

  • Move. getHandleProps(id, 'move') drags the item. A placed item keeps the point it was grabbed at under the pointer, and an item from the queue starts at the pointer. The item keeps its length and stays inside range, starting at the nearest grid point that fits. An item longer than range has no place, so its drag drops nothing.
  • Resize. getHandleProps(id, 'start') and getHandleProps(id, 'end') move that end along the item's own lane. An end stops minSpan from the other, and never reaches it.
  • Back to the queue. A placed item dropped on the element with getQueueProps() leaves the board.

A drop that changes a place calls onDrop(change), where change is { id, kind, from, to, verdict }. kind is assign, move, resize-start, resize-end or unassign. from is the item's place when the drag started, or null for an item from the queue, and to is where it goes, or null for unassign. A resize changes the item's length, so give its { span } the new length if it goes back to the queue. A drop outside the lanes and the queue, or one that leaves the item where it was, calls nothing. A create is not a drop, so onDrop never sees kind: 'create'.

Creating a place

Pass onCreate and a press on empty lane space drags out a new place along that lane's axis. Leave onCreate out and empty space starts nothing, so a board that only moves and resizes stays as it was. The press has to miss every item and its handles. It also misses a button, a link, a control with role="button", or anything you can tab to, which keeps its own press. The span follows the pointer backward as well as forward, and the hook orders it so start is less than end. It stays in the lane where the press went down, and both ends stay inside range.

Dragged forward, the press floors to the grid cell that contains it, the largest grid point at or below the press. Dragged back, that same cell runs up to the next grid point, so the pressed slot stays in the span, and a press that lands on a grid point uses the point after it. The end that follows the pointer snaps to the nearest grid point, and a tie keeps the earlier one. The span is at least minSpan. A press that never moves past the drag threshold is a click. The click places a slot minSpan long starting at that cell, so the slot contains the click, and shifts the slot back into range when it would stick out the end. That click does not reach the lane. minSpan defaults to 0, and then a click does nothing and the lane's own click still fires, because a place has to have length. A drag that snaps to a single point does nothing then too.

While the pointer is down, read the place with useLaneDragState. The value is a LaneCreateDrag. kind is create, id is null because there is no item yet, from is null, verdict is null, and to is the place. check does not run. Draw to yourself. The hook renders nothing. On release, onCreate runs once with that place. A place that cannot fit calls nothing, which is what a click does when range is shorter than minSpan. onDrop is not called.

Escape cancels the drag, clears the place, and does not call onCreate. A cancelled pointer, a pointer that comes back up with no button down, or the window losing focus ends it the same way. Give the lane touch-action: none, or the browser takes a finger to scroll.

This gesture has no keyboard path, and the hook announces nothing, because it does not know what the place means. Give the empty lane no role and no label. A control there would take focus for a gesture the keyboard cannot do. Put a button or a dialog beside the board, such as New event below, and announce the item when you add it.

import { Button, useAnnouncer, useLaneDrag, useLaneDragState, type LanePlace } from '@tounsoo/todae';
import { useRef, useState } from 'react';
import styles from './Create.module.css';

// One morning, 9:00 to 12:00, counted in minutes from 9:00. Places snap to
// quarter hours, and a click takes half an hour.
const DAY = 180;
const GRID = Array.from({ length: DAY / 15 + 1 }, (_, i) => i * 15);
const SLOT = 30;

type CalendarEvent = { id: string; at: LanePlace };

const clock = (minute: number) => {
  const total = 9 * 60 + minute;
  return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')}`;
};

const placeStyle = (at: LanePlace) => ({
  insetBlockStart: `${(at.start / DAY) * 100}%`,
  blockSize: `${((at.end - at.start) / DAY) * 100}%`,
.root {
  display: flex;
  flex-direction: column;
  gap: var(--todae-space-3);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--todae-space-3);
}

.note {
  margin: 0;
  color: var(--todae-color-fg-muted);
  font-size: var(--todae-text-label-font-size);
}

.frame {

Drag empty space to choose a time. New event does it from the keyboard.

Checking a place

check(id, to) runs each time the place under the pointer changes, not on every pointer move, and once more at the drop, so a rule that changed meanwhile still holds. It returns null or a verdict: { level, reason, with }. A blocked place takes no drop. A warning place does, and onDrop gets the verdict, so you can say why. with lists the ids of the other items the place conflicts with, which is how a rule that crosses lanes shows: in the example, a speaker in Room A at 10:30 blocks their other talk at 10:30 on any stage. The rules are yours, and the hook knows nothing about times, stages or speakers. Taking an item back to the queue isn't checked: to keep an item on the board, such as a talk that's been announced, leave its unassign out of onDrop. A check that throws ends the drag without a drop, and its error still reaches the page.

Showing the drag

The hook renders nothing and sets state on the elements during a drag, so the board doesn't render on every pointer move:

  • data-dragging on the dragged item and on the board. A create has no item, so the mark goes on the lane instead, along with the board.
  • data-drop-target on the lane the item would go to, or on the queue for unassign. A create marks the lane it is drawing in.
  • data-conflict, the verdict's level, on that lane and on each item in with. A create is not checked, so it sets no verdict.

When the drag ends, each element gets back the value it had, so an item can carry its own data-conflict between drags.

Style those, and draw the item's future place with useLaneDragState(lanes.drag, select). It returns select of the drag under way, or of null when there is none, and renders again only when that selection changes. Select the drag while its place is in one lane, as the example's preview does, and that lane renders each time the place in it changes, not on every pointer move, and not while the drag is in other lanes. Pass isEqual as a third argument when select builds a new object each time.

const change = useLaneDragState(lanes.drag, (state) => (state?.to?.lane === stage.id ? state : null));

The owner of useLaneDrag renders as a drag starts and ends. Keep the dragged item, its lane and the board mounted during a drag: a render that replaces one of them ends the drag without a drop. While the pointer is inside the element with getBoardProps() and near its edge, the board scrolls, so make that element the one that scrolls. Outside it, such as over a queue beside it, the board stays still.

When something sticky covers the board's lanes, such as a column of stage names, give the board a scroll-padding on that side that runs up to where the lanes begin, as a length or a percentage. The example sets scroll-padding-inline-start to the names' width. The board's edge then counts from past that strip, so the board scrolls while the pointer is near the strip or over it, and a drop over the strip lands wholly in view, at the first place past it, where the item fits there. Held there, the board scrolls back to the start of the day and the item follows, snapping fully into view as the pointer enters the strip. The strip has to catch the pointer, so leave pointer-events on the sticky part. A pointer over a bare part of the strip drops at the first place in view too. The browser's own scrolling, such as scrollIntoView, keeps clear of the strip too. Any scroll-padding the board has counts this way, so keep it to the part that is covered.

Touch and the pointer

A mouse, a finger and a pen drag the same way, with pointer events. The item drags once the pointer moves a few pixels from the press, so a click or a tap on a handle drags nothing, and the click a drop sends to its handle is dropped. A press on a button or link inside a handle is that control's, and the browser's own drag of an image or link inside a handle is stopped. Every handle needs touch-action: none, or the browser takes a finger to scroll the page and the drag ends. A lane that creates places needs it too. Keep them small, such as a grip, so a swipe on the rest of the board still scrolls it. A drag ends without a drop when its release is lost, as when the pointer moves with no button down or the window loses focus, so a test that sends pointer events by hand gives every move buttons: 1.

Escape cancels a drag and leaves every item where it was, before a Dialog around the board, which stays open until the next Escape.

Accessible composition

Todae handles: Dragging an item from a handle with a mouse, a finger or a pen. Dragging out a new place on empty lane space when you pass onCreate, and cancelling that drag on Escape. Working out the place a drop lands on from the lane's box and the text direction, snapping it, and keeping it inside the range. Asking your check and refusing a blocked drop. Marking the dragged item, the drop target and the items in conflict.

You supply: Another way to place, move, resize and unschedule an item that needs no dragging and works from the keyboard, such as the example's menu or a dialog with a time field. Dragging has no keyboard path. A way to create an item without dragging, such as the New event button or a dialog. The hook announces nothing for a create, because it does not know what the place means. Handles hidden from screen readers with aria-hidden, since that other way is the one they use. The rules in check, and a reason a person can read. A cue for each verdict besides color, such as the example's icons and outline styles. The announcement of a change. For an item that moves to another lane, a place for the focus its old element held.

Watch for: Drag as the only way to place, move or create an item (WCAG 2.2 SC 2.1.1 and SC 2.5.7). An empty lane that is a button or a tab stop. That would take focus for a gesture the keyboard cannot do. A conflict shown by color alone (SC 1.4.1). A handle that is a button or a tab stop. It does nothing from the keyboard. Resize ends narrower than 24 CSS pixels, which pass SC 2.5.8 only because another control does the same. A move handle that covers the whole item on a touch screen, which keeps a swipe over the items from scrolling the board. Touch dragging is tested with pointer events, not on a phone.

When to use

When: Letting a person place items on lanes along a shared axis, such as talks on stages over a conference's days or shifts on a rota, beside a way to move them without dragging.

When not: Reordering a list → useReorder. Moving cards between columns with no axis → useReorderGroup. Picking a time → Field with a time input.

Props

useLaneDrag props
NameTypeDescription
range Required{ start: number; end: number }

The span every lane's element covers along the axis, in the board's units, such as minutes from the first day's start.

onDrop Required(change: LaneDragChange) => void

Called on a drop that changes an item's place, with the change. A create calls onCreate instead.

axis'inline' | 'block'

The direction the range runs in each lane. `inline` follows the text direction, and `block` runs down. Default `inline`.

Default: 'inline'

gridreadonly number[]

The points a start or end snaps to, in ascending order. Without it, places don't snap.

minSpannumber

The shortest span a resize leaves, and the span a click on empty lane space creates, from the grid cell that contains the press. Default 0, so that click does nothing.

Default: 0

check(id: string, to: LanePlace) => LaneVerdict | null

Says whether the item `id` may go to `to`, each time the place under the pointer changes and again at the drop.

onCreate(place: LanePlace) => void

Called once when a press on empty lane space ends on a new place. Omit it and empty space starts nothing. A drag sets the span between the press and the pointer. Dragged forward, the press floors to the grid cell that contains it. Dragged back, that cell runs up to the next grid point. The end under the pointer snaps to the nearest grid point. A press that stays inside the drag threshold creates a slot `minSpan` long from that cell, swallows the lane's click, and does nothing when `minSpan` is 0.

disabledboolean

Turn dragging off. While `disabled` is true, a drag under way can't be dropped.

Default: false

useLaneDragState props
NameTypeDescription
drag RequiredLaneDragStore

The `drag` that `useLaneDrag` returns.

select Required(state: LaneDragChange | LaneCreateDrag | null) => T

Picks what this component shows from the drag under way, or from null when there is none.

isEqual(a: T, b: T) => boolean

Says when two selections are the same, so a selection built anew on each move renders only when it changes. Default `Object.is`.

Default: Object.is