Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accept any order of params #45

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,28 @@ export default function SomeCustomImplementationOfThemeProvider(props: Props) {

On config.js file of Storybook, just pass a `CustomThemeProvider`
```jsx
import { DEFAULT_SETTINGS } from "themeprovider-storybook"
import { SomeCustomImplementationOfThemeProvider } from "src/app/CustomThemeProvider.jsx"

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

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

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

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

Expand Down
16 changes: 14 additions & 2 deletions example/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// import { ThemeProvider } from "styled-components";
import { withThemesProvider } from "themeprovider-storybook"
const THEMES = [
{
Expand Down Expand Up @@ -44,8 +45,19 @@ export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
}

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

// with preview
export const decorators = [withThemesProvider(THEMES)];
export const decorators = [
withThemesProvider(THEMES, {
CustomThemeProvider: ThemeProvider
})
];

/**
* Example with custom provider
*/
// export const decorators = [withThemesProvider(THEMES, DEFAULT_SETTINGS, ThemeProvider)];
// or
// export const decorators = [withThemesProvider(THEMES, { CustomThemeProvider: ThemeProvider })];
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { Themes } from "./Themes";
export { ThemesProvider } from "./ThemesProvider";
export { withThemesProvider } from "./withThemesProvider";
export { withThemesProvider, DEFAULT_SETTINGS } from "./withThemesProvider";
17 changes: 17 additions & 0 deletions src/react-is.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function isClassComponent(component: any): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function isClassComponent(component: any): boolean {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isClassComponent(component: any): boolean {

Just a thought.

return (
typeof component === 'function' && !!component.prototype.isReactComponent
)
}

export function isFunctionComponent(component: any): boolean {
return (
typeof component === 'function' && String(component).includes('createElement')
)
}

export function isReactComponent(component: any): boolean {
return (
isClassComponent(component) || isFunctionComponent(component)
)
}
18 changes: 15 additions & 3 deletions src/withThemesProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@ import * as React from "react";

import { BackgroundHelper } from "./Background";
import { ModalProvider } from "./components/Modal";
import { isReactComponent } from "./react-is";
import { ThemesProvider } from "./ThemesProvider";
import { Theme } from "./types/Theme";

export type CustomThemeProvider = React.ComponentType<{ theme: Theme, children: React.ReactNode }>
export type ThemesProviderSettings = {
disableThemePreview: boolean
disableThemePreview?: boolean
CustomThemeProvider?: CustomThemeProvider
}

const DEFAULT_SETTINGS: ThemesProviderSettings = {
export const DEFAULT_SETTINGS: ThemesProviderSettings = {
disableThemePreview: false
}

export const withThemesProvider = (
themes: Theme[],
settings: ThemesProviderSettings = DEFAULT_SETTINGS,
CustomThemeProvider?: React.ComponentType<{ theme: Theme }>,
CustomThemeProvider?: CustomThemeProvider,
) => (story: any): JSX.Element => {

// compatibility with breaking change introduced without being deployed as breaking change...
if (settings !== null && isReactComponent(settings)) {
CustomThemeProvider = settings as CustomThemeProvider
} else if (settings !== DEFAULT_SETTINGS) {
settings = { ...DEFAULT_SETTINGS, ...settings }
}

if (settings.CustomThemeProvider) CustomThemeProvider = settings.CustomThemeProvider

return (
<ThemesProvider settings={settings} CustomThemeProvider={CustomThemeProvider} story={story} themes={themes}>
{settings?.disableThemePreview ? (
Expand Down