Skip to content

PostCSS Plugin

PostCSS plugin สำหรับ UnoCSS รองรับ directives @apply, @screen และ theme()

ซอร์สโค้ด

WARNING

แพ็กเกจนี้อยู่ในสถานะ experimental ในขณะนี้ ไม่ปฏิบัติตาม semver และอาจมีการเปลี่ยนแปลง breaking changes ใน patch versions

การติดตั้ง

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 at-rule เป็น placeholder จะถูกแทนที่ด้วย CSS ที่สร้างขึ้น

คุณยังสามารถ inject แต่ละ layer แยกกัน:

css
@unocss preflights;
@unocss default;

/*
  Fallback layer แนะนำให้รวมเสมอ
  เฉพาะ layers ที่ไม่ได้ใช้จะถูก inject ที่นี่
*/
@unocss;

หากคุณต้องการรวมทุก layers ไม่ว่าจะถูกรวมไปแล้วหรือไม่ คุณสามารถใช้ @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

directive @screen ช่วยให้คุณสร้าง media queries ที่อ้างอิง breakpoints ของคุณตามชื่อที่มาจาก 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));
  }
}
/* ... */

การรองรับ Breakpoint Variant

@screen ยังรองรับ variants ltat

@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() เพื่อเข้าถึงค่า theme config ของคุณโดยใช้ dot notation

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

จะถูก compile เป็น:

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

Released under the MIT License.