Skip to content

Commit 4743856

Browse files
committed
fix: add inline-block for icon classes, with transformer
1 parent f9fd6ad commit 4743856

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { SourceCodeTransformer } from 'unocss'
2+
3+
/**
4+
* Transformer that automatically adds `inline-block` next to any icon class
5+
* (`i-*`) in the source code, so the class appears in the rendered HTML.
6+
*/
7+
export function transformerIconInlineBlock(): SourceCodeTransformer {
8+
return {
9+
name: 'icon-inline-block',
10+
enforce: 'pre',
11+
transform(s) {
12+
const code = s.original
13+
// Match icon classes like i-lucide-star, i-mdi-home, i-custom-vlt etc.
14+
const iconClassRe = /\bi-[\w:-]+/g
15+
let match: RegExpExecArray | null
16+
17+
while ((match = iconClassRe.exec(code)) !== null) {
18+
const end = match.index + match[0].length
19+
// Skip if `inline-block` already follows (with optional whitespace)
20+
if (/^\s+inline-block\b/.test(code.slice(end))) {
21+
continue
22+
}
23+
s.appendRight(end, ' inline-block')
24+
}
25+
},
26+
}
27+
}

uno.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import type { Theme } from '@unocss/preset-wind4/theme'
99
import { presetRtl } from './uno-preset-rtl'
1010
import { presetA11y } from './uno-preset-a11y'
11+
import { transformerIconInlineBlock } from './uno-transformer-icon-inline-block'
1112

1213
const customIcons = {
1314
'agent-skills':
@@ -23,7 +24,6 @@ export default defineConfig({
2324
presetWind4(),
2425
presetIcons({
2526
extraProperties: {
26-
'display': 'inline-block',
2727
'forced-color-adjust': 'preserve-parent-color',
2828
},
2929
warn: true,
@@ -40,7 +40,7 @@ export default defineConfig({
4040
exclude: [/\.(css|postcss|sass|scss|less|stylus|styl)($|\?)/, /[/\\]node_modules[/\\]/],
4141
},
4242
},
43-
transformers: [transformerDirectives(), transformerVariantGroup()],
43+
transformers: [transformerDirectives(), transformerVariantGroup(), transformerIconInlineBlock()],
4444
theme: {
4545
spacing: { DEFAULT: '4px' },
4646
font: {

0 commit comments

Comments
 (0)