Skip to content

Commit 15de566

Browse files
authored
V1.2.7 (#59)
* update dev files * update docs, detypescriptify, simplify * update changelog * fix tooltipOptions type
1 parent 80127de commit 15de566

29 files changed

+313
-232
lines changed

changelog.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Changelog
22

3+
## [1.2.7](https://github.com/chrisrzhou/react-wordcloud/compare/v1.2.7...v1.2.6) (2020-08-07)
4+
- Internal code cleanup (detypscriptify source code)
5+
- Update docs, readme and other configs.
6+
7+
### 'Breaking' Changes: Tooltip CSS and Typings
8+
Decoupled the `tippy` CSS from the package. This will improve future versioning of the project, and is also the recommendation set by the `tippy` project. Tooltips with this new versions might look off without CSS styles, but not considering this a breaking change because you can easily add the following imports to address the issue:
9+
10+
```js
11+
import 'tippy.js/dist/tippy.css';
12+
import 'tippy.js/animations/scale.css'
13+
```
14+
15+
Some rarely used typings are no longer exported (e.g. `Enter`, `Spiral`, `WordToStringCallback`, `WordEventCallback`, `MinMaxPair`, `AttributeValue`). While this is a breaking change, taking the liberty to assume this as non-breaking in an effort to constrain the exposed typings for improved future versioning of the project.
16+
317
## [1.2.6](https://github.com/chrisrzhou/react-wordcloud/compare/v1.2.6...v1.2.5) (2020-07-21)
418
- Correctly expose typings that were missing in `v1.2.5`.
519
- Fix docs deploy by adding `.node-version`.

docs/common-issues.mdx

+80-14
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,76 @@ name: Common Issues
33
route: /common-issues
44
---
55

6-
import { Playground } from "docz";
7-
import { useMemo, useState } from "react";
6+
import { Playground } from 'docz';
7+
import { useMemo, useState } from 'react';
88

9-
import ReactWordcloud from "~/src";
10-
11-
import words from "./words";
9+
import ReactWordcloud from './react-wordcloud';
10+
import words from './words';
1211

1312
# Common Issues
1413

1514
## Re-rendering issues
1615

17-
It is a common scenario that
18-
`react-wordcloud` will unintentionally re-render (i.e. grow from the center) when the `size`, `callbacks`, `options` props are updated. Note that `react-wordcloud` is **unopinionated** about this rendering behavior since this is the default React behavior, namely that changes in props will re-render the component.
19-
20-
You should instead try to define these props statically (e.g. outside of the component) or memoize them accordingly. The comparison example below shows how to avoid this issue with `useMemo`.
16+
Various props for `react-wordcloud` such as `size`, `callbacks`, `options` are array/object-based. A common mistake in React components is to declare these conveniently as literal prop values:
17+
18+
```js
19+
function MyWordcloud() {
20+
return (
21+
<ReactWordcloud
22+
options={{
23+
fontFamily: 'courier new',
24+
fontSizes: [10, 20],
25+
}}
26+
size={[400, 300]}
27+
/>
28+
)
29+
}
30+
```
31+
32+
When your component re-renders, the array/object literal props are always initialized as new instances, and this causes the underlying `react-wordcloud` component to re-render as props have changed (React behavior). To avoid this situation, either declare these prop values outside of your component, or memoize them:
33+
34+
```js
35+
const size = [400, 300];
36+
const options = {
37+
fontFamily: 'courier new',
38+
fontSizes: [10, 20],
39+
};
40+
41+
function MyStaticWordcloud() {
42+
return (
43+
<ReactWordcloud
44+
options={options}
45+
size={size}
46+
/>
47+
)
48+
}
49+
```
50+
51+
```js
52+
import { useMemo } from 'react';
53+
54+
function MyMemoizedWordcloud() {
55+
const size = useMemo(() => {
56+
return [400, 300];
57+
}, []); // add dependencies to useMemo where appropriate
58+
59+
const options = useMemo(() => {
60+
return {
61+
fontFamily: 'courier new',
62+
fontSizes: [10, 20],
63+
}
64+
}, []); // add dependencies to useMemo where appropriate
65+
66+
return (
67+
<ReactWordcloud
68+
options={options}
69+
size={size}
70+
/>
71+
);
72+
}
73+
```
74+
75+
Here is a playground that illustrates the difference between memoizing th;e `size` prop.
2176

2277
<Playground>
2378
{() => {
@@ -29,9 +84,9 @@ You should instead try to define these props statically (e.g. outside of the com
2984
<button onClick={() => setIteration(iteration + 1)}>
3085
Click to re-render
3186
</button>
32-
<h3>Will re-render because the "size" prop is always recreated</h3>
87+
<h3>Will re-render because the 'size' prop is always recreated</h3>
3388
<ReactWordcloud size={size} words={words} />
34-
<h3>Will not re-render because the "size" prop is memoized</h3>
89+
<h3>Will not re-render because the 'size' prop is memoized</h3>
3590
<ReactWordcloud size={memoizedSize} words={words} />
3691
</div>
3792
);
@@ -62,9 +117,9 @@ This issue happens when the most _frequent_ word is also the _longest_ word. For
62117
<ReactWordcloud
63118
size={[200, 200]}
64119
words={[
65-
{ text: "this_is_a_long_word_and_also_the_most_frequent", value: 60 },
66-
{ text: "some_word", value: 30 },
67-
{ text: "another_word", value: 20 },
120+
{ text: 'this_is_a_long_word_and_also_the_most_frequent', value: 60 },
121+
{ text: 'some_word', value: 30 },
122+
{ text: 'another_word', value: 20 },
68123
]}
69124
/>
70125
</Playground>
@@ -74,3 +129,14 @@ If you see this console warning, it is recommended that you address it in the fo
74129
- Increase the wordcloud size (either using the `size` prop or the parent container).
75130
- Reduce the `options.fontSizes` values.
76131
- Avoid rendering long words at vertical angles (i.e. 90 degrees). Browser heights are more limited than widths, and the long words may not fit within the wordcloud height. You can control rotation angles using `rotationAngles` and `rotations` in the `options` props.
132+
133+
## Typescript errors
134+
Common typescript issues are frequently experienced when working with `Scale`, `Spiral`, `MinMaxPair` types where providing literal number arrays or strings will cause the TS compiler to complain. Most of these are type aliases (e.g. `[number, number]` and `'linear' | 'log' | 'sqrt'`) and you can resolve these issues by type-asserting them e.g.
135+
136+
```ts
137+
const fonSizes: MinMaxPair = [10, 20];
138+
const fontSizes = [10, 20] as MinMaxPair;
139+
140+
const scale = 'linear' as const;
141+
const scale = <const> 'linear';
142+
```

docs/hero.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { useEffect, useState } from 'react';
22

3-
import ReactWordcloud, { MinMaxPair, Scale, Spiral } from '~/src';
4-
import { choose } from '~/src/utils';
5-
3+
import { choose } from '../src/utils';
4+
import ReactWordcloud, { MinMaxPair, Scale, Spiral } from './react-wordcloud';
65
import words from './words';
76

87
const { random } = Math;

docs/index.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Home
33
route: /
44
---
55

6-
import Hero from "./hero";
6+
import Hero from './hero';
77

88
# `react-wordcloud`
99

docs/props/callbacks-interface.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { CallbacksProp } from '~/src';
1+
import { CallbacksProp } from '../react-wordcloud';
22

3-
function CallbacksInterface(_props: CallbacksProp): JSX.Element {
3+
export default function CallbacksInterface(_props: CallbacksProp): JSX.Element {
44
return null;
55
}
6-
7-
export default CallbacksInterface;

docs/props/component-props.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { Props } from '~/src';
1+
import { Props } from '../react-wordcloud';
22

3-
function ComponentProps(_props: Props): JSX.Element {
3+
export default function ComponentProps(_props: Props): JSX.Element {
44
return null;
55
}
6-
7-
export default ComponentProps;

docs/props/options-interface.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { OptionsProp } from '~/src';
1+
import { OptionsProp } from '../react-wordcloud';
22

3-
function OptionsInterface(_props: OptionsProp): JSX.Element {
3+
export default function OptionsInterface(_props: OptionsProp): JSX.Element {
44
return null;
55
}
6-
7-
export default OptionsInterface;

docs/props/props.mdx

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ name: Props
33
route: /props
44
---
55

6-
import { Props } from "docz";
6+
import { Props } from 'docz';
77

8-
import ReactWordcloud from "~/src";
9-
10-
import CallbacksInterface from "./callbacks-interface";
11-
import ComponentProps from "./component-props";
12-
import OptionsInterface from "./options-interface";
13-
import WordInterface from "./word-interface";
8+
import CallbacksInterface from './callbacks-interface';
9+
import ComponentProps from './component-props';
10+
import OptionsInterface from './options-interface';
11+
import WordInterface from './word-interface';
1412

1513
# Props
1614

docs/props/word-interface.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { Word } from '~/src';
1+
import { Word } from '../react-wordcloud';
22

3-
function WordInterface(_props: Word): JSX.Element {
3+
export default function WordInterface(_props: Word): JSX.Element {
44
return null;
55
}
6-
7-
export default WordInterface;

docs/react-wordcloud.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
3+
import 'tippy.js/dist/tippy.css';
4+
import 'tippy.js/animations/scale.css';
5+
6+
import ReactWordcloudSrc, { Props } from '..';
7+
8+
export * from '..';
9+
10+
export default function ReactWordcloud(props: Props): JSX.Element {
11+
return <ReactWordcloudSrc {...props} />;
12+
}

docs/usage/basic.mdx

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ menu: Usage
44
route: /usage/basic
55
---
66

7-
import { Playground } from "docz";
7+
import { Playground } from 'docz';
88

9-
import ReactWordcloud from "~/src";
10-
11-
import words from "../words";
9+
import ReactWordcloud from '../react-wordcloud';
10+
import words from '../words';
1211

1312
# Basic Example
1413

docs/usage/callbacks.mdx

+15-16
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ menu: Usage
44
route: /usage/callbacks
55
---
66

7-
import { select } from "d3-selection";
8-
import { Playground } from "docz";
7+
import { select } from 'd3-selection';
8+
import { Playground } from 'docz';
99

10-
import ReactWordcloud from "~/src";
11-
12-
import words from "../words";
10+
import ReactWordcloud from '../react-wordcloud';
11+
import words from '../words';
1312

1413
# Callbacks
1514

@@ -25,9 +24,9 @@ In the example below, we can pass custom `getWordColor` and `getWordTooltip` cal
2524
<ReactWordcloud
2625
words={words}
2726
callbacks={{
28-
getWordColor: ({ value }) => (value > 50 ? "blue" : "red"),
27+
getWordColor: ({ value }) => (value > 50 ? 'blue' : 'red'),
2928
getWordTooltip: ({ text, value }) =>
30-
`${text} (${value}) [${value > 50 ? "good" : "bad"}]`,
29+
`${text} (${value}) [${value > 50 ? 'good' : 'bad'}]`,
3130
}}
3231
/>
3332
</Playground>
@@ -39,26 +38,26 @@ You can pass callbacks such as `onWordClick`, `onWordMouseEnter` and `onWordMous
3938
<Playground>
4039
{() => {
4140
const getCallback = callbackName => (word, event) => {
42-
const isActive = callbackName !== "onWordMouseOut";
41+
const isActive = callbackName !== 'onWordMouseOut';
4342
const element = event.target;
4443
const text = select(element);
4544
text
46-
.on("click", () => {
45+
.on('click', () => {
4746
if (isActive) {
48-
window.open(`https://duckduckgo.com/?q=${word.text}`, "_blank");
47+
window.open(`https://duckduckgo.com/?q=${word.text}`, '_blank');
4948
}
5049
})
5150
.transition()
52-
.attr("background", "white")
53-
.attr("font-size", isActive ? "300%" : "100%")
54-
.attr("text-decoration", isActive ? "underline" : "none");
51+
.attr('background', 'white')
52+
.attr('font-size', isActive ? '300%' : '100%')
53+
.attr('text-decoration', isActive ? 'underline' : 'none');
5554
};
5655
return (
5756
<ReactWordcloud
5857
callbacks={{
59-
onWordClick: getCallback("onWordClick"),
60-
onWordMouseOut: getCallback("onWordMouseOut"),
61-
onWordMouseOver: getCallback("onWordMouseOver"),
58+
onWordClick: getCallback('onWordClick'),
59+
onWordMouseOut: getCallback('onWordMouseOut'),
60+
onWordMouseOver: getCallback('onWordMouseOver'),
6261
}}
6362
words={words}
6463
/>

docs/usage/optimizations.mdx

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ menu: Usage
44
route: /usage/optimizations
55
---
66

7-
import ReactWordcloud from '~/src';
8-
7+
import ReactWordcloud from '../react-wordcloud';
98
import words from '../words';
109

1110
# Optimizations

0 commit comments

Comments
 (0)