Skip to content

PostCSSプラグイン

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

ソースコード

WARNING

このパッケージは現在実験的な状態です。semverに従っておらず、パッチバージョンでも破壊的変更が入る可能性があります。

インストール

bash
pnpm add -D unocss @unocss/postcss
bash
yarn add -D unocss @unocss/postcss
bash
npm install -D unocss @unocss/postcss
bash
bun add -D unocss @unocss/postcss
ts
import UnoCSS from '@unocss/postcss'

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

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

使い方

@unocss

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

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

css
@unocss preflights;
@unocss default;

/*
  フォールバックレイヤー。常に含めることを推奨します。
  未使用のレイヤーのみここに挿入されます。
*/
@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));
  }
}
/* ... */

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

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

@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;
}

Released under the MIT License.