Skip to content

feat: add getTreeLinePrefix utility for tree connector line#324

Merged
TrevorBurnham merged 1 commit intojameskerr:mainfrom
rayshoo:feat/tree-lines
Apr 19, 2026
Merged

feat: add getTreeLinePrefix utility for tree connector line#324
TrevorBurnham merged 1 commit intojameskerr:mainfrom
rayshoo:feat/tree-lines

Conversation

@rayshoo
Copy link
Copy Markdown
Contributor

@rayshoo rayshoo commented Mar 18, 2026

Add a getTreeLinePrefix() utility function that generates tree connector line prefixes (, , ) for nodes, similar to the Unix tree command output.

Currently, react-arborist does not provide a built-in way to render tree connector lines. Users have to implement the parent-traversal logic themselves in custom node renderers. This utility solves that.

Features

  • Generates correct connector lines by traversing ancestor nodes
  • Customizable characters via TreeLineChars option
  • Works with any tree depth
  • Exported from package root for easy access

Usage

import { getTreeLinePrefix } from "react-arborist";

function MyNode({ node, style }: NodeRendererProps<MyData>) {
  const icon = node.isLeaf ? "📄" : node.isOpen ? "📂" : "📁";
  return (
    <div style={style}>
      <span style={{ fontFamily: "monospace", fontSize: 16 }}>
        {getTreeLinePrefix(node)}
      </span>
      {icon} {node.data.name}
    </div>
  );
}


Result

📂 src
 📂 components
  📄 Button.tsx
  📄 Input.tsx
  📂 layout
    📄 Header.tsx
    📄 Footer.tsx
    📄 Sidebar.tsx
 📂 pages
  📄 Home.tsx
  📄 About.tsx
 📄 App.tsx
 📄 main.tsx
📂 public
 📄 favicon.ico
 📄 index.html
📄 package.json
📄 README.md

Styling Note

The prefix uses Box Drawing characters which require a monospace font for correct alignment. Wrap the prefix in a <span> with fontFamily: "monospace" and use a consistent fontSize (e.g. 14–16px). Inherited line-height or font-size from parent elements
can cause misalignment.

Custom Characters

// ASCII-only style
getTreeLinePrefix(node, { last: "`- ", middle: "|- ", pipe: "| ", blank: "  " })

Add a utility function that generates tree-line prefix strings
(├, └, │) for nodes, similar to the Unix `tree` command output.

This enables users to render visual tree connectors in their
custom node renderers without implementing the parent-traversal
logic themselves.

Features:
- Generates correct connector lines by traversing ancestor nodes
- Customizable characters via TreeLineChars option
- Works with any tree depth
- Exported from package root for easy access

Styling note:
The prefix uses Box Drawing characters which require a monospace
font for correct alignment. Wrap the prefix in a <span> with
fontFamily: "monospace" and use a consistent fontSize (e.g. 14-16px).
Inherited line-height or font-size from parent elements can cause
misalignment.

Usage:
```tsx
import { getTreeLinePrefix } from "react-arborist";

function MyNode({ node, style }) {
  const icon = node.isLeaf ? "📄" : node.isOpen ? "📂" : "📁";
  return (
    <div style={style}>
      <span style={{ fontFamily: "monospace", fontSize: 16 }}>
        {getTreeLinePrefix(node)}
      </span>
      {icon} {node.data.name}
    </div>
  );
}
```
@TrevorBurnham TrevorBurnham merged commit 3e9f4a9 into jameskerr:main Apr 19, 2026
@TrevorBurnham TrevorBurnham mentioned this pull request Apr 20, 2026
3 tasks
TrevorBurnham added a commit that referenced this pull request Apr 20, 2026
- Document dndBackend prop (#326) and correct dndManager type in TreeProps
- Document opts arg on tree.select / tree.selectMulti (#266)
- Correct childrenAccessor return type to readonly T[] | null
- Add Utilities section documenting getTreeLinePrefix (#324)
TrevorBurnham added a commit that referenced this pull request Apr 20, 2026
- Document dndBackend prop (#326) and correct dndManager type in TreeProps
- Document opts arg on tree.select / tree.selectMulti (#266)
- Correct childrenAccessor return type to readonly T[] | null
- Add Utilities section documenting getTreeLinePrefix (#324)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants