Skip to content

Wind3 预设

UnoCSS 的 Tailwind CSS / Windi CSS 紧凑预设。

源代码

INFO

@unocss/preset-wind@unocss/preset-uno 已被弃用并重命名为 @unocss/preset-wind3。此预设继承自 @unocss/preset-mini

安装

bash
pnpm add -D @unocss/preset-wind3
bash
yarn add -D @unocss/preset-wind3
bash
npm install -D @unocss/preset-wind3
bash
bun add -D @unocss/preset-wind3
ts
import presetWind3 from '@unocss/preset-wind3'
import { defineConfig } from 'unocss'

export default defineConfig({
  presets: [
    presetWind3(),
  ],
})

TIP

此预设已包含在 unocss 包中,您也可以从那里导入:

ts
import { presetWind3 } from 'unocss'

规则

此预设的主要目标是提供与 Tailwind CSSWindi CSS 的兼容性。需要注意的是,完全兼容性可能无法保证。请参考他们的文档以获取详细使用方法。

有关此预设中包含的所有规则和预设,请参考我们的交互式文档或直接查看源代码

特性

暗黑模式

默认情况下,此预设通过 dark: 变体生成基于类的暗黑模式。

html
<div class="dark:bg-red:10" />

将生成:

css
.dark .dark\:bg-red\:10 {
  background-color: rgb(248 113 113 / 0.1);
}

基于媒体查询的暗黑模式

要全局使用基于媒体查询的暗黑模式,您可以更改 dark: 变体的配置:

ts
presetWind3({
  dark: 'media'
})

现在

html
<div class="dark:bg-red:10" />

将生成:

css
@media (prefers-color-scheme: dark) {
  .dark\:bg-red\:10 {
    background-color: rgb(248 113 113 / 0.1);
  }
}

可选的媒体查询暗黑模式

要使用可选的媒体查询暗黑模式,您可以使用 @dark: 变体:

html
<div class="@dark:bg-red:10" />
css
@media (prefers-color-scheme: dark) {
  .\@dark\:bg-red\:10 {
    background-color: rgb(248 113 113 / 0.1);
  }
}

与 Tailwind CSS 的差异

引号

由于提取器的工作方式,模板中使用引号是不支持的。例如,您将无法编写 before:content-['']。对于这些情况,您可以引入一个新的实用工具,如 class="before:content-empty"

带有任意值的背景位置

Tailwind 允许使用自定义值作为 background-position

html
<div class="bg-[center_top_1rem]"></div>

Wind 预设会将 center_top_1rem 解释为颜色。要实现相同效果,请使用 position: 前缀:

html
<div class="bg-[position:center_top_1rem]"></div>

动画

Tailwind CSS 的内置动画较少,我们完全支持其动画规则,并在内部集成了 Animate.css 以提供更多动画效果。

您可以使用 animate- 前缀快速找到所需的动画。

TIP

我们不会合并 Tailwind 和 Animate.css 中冲突的动画名称。如果您需要使用 Animate.css 的动画名称,请使用 animate-<name>-alt

例如

Tailwind CSSAnimate.css
animate-bounceanimate-bounce-alt

如果您想自定义或修改动画效果,我们提供了高度可自定义的配置项。您可以通过配置项修改动画的持续时间、延迟、速度曲线等。

ts
export default defineConfig({
  theme: {
    animation: {
      keyframes: {
        custom: '{0%, 100% { transform: scale(0.5); } 50% { transform: scale(1); }}',
      },
      durations: {
        custom: '1s',
      },
      timingFns: {
        custom: 'cubic-bezier(0.4,0,.6,1)',
      },
      properties: {
        custom: { 'transform-origin': 'center' },
      },
      counts: {
        custom: 'infinite',
      },
    }
  }
})

预览自定义动画:

animate-custom

TIP

您还可以添加 category 来对动画进行分组,以便更好地管理。这将使下游工具更容易使用动画效果。

ts
export default defineConfig({
  theme: {
    animation: {
      keyframes: {
        custom: '{0%, 100% { transform: scale(0.5); } 50% { transform: scale(1); }}',
      },
      // ...
      category: {
        custom: 'Zooming',
      },
    }
  }
})

与 Windi CSS 的差异

断点

Windi CSSUnoCSS
<sm:p-1lt-sm:p-1
@lg:p-1at-lg:p-1
>xl:p-1xl:p-1

括号语法中的空格

此预设使用 _ 替代 , 来表示括号语法中的空格。

Windi CSSUnoCSS
grid-cols-[1fr,10px,max-content]grid-cols-[1fr_10px_max-content]

由于某些 CSS 规则需要 , 作为值的一部分,例如 grid-cols-[repeat(3,auto)]

实验性特性

WARNING

此预设包含可能随时以破坏性方式更改的实验性特性。

媒体悬停

媒体悬停解决了粘性悬停问题,即在移动设备上点击包含悬停样式的目标会使悬停样式持续到点击其他地方。

由于常规的 :hover 样式被广泛使用,变体使用 @hover 语法来区分常规的 hover 伪类。

变体 @hover-text-red 将输出:

css
@media (hover: hover) and (pointer: fine) {
  .\@hover-text-red:hover {
    --un-text-opacity: 1;
    color: rgb(248 113 113 / var(--un-text-opacity));
  }
}

选项

INFO

这些预设选项继承自 @unocss/preset-mini

important

  • 类型: boolean | string
  • 默认值: false

important 选项允许您控制 UnoCSS 的实用工具是否应标记为 !important。当与具有高特异性选择器的现有 CSS 一起使用时,这可能非常有用。

WARNING

使用此选项将对 UnoCSS 生成的所有实用工具应用 important。如果您只想对特定实用工具应用,可以使用 important: 变体。

然而,将 important 设置为 true 可能会在合并第三方 JS 库(这些库会为元素添加内联样式)时引入一些问题。在这些情况下,UnoCSS 的 !important 实用工具会覆盖内联样式,这可能会破坏您的预期设计。

为了解决这个问题,您可以将 important 设置为 ID 选择器,如 #app

ts
import presetWind3 from '@unocss/preset-wind'
import { defineConfig } from 'unocss'

export default defineConfig({
  presets: [
    presetWind3({
      important: '#app',
    }),
  ],
})

这个配置将为您的所有实用工具添加给定的选择器前缀,有效地提高其特异性,而不是实际使用 !important

实用工具 dark:bg-blue 将输出:

css
#app :is(.dark .dark\:bg-blue) {
  --un-bg-opacity: 1;
  background-color: rgb(96 165 250 / var(--un-bg-opacity));
}

Released under the MIT License.