Skip to content

Commit ebe38df

Browse files
authored
Fix: Use proper CSS var for default lineHeight (#153)
Fixes #50
1 parent 8036fd8 commit ebe38df

File tree

5 files changed

+94
-24
lines changed

5 files changed

+94
-24
lines changed

.changeset/dirty-geckos-repeat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tailwindcss-capsize': patch
3+
---
4+
5+
Use correct custom property with default lineHeight values

package-lock.json

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

src/__tests__/plugin.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,73 @@ describe('Plugin', () => {
348348
`)
349349
})
350350
})
351+
352+
it('works with default line-height values', async () => {
353+
return await postcss(
354+
tailwindcss({
355+
theme: {
356+
...THEME_CONFIG,
357+
screens: {},
358+
fontSize: {
359+
base: ['1rem', '1.5rem'],
360+
},
361+
lineHeight: {},
362+
},
363+
corePlugins: false,
364+
plugins: [capsizePlugin],
365+
}),
366+
)
367+
.process('@tailwind utilities', {
368+
from: undefined,
369+
})
370+
.then((result) => {
371+
expect(result.css).toMatchCss(`
372+
.font-sans {
373+
--ascent-scale: 0.9688;
374+
--descent-scale: 0.2415;
375+
--cap-height-scale: 0.7273;
376+
--line-gap-scale: 0;
377+
--line-height-scale: 1.2102;
378+
font-family: Inter, sans-serif;
379+
}
380+
381+
.text-base {
382+
--font-size-px: 16;
383+
font-size: 1rem;
384+
--line-height-offset: calc(
385+
(((var(--line-height-scale) * var(--font-size-px)) - 24) / 2) /
386+
var(--font-size-px)
387+
);
388+
line-height: 1.5rem;
389+
}
390+
391+
.capsize::before {
392+
display: table;
393+
content: "";
394+
margin-bottom: calc(
395+
(
396+
(
397+
var(--ascent-scale) - var(--cap-height-scale) +
398+
var(--line-gap-scale) / 2
399+
) - var(--line-height-offset)
400+
) * -1em
401+
);
402+
}
403+
404+
.capsize::after {
405+
display: table;
406+
content: "";
407+
margin-top: calc(
408+
(
409+
(var(--descent-scale) + var(--line-gap-scale) / 2) -
410+
var(--line-height-offset)
411+
) * -1em
412+
);
413+
}
414+
`)
415+
})
416+
})
417+
351418
it('generates utility classes with a custom activation class', async () => {
352419
return await postcss(
353420
tailwindcss({

src/plugin.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import type { NestedObject } from '@navith/tailwindcss-plugin-author-types'
44
import plugin from 'tailwindcss/plugin'
55

66
import {
7-
getRelativeValue,
87
isPlainObject,
9-
isRelativeValue,
8+
lineHeightProperties,
109
makeCssSelectors,
1110
normalizeThemeValue,
1211
normalizeValue,
@@ -183,16 +182,7 @@ export default plugin.withOptions<Partial<PluginOptions>>(
183182
return {
184183
'--font-size-px': String(fontSizeActual),
185184
'font-size': fontSize,
186-
...(lineHeight === undefined
187-
? {}
188-
: {
189-
'--line-height-px': normalizeValue(
190-
lineHeight,
191-
rootSize,
192-
fontSizeActual,
193-
),
194-
'line-height': lineHeight,
195-
}),
185+
...lineHeightProperties(lineHeight, rootSize),
196186
...(letterSpacing === undefined
197187
? {}
198188
: { 'letter-spacing': letterSpacing }),
@@ -222,16 +212,7 @@ export default plugin.withOptions<Partial<PluginOptions>>(
222212
value,
223213
) as string
224214

225-
let lineHeightActual = isRelativeValue(lineHeight)
226-
? `calc(${getRelativeValue(
227-
lineHeight,
228-
)} * var(--font-size-px))`
229-
: normalizeValue(lineHeight, rootSize)
230-
231-
return {
232-
'--line-height-offset': `calc((((var(--line-height-scale) * var(--font-size-px)) - ${lineHeightActual}) / 2) / var(--font-size-px))`,
233-
'line-height': lineHeight,
234-
}
215+
return lineHeightProperties(lineHeight, rootSize)
235216
},
236217
},
237218
{

src/utils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,20 @@ export function normalizeThemeValue(key: string, value: ThemeValue) {
9999
export function round(value: number) {
100100
return parseFloat(value.toFixed(4)).toString()
101101
}
102+
103+
/**
104+
* Takes a theme value for lineHeight and returns the `line-height` property,
105+
* as well as a `--line-height-offset` custom property.
106+
*/
107+
export function lineHeightProperties(lineHeight: string, rootSize: number) {
108+
if (lineHeight === undefined) return {}
109+
110+
let lineHeightActual = isRelativeValue(lineHeight)
111+
? `calc(${getRelativeValue(lineHeight)} * var(--font-size-px))`
112+
: normalizeValue(lineHeight, rootSize)
113+
114+
return {
115+
'--line-height-offset': `calc((((var(--line-height-scale) * var(--font-size-px)) - ${lineHeightActual}) / 2) / var(--font-size-px))`,
116+
'line-height': lineHeight,
117+
}
118+
}

0 commit comments

Comments
 (0)