Skip to content

指令轉換器

UnoCSS 的 @apply@screentheme() 指令轉換器:@unocss/transformer-directives

安裝

bash
pnpm add -D @unocss/transformer-directives
bash
yarn add -D @unocss/transformer-directives
bash
npm install -D @unocss/transformer-directives
ts
// uno.config.ts
import { defineConfig } from 'unocss'
import transformerDirectives from '@unocss/transformer-directives'

export default defineConfig({
  // ...
  transformers: [
    transformerDirectives(),
  ],
})

用法

@apply

css
.custom-div {
  @apply text-center my-0 font-medium;
}

將被轉換為:

css
.custom-div {
  margin-top: 0rem;
  margin-bottom: 0rem;
  text-align: center;
  font-weight: 500;
}

--at-apply

為了與原生 CSS 兼容,你可以使用 CSS 自定義屬性替換 @apply 指令:

css
.custom-div {
  --at-apply: text-center my-0 font-medium;
}

此功能默認啟用,並帶有幾個別名,你可以通過以下方式配置或禁用:

js
transformerDirectives({
  // 默認值
  applyVariable: ['--at-apply', '--uno-apply', '--uno'],
  // 或禁用:
  // applyVariable: false
})

添加引號

要使用包含 : 的規則,你需要為整個值添加引號:

css
.custom-div {
  --at-apply: "hover:text-red hover:font-bold";
  /* 或 */
  @apply 'hover:text-red hover:font-bold';
}

@apply 後使用引號是可選的,以滿足某些格式化程序的行為。

@screen

@screen 指令允許你創建引用斷點名稱的媒體查詢,來自 theme.breakpoints

css
.grid {
  --uno: grid grid-cols-2;
}
@screen xs {
  .grid {
    --uno: grid-cols-1;
  }
}
@screen sm {
  .grid {
    --uno: grid-cols-3;
  }
}
/* ... */

將被轉換為:

css
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (min-width: 320px) {
  .grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
  }
}
@media (min-width: 640px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
/* ... */

斷點變體支持

@screen 還支持 ltat 變體:

@screen lt-

css
.grid {
  --uno: grid grid-cols-2;
}
@screen lt-xs {
  .grid {
    --uno: grid-cols-1;
  }
}
@screen lt-sm {
  .grid {
    --uno: grid-cols-3;
  }
}
/* ... */

將被轉換為:

css
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (max-width: 319.9px) {
  .grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
  }
}
@media (max-width: 639.9px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
/* ... */

@screen at-

css
.grid {
  --uno: grid grid-cols-2;
}
@screen at-xs {
  .grid {
    --uno: grid-cols-1;
  }
}
@screen at-xl {
  .grid {
    --uno: grid-cols-3;
  }
}
@screen at-xxl {
  .grid {
    --uno: grid-cols-4;
  }
}
/* ... */

將被轉換為:

css
.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (min-width: 320px) and (max-width: 639.9px) {
  .grid {
    grid-template-columns: repeat(1, minmax(0, 1fr));
  }
}
@media (min-width: 1280px) and (max-width: 1535.9px) {
  .grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
@media (min-width: 1536px) {
  .grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}
/* ... */

theme()

使用 theme() 函數通過點符號訪問你的主題配置值。

css
.btn-blue {
  background-color: theme('colors.blue.500');
}

將被編譯為:

css
.btn-blue {
  background-color: #3b82f6;
}

許可證

Released under the MIT License.