Skip to content

Commit 999e859

Browse files
committed
improve: allow adding class names to words
This commit adds new prop `wordClassNames`, which allows to define custom class name(s) for each word separately. This expands the ability to customize words presentation at call-site.
1 parent b7a35e3 commit 999e859

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ for more detailed props, please refer to below:
100100
| padding | The padding accessor function, which indicates the numerical padding for each word. | `number \| (d) => number` | | `1` |
101101
| random | The internal random number generator, used for selecting the initial position of each word, and the clockwise/counterclockwise direction of the spiral for each word. This should return a number in the range `[0, 1)`. | `(d) => number` | | `Math.random` |
102102
| fill | The fill accessor function, which indicates the color for each word. | `(d, i) => string` | | `(d, i) => schemeCategory10ScaleOrdinal(i)` |
103+
| wordClassNames | The classNames accessor function, which indicates the `class` for each word. | `(d, i) => string` | | `() => ''` |
103104
| onWordClick | The function will be called when `click` event is triggered on a word | `(event, d) => {}` | | null |
104105
| onWordMouseOver | The function will be called when `mouseover` event is triggered on a word | `(event, d) => {}` | | null |
105106
| onWordMouseOut | The function will be called when `mouseout` event is triggered on a word | `(event, d) => {}` | | null |

src/WordCloud.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type WordCloudProps = {
3535
padding?: number | ((word: Word, index: number) => number);
3636
random?: () => number;
3737
fill?: ValueFn<SVGTextElement, Word, string>;
38+
wordClassNames?: ValueFn<SVGTextElement, Word, string>;
3839
onWordClick?: (this: BaseType, event: any, d: Word) => void;
3940
onWordMouseOver?: (this: BaseType, event: any, d: Word) => void;
4041
onWordMouseOut?: (this: BaseType, event: any, d: Word) => void;
@@ -58,6 +59,7 @@ function WordCloud({
5859
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5960
// @ts-ignore The ordinal function should accept number
6061
fill = (_, i) => defaultScaleOrdinal(i),
62+
wordClassNames = () => '',
6163
onWordClick,
6264
onWordMouseOver,
6365
onWordMouseOut,
@@ -115,6 +117,7 @@ function WordCloud({
115117
((d) => `${d.size}px`) as ValueFn<SVGTextElement, Word, string>
116118
)
117119
.style('fill', fill)
120+
.attr('class', (d, i, nodes) => wordClassNames.call(nodes[i], d, i, nodes))
118121
.attr('text-anchor', 'middle')
119122
.attr('transform', (d) => `translate(${[d.x, d.y]})rotate(${d.rotate})`)
120123
.text((d) => d.text);

0 commit comments

Comments
 (0)