Vanilla Gantt Toolbar Technical Docs
<vn-gantt-toolbar gantt="<id>"> is a <vn-grid-toolbar> linked to the task
grid of a <vn-gantt>. The page names the Gantt it wrote, by the Gantt's own
id, instead of the embedded <vn-grid> the Gantt creates (which has no id).
Its own behavior is that link and two built-in commands,
scrollToSelectedTask and autoFitTaskList; everything else is the grid toolbar's, by
inheritance. Getting Started is a step-by-step guide
with live examples.
<vn-gantt-toolbar gantt="g"> <vn-gantt id="g">
(inherited templates, items, +-- <vn-grid class="vn-gantt-task-grid">
commands, state, events) ----> linked once getTaskGrid() returns it
The element
class VanillaGanttToolbarElement extends window.VanillaGridToolbarElement,
defined as vn-gantt-toolbar and exported as
window.VanillaGanttToolbarElement. It is one file,
src/vanilla-gantt-toolbar/vanilla-gantt-toolbar.js, with no stylesheet of
its own.
- Every attribute, property, method, event, template and item of
<vn-grid-toolbar>works unchanged:theme,theme-css-path,locale,state,ready(),registerCommand(),dispatchCommand(),setMessages()and the rest. observedAttributesaddsganttto the grid toolbar's list.- The constructor adds two commands and their message keys, and the script one icon (§ Built-in commands).
Built-in commands
scrollToSelectedTask
scrollToSelectedTask brings the task selected in the task grid into view.
A selection change already scrolls the timeline sideways to the selected bar
(Task Moving § 4), so the
command is the way back to the selection after either pane was scrolled away
or its parent collapsed:
<vn-grid-toolbar-command command="scrollToSelectedTask" icon="locate" variant="ghost"
title-key="scrollToSelectedTaskTitle" disabled-when="!hasSingleSelection"></vn-grid-toolbar-command>
| Kind | Name | Value |
|---|---|---|
| Command | scrollToSelectedTask |
Ignores arg. Calls the linked Gantt's scrollToTask(key, { focus: true }) for the one selected key and returns its boolean: ancestors expanded, row centred, bar scrolled into view, and the timeline focused (Timeline § 3). false when unlinked or without exactly one selected task |
| Message key | scrollToSelectedTaskTitle |
'Scroll to task' |
| Icon | locate |
A crosshair |
- Registered per instance. The constructor calls the inherited
registerCommand()and adds the key to the instance's messages, so a host replaces either the usual way:registerCommand('scrollToSelectedTask', fn),setMessages({ scrollToSelectedTaskTitle }). - Dispatched like every command. A click runs it through
dispatchCommand(): the cancelablevn-grid-toolbar-commandevent fires first, andpreventDefault()vetoes it. - Enabled by the markup.
disabled-when="!hasSingleSelection"is the page's choice, as!hasFilterOrSortis for clearing filters; the export gate disables it during an export too. The selection it reads is the task grid's single selection, which a row click, a bar press or the keys (Space, the row keys) set (Task Moving § 4). - The icon is registered once, inside the definition guard, with the
public
VanillaGridToolbarElement.registerIcon('locate', svg). Registered icons are declared on:is(vn-grid-toolbar, vn-gantt-toolbar), as the built-in ones, so they reach both tags. - A task the search or a filter hides has no row to centre: the timeline still scrolls to its dates, and focus stays on the button.
autoFitTaskList
autoFitTaskList fits the task grid's columns, then moves the splitter to
the last column's right edge. The inherited autoFitAllColumns fits the
columns only and leaves the task list's width as it is (columns wider than
the pane scroll sideways, narrower ones leave a strip of row filler); it stays
unchanged, as every inherited command does.
<vn-grid-toolbar-command command="autoFitTaskList" icon="auto-fit" variant="ghost"
title-key="autoFitTaskListTitle"></vn-grid-toolbar-command>
| Kind | Name | Value |
|---|---|---|
| Command | autoFitTaskList |
Passes arg to the task grid's autoFitAllColumns() (Column Resizing § 14.2), then calls the linked Gantt's fitTaskListWidth() (Web Component § 8), and returns the auto-fit's boolean. false when unlinked |
| Message key | autoFitTaskListTitle |
'Auto-fit columns and task list' |
| Icon | auto-fit |
The grid toolbar's own; no new icon |
- The width follows the columns at most up to the width that leaves the
timeline its
minTimelineWidth; wider columns scroll sideways. Between fits the splitter and the columns are independent. - The pane is fitted even when no column was (every column non-resizable): the columns' widths as they are still set it.
- The event. A fit that changed the width fires
vn-gantt-task-list-resizedwithsource: 'fit'on the Gantt. - Registered, dispatched and gated like
scrollToSelectedTask:registerCommand('autoFitTaskList', fn)replaces it,setMessages({ autoFitTaskListTitle })localizes it, avn-grid-toolbar-commandlistener'spreventDefault()vetoes it, and the export gate disables its button during an export.
The gantt link
- The attribute.
ganttis the id of a<vn-gantt>. It is resolved withgetElementById()on every link, so the Gantt may not exist yet. - When it links. The toolbar links the Gantt's task grid once
getTaskGrid()returns it, that is, afterinitializeGantt()(the Gantt's return-type contract keeps itundefinedbefore). The grid is linked through the grid toolbar's own_linkGrid():whenDefined('vn-grid'),ready(), the grid listeners. - The wait. Until then the toolbar is unlinked: it renders its zero
state,
grid,ganttandgridApiarenull,ready()is pending, and it logs nothing. It waits with the grid toolbar's document watch (see the grid toolbar's linking), which re-checks on every DOM change. That covers each late stage: the Gantt element added, upgraded (before it is, it has nogetTaskGrid()), then initialized (its grid builds its DOM). The wait ends on the link, on aganttchange and on a real removal of the toolbar. - A wrong element. A
ganttid naming an element that isn't a<vn-gantt>warns once, then waits as for a missing id, linking only a<vn-gantt>that later takes the id. - The property.
ganttmirrors the grid toolbar'sgrid:- set an id and it is stored as the attribute;
- set an element and it is stored, with the attribute synced to its id
(removed when it has none); an element that isn't a
<vn-gantt>is ignored with a warning; - get returns the linked
<vn-gantt>, ornullwhile unlinked.
- A change of the attribute relinks, on a connected toolbar, as a
gridchange does on<vn-grid-toolbar>. Removing a linked Gantt, or changing its id, does not relink.
gantt is the only link
- Without
gantt(and no element set through the property) the toolbar stays unlinked: no fallback to a nearby Gantt or grid, and no watch. - The inherited
gridattribute andsetGrid()(thegridsetter included) are ignored, each with a warning that namesgantt. - The
gridgetter still returns the linked task grid: the items and commands read it, as on<vn-grid-toolbar>.
So a Gantt toolbar never links an unrelated grid.
The subclass contract
The grid toolbar splits its link resolution into two overridable steps (see 02-implementation.md § Subclass contract). The Gantt toolbar overrides both:
| Method | Grid toolbar | Gantt toolbar |
|---|---|---|
_resolveNamedGrid(warn) |
The <vn-grid> the grid attribute names |
The task grid of the <vn-gantt> the gantt attribute names (else of the element the property stored); null while that Gantt is missing, not upgraded or not initialized |
_resolveFallbackGrid() |
The nearest <vn-grid>, with a warning |
null |
It also overrides attributeChangedCallback() (for gantt and grid) and
setGrid(). The grid toolbar's CSS and tooltip helpers don't depend on its
tag name, so they apply to <vn-gantt-toolbar> as they are.
Theme
The toolbar mirrors the linked grid's theme, as every toolbar does when it
has no theme attribute of its own. The Gantt gives its task grid its own
theme (Themes), so the Gantt
toolbar follows the Gantt's theme, and switches with it. The grid toolbar's
base and theme sheets name both tags, :is(vn-grid-toolbar, vn-gantt-toolbar),
with the specificity of a type selector, so the cascade and host overrides
are the same on both elements.
Loading and the dist bundle
- Load order.
vanilla-grid-toolbar.js(or its bundle) first: the script extends its class. Whenwindow.VanillaGridToolbarElementis missing it logs one error naming the missing script and defines nothing. The Gantt's script may load before or after it: the toolbar needs the Gantt only when it links. Onproject-tasks-jsthe order is grid, grid toolbar, Gantt, Gantt toolbar; the Gantt toolbar is defined synchronously, connects beforevn-ganttis defined, and waits. - Build.
build.jslists it inCOMPONENTSand builds it through the generic single-file path, asvanilla-resize-box:heavyprofile (not a hot path), renamed tovanilla-gantt-toolbar.bundle.jsindist/vanilla-components/latest/vanilla-gantt-toolbar/, with the.d.tscopied beside it. A sample's<script>tag is rewritten to the shared folder, renamed to the bundle and stamped with?v=, like the others. - Top-level names.
_ganttToolbarLoggerand_vnGanttToolbarIsGanttTag; the class is declared inside the definition guard.tests/node/gantt-bundle-coexistence.test.jsevaluates it after the four other components' sources and fails on any redeclaration.
Performance
Not on a hot path once linked. While it waits, the document watch's cost is
the grid toolbar's (one MutationObserver, a record per DOM change); each
batch adds one getElementById() and one getTaskGrid() call. On
project-tasks-js it waits from page load to initializeGantt(), while the
DOM barely changes.
TypeScript declarations
src/vanilla-gantt-toolbar/vanilla-gantt-toolbar.d.ts is hand-authored. It
imports VanillaGridToolbarElement from the grid toolbar's declarations and
VanillaGanttElement from the Gantt's, declares the gantt accessor pair
(getter VanillaGanttElement | null, setter also taking a string), and maps
vn-gantt-toolbar in HTMLElementTagNameMap. The command names, message keys
and icon are strings, so they get no type; the class's doc comment lists
them.
tests/types/vanilla-gantt-toolbar.dts-typecheck.ts compiles against it, and
tests/node/dts-conformance.test.js checks the declared accessors against the
runtime and that the element's own public members are declared.