LayerTree
layer-treeVisual-editor layer panel with nested groups, drag reordering, inline rename, visibility/lock toggles, and multi-select.
Usage
Slide Layer Panel
Supports group expand/collapse, click / Cmd multi-select / Shift range select, drag reordering into groups, double-click or F2 rename, and quick visibility/lock toggles.
const [layers, setLayers] = useState<LayerNode[]>(slideLayers);
const [selectedIds, setSelectedIds] = useState<string[]>(["main-title"]);
<LayerTree
layers={layers}
onLayersChange={setLayers}
selectedIds={selectedIds}
onSelectionChange={setSelectedIds}
/>Extra Row Actions & Badges
Mount status badges or quick action triggers on specific layers via renderActions.
<LayerTree
layers={layers}
onLayersChange={setLayers}
renderActions={(node) =>
node.type === "chart" ? <span className="text-[10px] text-primary">Live</span> : null
}
/>When to Use
Use LayerTree for the left-side layer panel in a design canvas, slide deck editor, or visual page builder alongside DesignCanvas and InspectorPanel. It handles nested layer groups, multi-selection, hover highlight synchronization, drag-and-drop nesting, and visibility/lock toggles.
- For repository directories with Git status badges, use FileTree.
- For general hierarchical data trees or checkable trees, use Tree.
- For flat sortable lists, use Sortable.
Import
import {
LayerTree,
flattenVisibleLayers,
moveLayerNode,
updateLayerNode,
collectLayerIds,
collectExpandableIds,
} from "@hulianui/ui"Props
| Name | Type | Default | Description |
|---|---|---|---|
| layers* | LayerNode[] | — | Layer tree data |
| selectedIds | string[] | — | Controlled list of selected layer IDs |
| defaultSelectedIds | string[] | [] | Initial selected layer IDs in uncontrolled mode |
| hoveredId | string | null | — | Controlled hovered layer ID for bidirectional canvas highlight sync |
| expandedIds | string[] | — | Controlled list of expanded group IDs |
| defaultExpandedIds | string[] | All group IDs | Initial expanded group IDs in uncontrolled mode (expands all group and frame nodes by default) |
| draggable | boolean | true | Enables drag-and-drop reordering and nesting |
| renameable | boolean | true | Enables inline renaming via double-click or F2 / Enter |
| readOnly | boolean | false | Disables drag, rename, visibility, and lock mutations while keeping selection and expand/collapse active |
| emptyText | string | "No layers" (zh-CN default in runtime) | Placeholder text shown when layers is empty |
| labels | Partial<LayerTreeLabels> | Built-in zh-CN labels | Overrides accessible and action button labels (tree, expand, collapse, show, hide, lock, unlock, rename) |
| className | string | — | Additional class name for the root tree container |
Events
| Event | Type | Description |
|---|---|---|
| onLayersChange | (layers: LayerNode[]) => void | Emits the full updated immutable tree after drag-reorder, rename, visibility, or lock changes |
| onSelectionChange | (ids: string[]) => void | Emits updated selection IDs on single click, Ctrl/Cmd toggle, or Shift visible-row range select |
| onHoverChange | (id: string | null) => void | Emits the hovered layer ID on pointer enter and null on pointer leave |
| onExpandedChange | (ids: string[]) => void | Emits updated expanded group IDs on expand/collapse |
| onRename | (id: string, name: string) => void | Called when an inline rename is confirmed (also triggers onLayersChange if provided) |
| onToggleVisible | (id: string, visible: boolean) => void | Called when visibility is toggled (also triggers onLayersChange if provided) |
| onToggleLocked | (id: string, locked: boolean) => void | Called when lock state is toggled (also triggers onLayersChange if provided) |
| onMove | (info: LayerTreeMoveInfo) => void | Called after a drag-and-drop move with { dragId, targetId, position, layers } |
Slots
| Slot | Type | Description |
|---|---|---|
| renderIcon | (node: LayerNode) => ReactNode | Custom icon renderer overriding built-in layer type icons and node.icon |
| renderActions | (node: LayerNode) => ReactNode | Optional extra row actions rendered to the left of the lock and visibility toggles |
Data Structures
LayerNode (aliased as LayerTreeItem)
| Name | Type | Default | Description |
|---|---|---|---|
| id* | string | — | Unique layer identifier |
| name* | string | — | Display name of the layer |
| type | "group" | "text" | "image" | "shape" | "chart" | "html" | "vector" | "media" | "frame" | "shape" | Layer kind controlling the built-in icon and container nesting behavior |
| visible | boolean | true | Visibility flag; descendants inherit hidden state when an ancestor is hidden |
| locked | boolean | false | Lock flag; descendants inherit locked state when an ancestor is locked |
| children | LayerNode[] | — | Child layers for container nodes such as group or frame |
| icon | ReactNode | — | Optional per-node custom icon override |
FlattenedLayerNode (returned by flattenVisibleLayers)
| Name | Type | Default | Description |
|---|---|---|---|
| node* | LayerNode | — | Reference to the original layer node |
| depth* | number | — | Nesting depth (0 for root layers) |
| parentId* | string | null | — | Parent node ID (null for root layers) |
| hasChildren* | boolean | — | Whether the node has one or more child layers |
| effectiveVisible* | boolean | — | Computed visibility inheriting ancestor hidden state |
| effectiveLocked* | boolean | — | Computed lock state inheriting ancestor locked state |
LayerTreeMoveInfo (passed to onMove)
| Name | Type | Default | Description |
|---|---|---|---|
| dragId* | string | — | Dragged layer ID |
| targetId* | string | — | Drop target layer ID |
| position* | "before" | "inside" | "after" | — | Drop placement relative to the target |
| layers* | LayerNode[] | — | Resulting immutable layer tree after the move |
Pitfalls
- Handling updates without
onLayersChange:LayerTreedoes not keep an internal copy oflayers. If you only passonToggleVisible,onToggleLocked, oronRename, update your externallayersstate in those callbacks. WhenonLayersChangeis provided, the component emits both the updated immutable tree and the specific callback. - Nesting drops: Only nodes with
typeof"group"or"frame", or nodes that already have achildrenarray, accept"inside"drops. Dropping a group into itself or any of its descendants is automatically blocked.
Related
Playground
<LayerTree layers={layers} onLayersChange={setLayers} />