awesome-timeline-react 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -48,6 +48,8 @@ The following properties for Timeline component are available:
48
48
  - `label`: `string` (plain-text label drawn inside the event bar)
49
49
  - `style`: `EventStyle` (per-event appearance: `fill`, `stroke`, `strokeWidth`, `textColor`, `font`, `borderRadius`, `opacity`)
50
50
  - `drawEvent`: `DrawEventFn` (per-event custom canvas renderer, see below)
51
+ - `groupId`: `string` (connects this event to others sharing the same tag; Cmd/Ctrl+click selects the whole group — see Selection)
52
+ - `isSelectable`: `boolean` (set `false` to opt this event out of selection while the Timeline is `selectable`; default selectable)
51
53
  - `isResizable`: `boolean` (defines if event is resizable, overrides the default property `eventsResize`)
52
54
  - `showPrompt`: `boolean` (defines if prompt will be shown after hover. Works only if `showEventPrompt` is set as true)
53
55
  - `metadata`: `unknown` (some data that can be used by event prompt template)
@@ -72,6 +74,12 @@ The following properties for Timeline component are available:
72
74
 
73
75
  - `onEventHover` – callback when the hovered event changes (`eventId`/`rowId`, `null` on hover-out).
74
76
 
77
+ - `selectable` – enable click selection (default `false`). See Selection.
78
+
79
+ - `defaultSelectedEventIds` – initial selection (uncontrolled).
80
+
81
+ - `onSelectionChange` – callback fired with the selected event ids whenever the selection changes.
82
+
75
83
  - `theme` – color/font tokens for everything drawn to canvas (see Theming).
76
84
 
77
85
  - `drawEvent` – custom canvas renderer for all events (see Custom event rendering).
@@ -86,7 +94,19 @@ The following properties for Timeline component are available:
86
94
 
87
95
  - `eventPromptTemplate` - function that can be passed for event prompt. Function receives currently hovered `event` object. It returns JSX Element so it can be customize in various ways. The example is in story. (The prompt is regular DOM — JSX still works here.)
88
96
 
89
- - `panZoom` – interaction configuration: `panOnDragBackground` (default true), `ctrlWheelZoom` (default true), `middleClickGranularity` (default true), `zoomStepSeconds` (default 900).
97
+ - `panZoom` – interaction configuration:
98
+ - `panOnDragBackground` (default true) – drag the background to pan.
99
+ - `ctrlWheelZoom` (default true) – **Ctrl/Cmd + wheel** (and Mac trackpad pinch) zooms the visible time frame around the cursor.
100
+ - `pinchZoom` (default true) – two-finger pinch zooms the time frame on touch devices.
101
+ - `dragZoom` (default true) – **Ctrl/Cmd + drag** the background to zoom (right = in, left = out).
102
+ - `minWindowSeconds` / `maxWindowSeconds` – zoom limits (defaults 900s / 30 days).
103
+ - `zoomWheelFactor` (default 1.15) – span multiplier per wheel notch.
104
+ - `middleClickGranularity` (default true) – middle-click toggles **wheel = grid granularity** mode.
105
+ - `zoomStepSeconds` (default 900) – grid-granularity step.
106
+
107
+ > Time-frame zoom changes how much time is visible; grid granularity (`cellWidth`) changes how wide a grid cell is drawn. Since 0.2.x, Ctrl+wheel does time-frame zoom and grid granularity lives on middle-click+wheel.
108
+
109
+ - `timeBar` – configure the two-row time bar (see Time bar rows). Defaults to a day row over an hour row.
90
110
 
91
111
  - `animations` – layout animations when events re-stack (rows growing, events changing lanes). `true` (default), `false` to disable, or `{ layoutMs?: number, fadeMs?: number }` to tune (defaults 200/120; `layoutMs` is the stacking tween, `fadeMs` the resize-handle fade). A dropped event always lands instantly — only the events re-stacking around it animate.
92
112
 
@@ -104,12 +124,170 @@ CSS classes cannot style canvas pixels, so canvas-drawn elements are themed with
104
124
  gridColor: "#e4dcdc",
105
125
  timeBarBorder: "yellow",
106
126
  timeBarTextColor: "white",
127
+ // selection visuals (see Selection)
128
+ dimmedOpacity: 0.35, // opacity of non-selected events while a selection is active
129
+ selectionShadowColor: "rgba(0,0,0,0.35)", // lift shadow of selected events
130
+ selectionShadowBlur: 8,
131
+ selectionElevation: 2, // px the selected bar is lifted
107
132
  font: "12px Inter", // defaults to the canvas element's computed font
133
+ // bar geometry — themeable since 0.2.x (defaults shown)
134
+ barHeight: 20, // event bar height
135
+ laneGap: 2, // gap between stacked lanes (laneHeight = barHeight + laneGap)
136
+ rowPaddingY: 10, // vertical padding above/below bars in a row
137
+ barRadius: 5, // event bar corner radius
108
138
  }}
109
139
  ...
110
140
  />
111
141
  ```
112
142
 
143
+ Bar geometry tokens drive row heights, lane stacking, hit-testing, and the drag
144
+ ghost together — changing `barHeight`/`laneGap` restacks and re-measures rows
145
+ automatically. (Per-event `style.borderRadius` still overrides `barRadius`.)
146
+
147
+ ## Selection
148
+
149
+ Set `selectable` to enable click selection. A selected event is **elevated**
150
+ (drawn on top with a shadow) while every other event is **dimmed**
151
+ (`theme.dimmedOpacity`), so the current selection reads at a glance.
152
+
153
+ - **Click** an event → selects just that event.
154
+ - **Cmd/Ctrl + click** → selects the whole *connected group*: every event
155
+ sharing the clicked event's `props.groupId`. (An event with no `groupId`
156
+ selects only itself.)
157
+ - **Background click** or **Escape** → clears the selection.
158
+
159
+ Individual events can opt out with `props.isSelectable = false`: they can't be
160
+ clicked into the selection, are excluded from a group selection, and are ignored
161
+ by `setSelection`. Clicking a non-selectable event leaves the current selection
162
+ unchanged (it's transparent to selection, not a clear).
163
+
164
+ `onSelectionChange(eventIds)` fires on every change. Selection is uncontrolled —
165
+ seed it with `defaultSelectedEventIds` and drive it imperatively:
166
+
167
+ ```tsx
168
+ const ref = useRef<TimelineHandle>(null);
169
+ ref.current?.setSelection(["a1", "a2"]); // replace the selection (no group expansion)
170
+ ref.current?.getSelection(); // string[]
171
+
172
+ <Timeline ref={ref} selectable onSelectionChange={(ids) => …} … />
173
+ ```
174
+
175
+ Selection is additive to `onEventClick` (both fire). Dragging and resizing
176
+ always operate on the single grabbed event, regardless of how many are selected.
177
+
178
+ > Follow-ups not yet implemented: rubber-band rectangle selection, group-drag
179
+ > (moving a whole selection together), and a controlled `selectedEventIds` prop.
180
+
181
+ ## Grouped (collapsible) rows
182
+
183
+ Rows can be nested one level: give a row a `parentId` to make it a **child** of a
184
+ parent row. A **group-parent event** (`props.isGroupParent`) on the parent row
185
+ summarizes the **child events** that point at it via `props.parentEventId`:
186
+
187
+ ```tsx
188
+ const rows = [
189
+ { id: "order", name: "Order #1001" }, // parent row
190
+ { id: "weld", name: "Weld", parentId: "order" }, // child rows
191
+ { id: "paint", name: "Paint", parentId: "order" },
192
+ ];
193
+
194
+ const events = [
195
+ // the parent's startTime/endTime are derived — supplied values are just a
196
+ // fallback shown until it has children
197
+ { id: "P", rowId: "order", startTime: 0, endTime: 0, props: { isGroupParent: true, label: "Order #1001" } },
198
+ { id: "w", rowId: "weld", startTime: t1, endTime: t2, props: { parentEventId: "P" } },
199
+ { id: "p", rowId: "paint", startTime: t3, endTime: t4, props: { parentEventId: "P" } },
200
+ ];
201
+ ```
202
+
203
+ Behavior:
204
+
205
+ - **Derived span** — the parent bar spans the earliest child `startTime` to the
206
+ latest child `endTime`, recomputed automatically as children move. A childless
207
+ parent keeps its supplied times.
208
+ - **Read-only parent** — a group-parent is never resizable and never draggable
209
+ (its bounds belong to its children). It stays clickable/selectable.
210
+ - **Group selection** — selecting a parent event (click, `setSelection`, …) also
211
+ selects all of its child events. Direction is parent → children only.
212
+ - **Collapse** — a parent row shows a caret in its header; collapsing it hides
213
+ the child rows while the summary bar stays. Collapse is **uncontrolled**: seed
214
+ it with `defaultCollapsedRowIds` and drive it imperatively. The state lives in
215
+ the timeline and **survives streaming updates / `setEvents` / `events`-prop
216
+ resets** — only a caret click or the handle changes it.
217
+
218
+ ```tsx
219
+ const ref = useRef<TimelineHandle>(null);
220
+ ref.current?.toggleRow("order"); // flip collapsed/expanded
221
+ ref.current?.setCollapsed("order", true); // collapse explicitly
222
+
223
+ <Timeline ref={ref} rows={rows} events={events} defaultCollapsedRowIds={["order"]} … />
224
+ ```
225
+
226
+ List child rows immediately after their parent row in `rows` for the clearest
227
+ visual nesting. Unresolved `parentEventId` links are harmless — the event just
228
+ draws as a plain event.
229
+
230
+ > Follow-ups not yet implemented: parent batch-drag (time-shifting all children
231
+ > together), child-pinning to sibling rows, deeper nesting, and a controlled
232
+ > `collapsedRowIds` prop.
233
+
234
+ ## Restricting where an event can be dropped
235
+
236
+ Give an event `props.droppableRowIds` to limit which rows it can be dragged onto:
237
+
238
+ ```tsx
239
+ { id: "weld", rowId: "r1", startTime, endTime,
240
+ props: { droppableRowIds: ["r1", "r3"] } } // only Welder / CNC
241
+ ```
242
+
243
+ - `undefined` (default) → droppable onto any row.
244
+ - A list → a drag only commits if the target row is in it; otherwise the event
245
+ **snaps back** (no `onDrop`), and the cursor shows `not-allowed` while hovering
246
+ a disallowed row. An **empty array** pins the event to no row.
247
+
248
+ If an event's own `rowId` is not in its `droppableRowIds` (a misconfiguration),
249
+ the library records the problem on `props.metadata` so your app can flag it —
250
+ merging into any object you already stored there, and clearing automatically
251
+ once the event is on an allowed row:
252
+
253
+ ```ts
254
+ import type { DroppableError } from "awesome-timeline-react";
255
+
256
+ const meta = event.props?.metadata as { droppableError?: DroppableError };
257
+ if (meta?.droppableError) {
258
+ // { rowId: "r4", droppableRowIds: ["r1", "r2"] } — surface it in your UI
259
+ }
260
+ ```
261
+
262
+ ## Time bar rows
263
+
264
+ The time bar has two rows. By default the top row shows calendar days and the
265
+ bottom row shows hours, but each row's unit and label are configurable via
266
+ `timeBar`:
267
+
268
+ ```tsx
269
+ <Timeline
270
+ timeBar={{
271
+ topRow: { unit: "day" }, // "hour" | "day" | "week" | "month"
272
+ bottomRow: {
273
+ unit: { stepSeconds: 8 * 3600 }, // or a fixed step in seconds
274
+ format: (start, end) => `Shift ${Math.floor(start.getHours() / 8) + 1}`,
275
+ },
276
+ }}
277
+ ...
278
+ />
279
+ ```
280
+
281
+ - `unit` – `"hour" | "day" | "week" | "month"`, or `{ stepSeconds }` for fixed
282
+ steps (weeks start on Monday; custom steps anchor to local midnight).
283
+ - `format(blockStart, blockEnd)` – optional label override; defaults mirror the
284
+ built-in `Mon 1 Jan` (day) and `HH:00` (hour) labels.
285
+
286
+ The time bar is purely presentational: it never changes the visible time range
287
+ or the grid. Vertical grid lines and drop snapping stay on the grid cell
288
+ (`cellWidth`), independent of the time-bar rows — so e.g. an 8-hour bottom row
289
+ can sit over an hourly grid.
290
+
113
291
  ## Custom event rendering (`drawEvent`)
114
292
 
115
293
  For visuals beyond `label` + `style`, draw the event yourself. Resolution order: per-event `props.drawEvent` → Timeline-level `drawEvent` → built-in default. Return `false` to fall through to the default.
@@ -171,6 +349,12 @@ Panning is smooth since 0.2.0: drag the background to move the time window with
171
349
 
172
350
  New in 0.2.0: `ref` (`TimelineHandle`), `theme`, `drawEvent`, `onEventClick`, `onEventHover`, `panZoom`.
173
351
 
352
+ New in 0.3.0:
353
+ - Themeable bar geometry — `theme.barHeight` / `laneGap` / `rowPaddingY` / `barRadius`.
354
+ - Configurable time-bar rows — `timeBar.topRow` / `bottomRow` (`unit` + `format`), units `hour`/`day`/`week`/`month` or `{ stepSeconds }`.
355
+ - Time-frame zoom — Ctrl/Cmd + wheel (and trackpad/touch pinch) and Ctrl + drag; grid granularity moved to middle-click + wheel. New `panZoom` keys: `pinchZoom`, `dragZoom`, `minWindowSeconds`, `maxWindowSeconds`, `zoomWheelFactor`.
356
+ - Fixes: time-bar hour labels follow panning on sub-day windows; grid lines stay aligned to the time-bar blocks while panning and zooming; the drag ghost inherits the dragged event's style; corrected weekday names.
357
+
174
358
  ## Roadmap
175
359
 
176
360
  Here, we will store major features planned for future releases (from highest to lowest priority):
@@ -179,6 +363,8 @@ Here, we will store major features planned for future releases (from highest to
179
363
  2. ~~Real time line~~ - published in 0.1.7 version
180
364
  3. ~~Customizable prompts after hovering over an event~~ - published in 0.1.14 version
181
365
  4. ~~Canvas rendering engine, virtualization, streaming API~~ - published in 0.2.0 version
182
- 5. Customizable second row of the time bar
183
- 6. Themeable bar geometry (heights, lane spacing, radius)
184
- 7. Multi-select and rubber-band selection
366
+ 5. ~~Customizable second row of the time bar~~ - `timeBar.topRow/bottomRow` (unit + format)
367
+ 6. ~~Themeable bar geometry (heights, lane spacing, radius)~~ - `theme.barHeight/laneGap/rowPaddingY/barRadius`
368
+ 7. ~~Tag-based selection & multi-select~~ — `selectable`, `props.groupId`, Cmd/Ctrl+click selects the connected group, `setSelection`/`getSelection`. (Rubber-band selection and group-drag remain follow-ups.)
369
+ 8. ~~Grouped (collapsible) rows~~ — `row.parentId`, `props.isGroupParent` + `parentEventId`, derived read-only parent span, parent→children selection, uncontrolled collapse (`defaultCollapsedRowIds`, `toggleRow`/`setCollapsed`). (Parent batch-drag and controlled collapse remain follow-ups.)
370
+ 9. ~~Per-event drop-target restriction~~ — `props.droppableRowIds` limits drag targets (snap-back + `not-allowed` cursor); a misconfigured row is flagged via `metadata.droppableError`. (Auto child-pinning to sibling group rows remains a follow-up.)