Directives transformer
Transformer UnoCSS cho các chỉ thị @apply, @screen và theme(): @unocss/transformer-directives.
Cài đặt
pnpm add -D @unocss/transformer-directivesyarn add -D @unocss/transformer-directivesnpm install -D @unocss/transformer-directivesbun add -D @unocss/transformer-directivesimport transformerDirectives from '@unocss/transformer-directives'
import { defineConfig } from 'unocss'
export default defineConfig({
// ...
transformers: [
transformerDirectives(),
],
})TIP
Preset này được bao gồm trong gói unocss, bạn cũng có thể import nó từ đó:
import { transformerDirectives } from 'unocss'Sử dụng
@apply
.custom-div {
@apply text-center my-0 font-medium;
}Sẽ được biến đổi thành:
.custom-div {
margin-top: 0rem;
margin-bottom: 0rem;
text-align: center;
font-weight: 500;
}--at-apply
Để tương thích với CSS thuần, bạn có thể dùng các thuộc tính tùy chỉnh CSS để thay thế chỉ thị @apply:
.custom-div {
--at-apply: text-center my-0 font-medium;
}Tính năng này được bật theo mặc định với một số bí danh, bạn có thể cấu hình hoặc tắt qua:
transformerDirectives({
// the defaults
applyVariable: ['--at-apply', '--uno-apply', '--uno'],
// or disable with:
// applyVariable: false
})Thêm dấu ngoặc kép
Để dùng các quy tắc với :, bạn sẽ phải đặt dấu ngoặc quanh toàn bộ giá trị:
.custom-div {
--at-apply: 'hover:text-red hover:font-bold';
/* or */
@apply 'hover:text-red hover:font-bold';
}Dùng dấu ngoặc kép sau @apply là tùy chọn, để đáp ứng hành vi của một số trình định dạng.
@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.
.grid {
--uno: grid grid-cols-2;
}
@screen xs {
.grid {
--uno: grid-cols-1;
}
}
@screen sm {
.grid {
--uno: grid-cols-3;
}
}
/* ... */
...;Sẽ được biến đổi thành:
.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ể lt、at:
@screen lt-
.grid {
--uno: grid grid-cols-2;
}
@screen lt-xs {
.grid {
--uno: grid-cols-1;
}
}
@screen lt-sm {
.grid {
--uno: grid-cols-3;
}
}
/* ... */Sẽ được biến đổi thành:
.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 {
--uno: grid grid-cols-2;
}
@screen at-xs {
.grid {
--uno: grid-cols-1;
}
}
@screen at-xl {
.grid {
--uno: grid-cols-3;
}
}
@screen at-xxl {
.grid {
--uno: grid-cols-4;
}
}
/* ... */Sẽ được biến đổi thành:
.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.
.btn-blue {
background-color: theme('colors.blue.500');
}Sẽ được biên dịch thành:
.btn-blue {
background-color: #3b82f6;
}icon()
Dùng hàm icon() để chuyển đổi tiện ích biểu tượng thành biểu tượng svg cụ thể.
WARNING
icon() phụ thuộc vào @unocss/preset-icons và sẽ dùng cấu hình, hãy đảm bảo bạn đã thêm preset này.
.icon {
background-image: icon('i-carbon-sun');
}Sẽ được biên dịch thành:
.icon {
background-image: url("data:image/svg+xml;utf8,%3Csvg viewBox='0 0 32 32' width='1em' height='1em' xmlns='http://www.w3.org/2000/svg' %3E%3Cpath fill='currentColor' d='M16 12.005a4 4 0 1 1-4 4a4.005 4.005 0 0 1 4-4m0-2a6 6 0 1 0 6 6a6 6 0 0 0-6-6M5.394 6.813L6.81 5.399l3.505 3.506L8.9 10.319zM2 15.005h5v2H2zm3.394 10.193L8.9 21.692l1.414 1.414l-3.505 3.506zM15 25.005h2v5h-2zm6.687-1.9l1.414-1.414l3.506 3.506l-1.414 1.414zm3.313-8.1h5v2h-5zm-3.313-6.101l3.506-3.506l1.414 1.414l-3.506 3.506zM15 2.005h2v5h-2z'/%3E%3C/svg%3E");
}Vì biểu tượng dùng currentColor làm màu tô theo mặc định, nếu bạn muốn tùy chỉnh màu của biểu tượng, bạn có thể dùng icon('tên biểu tượng', 'màu tùy chỉnh').
.icon {
background-image: icon('i-carbon-moon', '#fff');
background-image: icon('i-carbon-moon', 'theme("colors.red.500")'); /* use theme color */
}Sẽ được biên dịch thành:
.icon {
background-image: url("data:image/svg+xml;utf8,%3Csvg viewBox='0 0 32 32' width='1em' height='1em' xmlns='http://www.w3.org/2000/svg' %3E%3Cpath fill='%23fff' d='M13.503 5.414a15.076 15.076 0 0 0 11.593 18.194a11.1 11.1 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1 1 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.07 13.07 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3'/%3E%3C/svg%3E");
background-image: url("data:image/svg+xml;utf8,%3Csvg viewBox='0 0 32 32' width='1em' height='1em' xmlns='http://www.w3.org/2000/svg' %3E%3Cpath fill='%23ef4444' d='M13.503 5.414a15.076 15.076 0 0 0 11.593 18.194a11.1 11.1 0 0 1-7.975 3.39c-.138 0-.278.005-.418 0a11.094 11.094 0 0 1-3.2-21.584M14.98 3a1 1 0 0 0-.175.016a13.096 13.096 0 0 0 1.825 25.981c.164.006.328 0 .49 0a13.07 13.07 0 0 0 10.703-5.555a1.01 1.01 0 0 0-.783-1.565A13.08 13.08 0 0 1 15.89 4.38A1.015 1.015 0 0 0 14.98 3'/%3E%3C/svg%3E");
}Giấy phép
- MIT License © 2022-PRESENT hannoeru