# Interactions

> Move, resize and set progress by dragging bars, draw links between bar edges, validate changes, undo and redo, zoom between view presets, navigate the timeline and resize the grid with the splitter.

Most planning happens on the timeline: dragging bars, stretching them and drawing links. Every change is rescheduled immediately and can be validated before it is applied.

## Moving tasks

Press on a bar and drag it horizontally. The drag starts after 3 pixels, so a click still selects the task. While you drag, the bar shows where it will land, snapped to the time grid:

- in day, week, month and quarter presets, starts snap to whole days;
- in hour-based presets (`'hourAndDay'`, `'minuteAndHour'`), they snap to the preset's step, such as 15 minutes;
- the start then snaps to working time on the task's calendar, so a task dropped on a Saturday starts on Monday morning.

The dropped task gets a **start no earlier than** constraint at its new start; see [Dragging sets a constraint](https://docs.birchui.com/gantt/scheduling.md#dragging-sets-a-constraint). Dragging a summary moves all of its subtasks. Press <kbd>Escape</kbd> during a drag to cancel it.

## Resizing and progress

| Handle | Action |
| --- | --- |
| The right edge of a bar | Drag to change the finish, and so the duration. The new duration is working time on the task's calendar. |
| The progress handle inside a bar | Drag to set `percentDone`. |

Summaries cannot be resized or given progress (they roll up), and milestones have no duration. There is no handle on the start edge; move the task, then resize its finish. See [Limitations](https://docs.birchui.com/gantt/limitations.md).

## Drawing links

Each bar has a connector at its start and at its finish. Drag from one bar's connector onto another bar's start or finish to create a link. The link type follows from the edges you use:

| From | To | Type |
| --- | --- | --- |
| Finish | Start | `FS` |
| Start | Start | `SS` |
| Finish | Finish | `FF` |
| Start | Finish | `SF` |

While dragging, the target bar shows whether the link is allowed; links that break the [dependency rules](https://docs.birchui.com/gantt/dependencies.md#rules-and-validation) or your `validateDependency` are refused. Click a link to select it and press <kbd>Delete</kbd> to remove it. From the keyboard, press <kbd>D</kbd> on a focused bar; see [Keyboard and accessibility](https://docs.birchui.com/gantt/accessibility.md#linking-from-the-keyboard).

## Validating changes

`validateTaskChange` runs before a drag, resize, progress change, keyboard move or grid edit is applied. Return `false` to refuse it; the bar returns to where it was and the refusal is announced.

| Context field | Description |
| --- | --- |
| `task` | The scheduled task before the change. |
| `changes` | What would change: `{ start }` for moves, `{ duration }` for resizes, `{ percentDone }` for progress, or the edited field and the typed text for grid edits. |
| `start`, `end` | The task's start and finish after the change, as `Date`s. |
| `source` | `'drag'`, `'resize'`, `'progress'`, `'keyboard'` or `'edit'`. |

```ts
const freezeDate = new Date('2027-01-11T00:00');

createGantt(container, {
  tasks,
  dependencies,
  validateTaskChange: ({ task, start, source }) => {
    if (task.percentDone >= 100) return false;
    if (source === 'drag' && start < freezeDate) return false;
    return true;
  },
});
```

Task changes made through the API (`updateTask`, `addTask`, `removeTask`) are not passed to `validateTaskChange`; `addDependency` does go through `validateDependency`. Validators that throw refuse the change and log the error.

After a successful change, `taskChange` reports the task before and after with the `source`, then `tasksScheduled` lists every task that moved:

```ts
gantt.on('taskChange', ({ task, previous, source }) => {
  if (source === 'api') return;
  void api.patch(`/tasks/${task.id}`, { start: task.start, end: task.end, duration: task.duration, percentDone: task.percentDone });
});
```

## Selection and clicks

Clicking a bar or row selects the task; clicking a link selects the link. `selectionChange` reports the selected task or link and the previous ones. `taskClick` and `taskDoubleClick` carry the scheduled task and the native event, for example to open a task dialog on double-click.

```ts
gantt.on('taskDoubleClick', ({ task }) => taskDialog.open(task.id));
gantt.selectTask('racking');
gantt.getSelectedTask();
```

## Undo and redo

Every change is recorded: drags, resizes, progress, grid edits, links, adds, deletes, indents and API edits. <kbd>Ctrl</kbd>/<kbd>Cmd</kbd>+<kbd>Z</kbd> undoes; <kbd>Ctrl</kbd>/<kbd>Cmd</kbd>+<kbd>Y</kbd> or <kbd>Ctrl</kbd>/<kbd>Cmd</kbd>+<kbd>Shift</kbd>+<kbd>Z</kbd> redoes. A summary drag, a predecessor edit or an outdent is one step, however many tasks it touches. The last `historyLimit` steps are kept (default 100).

```ts
undoButton.addEventListener('click', () => gantt.undo());
redoButton.addEventListener('click', () => gantt.redo());
gantt.on('tasksScheduled', () => {
  undoButton.disabled = !gantt.canUndo();
  redoButton.disabled = !gantt.canRedo();
});
```

Undo and redo emit `tasksScheduled` but not `taskChange` or the dependency events, so a save routine that listens only to `taskChange` misses them. Listen to `tasksScheduled` and save the tasks in `changed` (or `toJSON()`) to stay in sync. `setTasks`, `setDependencies`, `fromJSON`, `fromMSProjectXML` and new `tasks` or `dependencies` through `update()` clear the history.

## Zoom and view presets

A view preset sets the time scale: the size of a tick, its width and the header rows. The default is `'weekAndDay'`.

| Preset | One tick | Tick width |
| --- | --- | --- |
| `'minuteAndHour'` | 15 minutes | 64 px |
| `'hourAndDay'` | 1 hour | 56 px |
| `'dayAndWeek'` | 1 day | 150 px |
| `'weekAndDay'` | 1 day | 34 px |
| `'dayAndMonth'` | 1 day | 40 px |
| `'weekAndMonth'` | 1 week | 120 px |
| `'monthAndQuarter'` | 1 week | 44 px |
| `'monthAndYear'` | 1 month | 110 px |

`GANTT_VIEW_PRESETS` holds all of them and `GANTT_ZOOM_LEVELS` the default zoom ladder: every preset except `'minuteAndHour'`, from most detailed to coarsest.

```ts
gantt.setViewPreset('weekAndMonth');
zoomInButton.addEventListener('click', () => (zoomInButton.disabled = !gantt.zoomIn()));
zoomOutButton.addEventListener('click', () => (zoomOutButton.disabled = !gantt.zoomOut()));
gantt.on('viewPresetChanged', ({ preset, previous }) => console.info(`Zoomed from ${previous.id} to ${preset.id}`));
```

`zoomIn()` and `zoomOut()` return `false` at the end of the ladder; restrict the ladder with `zoomLevels`. A custom preset object (`{ id, tickUnit, tickIncrement, tickWidth, headers, snap }`, the same shape as the [Scheduler's view presets](https://docs.birchui.com/scheduler/view-presets.md#custom-presets)) works anywhere a name does. Header labels use `locale` and `timeZone`, and weeks start on `weekStartsOn` (default Monday).

## Navigating the timeline

| Method | Description |
| --- | --- |
| `scrollToTask(id)` | Scroll a task's row and bar into view, expanding collapsed summaries. |
| `scrollToDate(date, align?)` | Scroll a date into view; `align` is `'start'`, `'center'` or `'end'`. |
| `getProjectRange()` | The project's start and finish. |
| `xFromDate(date)`, `dateFromX(x)` | Convert between dates and timeline pixels. |
| `getTaskBounds(id)` | A bar's `left`, `top`, `width` and `height` in timeline pixels, for overlays. |

`viewportScroll` fires when the timeline scrolls, with `left`, `top`, `visibleStart` and `visibleEnd`.

The **today line** marks the current time (`showTodayLine`, on by default) and moves along every minute; pass `today` to pin it for screenshots and tests. `taskTooltip` (on by default) shows each bar's description on hover; pass `false`, or a function `(task) => string` for your own text.

## Splitter

The bar between the grid and the timeline resizes the grid. Drag it, or focus it and use the keyboard: <kbd>ArrowLeft</kbd> and <kbd>ArrowRight</kbd> move it 16 pixels (64 with <kbd>Shift</kbd>), <kbd>Home</kbd> collapses the grid to its minimum of 120 pixels, and <kbd>End</kbd> widens it as far as it goes. It is a `role="separator"` with its current width in `aria-valuenow`. Set the starting width with `gridWidth`.

## Read-only mode

`readOnly: true` turns off dragging, resizing, progress, link drawing, grid editing, indenting, adding and deleting, and undo and redo from the keyboard. Selection, scrolling, zoom, the splitter and keyboard navigation keep working. Most API methods still work, so a status board can update tasks from a live feed.

## Next steps

- [Keyboard and accessibility](https://docs.birchui.com/gantt/accessibility.md) for editing without a pointer.
- [Limitations](https://docs.birchui.com/gantt/limitations.md) for what dragging does not do yet.
