Coder Social home page Coder Social logo

semoal / themeprovider-storybook Goto Github PK

View Code? Open in Web Editor NEW
69.0 2.0 7.0 6.39 MB

πŸ’… Use your theme-provider on your favourite storybook πŸ’…

Home Page: https://semoal.github.io/themeprovider-storybook

License: MIT License

JavaScript 25.17% TypeScript 73.59% Shell 1.23%
styled-components storybook storybook-ui typescript react reactjs themeprovider-storybook addon themeprovider theme-provider

themeprovider-storybook's Introduction

Storybook SC ThemeProvider

GitHub package.json version CircleCI (all branches) GitHub last commit npm GitHub BundleSize Semantic Release

This addon helps you to display a Styled-Components ThemeProvider or a custom one in your Storybook.

  • Works on Storybook 5.x.x and 6.x.x (latest release)
  • Switches background color.
  • Works on iframes or visual regression testing.
  • Allows passing a custom implementation of your own theme provider.
  • Displays a popup with all the current keys of the theme. If you want, you can disable it
  • You can copy individually a value from the theme.

Screenshot

Getting Started

First, install the addon

yarn add themeprovider-storybook --dev
npm install --save-dev themeprovider-storybook

Add this line to your addons array inside main.js file (create this file inside your storybook config directory if needed).

module.exports = {
  addons: [
    "themeprovider-storybook/register"
  ]
}

Set options globally

Import and use the withThemesProvider function in your preview.js file.

import { withThemesProvider } from "themeprovider-storybook";

// Options:
const themes = [
  {
    name: 'Theme1' // Required it's used for displaying the button label,
    backgroundColor: '#fff' // Optional, it's used for setting dynamic background color on storybook
    ..., // Your theme keys (Check example if you need some help)
  },
  {
    name: 'Theme2' // Required it's used for displaying the button label,
    backgroundColor: '#000'// Optional, it's used for setting dynamic background color on storybook
    ..., // Your theme keys (Check example if you need some help)
  }
]

export const decorators = [withThemesProvider(themes)];

Examples

version documentation
For Storybook v5.x.x OLD readme
For Storybook v6.x.x Current readme

Disable popup

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

How to use your own implementation of ThemeProvider

Thanks to @ckknight suggestion, you can easily use your own context for themeprovider.

This is just an example of a custom theme provider, probably this is not a working, just for suggesting purposes.

const ThemeContext: Context<Theme | void> = React.createContext();
const ThemeConsumer = ThemeContext.Consumer;

export default function SomeCustomImplementationOfThemeProvider(props: Props) {
  const outerTheme = useContext(ThemeContext);
  const themeContext = useMemo(() => mergeTheme(props.theme, outerTheme), [
    props.theme,
    outerTheme,
  ]);

  if (!props.children) {
    return null;
  }

  return <ThemeContext.Provider value={themeContext}>{props.children}</ThemeContext.Provider>;
}

On config.js file of Storybook, just pass a CustomThemeProvider

import { DEFAULT_SETTINGS } from "themeprovider-storybook"
import { SomeCustomImplementationOfThemeProvider } from "src/app/CustomThemeProvider.jsx"

addDecorator(
  withThemesProvider(
    themes,
    DEFAULT_SETTINGS,
    SomeCustomImplementationOfThemeProvider
  )
);

also you can pass inside settings object the custom implementation of your theme provider.

import { SomeCustomImplementationOfThemeProvider } from "src/app/CustomThemeProvider.jsx"

addDecorator(
  withThemesProvider(
    themes,
    { customThemeProvider: SomeCustomImplementationOfThemeProvider },
  )
);

SomeCustomImplementationOfThemeProvider must admit a theme as prop.

themeprovider-storybook's People

Contributors

ckknight avatar greenkeeper[bot] avatar jimmy-guzman avatar micmro avatar semantic-release-bot avatar semoal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

themeprovider-storybook's Issues

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Missing package.json file.

A package.json file at the root of your project is required to release on npm.

Please follow the npm guideline to create a valid package.json file.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Storyshots are blank when using this addon

When you add the @storybook/addon-storyshots addon, and update the storyshots they are all blank. I'm using create-react-app, so the command I'm using is

react-scripts test --watchAll=false -u

But I'd assuming it would be the same outcome if I just used jest. Regular snapshot tests are not affected

The issue might be here:

Apologies for all of the issues this morning, just adding this to our application since it is pretty close to what we need. Thank you for all of the quick replies! :)

Custom ThemeProvider support

Is your feature request related to a problem? Please describe.
We use a custom ThemeProvider component and want to be able to specify that rather than always using styled-components' ThemeProvider.

Describe the solution you'd like
withThemeProvider could have an optional second argument where the ThemeProvider component could be passed in. This would then be passed to the ThemesProvider component as a prop. If undefined, fall back to the styled-components ThemeProvider.

Describe alternatives you've considered
We could extract the logic that the custom ThemeProvider component does, but I'd prefer not to.

I have made a patch that we are using via patch-package, though official support for this would be much more preferable.

diff --git a/node_modules/themeprovider-storybook/ThemesProvider.d.ts b/node_modules/themeprovider-storybook/ThemesProvider.d.ts
index 0b8b74b..d64c810 100644
--- a/node_modules/themeprovider-storybook/ThemesProvider.d.ts
+++ b/node_modules/themeprovider-storybook/ThemesProvider.d.ts
@@ -4,6 +4,7 @@ import { Theme } from "./types/Theme";
 export interface ThemesProviderProps {
     themes: List<Theme>;
     story?: any;
+    ThemeProvider?: React.ComponentType<{ theme: any }>;
     children: React.ReactChild;
 }
 export declare const ThemesProvider: React.FunctionComponent<ThemesProviderProps>;
diff --git a/node_modules/themeprovider-storybook/ThemesProvider.js b/node_modules/themeprovider-storybook/ThemesProvider.js
index 0647669..ca582ce 100644
--- a/node_modules/themeprovider-storybook/ThemesProvider.js
+++ b/node_modules/themeprovider-storybook/ThemesProvider.js
@@ -14,7 +14,7 @@ var addons_1 = __importDefault(require("@storybook/addons"));
 var React = __importStar(require("react"));
 var styled_components_1 = require("styled-components");
 exports.ThemesProvider = function (_a) {
-    var story = _a.story, children = _a.children, themes = _a.themes;
+    var story = _a.story, children = _a.children, themes = _a.themes, ThemeProvider = _a.ThemeProvider || styled_components_1.ThemeProvider;
     var _b = React.useState(null), theme = _b[0], setTheme = _b[1];
     React.useEffect(function () {
         var channel = addons_1.default.getChannel();
@@ -26,7 +26,7 @@ exports.ThemesProvider = function (_a) {
         };
     }, [themes, children]);
     if (!theme && story)
-        return React.createElement(styled_components_1.ThemeProvider, { theme: themes.first() }, story());
-    return React.createElement(styled_components_1.ThemeProvider, { theme: theme }, children);
+        return React.createElement(ThemeProvider, { theme: themes.first() }, story());
+    return React.createElement(ThemeProvider, { theme: theme }, children);
 };
 //# sourceMappingURL=ThemesProvider.js.map
\ No newline at end of file
diff --git a/node_modules/themeprovider-storybook/withThemesProvider.d.ts b/node_modules/themeprovider-storybook/withThemesProvider.d.ts
index fcdfd43..2dd6ad6 100644
--- a/node_modules/themeprovider-storybook/withThemesProvider.d.ts
+++ b/node_modules/themeprovider-storybook/withThemesProvider.d.ts
@@ -1 +1 @@
-export declare const withThemesProvider: (themes: any[]) => (story: any) => JSX.Element;
+export declare const withThemesProvider: (themes: any[], ThemeProvider?: import('react').ComponentType<{ theme: any }>) => (story: any) => JSX.Element;
diff --git a/node_modules/themeprovider-storybook/withThemesProvider.js b/node_modules/themeprovider-storybook/withThemesProvider.js
index e52c434..573859e 100644
--- a/node_modules/themeprovider-storybook/withThemesProvider.js
+++ b/node_modules/themeprovider-storybook/withThemesProvider.js
@@ -12,8 +12,8 @@ var React = __importStar(require("react"));
 var Background_1 = require("./Background");
 var Modal_1 = require("./components/Modal");
 var ThemesProvider_1 = require("./ThemesProvider");
-exports.withThemesProvider = function (themes) { return function (story) {
-    return (React.createElement(ThemesProvider_1.ThemesProvider, { story: story, themes: immutable_1.List(themes) },
+exports.withThemesProvider = function (themes, ThemeProvider) { return function (story) {
+    return (React.createElement(ThemesProvider_1.ThemesProvider, { story: story, themes: immutable_1.List(themes), ThemeProvider: ThemeProvider },
         React.createElement(Modal_1.ModalProvider, null,
             React.createElement(Background_1.BackgroundHelper, { themes: immutable_1.List(themes) }, story()))));
 }; };

Expose DEFAULT_SETTINGS or have CustomThemeProvider part of "settings"

Is your feature request related to a problem? Please describe.
With the introduction of disableThemePreview, what is mentioned in the README no longer works for CustomThemeProvider since it's now the 3rd param

import { SomeCustomImplementationOfThemeProvider, DEFAULT_SETTINGS } from "src/app/CustomThemeProvider.jsx"

addDecorator(
  withThemesProvider(themes, SomeCustomImplementationOfThemeProvider)
);

Describe the solution you'd like

  • This is mitigated simply by updating the README to withThemesProvider(themes, { disableThemePreview: false }, SomeCustomImplementationOfThemeProvider) but it would be nice if DEFAULT_SETTINGS is exposed so we can do:
import { SomeCustomImplementationOfThemeProvider, DEFAULT_SETTINGS } from "src/app/CustomThemeProvider.jsx"

addDecorator(
  withThemesProvider(themes, DEFAULT_SETTINGS, SomeCustomImplementationOfThemeProvider)
);

Describe alternatives you've considered

  • Make CustomThemeProvider part of ThemesProviderSettings so we can do something like: (preferred but it's a breaking change which can be mitigated by supporting both options until the next planned major version)
withThemesProvider(themes, { CustomThemeProvider: SomeCustomImplementationOfThemeProvider })

Additional context
I'm willing to work on either solution :)

An in-range update of styled-components is breaking the build 🚨

The devDependency styled-components was updated from 4.2.0 to 4.2.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

styled-components is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ ci/circleci: test: Your tests failed on CircleCI (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Stories are not displayed correctly in DocsPage

Description
If you install the @storybook/addon-docs addon, it adds an autogenerated DocsPage containing all of the stories. However, there are styles included with this addon that force the height of the container for each story to be 100vh, which is undesired.

The issue is because of the Background component:

const BackgroundContainer = styled.div<{ backgroundColor: string }>`

Expected behavior
I'd expect that this addon does not change any of the styles

Screenshots
image
image

RFC: V1.2.0 Roadmap

Hi guys,

I open this issue for any suggerence you would like to get in a 2.0.0 version.

Suggerences:

  • Add semantic release and auto deploy with canary releases
  • When long click on the theme, must show theme keys. (for someone who wants implement that theme in his website, would be cool) #14

Add themes trough shareable link as parameter

Is your feature request related to a problem? Please describe.
I want to share a Storybook component via the /iframe.html link. It's possible to add knobs as parameters like so: &knob-[key]=[value]. That doesn't seem to work for the themes.

Describe the solution you'd like
Being able to select a theme by adding a theme parameter. Is it already possible and am I missing stuff?

Describe alternatives you've considered
Changing the themes trough knobs which can be passed as parameter.

Additional context
x

Cannot use hooks inside of components story

Describe the bug
Cannot use React hooks in the story directly because of directly call on component like a fuction. I have a temporary workaround by placing the following code in front of with addDecorator(withThemesProvider(themes));. But I feel this might be an issue inside of withThemesProvider

`addDecorator(Story => <Story />);

To Reproduce
Steps to reproduce the behavior:

  1. Have a story written with React Hooks
  2. Go to the storybook
  3. See error

Expected behavior
Storybook should render as normal

Screenshots
image

Desktop (please complete the following information):

  • OS: MacOS Catalina V10.15.4
  • Browser Chrome
  • Version v80

Deprecated addDecorator

Is your feature request related to a problem? Please describe.
The documentation describes how to use a custom implementation of ThemeProvider, through the addDecorator function.
However, it has been deprecated since the release of Storybook 7.

Describe the solution you'd like
Update the documentation to match the new syntax.

Describe alternatives you've considered
ΓΈ

Additional context
ΓΈ

Is it possible to control the storybook theme base (eg light or dark) via an iframe param?

Is your feature request related to a problem? Please describe.
We are embedding storybook docs pages into our design system documentation website. This website currently supports 2 themes, light and dark. Storybook docs pages look quite out of place when the site's theme does not match the storybook docs page's theme.
example implementation: https://biome.immutable.com/storybook

Describe the solution you'd like
I'm hoping to be able to control the base theme used by storybook (for both docs and ui) - by passing in parameter(s) into the iframe embed URL. eg:

<iframe
  src="https://biome-storybook.immutable.com/iframe.html?id=biom3-react-statefulbuttcon--docs&viewMode=docs&shortcuts=false&uiTheme=dark&docsTheme=dark"
/>

This way, we can vary which theme we show the storybook docs in, based on the hosting application's color theme (eg light or dark).

Describe alternatives you've considered
I've tried adding params like theme=dark etc into the iframe url, with no luck.

Console warning "Each child in a list should have a unique "key" prop"

Description
This warning is thrown in the console when using this addon

vendors~main.b9972167133ca606f27b.bundle.js:164613 Warning: Each child in a list should have a unique "key" prop.

Check the top-level render call using <Styled(div)>. See https://fb.me/react-warning-keys for more information.
    in Unknown
    in Unknown
    in Unknown
    in Unknown
    in Unknown (created by Context.Consumer)
    in ManagerConsumer (created by _default)
    in _default (created by Layout)
    in div (created by Context.Consumer)
    in Styled(div) (created by Panel)
    in Panel (created by Layout)
    in div (created by Context.Consumer)
    in Styled(div) (created by Main)
    in div (created by Context.Consumer)
    in Styled(div) (created by Main)
    in Main (created by Layout)
    in Layout (created by Context.Consumer)
    in WithTheme(Layout)
    in Unknown
    in Unknown
    in div (created by Context.Consumer)
    in Styled(div)
    in Unknown
    in Unknown (created by SizeMeRenderer(Component))
    in SizeMeReferenceWrapper (created by SizeMeRenderer(Component))
    in SizeMeRenderer(Component) (created by SizeMe(Component))
    in SizeMe(Component) (created by Manager)
    in ThemeProvider (created by Manager)
    in Manager (created by Context.Consumer)
    in Location (created by QueryLocation)
    in QueryLocation (created by Root)
    in LocationProvider (created by Root)
    in HelmetProvider (created by Root)
    in Root

I'm using NodeJS v12.14.1, Storybook v5.3.10, and themeprovider-storybook 1.2.5.

My main.js file looks like this. Taking out the themeprovider-storybook addon removes the warning

module.exports = {
  stories: ["../src/**/*.stories.(js|mdx)"],
  addons: [
    "@storybook/preset-create-react-app",
    "@storybook/addon-actions",
    "themeprovider-storybook",
    {
      name: "@storybook/addon-docs",
      options: {
        configureJSX: true,
        babelOptions: {},
        sourceLoaderOptions: null
      }
    }
  ]
};

Remove react-json-view with a more lightweight option

Is your feature request related to a problem? Please describe.
The problem is that react-json-view weights 140kb... We'll reduce the library to just 40-50kb

Describe the solution you'd like
Build a custom solution with:

  • Copy of values/keys
  • Un/Collapsable
  • Show similar to json-view

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.