Skip to content

PostCSS Plugin

Plugin PostCSS cho UnoCSS. Hỗ trợ các chỉ thị @apply, @screentheme().

Mã nguồn

WARNING

Gói này hiện đang ở trạng thái thử nghiệm. Nó không tuân theo semver, và có thể giới thiệu các thay đổi phá vỡ trong các phiên bản patch.

Cài đặt

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;

Sử dụng

@unocss

At-rule @unocss là một placeholder. Nó sẽ được thay thế bằng CSS được tạo.

Bạn cũng có thể chèn từng layer riêng lẻ:

css
@unocss preflights;
@unocss default;

/*
  Fallback layer. It's always recommended to include.
  Only unused layers will be injected here.
*/
@unocss;

Nếu bạn muốn bao gồm tất cả các layer bất kể chúng đã được bao gồm trước đó hay không, bạn có thể dùng @unocss all. Điều này hữu ích nếu bạn muốn bao gồm CSS được tạo trong nhiều tệp.

css
@unocss all;

@apply

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

Sẽ được biến đổi thành:

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

@screen

Chỉ thị @screen cho phép bạn tạo các truy vấn media tham chiếu đến các điểm ngắt của bạn theo tên đến từ theme.breakpoints.

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

Sẽ được biến đổi thành:

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

Hỗ trợ Biến thể Điểm ngắt

@screen cũng hỗ trợ các biến thể 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;
  }
}
/* ... */

Sẽ được biến đổi thành:

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

Sẽ được biến đổi thành:

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()

Dùng hàm theme() để truy cập các giá trị cấu hình chủ đề của bạn bằng ký hiệu dấu chấm.

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

Sẽ được biên dịch thành:

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

Released under the MIT License.