# Resources and events

> The data model of the scheduler, including resource hierarchies, event fields, accepted date formats, overlap lanes and the methods that update data in place.

The scheduler draws two lists: **resources**, one row each, and **events**, bars placed on a resource row between a start and an end time. Both are plain objects, so they can come straight from your API.

## Resources

A resource needs an `id` and a `name`. Everything else is optional.

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | Unique identifier. Events point at it through `resourceId`. |
| `name` | `string` | Label shown in the resource column. |
| `parentId` | `string \| null` | Places the resource under another resource, which then becomes a collapsible group row. |
| `color` | `string` | Default bar color for the resource's events. Any CSS color. |
| `expanded` | `boolean` | Initial expanded state of a group row. |
| any other key | `unknown` | Your own fields, available in renderers and event payloads. |

```ts
import type { SchedulerResource } from '@birchui/scheduler';

interface Technician extends SchedulerResource {
  skills: string[];
  vanId: string;
}

const technicians: Technician[] = [
  { id: 'tech-amira', name: 'Amira Haddad', skills: ['boilers', 'gas'], vanId: 'VAN-12', color: '#2563eb' },
  { id: 'tech-jonas', name: 'Jonas Weber', skills: ['heat pumps'], vanId: 'VAN-07', color: '#0f766e' },
  { id: 'tech-lucia', name: 'Lucía Romero', skills: ['inspections', 'gas'], vanId: 'VAN-03', color: '#b45309' },
];
```

### Resource hierarchy

Set `parentId` to nest resources. Parents render as group rows with an expand toggle, children are indented under them, and nesting can go as deep as you need. A `parentId` that does not match any resource is ignored and the resource is shown at the top level.

```ts
const resources: SchedulerResource[] = [
  { id: 'depot-north', name: 'North depot', expanded: true },
  { id: 'tech-amira', name: 'Amira Haddad', parentId: 'depot-north' },
  { id: 'tech-jonas', name: 'Jonas Weber', parentId: 'depot-north' },
  { id: 'depot-south', name: 'South depot', expanded: false },
  { id: 'tech-lucia', name: 'Lucía Romero', parentId: 'depot-south' },
  { id: 'tech-tomasz', name: 'Tomasz Nowak', parentId: 'depot-south' },
];
```

Group rows are resources like any other, so events can point at them and are drawn in the group row. A common pattern is to book shared equipment, such as a crane or a cherry picker, on the depot row.

Expand and collapse from code with `expandResource(id)` and `collapseResource(id)`. Every toggle, by pointer, keyboard or API, emits `resourceToggle`:

```ts
scheduler.on('resourceToggle', ({ resource, isExpanded }) => {
  localStorage.setItem(`depot-open:${resource.id}`, String(isExpanded));
});

scheduler.collapseResource('depot-north');
```

### Resource columns

By default the left pane shows one column, **Name**, 200 pixels wide. Use `resourceColumns` to show more fields. Each column is 200 pixels unless you give it a `width`, and the pane is as wide as its columns combined unless you set `resourceWidth`.

```ts
createScheduler(container, {
  resources: technicians,
  events: jobs,
  start: '2026-12-07',
  end: '2026-12-12',
  resourceWidth: 320,
  resourceColumns: [
    { field: 'name', header: 'Technician', width: 180 },
    { field: 'vanId', header: 'Van', width: 70 },
    {
      field: 'skills',
      header: 'Skills',
      width: 120,
      renderer: (tech) => (tech.skills as string[]).join(', '),
    },
  ],
});
```

A column `renderer` receives the resource and returns a string or a DOM `Node`.

## Events

| Field | Type | Description |
| --- | --- | --- |
| `id` | `string` | Unique identifier, used by `updateEvent`, `removeEvent`, `scrollToEvent` and `selectEvent`. |
| `resourceId` | `string` | The resource row the event belongs to. |
| `start` | `Date \| string \| number` | Start time. See [dates and times](#dates-and-times). |
| `end` | `Date \| string \| number` | End time, exclusive. |
| `title` | `string` | Text shown on the bar and announced to screen readers. |
| `color` | `string` | Bar color. Overrides the resource color. |
| `draggable` | `boolean` | Set `false` to lock this event in place. |
| `resizable` | `boolean \| 'start' \| 'end'` | Set `false` to lock the duration, or allow only one edge to move. |
| `className` | `string` | Extra CSS class on the bar. |
| any other key | `unknown` | Your own fields, available in `eventRenderer`, tooltips and payloads. |

```ts
import type { SchedulerEvent } from '@birchui/scheduler';

interface ServiceJob extends SchedulerEvent {
  customer: string;
  priority: 'routine' | 'urgent';
  workOrder: string;
}

const jobs: ServiceJob[] = [
  {
    id: 'job-4471',
    resourceId: 'tech-amira',
    title: 'Boiler service',
    customer: 'Harbor View Apartments',
    workOrder: 'WO-20931',
    priority: 'routine',
    start: '2026-12-07T08:00',
    end: '2026-12-07T10:30',
  },
  {
    id: 'job-4480',
    resourceId: 'tech-amira',
    title: 'No heat call-out',
    customer: 'Bluebell Primary School',
    workOrder: 'WO-20957',
    priority: 'urgent',
    start: '2026-12-07T11:00',
    end: '2026-12-07T12:30',
    color: '#dc2626',
    className: 'job-urgent',
  },
  {
    id: 'job-4490',
    resourceId: 'tech-jonas',
    title: 'Heat pump commissioning',
    customer: 'Ortega residence',
    workOrder: 'WO-20961',
    priority: 'routine',
    start: '2026-12-08T09:00',
    end: '2026-12-08T16:00',
    resizable: 'end',
  },
];
```

Typing your own interfaces makes the scheduler generic over them: `createScheduler<ServiceJob, Technician>(…)` types every payload, renderer argument and API return value, so `event.customer` needs no cast.

## Dates and times

`start`, `end`, the scheduler's own `start`/`end` range and the arguments of methods such as `setRange` and `scrollToDate` all accept the same three forms:

| Input | Example | Interpreted as |
| --- | --- | --- |
| Local ISO string | `'2026-12-07T08:30'`, `'2026-12-07'` | Wall-clock time in the scheduler's `timeZone`. A date without a time means midnight. |
| ISO string with offset | `'2026-12-07T14:30:00Z'`, `'2026-12-07T08:30-06:00'` | That exact instant. |
| `Date` or number | `new Date(…)`, `1796653800000` | That exact instant (a number is epoch milliseconds). |

Local strings are the easiest way to work: `'2026-12-07T08:00'` means 8:00 on the scheduler's clock whatever time zone the browser runs in. Instants are converted to the scheduler's time zone for display. [Time zones](https://docs.birchui.com/scheduler/time-zones.md) explains the conversion and daylight saving behaviour.

Dates in event payloads, such as `eventDrop` and `eventCreate`, are always `Date` objects. Store them as ISO strings with `toISOString()` and they round-trip exactly.

### Invalid data

The scheduler never throws on bad rows. An event without an `id`, or with a `start` or `end` that cannot be parsed, is skipped and reported with one `console.warn` that counts the skipped rows. When two events share an `id`, the later one is kept and a warning is printed.

> **Warning**
>
> `end` is exclusive. A job from 08:00 to 10:00 ends exactly at 10:00 and does not overlap a job that starts at 10:00. An event whose `end` is before its `start` is treated as ending at its start and drawn as a thin marker, so validate durations before they reach the scheduler.

## Overlapping events

When events on one resource overlap in time, the scheduler assigns each one a lane. `eventLayout` controls how lanes are drawn:

| Value | Layout |
| --- | --- |
| `'stack'` | Each lane is a full-height bar position below the previous one, and the row grows to fit its lanes. Every bar keeps the same height. |
| `'pack'` | The row keeps its height. Events that overlap each other split it between them, so bars in a busy cluster get thinner (never below 4 pixels); events with no overlap keep the full height. |
| `'overlap'` | No lanes: every bar is drawn at full height in the same position, and later bars cover earlier ones. |

`eventLayout` defaults to `'stack'`. `rowHeight` (default 44 pixels) sets the height of a row, or of one lane when stacking, and `barMargin` (default 5 pixels) sets the gap between bars and the row edges. `barMargin` must be less than half of `rowHeight`.

```ts
createScheduler(container, {
  resources,
  events,
  start: '2026-12-07',
  end: '2026-12-12',
  eventLayout: 'stack',
  rowHeight: 44,
  barMargin: 4,
});
```

Lane assignment runs in O(n log n) per row, so rows with hundreds of events lay out instantly. The lane index of each bar is passed to [`eventRenderer`](https://docs.birchui.com/scheduler/event-rendering.md#custom-event-content).

## Updating data

Change data through the API rather than by mutating your arrays in place. The methods update the existing scheduler; nothing is recreated.

| Method | Description |
| --- | --- |
| `setEvents(events)` | Replaces all events. |
| `addEvent(event)` | Adds one event. |
| `updateEvent(id, patch)` | Merges `patch` into the event and returns the updated event, or `undefined` if the id is unknown. |
| `removeEvent(id)` | Removes the event, emits `eventRemove` with `source: 'api'` and returns the removed event. |
| `setResources(resources)` | Replaces all resources. |
| `getEvents()` / `getEventById(id)` | Read the current events, including changes made by dragging. |
| `getResources()` / `getResourceById(id)` | Read the current resources. |
| `getEventsInRange(start, end, resourceId?)` | Events that overlap a time span, optionally on one resource. |

```ts
const job = scheduler.updateEvent('job-4471', { resourceId: 'tech-lucia', color: '#b45309' });

scheduler.addEvent({
  id: 'job-4502',
  resourceId: 'tech-jonas',
  title: 'Radiator replacement, 14 Elm Street',
  start: '2026-12-09T13:00',
  end: '2026-12-09T15:30',
});

scheduler.removeEvent('job-4480');

const tuesdayLoad = scheduler.getEventsInRange('2026-12-08', '2026-12-09', 'tech-jonas');
```

`update({ events })` and `update({ resources })` replace the data in the same way. That is what the framework adapters do when your props change.

### Keeping a server in sync

The scheduler applies a user's move or resize itself and then emits an event. Persist the change in the listener and roll back with `updateEvent` if the server rejects it:

```ts
scheduler.on('eventDrop', async ({ event, start, end, resourceId, previous }) => {
  const response = await fetch(`/api/jobs/${event.id}`, {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ start: start.toISOString(), end: end.toISOString(), technicianId: resourceId }),
  });
  if (!response.ok) {
    scheduler.updateEvent(event.id, previous);
    showToast(`${event.title} could not be moved: the job is already in progress.`);
  }
});
```

To stop an invalid move before it happens, use `validateDrop` instead. See [validation](https://docs.birchui.com/scheduler/interactions.md#validation).

## Next steps

- [View presets and zoom](https://docs.birchui.com/scheduler/view-presets.md) to choose the time scale.
- [Drag, drop, resize and create](https://docs.birchui.com/scheduler/interactions.md) for editing.
- [Event rendering](https://docs.birchui.com/scheduler/event-rendering.md) to customize bars.
