Skip to main content
The ContextMenu component wraps the Gantt component and intercepts right-click events on tasks. It presents a menu of task actions (add, edit, delete, move, convert, etc.) and dispatches them to the Gantt via the shared api object.

Import

import { ContextMenu } from "@svar-ui/svelte-gantt";

Basic usage

Wrap the Gantt component with ContextMenu and pass the same api reference to both:
<script>
  import { Gantt, ContextMenu, Editor } from "@svar-ui/svelte-gantt";
  import { getData } from "./data";

  const data = getData();
  let api = $state();
</script>

<ContextMenu {api}>
  <Gantt
    bind:this={api}
    tasks={data.tasks}
    links={data.links}
    scales={data.scales}
  />
</ContextMenu>
<Editor {api} />

Props

api
IApi
required
The Gantt API object. Obtain it by binding this on the Gantt component. The context menu uses this reference to dispatch task actions when a menu item is selected.
<script>
  let api = $state();
</script>

<ContextMenu {api}>
  <Gantt bind:this={api} tasks={[]} scales={[]} />
</ContextMenu>
items
IOptionConfig[]
Array of menu option definitions that override the default menu. When omitted, the default set of options is used.
interface IOptionConfig {
  id?: TID;
  separator?: boolean;
  text?: string;
  icon?: string;
  type?: string;
  subtext?: string;
  data?: IOptionConfig[];  // sub-menu items
  isDisabled?: (task: IParsedTask, state: IData, target?: TID | IDataHash) => boolean;
  isHidden?:   (task: IParsedTask, state: IData, target?: TID | IDataHash) => boolean;
}
children
Snippet
The Gantt component (or any other content) to wrap. The context menu attaches its listener to the content inside this slot.

Default menu items

IDLabelNotes
add-taskAddSub-menu: Child task, Task above, Task below
(separator)
convert-taskConvert toSub-menu: built from taskTypes
edit-taskEditHidden for segments
(separator)
cut-taskCutCtrl+X
copy-taskCopyCtrl+C
paste-taskPasteCtrl+V
move-taskMoveSub-menu: Up, Down
(separator)
indent-task:addIndent
indent-task:removeOutdent
(separator)
delete-taskDeleteCtrl+D / Backspace

Customising menu items

Replace the default menu with a minimal set:
<script>
  import { Gantt, ContextMenu } from "@svar-ui/svelte-gantt";

  let api = $state();

  const items = [
    { id: "edit-task",   text: "Edit",   icon: "wxi-edit" },
    { id: "delete-task", text: "Delete", icon: "wxi-delete" },
  ];
</script>

<ContextMenu {api} {items}>
  <Gantt bind:this={api} tasks={[]} scales={[]} />
</ContextMenu>
Import defaultMenuOptions from @svar-ui/svelte-gantt and spread it to extend the built-in menu rather than replacing it entirely.

Build docs developers (and LLMs) love