storybookjs / storybook

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation
https://storybook.js.org
MIT License
83.94k stars 9.21k forks source link

[Bug]: using tailwindcss in storybook, the presets:[xxx] is not working #28451

Closed rick-liyue-huang closed 3 weeks ago

rick-liyue-huang commented 2 months ago

Describe the bug

I create two vite react tailwindcss storybook project, project-one and project-two, I created the customized style in project-one tailwind.config.js, and export it to project two in project-two, I define the tailwind.config.js as:

import * as uiCoreConfig from 'project-one';

/** @type {import('tailwindcss').Config} */
export default {
    presets: [uiCoreConfig],
    content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
    theme: {
        extend: {},
    },
    plugins: [],
};
``` I found that it is working when run `npm run dev`, the div or p tag can use the customized style properly, but when it applied on storybook, it is not work. 

### Reproduction link

a

### Reproduction steps

_No response_

### System

```bash
a

Additional context

No response

greptile-apps[bot] commented 2 months ago

To resolve the issue with Tailwind CSS presets not working in Storybook, follow these steps:

  1. Import Tailwind CSS in .storybook/preview.js:
import { Preview } from '@storybook/react';
+import '../src/index.css';

const preview: Preview = {
  parameters: { /* ... */ },
};

export default preview;
  1. Ensure Tailwind CSS configuration is correctly set up in tailwind.config.js:
import * as uiCoreConfig from 'project-one';

/** @type {import('tailwindcss').Config} */
export default {
    presets: [uiCoreConfig],
    content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
    theme: {
        extend: {},
    },
    plugins: [],
};
  1. Register the necessary addons in .storybook/main.js:
export default {
  stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
  addons: [
    '@storybook/addon-essentials',
+   '@storybook/addon-themes',
  ],
};
  1. Add the withThemeByClassName decorator in .storybook/preview.js:
import { Preview, Renderer } from '@storybook/react';
+import { withThemeByClassName } from '@storybook/addon-themes';
import '../src/index.css';

const preview: Preview = {
  parameters: { /* ... */ },
+ decorators: [
+  withThemeByClassName<Renderer>({
+    themes: {
+      light: '',
+      dark: 'dark',
+    },
+    defaultTheme: 'light',
+  }),
+ ]
};

export default preview;

References

/code/addons/themes/docs/getting-started/tailwind.md /code/.storybook/preview.tsx /code/.storybook/main.ts

#### About Greptile This response provides a starting point for your research, not a precise solution. Help us improve! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant. [Ask Greptile](https://app.greptile.com/chat/github/storybookjs/storybook/next) ยท [Edit Issue Bot Settings](https://app.greptile.com/apps/github)
vanessayuenn commented 1 month ago

Hi, thanks for reporting this. do you have a reproduction repo you can share? If not, can you please create one via storybook.new? We prioritize bug reports that have reproduction. Thank you! ๐Ÿ™

github-actions[bot] commented 1 month ago

Hi there! Thank you for opening this issue, but it has been marked as stale because we need more information to move forward. Could you please provide us with the requested reproduction or additional information that could help us better understand the problem? We'd love to resolve this issue, but we can't do it without your help!

github-actions[bot] commented 3 weeks ago

I'm afraid we need to close this issue for now, since we can't take any action without the requested reproduction or additional information. But please don't hesitate to open a new issue if the problem persists โ€“ we're always happy to help. Thanks so much for your understanding.