Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Allow provider to be optional #2

Merged
merged 1 commit into from
Feb 15, 2023
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
6 changes: 3 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ Available options:

| option | type | required? | Description |
| ------------ | --------------------- | :-------: | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| themes | `Record<string, any>` | | An object of theme configurations where the key is the name of the theme and the value is the theme object. If multiple themes are provided, a toolbar item will be added to switch between themes. |
| defaultTheme | `string` | | The name of the default theme to use |
| Provider | | | The JSX component to provide themes |
| themes | `Record<string, any>` | | An object of theme configurations where the key is the name of the theme and the value is the theme object. If multiple themes are provided, a toolbar item will be added to switch between themes. |
| defaultTheme | `string` | | The name of the default theme to use |
| Provider | | | The JSX component to provide themes |
| GlobalStyles | | | A JSX component containing global css styles. |

### `withThemeByClassName`
Expand Down
16 changes: 10 additions & 6 deletions src/decorators/provider.strategy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type Theme = Record<string, any>;
type ThemeMap = Record<string, Theme>;

export interface ProviderStrategyConfiguration {
Provider: any;
Provider?: any;
GlobalStyles?: any;
defaultTheme?: string;
themes: ThemeMap;
themes?: ThemeMap;
}

const pluckThemeFromKeyPairTuple = ([_, themeConfig]: [string, Theme]): Theme =>
Expand All @@ -24,9 +24,11 @@ export const withThemeFromJSXProvider = ({
Provider,
GlobalStyles,
defaultTheme,
themes,
themes = {},
}: ProviderStrategyConfiguration): DecoratorFunction => {
initializeThemeState(Object.keys(themes), defaultTheme);
const themeNames = Object.keys(themes);

initializeThemeState(themeNames, defaultTheme || themeNames[0]);

return (storyFn, context) => {
const { themeOverride } = useThemeParameters();
Expand All @@ -41,11 +43,13 @@ export const withThemeFromJSXProvider = ({
: themes[selectedThemeName];
}, [themes, selected, themeOverride]);

const ProviderComponent = Provider || React.Fragment;

return (
<Provider theme={theme}>
<ProviderComponent theme={theme}>
{GlobalStyles && <GlobalStyles />}
{storyFn()}
</Provider>
</ProviderComponent>
);
};
};