Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Configure and use UnoCSS atomic CSS engine with presets like Wind (Tailwind-compatible), Icons, and Attributify.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/core-safelist.md
1---2name: unocss-safelist-blocklist3description: Force include or exclude specific utilities4---56# Safelist and Blocklist78Control which utilities are always included or excluded.910## Safelist1112Utilities always included, regardless of detection:1314```ts15export default defineConfig({16safelist: [17'p-1', 'p-2', 'p-3',18// Dynamic generation19...Array.from({ length: 4 }, (_, i) => `p-${i + 1}`),20],21})22```2324### Function Form2526```ts27safelist: [28'p-1',29() => ['m-1', 'm-2'],30(context) => {31const colors = Object.keys(context.theme.colors || {})32return colors.map(c => `bg-${c}-500`)33},34]35```3637### Common Use Cases3839```ts40safelist: [41// Dynamic colors from CMS42() => ['primary', 'secondary'].flatMap(c => [43`bg-${c}`, `text-${c}`, `border-${c}`,44]),4546// Component variants47() => {48const variants = ['primary', 'danger']49const sizes = ['sm', 'md', 'lg']50return variants.flatMap(v => sizes.map(s => `btn-${v}-${s}`))51},52]53```5455## Blocklist5657Utilities never generated:5859```ts60blocklist: [61'p-1', // Exact match62/^p-[2-4]$/, // Regex63]64```6566### With Messages6768```ts69blocklist: [70['bg-red-500', { message: 'Use bg-red-600 instead' }],71[/^text-xs$/, { message: 'Use text-sm for accessibility' }],72]73```7475## Safelist vs Blocklist7677| Feature | Safelist | Blocklist |78|---------|----------|-----------|79| Purpose | Always include | Always exclude |80| Strings | ✅ | ✅ |81| Regex | ❌ | ✅ |82| Functions | ✅ | ❌ |8384**Note:** Blocklist wins if utility is in both.8586## Best Practice8788Prefer static mappings over safelist:8990```ts91// Better: UnoCSS extracts automatically92const sizes = {93sm: 'text-sm p-2',94md: 'text-base p-4',95}9697// Avoid: Large safelist98safelist: ['text-sm', 'text-base', 'p-2', 'p-4']99```100101<!--102Source references:103- https://unocss.dev/config/safelist104- https://unocss.dev/guide/extracting105-->106