Skip to content

Commit

Permalink
Merge pull request #63 from adamyonk/patch-2
Browse files Browse the repository at this point in the history
Fix useEffect warning/memory leak
  • Loading branch information
hipstersmoothie authored Oct 30, 2019
2 parents 55e8c16 + 6468db3 commit 4efb839
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 78 deletions.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"release": "auto shipit"
},
"peerDependencies": {
"@storybook/api": ">= 5.x",
"@storybook/addons": ">= 5.x",
"@storybook/components": ">= 5.x",
"@storybook/core-events": ">= 5.x",
Expand All @@ -32,10 +33,11 @@
"memoizerific": "^1.11.3"
},
"devDependencies": {
"@storybook/addons": "5.2.1",
"@storybook/components": "5.2.1",
"@storybook/core-events": "5.2.1",
"@storybook/theming": "5.2.1",
"@storybook/addons": "5.2.5",
"@storybook/api": "5.2.5",
"@storybook/components": "5.2.5",
"@storybook/core-events": "5.2.5",
"@storybook/theming": "5.2.5",
"@types/react": "16.9.11",
"@typescript-eslint/eslint-plugin": "2.6.0",
"@typescript-eslint/parser": "2.6.0",
Expand Down
32 changes: 16 additions & 16 deletions src/Tool.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import * as React from 'react';
import { themes, ThemeVars } from '@storybook/theming';
import { IconButton } from '@storybook/components';
import { API } from '@storybook/api';
import equal from 'fast-deep-equal';

import Sun from './icons/Sun';
import Moon from './icons/Moon';

interface StorybookAPI {
getChannel(): { on(event: string, cb: () => void): void };
setOptions(options: any): void;
on(event: string, callback: (data: any) => void): void;
off(event: string, callback: (data: any) => void): void;
getCurrentStoryData(): any;
}

interface DarkModeProps {
api: StorybookAPI;
channel: {
emit(event: string, value: any): void;
};
api: API;
}

interface DarkModeStore {
Expand Down Expand Up @@ -75,12 +65,17 @@ export const DarkMode: React.FunctionComponent<DarkModeProps> = props => {
});
props.api.setOptions({ theme: currentStore[current] });
setDark(!isDark);
props.channel.emit('DARK_MODE', !isDark);
props.api.getChannel().emit('DARK_MODE', !isDark);
}

function renderTheme() {
const { parameters } = props.api.getCurrentStoryData();
const data = props.api.getCurrentStoryData();

if (!('parameters' in data)) {
return;
}

const { parameters } = data;
let darkTheme = themes.dark;
let lightTheme = themes.light;

Expand All @@ -97,15 +92,20 @@ export const DarkMode: React.FunctionComponent<DarkModeProps> = props => {

props.api.setOptions({ theme: currentStore[current] });
setDark(current === 'dark');
props.channel.emit('DARK_MODE', current === 'dark');
props.api.getChannel().emit('DARK_MODE', current === 'dark');
}

React.useEffect(() => {
const channel = props.api.getChannel();
channel.on('storyChanged', renderTheme);
channel.on('storiesConfigured', renderTheme);
channel.on('docsRendered', renderTheme);
}, []);
return () => {
channel.removeListener('storyChanged', renderTheme);
channel.removeListener('storiesConfigured', renderTheme);
channel.removeListener('docsRendered', renderTheme);
};
});

return (
<IconButton
Expand Down
2 changes: 1 addition & 1 deletion src/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ addons.register('storybook/dark-mode', api => {
title: 'dark mode',
type: types.TOOL,
match: ({ viewMode }) => viewMode === 'story',
render: () => <Tool api={api} channel={addons.getChannel()} />
render: () => <Tool api={api} />
});
});
141 changes: 84 additions & 57 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -640,64 +640,65 @@
dependencies:
any-observable "^0.3.0"

"@storybook/[email protected].1":
version "5.2.1"
resolved "https://registry.yarnpkg.com/@storybook/addons/-/addons-5.2.1.tgz#6e52aa1fa2737e170fb675eb1fcceebd0a915a0b"
integrity sha512-kdx97tTKsMf/lBlT40uLYsHMF1J71mn2j41RNaCXmWw/PrKCDmiNfinemN2wtbwRSvGqb3q/BAqjKLvUtWynGg==
dependencies:
"@storybook/api" "5.2.1"
"@storybook/channels" "5.2.1"
"@storybook/client-logger" "5.2.1"
"@storybook/core-events" "5.2.1"
"@storybook/[email protected].5":
version "5.2.5"
resolved "https://registry.yarnpkg.com/@storybook/addons/-/addons-5.2.5.tgz#e3e23d5ea6eb221df31e1a5d125be47454e9a0e8"
integrity sha512-CvMj7Bs3go9tv5rZuAvFwuwe8p/16LDCHS7+5nVFosvcL8nuN339V3rzakw8nLy/S6XKeZ1ACu4t3vYkreRE3w==
dependencies:
"@storybook/api" "5.2.5"
"@storybook/channels" "5.2.5"
"@storybook/client-logger" "5.2.5"
"@storybook/core-events" "5.2.5"
core-js "^3.0.1"
global "^4.3.2"
util-deprecate "^1.0.2"

"@storybook/[email protected].1":
version "5.2.1"
resolved "https://registry.yarnpkg.com/@storybook/api/-/api-5.2.1.tgz#b9cd6639019e044a8ade6fb358cade79c0e3b5d3"
integrity sha512-EXN6sqkGHRuNq0W6BZXOlxe2I2dmN0yUdQLiUOpzH2I3mXnVHpad/0v76dRc9fZbC4LaYUSxR8lBTr0rqIb4mA==
dependencies:
"@storybook/channels" "5.2.1"
"@storybook/client-logger" "5.2.1"
"@storybook/core-events" "5.2.1"
"@storybook/router" "5.2.1"
"@storybook/theming" "5.2.1"
"@storybook/[email protected].5":
version "5.2.5"
resolved "https://registry.yarnpkg.com/@storybook/api/-/api-5.2.5.tgz#dcc68c873820485372a47c095a8fc5e4fb53a34c"
integrity sha512-JvLafqFVgA3dIWpLMoGNk4sRuogE5imhD6/g0d8DOwnCID9xowj5xIptSrCTKvGGGxuN3wWRGn6I2lEbY6969g==
dependencies:
"@storybook/channels" "5.2.5"
"@storybook/client-logger" "5.2.5"
"@storybook/core-events" "5.2.5"
"@storybook/router" "5.2.5"
"@storybook/theming" "5.2.5"
core-js "^3.0.1"
fast-deep-equal "^2.0.1"
global "^4.3.2"
lodash "^4.17.11"
lodash "^4.17.15"
memoizerific "^1.11.3"
prop-types "^15.6.2"
react "^16.8.3"
semver "^6.0.0"
shallow-equal "^1.1.0"
store2 "^2.7.1"
telejson "^2.2.2"
telejson "^3.0.2"
util-deprecate "^1.0.2"

"@storybook/[email protected].1":
version "5.2.1"
resolved "https://registry.yarnpkg.com/@storybook/channels/-/channels-5.2.1.tgz#e5e35f6d9fb1b1fba4f18b171f31d5f6540f3bef"
integrity sha512-AsF/Hwx91SDOgiOGOBSWS8EJAgqVm939n2nkfdLSJQQmX5EdPRAc3EIE3f13tyQub2yNx0OR4UzQDWgjwfVsEQ==
"@storybook/[email protected].5":
version "5.2.5"
resolved "https://registry.yarnpkg.com/@storybook/channels/-/channels-5.2.5.tgz#d6ca2b490281dacb272096563fe760ccb353c4bb"
integrity sha512-I+zB3ym5ozBcNBqyzZbvB6gRIG/ZKKkqy5k6LwKd5NMx7NU7zU74+LQUBBOcSIrigj8kCArZz7rlgb0tlSKXxQ==
dependencies:
core-js "^3.0.1"

"@storybook/[email protected].1":
version "5.2.1"
resolved "https://registry.yarnpkg.com/@storybook/client-logger/-/client-logger-5.2.1.tgz#5c1f122b65386f04a6ad648808dfa89f2d852d7a"
integrity sha512-wzxSE9t3DaLCdd/gnGFnjevmYRZ92F3TEwhUP/QDXM9cZkNsRKHkjE61qjiO5aQPaZQG6Ea9ayWEQEMgZXDucg==
"@storybook/[email protected].5":
version "5.2.5"
resolved "https://registry.yarnpkg.com/@storybook/client-logger/-/client-logger-5.2.5.tgz#6f386ac6f81b4a783c57d54bb328281abbea1bab"
integrity sha512-6DyYUrMgAvF+th0foH7UNz+2JJpRdvNbpvYKtvi/+hlvRIaI6AqANgLkPUgMibaif5TLzjCr0bLdAYcjeJz03w==
dependencies:
core-js "^3.0.1"

"@storybook/[email protected].1":
version "5.2.1"
resolved "https://registry.yarnpkg.com/@storybook/components/-/components-5.2.1.tgz#a4519c5d435c2c25c481e2b64a768e1e568a223f"
integrity sha512-cik5J/mTm1b1TOI17qM+2Mikk3rjb3SbBD4WlNz3Zvn+Hw0ukgbx6kQwVBgujhMlDtsHreidyEgIg4TM13S0Tg==
"@storybook/[email protected].5":
version "5.2.5"
resolved "https://registry.yarnpkg.com/@storybook/components/-/components-5.2.5.tgz#40190dafbee34f083182255d26c19a0ea50789c8"
integrity sha512-6NVaBJm5wY53e9k+2ZiL2ABsHghE1ssQciLTG3jJPahnM6rfkM8ue66rhxhP88jE9isT48JgOZOJepEyxDz/fg==
dependencies:
"@storybook/client-logger" "5.2.1"
"@storybook/theming" "5.2.1"
"@storybook/client-logger" "5.2.5"
"@storybook/theming" "5.2.5"
"@types/react-syntax-highlighter" "10.1.0"
"@types/react-textarea-autosize" "^4.3.3"
core-js "^3.0.1"
global "^4.3.2"
markdown-to-jsx "^6.9.1"
Expand All @@ -714,34 +715,34 @@
react-textarea-autosize "^7.1.0"
simplebar-react "^1.0.0-alpha.6"

"@storybook/[email protected].1":
version "5.2.1"
resolved "https://registry.yarnpkg.com/@storybook/core-events/-/core-events-5.2.1.tgz#bc28d704938d26dd544d0362d38ef08e8cfed916"
integrity sha512-AIYV/I+baQ0KxvEM7QAKqUedLn2os0XU9HTdtfZJTC3U9wjmR2ah2ScD6T0n7PBz3MderkvZG6dNjs9h8gRquQ==
"@storybook/[email protected].5":
version "5.2.5"
resolved "https://registry.yarnpkg.com/@storybook/core-events/-/core-events-5.2.5.tgz#62881164a4a01aa99ff0691e70eaed2dd58e229e"
integrity sha512-O5GM8XEBbYNbM6Z7a4H1bbnbO2cxQrXMhEwansC7a7YinQdkTPiuGxke3NiyK+7pLDh778kpQyjoCjXq6UfAoQ==
dependencies:
core-js "^3.0.1"

"@storybook/[email protected].1":
version "5.2.1"
resolved "https://registry.yarnpkg.com/@storybook/router/-/router-5.2.1.tgz#9c49df79343d3be10c7f984858fb5c9ae3eb7491"
integrity sha512-Mlk275cyPoKtnP4DwQ5D8gTfnaRPL6kDZOSn0wbTMa6pQOfYKgJsa7tjzeAtZuZ/j8hKI4gAfT/auMgH6g+94A==
"@storybook/[email protected].5":
version "5.2.5"
resolved "https://registry.yarnpkg.com/@storybook/router/-/router-5.2.5.tgz#a005332bc6aa1e7849503187ad50c41b3f3bef92"
integrity sha512-e6ElDAWSoEW1KSnsTbVwbpzaZ8CNWYw0Ok3b5AHfY2fuSH5L4l6s6k/bP7QSYqvWUeTvkFQYux7A2rOFCriAgA==
dependencies:
"@reach/router" "^1.2.1"
"@types/reach__router" "^1.2.3"
core-js "^3.0.1"
global "^4.3.2"
lodash "^4.17.11"
lodash "^4.17.15"
memoizerific "^1.11.3"
qs "^6.6.0"

"@storybook/[email protected].1":
version "5.2.1"
resolved "https://registry.yarnpkg.com/@storybook/theming/-/theming-5.2.1.tgz#913e383632e4702035a107c2cc5e5cb27231b389"
integrity sha512-lbAfcyI7Tx8swduIPmlu/jdWzqTBN/v82IEQbZbPR4LS5OHRPmhXPNgFGrcH4kFAiD0GoezSsdum1x0ZZpsQUQ==
"@storybook/[email protected].5":
version "5.2.5"
resolved "https://registry.yarnpkg.com/@storybook/theming/-/theming-5.2.5.tgz#9579e7944f61ded637d1d79be5fb859a617620f5"
integrity sha512-PGZNYrRgAhXFJKnktFpyyKlaDXEhtTi5XPq5ASVJrsPW6l963Mk2EMKSm4TCTxIJhs0Kx4cv2MnNZFDqHf47eg==
dependencies:
"@emotion/core" "^10.0.14"
"@emotion/styled" "^10.0.14"
"@storybook/client-logger" "5.2.1"
"@storybook/client-logger" "5.2.5"
common-tags "^1.8.0"
core-js "^3.0.1"
deep-object-diff "^1.1.0"
Expand Down Expand Up @@ -826,6 +827,11 @@
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.3.tgz#856c99cdc1551d22c22b18b5402719affec9839a"
integrity sha512-cS5owqtwzLN5kY+l+KgKdRJ/Cee8tlmQoGQuIE9tWnSmS3JMKzmxo2HIAk2wODMifGwO20d62xZQLYz+RLfXmw==

"@types/is-function@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/is-function/-/is-function-1.0.0.tgz#1b0b819b1636c7baf0d6785d030d12edf70c3e83"
integrity sha512-iTs9HReBu7evG77Q4EC8hZnqRt57irBDkK9nvmHroiOIVwYMQc4IvYvdRgwKfYepunIY7Oh/dBuuld+Gj9uo6w==

"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.1.tgz#42995b446db9a48a11a07ec083499a860e9138ff"
Expand Down Expand Up @@ -891,6 +897,13 @@
dependencies:
"@types/react" "*"

"@types/react-textarea-autosize@^4.3.3":
version "4.3.4"
resolved "https://registry.yarnpkg.com/@types/react-textarea-autosize/-/react-textarea-autosize-4.3.4.tgz#9a93f751c91ad5e86387bce75e3b7e11ed195813"
integrity sha512-LLqG27BJGt8ja9x4umQXbnK9pRd0dI23X/GXBcuf476feOZ+e5QiKJYmWOHwAJC3YLl3YixDSigzfF4gzVQZ5w==
dependencies:
"@types/react" "*"

"@types/react@*":
version "16.9.2"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.2.tgz#6d1765431a1ad1877979013906731aae373de268"
Expand Down Expand Up @@ -2768,6 +2781,14 @@ global@^4.3.2:
min-document "^2.19.0"
process "~0.5.1"

global@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/global/-/global-4.4.0.tgz#3e7b105179006a323ed71aafca3e9c57a5cc6406"
integrity sha512-wv/LAoHdRE3BeTGz53FAamhGlPLhlssK45usmGFThIi4XqnBmjKQ16u+RNbP7WvigRZDxUsM0J3gcQ5yicaL0w==
dependencies:
min-document "^2.19.0"
process "^0.11.10"

globals@^11.1.0, globals@^11.7.0:
version "11.11.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-11.11.0.tgz#dcf93757fa2de5486fbeed7118538adf789e9c2e"
Expand Down Expand Up @@ -4114,7 +4135,7 @@ lodash@^4.17.11:
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==

lodash@^4.17.12, lodash@^4.17.14:
lodash@^4.17.12, lodash@^4.17.14, lodash@^4.17.15:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
Expand Down Expand Up @@ -5014,6 +5035,11 @@ process-nextick-args@~2.0.0:
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.0.tgz#a37d732f4271b4ab1ad070d35508e8290788ffaa"
integrity sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==

process@^0.11.10:
version "0.11.10"
resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182"
integrity sha1-czIwDoQBYb2j5podHZGn1LwW8YI=

process@~0.5.1:
version "0.5.2"
resolved "https://registry.yarnpkg.com/process/-/process-0.5.2.tgz#1638d8a8e34c2f440a91db95ab9aeb677fc185cf"
Expand Down Expand Up @@ -6054,17 +6080,18 @@ tar@^4:
safe-buffer "^5.1.2"
yallist "^3.0.2"

telejson@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/telejson/-/telejson-2.2.2.tgz#d61d721d21849a6e4070d547aab302a9bd22c720"
integrity sha512-YyNwnKY0ilabOwYgC/J754En1xOe5PBIUIw+C9e0+5HjVVcnQE5/gdu2yET2pmSbp5bxIDqYNjvndj2PUkIiYA==
telejson@^3.0.2:
version "3.1.0"
resolved "https://registry.yarnpkg.com/telejson/-/telejson-3.1.0.tgz#c648479afe0d8edd90aeaf478b0b8a2fe9f59513"
integrity sha512-mhiVy+xp2atri1bzSzdy/gVGXlOhibaoZ092AUq5xhnrZGdzhF0fLaOduHJQghkro+qmjYMwhsOL9CkD2zTicg==
dependencies:
global "^4.3.2"
"@types/is-function" "^1.0.0"
global "^4.4.0"
is-function "^1.0.1"
is-regex "^1.0.4"
is-symbol "^1.0.2"
isobject "^3.0.1"
lodash "^4.17.11"
isobject "^4.0.0"
lodash "^4.17.15"
memoizerific "^1.11.3"

test-exclude@^5.0.0:
Expand Down

0 comments on commit 4efb839

Please sign in to comment.