Skip to content

Commit d57ce46

Browse files
committed
docs(readme): styling for default nodes and links
1 parent 5c19ff3 commit d57ce46

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

README.md

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ React D3 Tree is a [React](http://facebook.github.io/react/) component that lets
3131
- [Installation](#installation)
3232
- [Usage](#usage)
3333
- [Props](#props)
34+
- [Up & Running](#up--running)
35+
- [Styling nodes](#styling-nodes)
36+
- [Styling links](#styling-links)
37+
- [Rendering custom nodes](#rendering-custom-nodes)
3438
- [Development](#development)
3539
- [Setup](#setup)
3640
- [Hot reloading](#hot-reloading)
@@ -56,6 +60,8 @@ npm i --save react-d3-tree
5660
import React from 'react';
5761
import Tree from 'react-d3-tree';
5862

63+
// This is a simplified example of an org chart with a depth of 2.
64+
// Note how deeper levels are defined recursively via the `children` property.
5965
const orgChartJson = {
6066
name: 'CEO',
6167
children: [
@@ -72,7 +78,7 @@ const orgChartJson = {
7278
},
7379
children: [
7480
{
75-
name: 'Workers',
81+
name: 'Worker',
7682
},
7783
],
7884
},
@@ -83,7 +89,7 @@ const orgChartJson = {
8389
},
8490
children: [
8591
{
86-
name: 'Workers',
92+
name: 'Worker',
8793
},
8894
],
8995
},
@@ -105,6 +111,94 @@ export default function OrgChartTree() {
105111
## Props
106112
For details on the props accepted by `Tree`, check out the [TreeProps reference docs](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html).
107113

114+
<!--(TODO: REVISE TITLE) -->
115+
## Up & Running
116+
### Styling nodes
117+
`Tree` provides the following props to style different types of nodes, all of which use an SVG `circle` by default:
118+
119+
- `rootNodeClassName` - will be applied to the root node.
120+
- `branchNodeClassName` - will be applied to any node with 1+ children.
121+
- `leafNodeClassName` - will be applied to any node without children.
122+
123+
To visually distinguish these three types of nodes from each other by color, we could provide each with their own class:
124+
125+
```css
126+
/* custom-tree.css */
127+
128+
.node__root {
129+
fill: red;
130+
}
131+
.node__branch {
132+
fill: yellow
133+
}
134+
.node__leaf {
135+
fill: green;
136+
}
137+
```
138+
139+
```jsx
140+
import React from 'react';
141+
import Tree from 'react-d3-tree';
142+
import './custom-tree.css';
143+
144+
// ...
145+
146+
export default function StyledNodesTree() {
147+
return (
148+
<div id="treeWrapper" style={{ width: '50em', height: '20em' }}>
149+
<Tree
150+
data={data}
151+
rootNodeClassName="node__root"
152+
branchNodeClassName="node__branch"
153+
leafNodeClassName="node__leaf"
154+
/>
155+
</div>
156+
);
157+
}
158+
```
159+
160+
TODO: link to TreeProps page
161+
162+
> For more details on the `className` props for nodes, see the [TreeProps reference docs](https://bkrem.github.io/react-d3-tree/docs/interfaces/_tree_types_.treeprops.html).
163+
164+
<!-- For a full list of options of CSS properties that can be used for the default nodes, check the SVG circle [specification](TODO:) -->
165+
166+
### Styling links
167+
`Tree` provides the `pathClassFunc` property to pass additional classNames to every link to be rendered.
168+
169+
Each link calls `pathClassFunc` with its own `TreeLinkDatum` and the tree's current `orientation`. `Tree` expects `pathClassFunc` to return a `className` string.
170+
171+
```jsx
172+
function StyledLinksTree() {
173+
const getDynamicPathClass = ({ sourceNode, targetNode }, orientation) => {
174+
if (!targetNode.children) {
175+
// Node has no children -> this link leads to a leaf node.
176+
return 'link__to-leaf';
177+
}
178+
179+
// Style it as a link connecting two branch nodes by default.
180+
return 'link__to-branch';
181+
};
182+
183+
return (
184+
<Tree
185+
data={data}
186+
// Statically apply same className(s) to all links
187+
pathClassFunc={() => 'custom-link'}
188+
// Want to apply multiple static classes? `Array.join` is your friend :)
189+
pathClassFunc={() => ['custom-link', 'extra-custom-link'].join(' ')}
190+
// Dynamically determine which `className` to pass based on the link's properties.
191+
pathClassFunc={getDynamicPathClass}
192+
/>
193+
);
194+
}
195+
```
196+
197+
> For more details, see the `PathClassFunction` [reference docs](https://bkrem.github.io/react-d3-tree/docs/modules/_types_common_.html#pathclassfunction).
198+
199+
## Rendering custom nodes
200+
TODO:
201+
108202
## Development
109203
### Setup
110204
To set up `react-d3-tree` for local development, clone the repo and follow the steps below:

0 commit comments

Comments
 (0)