PostCSS プラグイン
UnoCSS 用の PostCSS プラグイン。@apply
、@screen
、および theme()
ディレクティブをサポートします。
警告
このパッケージは現在実験的な状態です。セマンティックバージョニングに従っておらず、パッチバージョンで破壊的な変更が導入される可能性があります。
インストール
bash
pnpm add -D unocss @unocss/postcss
bash
yarn add -D unocss @unocss/postcss
bash
npm install -D unocss @unocss/postcss
ts
import UnoCSS from '@unocss/postcss'
export default {
plugins: [
UnoCSS(),
],
}
ts
import { defineConfig, presetUno } from 'unocss'
export default defineConfig({
content: {
filesystem: [
'**/*.{html,js,ts,jsx,tsx,vue,svelte,astro}',
],
},
presets: [
presetUno(),
],
})
css
@unocss;
使い方
@unocss
@unocss
at-rule はプレースホルダーです。生成された CSS に置き換えられます。
各レイヤーを個別に注入することもできます
css
@unocss preflights;
@unocss default;
/*
Fallback layer. It's always recommended to include.
Only unused layers will be injected here.
*/
@unocss;
以前に含まれているかどうかにかかわらず、すべてのレイヤーを含めたい場合は、@unocss all
を使用できます。これは、生成された CSS を複数のファイルに含めたい場合に役立ちます。
css
@unocss all;
@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;
}
@screen
@screen
ディレクティブを使用すると、theme.breakpoints
から取得した名前でブレークポイントを参照するメディアクエリを作成できます。
css
.grid {
@apply grid grid-cols-2;
}
@screen xs {
.grid {
@apply grid-cols-1;
}
}
@screen sm {
.grid {
@apply 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
は lt
、at
バリアントもサポートしています
@screen lt
css
.grid {
@apply grid grid-cols-2;
}
@screen lt-xs {
.grid {
@apply grid-cols-1;
}
}
@screen lt-sm {
.grid {
@apply 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 {
@apply grid grid-cols-2;
}
@screen at-xs {
.grid {
@apply grid-cols-1;
}
}
@screen at-xl {
.grid {
@apply grid-cols-3;
}
}
@screen at-xxl {
.grid {
@apply 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;
}