tailwind css_带有Tailwind CSS和Nuxt.js的深色和浅色主题

tailwind css

Dark and light mode support has been a recent trend due to the impact on our eyesight caused by the time spent on devices. With Tailwind CSS and the proper Nuxt.js (Nuxt) modules, we can easily enable this feature, with the ability to customize each mode’s look and feel to your taste.

由于在设备上花费的时间对我们的视力造成了影响,因此对明暗模式的支持已成为最近的趋势。 借助Tailwind CSS和适当的Nuxt.js (Nuxt)模块,我们可以轻松启用此功能,并能够根据您的喜好自定义每种模式的外观。

TL; DR (TL;DR)

In short, you follow the simple steps below to enable dark/light mode with Tailwind CSS and the Nuxt color mode module:

简而言之,您可以按照以下简单步骤使用Tailwind CSS和Nuxt颜色模式模块启用暗/亮模式:

  • Install a Nuxt project using yarn create nuxt-app and choose Tailwind CSS as a UI framework from the configuration selection. In case of an existing Nuxt project, run yarn add --dev @nuxtjs/tailwindcss and add the module @nuxtjs/tailwindcss to the list of buildModules in nuxt.config.js.

    使用yarn create nuxt-app 安装Nuxt项目,然后从配置选择中选择Tailwind CSS作为UI框架。 如果现有Nuxt项目,请运行yarn add --dev @nuxtjs/tailwindcss并将模块@nuxtjs/tailwindcss添加到buildModules中的nuxt.config.js列表中。

  • Install tailwindcss-dark-mode and @nuxtjs/color-mode.

    安装@nuxtjs/color-mode tailwindcss-dark-mode@nuxtjs/color-mode

  • Declare the use of tailwindcss-dark-mode as a plugin in the collection of plugins in tailwind.config.js. Do the same with @nuxtjs/color-mode by adding require('tailwindcss-dark-mode')() to the collection of plugins in nuxt.config.js.

    声明使用的tailwindcss-dark-mode的集合中的一个插件, pluginstailwind.config.js 。 通过在nuxt.config.jsplugins集合中添加require('tailwindcss-dark-mode')()来对@nuxtjs/color-mode进行相同的nuxt.config.js

  • Declare the use of dark mode variants per CSS utilities in the variants field in tailwind.config.js.

    tailwind.config.js中的variants字段中声明每个CSS实用工具使用深色模式变variants

  • Start assigning dark mode styles for elements using generated classes with syntax ${dark-mode-variant}:${normal-generated-class-for-css-property}.

    开始使用语法${dark-mode-variant}:${normal-generated-class-for-css-property}生成类为元素分配暗模式样式。

Too short to understand? Let’s go through slowly, shall we?

太短了以至于无法理解? 让我们慢慢来吧?

什么是Nuxt.js? (What Is Nuxt.js?)

Nuxt.js — or Nuxt for short — is a server-side rendering (SSR) framework based on Vue.js, and it offers developers:

Nuxt.js(简称为Nuxt)是基于Vue.js的服务器端渲染(SSR)框架,它为开发人员提供:

  • Flexibility — With a single codebase, developers have three build options to output their application, such as universal (pure SSR), single-page application (SPA), and static sites.

    灵活性-使用单个代码库,开发人员可以使用三种构建选项来输出其应用程序,例如通用(纯SSR),单页应用程序(SPA)和静态站点。
  • Organized code structure — A Nuxt app template comes with logical folder structures with a built-in router mechanism.

    有组织的代码结构-Nuxt应用程序模板带有带有内置路由器机制的逻辑文件夹结构。
  • Performance optimization — It has auto-code-splitting per page, keeping JavaScript bundled size relatively small for faster delivery.

    性能优化-它具有每页自动代码拆分功能,可将JavaScript绑定大小保持相对较小,以便更快地交付。

Also, Nuxt is surrounded by a well-maintained ecosystem — the Nuxt community, which provides lots of practical modules, plugins, and support to simplify the development process.

此外,Nuxt周围还有一个维护良好的生态系统-Nuxt社区,该社区提供了许多实用的模块,插件和支持,以简化开发过程。

什么是Tailwind CSS? (What Is Tailwind CSS?)

Tailwind CSS is a very efficient utility-first CSS framework. It offers developers a set of ready classes for different CSS utility properties, such as margin, padding, and font-size for styling.

Tailwind CSS是一个非常有效的实用程序优先CSS框架。 它为开发人员提供了一组用于不同CSS实用程序属性的就绪类,例如marginpaddingfont-size用于样式设置。

For example, to make the button above, it’s enough to apply the available classes accordingly:

例如,要使按钮上方,足以相应地应用可用的类:

 class="py-2 px-3 bg-green-500 rounded shadow-md text-white uppercase"
>
Button

In which:

其中:

  • py-—prefix for styles applied to padding-top and padding-bottom, similarly with px- for setting padding-left and padding-right properties

    py-适用于padding-toppadding-bottom样式的前缀,与px-类似,用于设置padding-leftpadding-right属性

  • bg- — prefer for background, followed by a color palette

    bg-偏爱background ,后跟调色板

  • rounded—set the value of border-radius

    rounded -设置border-radius的值

  • shadow — —prefix for box-shadow

    shadow — box-shadow前缀

  • text-—prefix for any text-related CSS properties (font-size and color), like text-white for setting the color to white

    text-任何与文本相关CSS属性( font-sizecolor )的前缀,例如text-white用于将color设置为white

We can also combine different classes to create a custom class, such as the .btn class from the classes used in the example above. The significant advantage of Tailwind CSS is that it allows developers to write less (and less repetitive) CSS code for utilities, and hence keep the overall design consistent, with @apply.

我们还可以组合不同的类来创建自定义类,例如上例中使用的类中的.btn类。 Tailwind CSS的显着优势在于,它允许开发人员为实用程序编写更少(和更少重复)CSS代码,从而使总体设计与@apply保持一致。

.btn {
         
@apply py-2 px-3 bg-green-500 rounded-md shadow-md text-white uppercase;
}

Tailwind CSS will generate the related styles into a single rule set for the .btn class selector, as seen in the browser:

Tailwind CSS会将相关样式生成为.btn类选择器的单个规则集,如浏览器所示:

By using PurgeCSS in the back, Tailwind CSS offers the ability to remove unused classes in our application and thus optimize the size of CSS needed on production.

通过在后面使用PurgeCSS ,Tailwind CSS可以删除应用程序中未使用的类,从而优化生产所需CSS大小。

Got the idea of Nuxt and Tailwind CSS? Great. How do we start?

有Nuxt和Tailwind CSS的想法吗? 大。 我们如何开始?

使用Tailwind CSS设置Nuxt项目 (Set Up a Nuxt Project With Tailwind CSS)

The most straightforward way to initialize a new Nuxt project is to use the create-nuxt-app scaffolding tool made by the official Nuxt team. To install create-nuxt-app, we use the following command:

初始化新Nuxt项目的最直接方法是使用官方Nuxt团队制作的create-nuxt-app脚手架工具。 要安装create-nuxt-app ,我们使用以下命令:

npm i create-nuxt-app

Note: If you have create-nuxt-app installed previously lower than 3.1.0, make sure to rerun this command and update it to the latest version.

注意:如果您以前安装的create-nuxt-app低于3.1.0,请确保重新运行此命令并将其更新为最新版本。

Once we have create-nuxt-app installed, let's create an empty Nuxt project using one of the following methods:

安装create-nuxt-app ,让我们使用以下方法之一创建一个空的Nuxt项目:

#Using npm (v6.1 onwards)npm init nuxt-app #Or using YARNyarn create nuxt-app #Or using npxnpx create-nuxt-app 

And then select the configuration for our app accordingly.

然后相应地为我们的应用选择配置。

To add Tailwind CSS, we simply choose Tailwind CSS from a selection list of the UI framework, as seen below:

要添加Tailwind CSS,我们只需从UI框架的选择列表中选择Tailwind CSS,如下所示:

And that’s it. We have Tailwind CSS added to our newly-created application and ready to use.

就是这样。 我们已经将Tailwind CSS添加到了我们新创建的应用程序中并可以使用。

Note: For adding Tailwind CSS to existing Nuxt project, we install the Nuxt module @nuxtjs/tailwindcss:

注意 :要将Tailwind CSS添加到现有的Nuxt项目中,请安装Nuxt模块@nuxtjs/tailwindcss

yarn add --dev @nuxtjs/tailwindcss #OR
npm i --save-dev @nuxtjs/tailwindcss

Then add @nuxtjs/tailwindcss as a module to buildModules (or modules, for Nuxt version < 2.9.0) in the nuxt.config.js file:

然后添加@nuxtjs/tailwindcss作为一个模块来buildModules (或modules ,用于Nuxt版本<2.9.0)在nuxt.config.js文件:

Simple as that.

就那么简单。

Now let’s navigate to our project directory and start theming.

现在,让我们导航到我们的项目目录并开始主题化。

使用Tailwind CSS为项目设置主题 (Theme Your Project With Tailwind CSS)

The Nuxt module for Tailwind CSS will automatically add two files to your project directory:

Tailwind CSS的Nuxt模块将自动将两个文件添加到您的项目目录中:

  • ~/assets/css/tailwind.css — includes all the basic Tailwind styles, like basics, components, and utilities for use in our application

    ~/assets/css/tailwind.css —包括所有基本的Tailwind样式,例如在我们的应用utilities中使用的basicscomponents和Utility

  • ~/tailwind.config.js — the configuration file for additional customization, as shown below:

    ~/tailwind.config.js —用于其他定制的配置文件,如下所示:

Let’s look a bit into the properties defined in this file:

让我们看一下此文件中定义的属性:

  • theme — where we set up all the project’s additional customized theming, including color palette, font family, breakpoints, border, minimum/maximum sizes, etc.

    theme -我们在其中设置项目的所有其他自定义主题,包括调色板,字体系列,断点,边框,最小/最大尺寸等。

  • variant — where we define an array of responsive and pseudo-class variants for selected core utility plugins, such as appearance, borderColor, outline , zIndex , etc.

    variant —我们为选定的核心实用程序插件定义了响应和伪类变体的数组,例如: appearanceborderColoroutlinezIndex等。

  • plugins — a collection of JavaScript functions that allow us to register additional styles programmatically

    plugins -JavaScript函数的集合,使我们能够以编程方式注册其他样式

  • purge — can be an array, an object, or a Boolean value indicating how we want to remove unused styles (or not). The Nuxt module for Tailwind CSS automatically adds the needed code to enable purging CSS code during production, following with a list of files that have reference to any used CSS styles by name.

    purge —可以是数组,对象或布尔值,指示我们如何删除(或不删除)未使用的样式。 用于Tailwind CSS的Nuxt模块会自动添加所需的代码,以在生产期间清除CSS代码,然后列出按名称引用了所有使用过CSS样式的文件列表。

We now use tailwind.config.js to configure the custom look and feel for our application.

现在,我们使用tailwind.config.js为我们的应用程序配置自定义外观。

tailwind.config.js配置您的自定义颜色主题 (Configure Your Custom Color Theme in tailwind.config.js)

Assume we have the following color palettes we would like to use as the main theme in our application:

假设我们要使用以下调色板作为应用程序中的主要主题:

To override the default theme given by Tailwind CSS, we can modify the theme object with the colors field directly.

要覆盖Tailwind CSS提供的默认主题,我们可以直接使用colors字段修改theme对象。

However, in case we would like to keep the default theme, Tailwind CSS provides us the option to add an extra theme by using the theme.extend key, which receives an object of theming options as a value, same as theme.

但是,如果我们想保留默认主题,则Tailwind CSS为我们提供了使用theme.extend键添加一个额外主题的选项,该键接收一个主题选项的对象作为值,与theme相同。

Personally, I recommend using extend to enjoy the benefit of both the main custom theme colors and the default set of colors for other minor uses.

就个人而言,我建议使用extend来享受主要自定义主题颜色和其他次要用途的默认颜色集的好处。

Let’s define our above set of color palettes as properties under the colors field of theme.extend, as shown below:

让我们将上面的调色板集定义为theme.extendcolors字段下的theme.extend ,如下所示:

Tip: You can even group palettes into a nested object under colors and define them as modifiers, keeping your code organized:

提示 :您甚至可以将调色板分组为colors下的嵌套对象,并将它们定义为修饰符,从而使代码井井有条:

That’s all we need to do. Now our color palettes are ready to use.

这就是我们要做的。 现在我们的调色板可以使用了。

在您的Nuxt项目中使用Tailwind CSS (Use Tailwind CSS in Your Nuxt Project)

All the added color palettes are available under generated classes with the following syntax:

所有添加的调色板都可以在生成的类下使用以下语法获得:

${prefix-for-css-property}-${color-key}-${modifier}

The prefix is the variant that stands for a CSS property relatively. In this case, Tailwind CSS only generates the new classes for CSS properties that accept color as their value, such as background, color, border, etc. An example of newly generated classes from the above color palette are:

前缀是相对代表CSS属性的变体。 在这种情况下,Tailwind CSS仅为接受颜色作为其值CSS属性生成新类,例如backgroundcolorborder等。从上述调色板中新生成的类的示例如下:

To apply our green palette to the background color of the whole app, add bg-green as a class of the main div of the default layout:

要将我们的green调色板应用于整个应用的背景色,请添加bg-green作为默认布局的主要div类:

Our main page will change from the default background color:

我们的主页将从默认的背景颜色更改:

to the selected green color:

到所选的绿色:

Simple right? And we can continue building our app’s custom look and feel with these custom-generated classes.

简单吧? 我们可以使用这些自定义生成的类继续构建应用的自定义外观。

Now let’s continue to our next topic: How do we enable different color themes for dark/light mode with Tailwind CSS?

现在让我们继续下一个主题:如何通过Tailwind CSS为暗/亮模式启用不同的颜色主题?

具有@nuxtjs/color-mode模块的暗/ @nuxtjs/color-mode (Dark/Light Mode With @nuxtjs/color-mode Module)

Now that we have our green color palette ready for use, our next goal is to map green.olive to dark mode as its theme and green.yellow for light mode, according the below design:

现在我们已经准备好使用绿色调色板,我们的下一个目标是根据以下设计,将green.olive映射为暗模式作为主题,将green.yellow为轻模式。

To achieve our goal, we will need to set up two additional plugins:

为了实现我们的目标,我们将需要设置两个额外的插件:

  • @nuxtjs/color-mode — a Nuxt plugin for toggling between dark/light mode for the site and auto-detecting the right mode from the device's system appearance

    @nuxtjs/color-mode —一个Nuxt插件 ,用于在站点的暗/亮模式之间切换并从设备的系统外观自动检测正确的模式

  • tailwindcss-dark-mode — a Tailwind CSS plugin for injecting and enabling dark mode variants

    tailwindcss-dark-mode mode-一个Tailwind CSS插件,用于注入和启用黑暗模式变量

添加色彩模式模块 (Add the Color Mode Module)

We first install the Nuxt module @nuxtjs/color-mode by running one of the following commands:

我们首先通过运行以下命令之一来安装Nuxt模块@nuxtjs/color-mode

yarn add --dev @nuxtjs/color-mode #Or
npm i --save-dev @nuxtjs/color-mode

Then we configure our app to use it as a module of buildModules in nuxt.config.js:

然后,我们将应用程序配置为在nuxt.config.js其用作buildModules的模块:

/* ~/nuxt.config.js */module.exports = {
         
buildModules: [ '@nuxtjs/color-mode' ]
}

Once installed, the color mode module will add to the root element a class with the syntax .${colorMode}-mode. The value of colorMode can be dark, light, and system. Also, it exposes to all Nuxt components within the application a helper object $colorMode, containing the following fields:

安装后,颜色模式模块将使用语法.${colorMode}-mode向根元素一个类。 colorMode的值可以是darklightsystem 。 而且,它向应用程序内的所有Nuxt组件提供一个辅助对象$colorMode ,其中包含以下字段:

  • preference — the actual preferred (selected) color mode by the user

    preference —用户实际首选的(选择的)颜色模式

  • value — read-only, indicating the detected color mode from the device's system appearance

    value —只读,指示从设备的系统外观中检测到的颜色模式

  • unknown — read-only, indicating whether we need to render a placeholder.

    unknown —只读,指示我们是否需要渲染占位符。

We can set the basic color theme (background and text color) for our entire app in response to dark/light mode using the generated class dark-mode and light-mode , as below:

我们可以使用生成的类dark-modelight-mode ,为整个应用设置基本颜色主题(背景和文本颜色)以响应暗/亮light-mode ,如下所示:

Since our theme is set up, we need a way to toggle between these two modes. So let’s make a button!

由于我们已经设置了主题,因此我们需要一种在这两种模式之间切换的方法。 因此,让我们做一个按钮!

为暗/亮模式设置一个切换按钮 (Make a Toggle Button for Dark/Light Mode)

To switch between dark/light mode, we create a button whose labels will toggle between Dark and Light. Add a new file ColorMode.vue as a Vue component to the components folder and add the following code to the