logo

X6

  • Tutorials
  • API
  • Examples
  • Q&A
  • Change Log
  • XFlow
  • Productsantv logo arrow
  • 2.x
  • Introduction
  • Quickstart
  • Basic
    • Graph
    • Nodes
    • Edges
    • Connection Pile
    • Interaction
    • Events
    • Data Serialization
  • Intermediate
    • Connection Points
    • Tools
    • Group
    • React Nodes
    • Vue Nodes
    • Angular Nodes
    • HTML Nodes
  • Plugin
    • Graphic Transformations
    • Snapline
    • Clipboard
    • Keyboard
    • History
    • Selection Box
    • Scroller
    • Dnd
    • Mini Map
    • Stencil
    • Export
  • Upgrade to Version 2.x
  • Developer Tools

Stencil

Previous
Mini Map
Next
Export

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

By reading this chapter, you will learn

  • How to further enhance DND capabilities through the Stencil plugin

Usage

Stencil is a further encapsulation based on Dnd, providing a sidebar-like UI component that supports grouping, collapsing, searching, and other capabilities. We offer a standalone plugin package @antv/x6-plugin-stencil to use this feature.

# npm
$ npm install @antv/x6-plugin-stencil --save
# yarn
$ yarn add @antv/x6-plugin-stencil

Then we can use it in the code like this:

import { Stencil } from '@antv/x6-plugin-stencil'
const graph = new Graph({
background: {
color: '#F2F7FA',
},
})
const stencil = new Stencil({
target: graph,
groups: [
{
name: 'group1',
},
{
name: 'group2',
},
],
})
const rect1 = graph.createNode({
shape: 'rect',
width: 100,
height: 40,
})
const rect2 = rect1.clone()
// A DOM container to hold the stencil, stencilContainer
stencilContainer.appendChild(stencil.container)
stencil.load([rect1, rect2], 'group1')

Demo

Configuration

OptionTypeRequiredDefault ValueDescription
titlestring'Stencil'Title.
groupsGroup[]✓️-Group information.
searchFilterfalseSearch option.
placeholderstring'Search'Placeholder text for the search input.
notFoundTextstring'No matches found'Text displayed when no search results are found.
collapsablebooleanfalseWhether to show a global collapse/expand button.
layout(this: Stencil, model: Model, group?: Group | null) => anyGrid layoutLayout method for nodes in the stencil canvas.
layoutOptionsany-Layout options.
stencilGraphWidthnumber200Width of the stencil canvas.
stencilGraphHeightnumber800Height of the stencil canvas. Set to 0 for auto-adjustment.
stencilGraphPaddingnumber10Padding for the stencil canvas.
stencilGraphOptionsGraph.Options-Options for the stencil canvas.

Tip

In addition to the configurations above, Stencil also inherits all configurations from Dnd.

Grouping

When initializing, a template canvas will be rendered in each group according to the groups provided. The type definition for groups is as follows:

export interface Group {
name: string // Group name
title?: string // Group title, defaults to `name`
collapsable?: boolean // Whether the group is collapsible, defaults to true
collapsed?: boolean // Initial state whether it is collapsed
nodeMovable?: boolean // Whether nodes in the stencil canvas can be dragged
graphWidth?: number // Width of the stencil canvas
graphHeight?: number // Height of the stencil canvas
graphPadding?: number // Padding for the stencil canvas
graphOptions?: Graph.Options // Options for the stencil canvas
layout?: (this: Stencil, model: Model, group?: Group | null) => any
layoutOptions?: any // Layout options
}

As you can see, some configurations within the group overlap with the outer configurations, such as graphWidth and stencilGraphHeight, with the group configurations taking higher priority.

Layout

When adding nodes, use the group or global layout and layoutOptions to automatically layout the nodes. By default, the grid layout method is used to layout the template nodes, and the supported layout options are:

OptionTypeDefault ValueDescription
columnsnumber2Number of columns in the grid layout, defaults to 2. The number of rows is calculated automatically based on the number of nodes.
columnWidthnumber | 'auto' | 'compact''auto'Column width. auto: width of the widest node among all nodes, compact: width of the widest node in that column.
rowHeightnumber | 'auto' | 'compact''auto'Row height. auto: height of the tallest node among all nodes, compact: height of the tallest node in that row.
dxnumber10Offset of the cell on the X-axis, defaults to 10.
dynumber10Offset of the cell on the Y-axis, defaults to 10.
marginXnumber0Margin of the cell on the X-axis, defaults to 0.
marginYnumber0Margin of the cell on the Y-axis, defaults to 0.
centerbooleantrueWhether the nodes are center-aligned with the grid, defaults to true.
resizeToFitbooleanfalseWhether to automatically adjust the size of the nodes to fit the grid size, defaults to false.

You can also implement a custom layout according to the signature (this: Stencil, model: Model, group?: Group | null) => any.

Search

Stencil also provides search capabilities.

// Only search for rect nodes
const stencil = new Stencil({
search: (cell, keyword, groupName, stencil) => {
if (keyword) {
return cell.shape === 'rect'
}
return true
},
})
// Search for rect nodes whose text contains the keyword
const stencil = new Stencil({
search: (cell, keyword, groupName, stencil) => {
if (keyword) {
return cell.shape === 'rect' && cell.attr('text/text').includes(keyword)
}
return true
},
})

API

stencil.load(...)

load(nodes: (Node | Node.Metadata)[], groupName?: string): this

Load nodes. Parameters are as follows:

NameTypeRequiredDefault ValueDescription
nodes(Node | Node.Metadata)[]√-Nodes to load.
groupNamestring-Name of the group to load nodes into.

stencil.unload(...)

unload(nodes: (Node | Node.Metadata)[], groupName?: string): this

Unload nodes. Parameters are as follows:

NameTypeRequiredDefault ValueDescription
nodes(Node | Node.Metadata)[]√-Nodes to unload.
groupNamestring-Name of the group to unload nodes from.

stencil.addGroup(...)

addGroup(group: Stencil.Group | Stencil.Group[])

Add a new group.

stencil.addGroup({
name: 'group1',
graphHeight: 100,
})

stencil.removeGroup(...)

removeGroup(groupName: string | string[])

Remove a group.

stencil.resizeGroup(...)

resizeGroup(groupName: string, size: { width: number; height: number })

Modify the size of a group.