UnoCSS PostCSS プラグイン

PostCSS プラグイン

UnoCSS のための PostCSS プラグインです。@apply@screentheme() ディレクティブをサポートしています。

ソースコード

::: warning このパッケージは現在実験的な状態です。セマンティックバージョニングに従っておらず、パッチバージョンで破壊的変更が導入される可能性があります。 :::

インストール

::: code-group

pnpm
pnpm add -D unocss @unocss/postcss
yarn
yarn add -D unocss @unocss/postcss
npm
npm install -D unocss @unocss/postcss
bun
bun add -D unocss @unocss/postcss

:::

postcss.config.mjs
import UnoCSS from '@unocss/postcss'

export default {
  plugins: [
    UnoCSS(),
  ],
}
uno.config.ts
import { defineConfig, presetWind3 } from 'unocss'

export default defineConfig({
  content: {
    filesystem: [
      '**/*.{html,js,ts,jsx,tsx,vue,svelte,astro,marko}',
    ],
  },
  presets: [
    presetWind3(),
  ],
})
style.css
@unocss;

使用法

@unocss

@unocss アットルールはプレースホルダーです。生成された CSS に置き換えられます。

各レイヤーを個別に挿入することもできます:

style.css
@unocss preflights;
@unocss default;

/*
  フォールバックレイヤー。常に含めることをお勧めします。
  未使用のレイヤーのみがここに挿入されます。
*/
@unocss;

すべてのレイヤーを含めたい場合は、@unocss all を使用できます。これは、複数のファイルに生成された CSS を含めたい場合に便利です。

@unocss all;

特定のレイヤーを除外したい場合は、@unocss !<layer> ディレクティブを使用できます:

@unocss !preflights, !<other-layer>;

@apply

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

次のように変換されます:

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

@screen

@screen ディレクティブを使用すると、theme.breakpoints から名前で参照されるブレークポイントを使用してメディアクエリを作成できます。

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

次のように変換されます:

.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));
  }
}
/* ... */

ブレークポイントバリアントのサポート

@screenltat バリアントもサポートしています。

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

次のように変換されます:

.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
.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;
  }
}
/* ... */

次のように変換されます:

.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() 関数を使用して、ドット表記でテーマ設定値にアクセスします。

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

次のようにコンパイルされます:

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