Skip to content

快捷方式

¥Shortcuts

Windi CSS 的 启发,快捷方式可让你将多个规则组合成一个简写。

¥Shortcuts let you combine multiple rules into a single shorthand, inspired by Windi CSS's.

用法

¥Usage

ts
shortcuts: {
  // shortcuts to multiple utilities
  'btn': 'py-2 px-4 font-semibold rounded-lg shadow-md',
  'btn-green': 'text-white bg-green-500 hover:bg-green-700',
  // single utility alias
  'red': 'text-red-100',
}

除了普通映射之外,UnoCSS 还允许你定义动态快捷方式。

¥In addition to the plain mapping, UnoCSS also allows you to define dynamic shortcuts.

规则 类似,动态快捷方式是匹配器 RegExp 和处理函数的组合。

¥Similar to Rules, a dynamic shortcut is the combination of a matcher RegExp and a handler function.

ts
shortcuts: [
  // you could still have object style
  {
    btn: 'py-2 px-4 font-semibold rounded-lg shadow-md',
  },
  // dynamic shortcuts
  [/^btn-(.*)$/, ([, c]) => `bg-${c}-400 text-${c}-100 py-2 px-4 rounded-lg`],
]

这样,我们就可以使用 btn-greenbtn-red 生成以下 CSS:

¥With this, we could use btn-green and btn-red to generate the following CSS:

css
.btn-green {
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
  padding-left: 1rem;
  padding-right: 1rem;
  --un-bg-opacity: 1;
  background-color: rgb(74 222 128 / var(--un-bg-opacity));
  border-radius: 0.5rem;
  --un-text-opacity: 1;
  color: rgb(220 252 231 / var(--un-text-opacity));
}
.btn-red {
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
  padding-left: 1rem;
  padding-right: 1rem;
  --un-bg-opacity: 1;
  background-color: rgb(248 113 113 / var(--un-bg-opacity));
  border-radius: 0.5rem;
  --un-text-opacity: 1;
  color: rgb(254 226 226 / var(--un-text-opacity));
}