LoginSignup
6
1

More than 3 years have passed since last update.

Nuxt color-mode + tailwindcssでテーマ切り替え

Last updated at Posted at 2020-07-08

最近多くなってきたスマホやPCのダークモード対応

115cc9326966a97927fc2dcaf39df180.gif

このQuick way to add darkmode in Nuxt.js & Tailwindcss project— Corona Virus Trackerを参考に。

まず yarn add -D @nuxtjs/color-mode tailwindcss-dark-mode します。

そして
nuxt.config.js

export default {
  ...
  buildModules: [
    ...
    '@nuxtjs/tailwindcss',
    '@nuxtjs/color-mode',
    ...
  ],
  ...
  purgeCSS: {
    whitelist: ['dark-mode', 'system', 'light-mode'],
  },
}

Nuxt also includes purgeCSS when we installed the TailwindCSS framework, which auto removes unused css, since we are adding dark selector classes manually, we need to whitelist them in our nuxt config

らしいので念の為すべてをpurgeCSSに追加してます。
yarn dev すると
html class='light-mode'html class='dark-mode' が付与されるので適宜css追加したり tailwindcssのプレフィックスを使用して class='dark:bg-red-600 bg-red-100' 当指定してあげれば切り替えることが出来ます。

又、computedを使用すればsvg等のfillの色を変えたりも出来ます。

7dc3c0ecf9c558e50b661a4018899cf7.gif

  computed: {
    logoColor() {
      // オブジェクトで:style="logoColor"に渡してあげる
      return { fill: this.$colorMode.value === 'light' ? 'black' : 'white' }
    },
  },

追記

vscodeでtailwindcss使うならTailwind CSS IntelliSenseが凄く便利です。

image.png

6
1
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
6
1