Skip to content

Commit 2df7b07

Browse files
zyyvantfu
andauthored
feat(preset-mini): support fontSize with letter-space (#3149)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 08ebc33 commit 2df7b07

File tree

7 files changed

+55
-11
lines changed

7 files changed

+55
-11
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"semver": "^7.5.4",
110110
"simple-git-hooks": "^2.9.0",
111111
"splitpanes": "^3.1.5",
112+
"std-env": "^3.4.3",
112113
"taze": "^0.11.4",
113114
"terser": "^5.22.0",
114115
"tsup": "^7.2.0",

packages/preset-mini/build.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineBuildConfig } from 'unbuild'
2+
import { isWindows } from 'std-env'
23

34
export default defineBuildConfig({
45
entries: [
@@ -13,5 +14,9 @@ export default defineBuildConfig({
1314
declaration: true,
1415
rollup: {
1516
emitCJS: true,
17+
dts: {
18+
respectExternal: false,
19+
},
1620
},
21+
failOnWarn: !isWindows,
1722
})

packages/preset-mini/src/_rules/typography.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { Rule } from '@unocss/core'
1+
import type { CSSObject, Rule } from '@unocss/core'
22
import { toArray } from '@unocss/core'
33
import type { Theme } from '../theme'
44
import { colorResolver, colorableShadows, h, splitShorthand } from '../utils'
55

6-
function handleLineHeight(s: string, theme: Theme) {
7-
return theme.lineHeight?.[s] || h.bracket.cssvar.global.rem(s)
6+
function handleThemeByKey(s: string, theme: Theme, key: 'lineHeight' | 'letterSpacing') {
7+
return theme[key]?.[s] || h.bracket.cssvar.global.rem(s)
88
}
99

1010
export const fonts: Rule<Theme>[] = [
@@ -13,14 +13,21 @@ export const fonts: Rule<Theme>[] = [
1313
/^text-(.+)$/,
1414
([, s = 'base'], { theme }) => {
1515
const [size, leading] = splitShorthand(s, 'length')
16-
const sizePairs = toArray(theme.fontSize?.[size])
17-
const lineHeight = leading ? handleLineHeight(leading, theme) : undefined
16+
const sizePairs = toArray(theme.fontSize?.[size]) as [string, string | CSSObject, string?]
17+
const lineHeight = leading ? handleThemeByKey(leading, theme, 'lineHeight') : undefined
1818

1919
if (sizePairs?.[0]) {
20-
const [fontSize, height] = sizePairs
20+
const [fontSize, height, letterSpacing] = sizePairs
21+
if (typeof height === 'object') {
22+
return {
23+
'font-size': fontSize,
24+
...height,
25+
}
26+
}
2127
return {
2228
'font-size': fontSize,
2329
'line-height': lineHeight ?? height ?? '1',
30+
'letter-spacing': letterSpacing ? handleThemeByKey(letterSpacing, theme, 'letterSpacing') : undefined,
2431
}
2532
}
2633

@@ -37,7 +44,7 @@ export const fonts: Rule<Theme>[] = [
3744
{ autocomplete: 'text-$fontSize' },
3845
],
3946
[/^(?:text|font)-size-(.+)$/, ([, s], { theme }) => {
40-
const themed = toArray(theme.fontSize?.[s])
47+
const themed = toArray(theme.fontSize?.[s]) as [string, string | CSSObject]
4148
const size = themed?.[0] ?? h.bracket.cssvar.global.rem(s)
4249
if (size != null)
4350
return { 'font-size': size }
@@ -58,7 +65,7 @@ export const fonts: Rule<Theme>[] = [
5865
// leadings
5966
[
6067
/^(?:font-)?(?:leading|lh|line-height)-(.+)$/,
61-
([, s], { theme }) => ({ 'line-height': handleLineHeight(s, theme) }),
68+
([, s], { theme }) => ({ 'line-height': handleThemeByKey(s, theme, 'lineHeight') }),
6269
{ autocomplete: '(leading|lh|line-height)-$lineHeight' },
6370
],
6471

packages/preset-mini/src/_theme/font.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const fontSize: Theme['fontSize'] = {
5151
'7xl': ['4.5rem', '1'],
5252
'8xl': ['6rem', '1'],
5353
'9xl': ['8rem', '1'],
54-
}satisfies Theme['fontSize']
54+
} satisfies Theme['fontSize']
5555

5656
export const textIndent: Theme['textIndent'] = {
5757
'DEFAULT': '1.5rem',

packages/preset-mini/src/_theme/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Arrayable } from '@unocss/core'
1+
import type { Arrayable, CSSObject } from '@unocss/core'
22

33
export interface ThemeAnimation {
44
keyframes?: Record<string, string>
@@ -30,7 +30,7 @@ export interface Theme {
3030
verticalBreakpoints?: Record<string, string>
3131
colors?: Colors
3232
fontFamily?: Record<string, string>
33-
fontSize?: Record<string, string | [string, string]>
33+
fontSize?: Record<string, string | [string, string | CSSObject] | [string, string, string]>
3434
fontWeight?: Record<string, string>
3535
lineHeight?: Record<string, string>
3636
letterSpacing?: Record<string, string>

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/preset-mini.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,32 @@ describe('preset-mini', () => {
325325
.z-header{z-index:500;}"
326326
`)
327327
})
328+
329+
it('theme font-size with letter-space', async () => {
330+
const uno = createGenerator({
331+
presets: [
332+
presetMini(),
333+
],
334+
theme: {
335+
fontSize: {
336+
normal: '24px',
337+
ls: ['8rem', '1', '2.25rem'],
338+
obj: ['8rem', {
339+
'line-height': '2.25rem',
340+
'letter-spacing': '-0.02em',
341+
'font-weight': '700',
342+
}],
343+
},
344+
},
345+
})
346+
347+
expect((await uno.generate('text-sm text-normal text-ls text-obj', { preflights: false })).css)
348+
.toMatchInlineSnapshot(`
349+
"/* layer: default */
350+
.text-ls{font-size:8rem;line-height:1;letter-spacing:2.25rem;}
351+
.text-normal{font-size:24px;line-height:1;}
352+
.text-obj{font-size:8rem;line-height:2.25rem;letter-spacing:-0.02em;font-weight:700;}
353+
.text-sm{font-size:0.875rem;line-height:1.25rem;}"
354+
`)
355+
})
328356
})

0 commit comments

Comments
 (0)