Skip to content

Commit 43bb0f5

Browse files
ngrushkovskyRobinMalfait
authored andcommitted
Add support for configuring default font-variation-settings for a font-family
1 parent 16a002c commit 43bb0f5

6 files changed

+404
-286
lines changed

src/corePlugins.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1915,13 +1915,16 @@ export let corePlugins = {
19151915
font: (value) => {
19161916
let [families, options = {}] =
19171917
Array.isArray(value) && isPlainObject(value[1]) ? value : [value]
1918-
let { fontFeatureSettings } = options
1918+
let { fontFeatureSettings, fontVariationSettings } = options
19191919

19201920
return {
19211921
'font-family': Array.isArray(families) ? families.join(', ') : families,
19221922
...(fontFeatureSettings === undefined
19231923
? {}
19241924
: { 'font-feature-settings': fontFeatureSettings }),
1925+
...(fontVariationSettings === undefined
1926+
? {}
1927+
: { 'font-variation-settings': fontVariationSettings }),
19251928
}
19261929
},
19271930
},

src/css/preflight.css

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
3. Use a more readable tab size.
2424
4. Use the user's configured `sans` font-family by default.
2525
5. Use the user's configured `sans` font-feature-settings by default.
26+
6. Use the user's configured `sans` font-variation-settings by default.
2627
*/
2728

2829
html {
@@ -32,6 +33,7 @@ html {
3233
tab-size: 4; /* 3 */
3334
font-family: theme('fontFamily.sans', ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"); /* 4 */
3435
font-feature-settings: theme('fontFamily.sans[1].fontFeatureSettings', normal); /* 5 */
36+
font-variation-settings: theme('fontFamily.sans[1].fontVariationSettings', normal); /* 6 */
3537
}
3638

3739
/*

tests/evaluateTailwindFunctions.test.js

+66
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,72 @@ crosscheck(({ stable, oxide }) => {
591591
})
592592
})
593593

594+
test('font-family values are retrieved without font-variation-settings', () => {
595+
let input = css`
596+
.heading-1 {
597+
font-family: theme('fontFamily.sans');
598+
}
599+
.heading-2 {
600+
font-family: theme('fontFamily.serif');
601+
}
602+
.heading-3 {
603+
font-family: theme('fontFamily.mono');
604+
}
605+
`
606+
607+
let output = css`
608+
.heading-1 {
609+
font-family: Inter;
610+
}
611+
.heading-2 {
612+
font-family: Times, serif;
613+
}
614+
.heading-3 {
615+
font-family: Menlo, monospace;
616+
}
617+
`
618+
619+
return run(input, {
620+
theme: {
621+
fontFamily: {
622+
sans: ['Inter', { fontVariationSettings: '"opsz" 32' }],
623+
serif: [['Times', 'serif'], { fontVariationSettings: '"opsz" 32' }],
624+
mono: ['Menlo, monospace', { fontVariationSettings: '"opsz" 32' }],
625+
},
626+
},
627+
}).then((result) => {
628+
expect(result.css).toMatchCss(output)
629+
expect(result.warnings().length).toBe(0)
630+
})
631+
})
632+
633+
test('font-variation-settings values can be retrieved', () => {
634+
let input = css`
635+
.heading {
636+
font-family: theme('fontFamily.sans');
637+
font-variation-settings: theme('fontFamily.sans[1].fontVariationSettings');
638+
}
639+
`
640+
641+
let output = css`
642+
.heading {
643+
font-family: Inter;
644+
font-variation-settings: 'opsz' 32;
645+
}
646+
`
647+
648+
return run(input, {
649+
theme: {
650+
fontFamily: {
651+
sans: ['Inter', { fontVariationSettings: "'opsz' 32" }],
652+
},
653+
},
654+
}).then((result) => {
655+
expect(result.css).toMatchCss(output)
656+
expect(result.warnings().length).toBe(0)
657+
})
658+
})
659+
594660
test('font-family values are joined when an array', () => {
595661
let input = css`
596662
.element {

tests/plugins/fontFamily.test.js

+40
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,43 @@ crosscheck(() => {
9898
})
9999
})
100100
})
101+
102+
test('font-variation-settings can be provided when families are defined as a string', () => {
103+
let config = {
104+
content: [{ raw: html`<div class="font-sans"></div>` }],
105+
theme: {
106+
fontFamily: {
107+
sans: ['Inter, sans-serif', { fontVariationSettings: '"opsz" 32' }],
108+
},
109+
},
110+
}
111+
112+
return run('@tailwind utilities', config).then((result) => {
113+
expect(result.css).toMatchCss(`
114+
.font-sans {
115+
font-family: Inter, sans-serif;
116+
font-variation-settings: "opsz" 32;
117+
}
118+
`)
119+
})
120+
})
121+
122+
test('font-variation-settings can be provided when families are defined as an array', () => {
123+
let config = {
124+
content: [{ raw: html`<div class="font-sans"></div>` }],
125+
theme: {
126+
fontFamily: {
127+
sans: [['Inter', 'sans-serif'], { fontVariationSettings: '"opsz" 32' }],
128+
},
129+
},
130+
}
131+
132+
return run('@tailwind utilities', config).then((result) => {
133+
expect(result.css).toMatchCss(`
134+
.font-sans {
135+
font-family: Inter, sans-serif;
136+
font-variation-settings: "opsz" 32;
137+
}
138+
`)
139+
})
140+
})

0 commit comments

Comments
 (0)