Skip to content

Commit

Permalink
v1 ready
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisrzhou committed Mar 5, 2019
1 parent 1eaf0d8 commit fcf4522
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
},
],
'react-hooks/rules-of-hooks': 'error',
// 'react-hooks/exhaustive-deps': 'warn',
'react-hooks/exhaustive-deps': 'warn',
},
settings: {
react: {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ yarn dev

### Codebase Overview

- `index.tsx`: Pure React code that exposes an interface of props. Tooltip and cloud layout is also configured here.
- `render.ts`: Pure D3 rendering code when a D3 selection and other data are provided.
- `index.tsx`: Pure React code that exposes an interface of props.
- `render.ts`: Pure D3 rendering code to render wordcloud given a valid D3 selection and other data.
- `hooks.ts`: React hooks that build and destroy responsive SVG containers with D3.
- `types.ts`: Typescript types.
- `utils.ts`: Various simple functions used to compute derived data.
Expand Down
32 changes: 13 additions & 19 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ import { MinMaxPair, Selection } from './types';

const { useEffect, useRef, useState } = React;

export function usePrevious<T>(value: T): T {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
});
return ref.current;
}

export function useResize(ref: React.RefObject<HTMLDivElement>): MinMaxPair {
const [size, setSize] = useState<MinMaxPair>([0, 0]);

Expand All @@ -28,7 +20,7 @@ export function useResize(ref: React.RefObject<HTMLDivElement>): MinMaxPair {
});
resizeObserver.observe(element);
return () => resizeObserver.unobserve(element);
}, []);
}, [ref]);
return size;
}

Expand All @@ -41,30 +33,31 @@ export function useResponsiveSVG<T>(
const [size, setSize] = useState(initialSize);
const resized = useResize(ref);

const hasResized = initialSize === undefined && (resized[0] || resized[1]);

useEffect(() => {
const element = ref.current;
const {
offsetWidth: parentWidth,
offsetHeight: parentHeight,
} = element.parentNode;

// Update size with the following logic:
// - Use initialSize if it is provided
// - Use parentNode size if resized has not updated
// - Use resized when there are resize changes
// - Ensure that minSize is always applied/handled before updating size
let width;
let height;
let width = 0;
let height = 0;
// Use initialSize if it is provided
if (initialSize !== undefined) {
[width, height] = initialSize;
} else {
if (resized[0] === 0 && resized[0] === 0) {
// Use parentNode size if resized has not updated
if (resized[0] === 0 && resized[1] === 0) {
width = parentWidth;
height = parentHeight;
// Use resized when there are resize changes
} else {
[width, height] = resized;
}
}
// Ensure that minSize is always applied/handled before updating size
width = Math.max(width, minSize[0]);
height = Math.max(height, minSize[1]);
setSize([width, height]);
Expand All @@ -82,11 +75,12 @@ export function useResponsiveSVG<T>(
}

return () => {
d3.select(ref.current)
d3.select(element)
.selectAll('*')
.remove();
};
}, [initialSize, initialSize === undefined && (resized[0] || resized[1])]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [initialSize, hasResized]);

return [ref, selection, size];
}
7 changes: 6 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,12 @@ function Wordcloud({
}
}, [callbacks, maxWords, options, selection, size, words]);

return <div ref={ref} />;
// outer div is the parent container while inner div houses the wordcloud svg
return (
<div>
<div ref={ref} />
</div>
);
}

Wordcloud.defaultProps = {
Expand Down
89 changes: 52 additions & 37 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1660,9 +1660,9 @@ ajv-keywords@^3.1.0:
integrity sha512-aUjdRFISbuFOl0EIZc+9e4FfZp0bDZgAdOOf30bJmw8VM9v84SHyVyxDfbWxpGYbdZD/9XoKxfHVNmxPkhwyGw==

ajv@^6.1.0, ajv@^6.5.5, ajv@^6.9.1:
version "6.9.2"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.9.2.tgz#4927adb83e7f48e5a32b45729744c71ec39c9c7b"
integrity sha512-4UFy0/LgDo7Oa/+wOAlj44tp9K78u38E5/359eSrqEp1Z5PdVfimCcs7SluXMP755RUQu6d2b4AvF0R1C9RZjg==
version "6.10.0"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.10.0.tgz#90d0d54439da587cd7e843bfb7045f50bd22bdf1"
integrity sha512-nffhOpkymDECQyR0mnsUtoCE8RlX38G0rYP+wgLWFyZuUyuuojSSvi/+euOiQBIn63whYwYVIIH1TvE3tu4OEg==
dependencies:
fast-deep-equal "^2.0.1"
fast-json-stable-stringify "^2.0.0"
Expand Down Expand Up @@ -2485,14 +2485,14 @@ camelcase@^4.1.0:
integrity sha1-1UVjW+HjPFQmScaRc+Xeas+uNN0=

camelcase@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.0.0.tgz#03295527d58bd3cd4aa75363f35b2e8d97be2f42"
integrity sha512-faqwZqnWxbxn+F1d399ygeamQNy3lPp/H9H6rNrqYh4FSVCtcY+3cub1MxA8o9mDd55mM8Aghuu/kuyYA6VTsA==
version "5.2.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.2.0.tgz#e7522abda5ed94cc0489e1b8466610e88404cf45"
integrity sha512-IXFsBS2pC+X0j0N/GE7Dm7j3bsEBp+oTpb7F50dwEVX7rf3IgwO9XatnegTsDtniKCUtEJH4fSU6Asw7uoVLfQ==

caniuse-lite@^1.0.30000929, caniuse-lite@^1.0.30000939:
version "1.0.30000939"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000939.tgz#b9ab7ac9e861bf78840b80c5dfbc471a5cd7e679"
integrity sha512-oXB23ImDJOgQpGjRv1tCtzAvJr4/OvrHi5SO2vUgB0g0xpdZZoA/BxfImiWfdwoYdUTtQrPsXsvYU/dmCSM8gg==
version "1.0.30000941"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000941.tgz#f0810802b2ab8d27f4b625d4769a610e24d5a42c"
integrity sha512-4vzGb2MfZcO20VMPj1j6nRAixhmtlhkypM4fL4zhgzEucQIYiRzSqPcWIu1OF8i0FETD93FMIPWfUJCAcFvrqA==

capitalize@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -2981,14 +2981,14 @@ cosmiconfig@^5.0.5, cosmiconfig@^5.0.7:
parse-json "^4.0.0"

cp-file@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/cp-file/-/cp-file-6.0.0.tgz#f38477ece100b403fcf780fd34d030486beb693e"
integrity sha512-OtHMgPugkgwHlbph25wlMKd358lZNhX1Y2viUpPoFmlBPlEiPIRhztYWha11grbGPnlM+urp5saVmwsChCIOEg==
version "6.1.0"
resolved "https://registry.yarnpkg.com/cp-file/-/cp-file-6.1.0.tgz#b48d2d80577d4c5025c68eb597a38093c1dc9ccf"
integrity sha512-an34I0lJwKncRKjxe3uGWUuiIIVYsHHjBGKld3OQB56hfoPCYom31VysvfuysKqHLbz6drnqP5YrCfLw17I2kw==
dependencies:
graceful-fs "^4.1.2"
make-dir "^1.0.0"
make-dir "^2.0.0"
nested-error-stacks "^2.0.0"
pify "^3.0.0"
pify "^4.0.1"
safe-buffer "^5.0.1"

cpy@^7.0.1:
Expand Down Expand Up @@ -4155,9 +4155,9 @@ eslint-visitor-keys@^1.0.0:
integrity sha512-qzm/XxIbxm/FHyH341ZrbnMUpe+5Bocte9xkmFMzPMjRaZMcXww+MpBptFvtU+79L362nqiLhekCxCxDPaUMBQ==

eslint@^5.15.0:
version "5.15.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-5.15.0.tgz#f313a2f7c7628d39adeefdba4a9c41f842012c9e"
integrity sha512-xwG7SS5JLeqkiR3iOmVgtF8Y6xPdtr6AAsN6ph7Q6R/fv+3UlKYoika8SmNzmb35qdRF+RfTY35kMEdtbi+9wg==
version "5.15.1"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-5.15.1.tgz#8266b089fd5391e0009a047050795b1d73664524"
integrity sha512-NTcm6vQ+PTgN3UBsALw5BMhgO6i5EpIjQF/Xb5tIh3sk9QhrFafujUOczGz4J24JBlzWclSB9Vmx8d+9Z6bFCg==
dependencies:
"@babel/code-frame" "^7.0.0"
ajv "^6.9.1"
Expand Down Expand Up @@ -4197,9 +4197,9 @@ eslint@^5.15.0:
text-table "^0.2.0"

esm@^3.0.84:
version "3.2.9"
resolved "https://registry.yarnpkg.com/esm/-/esm-3.2.9.tgz#9dca653e3b39f89c3c65c5e84cebfa4af345c10d"
integrity sha512-mATFs9dpCjnEyNv27z29UNDmJmBBX8zMdcFip7aIOrBRTpLs8SA+6Ek1QtsWfvecAJVeZy+X5D3Z6xZVtUvYdg==
version "3.2.10"
resolved "https://registry.yarnpkg.com/esm/-/esm-3.2.10.tgz#fea61120e0d89b87897756f506f169c2fb0ca78e"
integrity sha512-r6v4kFfezz5/Qo6Q5ThaNPPUi7a2E3J4ZWxFuohhxJRAa6X7hg6Ca7GRPaixsi5ALcokhif/04Ys4zAxab4Edw==

espree@^5.0.1:
version "5.0.1"
Expand Down Expand Up @@ -5099,6 +5099,13 @@ hoist-non-react-statics@^2.3.1, hoist-non-react-statics@^2.5.0:
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-2.5.5.tgz#c5903cf409c0dfd908f388e619d86b9c1174cb47"
integrity sha512-rqcy4pJo55FTTLWt+bU8ukscqHeE/e9KWvsOW2b/a3afxQZhwkQdT1rPPCJ0rYXdj4vNcasY8zHTH+jF/qStxw==

hoist-non-react-statics@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/hoist-non-react-statics/-/hoist-non-react-statics-3.3.0.tgz#b09178f0122184fb95acf525daaecb4d8f45958b"
integrity sha512-0XsbTXxgiaCDYDIWFcwkmerZPSwywfUqYmwT4jzewKTQSWoE6FCMoUVOeBJWK3E/CrWbxRG3m5GzY4lnIwGRBA==
dependencies:
react-is "^16.7.0"

hoopy@^0.1.2:
version "0.1.4"
resolved "https://registry.yarnpkg.com/hoopy/-/hoopy-0.1.4.tgz#609207d661100033a9a9402ad3dea677381c1b1d"
Expand Down Expand Up @@ -6018,11 +6025,6 @@ lodash.keys@^3.1.2:
lodash.isarguments "^3.0.0"
lodash.isarray "^3.0.0"

lodash.merge@^4.6.1:
version "4.6.1"
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.1.tgz#adc25d9cb99b9391c59624f379fbba60d7111d54"
integrity sha512-AOYza4+Hf5z1/0Hztxpm2/xiPZgi/cjMqdnKTUWTBSKchJlxXXuUSxCCl8rJlf4g6yww/j6mA8nC8Hw/EZWxKQ==

lodash.throttle@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4"
Expand Down Expand Up @@ -6125,6 +6127,14 @@ make-dir@^1.0.0:
dependencies:
pify "^3.0.0"

make-dir@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5"
integrity sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==
dependencies:
pify "^4.0.1"
semver "^5.6.0"

mamacro@^0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/mamacro/-/mamacro-0.0.3.tgz#ad2c9576197c9f1abf308d0787865bd975a3f3e4"
Expand Down Expand Up @@ -7175,6 +7185,11 @@ pify@^3.0.0:
resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176"
integrity sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=

pify@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/pify/-/pify-4.0.1.tgz#4b2cd25c50d598735c50292224fd8c6df41e3231"
integrity sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==

pinkie-promise@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/pinkie-promise/-/pinkie-promise-2.0.1.tgz#2135d6dfa7a358c069ac9b178776288228450ffa"
Expand Down Expand Up @@ -7671,15 +7686,15 @@ react-feather@^1.1.5:
integrity sha512-iCofWhTjX+vQwvDmg7o6vg0XrUg1c41yBDZG+l83nz1FiCsleJoUgd3O+kHpOeWMXuPrRIFfCixvcqyOLGOgIg==

react-hot-loader@^4.6.3:
version "4.7.1"
resolved "https://registry.yarnpkg.com/react-hot-loader/-/react-hot-loader-4.7.1.tgz#1260b939297859cff35aff61412887a92c9b4275"
integrity sha512-OVq9tBndJ+KJWyWbj6kAkJbRVFx3Ykx+XOlndT3zyxAQMBFFygV+AS9RQi6Z2axkPIcEkuE5K6nkpcjlzR8I9w==
version "4.7.2"
resolved "https://registry.yarnpkg.com/react-hot-loader/-/react-hot-loader-4.7.2.tgz#54cd99441c2d594bdc58c90673690c245dcfcaff"
integrity sha512-kkvGHmvLrbg6RH7svQ28T1swM2JFsHYBRT92xz4k4ubSyPcE2i8YVPQmoKWsDk/zNNpC710M9Md10oZzugecOw==
dependencies:
fast-levenshtein "^2.0.6"
global "^4.3.0"
hoist-non-react-statics "^2.5.0"
hoist-non-react-statics "^3.3.0"
loader-utils "^1.1.0"
lodash.merge "^4.6.1"
lodash "^4.17.11"
prop-types "^15.6.1"
react-lifecycles-compat "^3.0.4"
shallowequal "^1.0.2"
Expand All @@ -7696,7 +7711,7 @@ react-imported-component@^5.2.4:
prop-types "15.6.2"
scan-directory "^1.0.0"

react-is@^16.8.1:
react-is@^16.7.0, react-is@^16.8.1:
version "16.8.3"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.3.tgz#4ad8b029c2a718fc0cfc746c8d4e1b7221e5387d"
integrity sha512-Y4rC1ZJmsxxkkPuMLwvKvlL1Zfpbcu+Bf4ZigkHup3v9EfdYhAlWAaVyA19olXq2o2mGn0w+dFKvk3pVVlYcIA==
Expand Down Expand Up @@ -8237,9 +8252,9 @@ rollup-pluginutils@^2.3.0, rollup-pluginutils@^2.3.1:
micromatch "^3.1.10"

rollup@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.4.0.tgz#fdc965b4ea7948e0b8e1a9051067dce1a61b3a24"
integrity sha512-E5HP4rgvAqkXQNYfNHgCDnX5HDHwAPDLUVSNp8iTmT49vY34G0DxEfhjPWnqnFi7v0vQtDGkKl2hvLfmsMvmcA==
version "1.4.1"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.4.1.tgz#cc03ef6fb49dd72a878e3da0131c0a3696de14a7"
integrity sha512-YWf5zeR6SWtqZmCnuYs4a+ZJetj8NT4yfBMPXekWHW4L3144jM+J2AWagQVejB0FwCqjEUP9l8o4hg1rPDfQlg==
dependencies:
"@types/estree" "0.0.39"
"@types/node" "^11.9.5"
Expand Down Expand Up @@ -9086,9 +9101,9 @@ tiny-emitter@^2.0.0:
integrity sha512-NB6Dk1A9xgQPMoGqC5CVXn123gWyte215ONT5Pp5a0yt4nlEoO1ZWeCwpncaekPHXO60i47ihFnZPiRPjRMq4Q==

tippy.js@^4.0.3:
version "4.0.3"
resolved "https://registry.yarnpkg.com/tippy.js/-/tippy.js-4.0.3.tgz#fc1bdb1f995616099bd1007e42d9dbb66d081797"
integrity sha512-an3hGGD29DruVArvST9LhiqrFEK8Lf7/nvhUir2Z/D8LmsmrO3Folw3YoG0ha2QUqbmAMKxpcSOvnHWi3rdl2Q==
version "4.0.4"
resolved "https://registry.yarnpkg.com/tippy.js/-/tippy.js-4.0.4.tgz#1cfd83fc6cfd1c459bb7e08b5ad9bc97008f61bb"
integrity sha512-VXwJ2PXUvSTqV0RA5n5jzAdXsXnhMNX/aBYhC2j2YLg1/gekFWqVffbUQMVvwrqT4zmpm4Bp7tO8MVkvaaZ5Pg==
dependencies:
popper.js "^1.14.7"

Expand Down

0 comments on commit fcf4522

Please sign in to comment.