Back to Storybook

Mock Provider In Preview

docs/_snippets/mock-provider-in-preview.md

10.3.63.1 KB
Original Source
jsx
import React from 'react';

import { ThemeProvider } from 'styled-components';

// themes = { light, dark }
import * as themes from '../src/themes';

export default {
  decorators: [
    // ๐Ÿ‘‡ Defining the decorator in the preview file applies it to all stories
    (Story, { parameters }) => {
      // ๐Ÿ‘‡ Make it configurable by reading the theme value from parameters
      const { theme = 'light' } = parameters;
      return (
        <ThemeProvider theme={themes[theme]}>
          <Story />
        </ThemeProvider>
      );
    },
  ],
};
tsx
import React from 'react';

// Replace your-framework with the framework you are using, e.g. react-vite, nextjs, nextjs-vite, etc.
import type { Preview } from '@storybook/your-framework';

import { ThemeProvider } from 'styled-components';

// themes = { light, dark }
import * as themes from '../src/themes';

const preview: Preview = {
  decorators: [
    // ๐Ÿ‘‡ Defining the decorator in the preview file applies it to all stories
    (Story, { parameters }) => {
      // ๐Ÿ‘‡ Make it configurable by reading the theme value from parameters
      const { theme = 'light' } = parameters;
      return (
        <ThemeProvider theme={themes[theme]}>
          <Story />
        </ThemeProvider>
      );
    },
  ],
};

export default preview;
tsx
import React from 'react';

// Replace your-framework with the framework you are using (e.g., react-vite, nextjs, nextjs-vite)
import { definePreview } from '@storybook/your-framework';

import { ThemeProvider } from 'styled-components';

// themes = { light, dark }
import * as themes from '../src/themes';

export default definePreview({
  decorators: [
    // ๐Ÿ‘‡ Defining the decorator in the preview file applies it to all stories
    (Story, { parameters }) => {
      // ๐Ÿ‘‡ Make it configurable by reading the theme value from parameters
      const { theme = 'light' } = parameters;
      return (
        <ThemeProvider theme={themes[theme]}>
          <Story />
        </ThemeProvider>
      );
    },
  ],
});
<!-- JS snippets still needed while providing both CSF 3 & Next -->
jsx
import React from 'react';

// Replace your-framework with the framework you are using (e.g., react-vite, nextjs, nextjs-vite)
import { definePreview } from '@storybook/your-framework';

import { ThemeProvider } from 'styled-components';

// themes = { light, dark }
import * as themes from '../src/themes';

export default definePreview({
  decorators: [
    // ๐Ÿ‘‡ Defining the decorator in the preview file applies it to all stories
    (Story, { parameters }) => {
      // ๐Ÿ‘‡ Make it configurable by reading the theme value from parameters
      const { theme = 'light' } = parameters;
      return (
        <ThemeProvider theme={themes[theme]}>
          <Story />
        </ThemeProvider>
      );
    },
  ],
});