Skip to content

Commit

Permalink
fix: fixed disable param
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio committed Aug 26, 2020
1 parent 745199b commit c101eb8
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const decorators = [withThemesProvider(themes)];
### Disable popup

```jsx
export const decorators = [withThemesProvider(themes, { disableThemePreview: false })];
export const decorators = [withThemesProvider(themes, { disableThemePreview: true })];
```

### How to use your own implementation of ThemeProvider
Expand Down
11 changes: 11 additions & 0 deletions example/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
module.exports = {
webpackFinal: async (config, { configType }) => {
// `configType` has a value of 'DEVELOPMENT' or 'PRODUCTION'
// You can change the configuration based on that.
// 'PRODUCTION' is used when building the static version of storybook.

// Make whatever fine-grained changes you need
// Make whatever fine-grained changes you need
config.resolve.alias['styled-components'] = require.resolve('styled-components')
// Return the altered config
return config;
},
stories: [
"../src/**/*.stories.mdx",
"../src/**/*.stories.@(js|jsx|ts|tsx)"
Expand Down
7 changes: 6 additions & 1 deletion example/.storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ const THEMES = [
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
}
export const decorators = [withThemesProvider(THEMES)];

// disabled preview
// export const decorators = [withThemesProvider(THEMES, { disableThemePreview: true })];

// with preview
export const decorators = [withThemesProvider(THEMES)];
59 changes: 56 additions & 3 deletions example/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"prop-types": "15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"themeprovider-storybook": "^1.5.1",
"themeprovider-storybook": "^1.6.0",
"styled-components": "^5.1.1",
"typescript": "^3.9.2",
"webpack": "^4.43.0"
}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"test": "jest",
"bundle-size": "bundlesize",
"release": "semantic-release --debug",
"publish-storybook": "cd example && npm i --save themeprovider-storybook@latest && npm run build-storybook"
"publish-storybook": "cd example && npm i && npm i --save themeprovider-storybook@latest && npm run build-storybook"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
Expand Down
13 changes: 12 additions & 1 deletion src/Themes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as React from "react";
import { Button, Row } from "./components/Button";
import SvgIcon from "./components/SvgIcon";
import { Theme } from "./types/Theme";
import { ThemesProviderSettings } from "./withThemesProvider";
// extrange issue on action file:
// Argument of type '(api: import("/github/workspace/node_modules/@storybook/api/dist/index").API) => void' is not assignable to parameter of type '(api: import("/github/workspace/node_modules/@storybook/addons/node_modules/@storybook/api/dist/index").API) => void'.
// import { Channel } from '@storybook/channels';
Expand All @@ -18,13 +19,15 @@ interface ButtonProps {
onSelectTheme: (theme: Theme) => void;
theme: Theme;
onOpenModal: () => void;
settings: ThemesProviderSettings;
themes: List<Theme>;
}

const BaseComponent: React.FunctionComponent<ButtonProps> = ({
onSelectTheme,
themes,
theme,
settings,
onOpenModal,
}) => (
/* tslint:disable:jsx-no-multiline-js jsx-no-lambda*/
Expand All @@ -37,7 +40,7 @@ const BaseComponent: React.FunctionComponent<ButtonProps> = ({
onClick={() => onSelectTheme(th)}
>
<span>{th.name}</span>
<SvgIcon name="info" onClick={() => onOpenModal()} />
{!settings.disableThemePreview && <SvgIcon name="info" onClick={() => onOpenModal()} />}
</Button>
))
.toArray()}
Expand All @@ -49,6 +52,7 @@ export const Themes: React.FunctionComponent<ThemeProps> = ({
active,
}) => {
const [theme, setTheme] = React.useState(null);
const [settings, setSettings] = React.useState<ThemesProviderSettings>({ disableThemePreview: false });
const [themes, setThemes] = React.useState(List());

const onReceiveThemes = (newThemes: Theme[]) => {
Expand Down Expand Up @@ -86,6 +90,10 @@ export const Themes: React.FunctionComponent<ThemeProps> = ({
channel.emit("openModal", true);
};

const onReceiveSettings = (settings: ThemesProviderSettings) => {
setSettings(settings);
}

// When swiching to docs page we disable background color, because it's a more complex design
// On a future release of storybook,
// we hope they enable an internal naming(id, or theme) for setting only the background of each box.
Expand All @@ -98,16 +106,19 @@ export const Themes: React.FunctionComponent<ThemeProps> = ({

React.useEffect(() => {
channel.on("setThemes", onReceiveThemes);
channel.on("setSettings", onReceiveSettings)
channel.on("setCurrentStory", onHandleDocsPage);
return () => {
channel.removeListener("setThemes", onReceiveThemes);
channel.removeListener("setCurrentStory", onHandleDocsPage);
channel.removeListener("setSettings", onReceiveSettings);
};
}, []);

return theme && active ? (
<BaseComponent
onOpenModal={onOpenModal}
settings={settings}
onSelectTheme={onSelectTheme}
themes={themes}
theme={theme}
Expand Down
5 changes: 4 additions & 1 deletion src/ThemesProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { List } from "immutable";
import * as React from "react";
import { ThemeProvider } from "styled-components";
import { Theme } from "./types/Theme";
import { ThemesProviderSettings } from "./withThemesProvider";

export interface ThemesProviderProps {
themes: List<Theme>;
settings: ThemesProviderSettings;
CustomThemeProvider?: React.ComponentType<{ theme: Theme }>;
story?: any;
children: React.ReactChild;
Expand All @@ -16,13 +18,14 @@ interface ThemesProvider {
theme: Theme;
}

export const ThemesProvider: React.FunctionComponent<ThemesProviderProps> = ({ story, children, themes, CustomThemeProvider }) => {
export const ThemesProvider: React.FunctionComponent<ThemesProviderProps> = ({ settings, story, children, themes, CustomThemeProvider }) => {
const [theme, setTheme] = React.useState(null);

React.useEffect(() => {
const channel = addons.getChannel();
channel.on("selectTheme", setTheme);
channel.emit("setThemes", themes);
channel.emit("setSettings", settings);
return () => {
const channelUnmount = addons.getChannel();
channelUnmount.removeListener("selectTheme", setTheme);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const Modal = React.memo(({ children, isOpen, toggleModal, headerTitle }: ModalP
>
<Header>
{headerTitle}
<SvgIcon name="close" onClick={toggleModal} />
<SvgIcon style={{ cursor: "pointer", fill: "grey" }} name="close" onClick={toggleModal} />
</Header>
<Body>
{children}
Expand Down
4 changes: 2 additions & 2 deletions src/withThemesProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ModalProvider } from "./components/Modal";
import { ThemesProvider } from "./ThemesProvider";
import { Theme } from "./types/Theme";

type ThemesProviderSettings = {
export type ThemesProviderSettings = {
disableThemePreview: boolean
}

Expand All @@ -21,7 +21,7 @@ export const withThemesProvider = (
) => (story: any): JSX.Element => {

return (
<ThemesProvider CustomThemeProvider={CustomThemeProvider} story={story} themes={List(themes)}>
<ThemesProvider settings={settings} CustomThemeProvider={CustomThemeProvider} story={story} themes={List(themes)}>
{settings?.disableThemePreview ? (
<BackgroundHelper themes={List(themes)}>{story()}</BackgroundHelper>
): (
Expand Down

0 comments on commit c101eb8

Please sign in to comment.