1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Next.jsでMaterial UIとTailwind CSSとStorybookを併用する時のメモ

Last updated at Posted at 2022-04-01

Material UIとTailwind CSSは事前に導入されている前提です。
StorybookとTailwind CSSを併用する場合、ちょっと設定が必要なのでメモ

Storybookに@storybook/addon-postcssを導入しないとyarn storybook実行時にエラーがでてしまいます。
StorybookとTailwindでPostCSSが競合?してしまうことが原因みたいです。(よくわかっていない)

addon-postcssを導入
yarn add -D @storybook/addon-postcss
.storybook/main.js
module.exports = {
  addons: [
    // 追加
    {
      name: '@storybook/addon-postcss',
      options: {
        postcssLoaderOptions: {
          implementation: require('postcss'),
        },
      },
    },
  ],
};

Material UIのコンポーネントにclassName属性にTailwind CSSのクラスを指定すると、既存のスタイルを上書きします。

tailwind.config.js
module.exports = {
  variants: {
    extend: {},
  },
  plugins: [],
+ important: true, //追加
}
test.tsx
<Button variant="contained" className="bg-blue-500 hover:cursor-pointer hover:bg-blue-600"></Button>

StorybookもでTailwindのスタイルが反映するようにする

.storybook/preview.js
// 追加
import '../styles/globals.css';
globals.css
@tailwind base; // 追加
@tailwind components; // 追加
@tailwind utilities; // 追加
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?