Skip to content

Commit

Permalink
Add support for configuring default font-variation-settings for a `…
Browse files Browse the repository at this point in the history
…font-family` (#10515)

* Add support for configuring default `font-variation-settings` for a `font-family`

* update changelog

---------

Co-authored-by: Nikolai Grushkovsky <n@ngrushkovsky.com>
  • Loading branch information
RobinMalfait and ngrushkovsky authored Feb 7, 2023
1 parent 16a002c commit 8bd2846
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 286 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add logical properties support for inline direction ([#10166](https://github.com/tailwindlabs/tailwindcss/pull/10166))
- Add `hyphens` utilities ([#10071](https://github.com/tailwindlabs/tailwindcss/pull/10071))
- [Oxide] Use `lightningcss` for nesting and vendor prefixes in PostCSS plugin ([#10399](https://github.com/tailwindlabs/tailwindcss/pull/10399))
- Add support for configuring default `font-variation-settings` for a `font-family` ([#10034](https://github.com/tailwindlabs/tailwindcss/pull/10034), [#10515](https://github.com/tailwindlabs/tailwindcss/pull/10515))

### Fixed

Expand Down
5 changes: 4 additions & 1 deletion src/corePlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -1915,13 +1915,16 @@ export let corePlugins = {
font: (value) => {
let [families, options = {}] =
Array.isArray(value) && isPlainObject(value[1]) ? value : [value]
let { fontFeatureSettings } = options
let { fontFeatureSettings, fontVariationSettings } = options

return {
'font-family': Array.isArray(families) ? families.join(', ') : families,
...(fontFeatureSettings === undefined
? {}
: { 'font-feature-settings': fontFeatureSettings }),
...(fontVariationSettings === undefined
? {}
: { 'font-variation-settings': fontVariationSettings }),
}
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/css/preflight.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
3. Use a more readable tab size.
4. Use the user's configured `sans` font-family by default.
5. Use the user's configured `sans` font-feature-settings by default.
6. Use the user's configured `sans` font-variation-settings by default.
*/

html {
Expand All @@ -32,6 +33,7 @@ html {
tab-size: 4; /* 3 */
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 */
font-feature-settings: theme('fontFamily.sans[1].fontFeatureSettings', normal); /* 5 */
font-variation-settings: theme('fontFamily.sans[1].fontVariationSettings', normal); /* 6 */
}

/*
Expand Down
66 changes: 66 additions & 0 deletions tests/evaluateTailwindFunctions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,72 @@ crosscheck(({ stable, oxide }) => {
})
})

test('font-family values are retrieved without font-variation-settings', () => {
let input = css`
.heading-1 {
font-family: theme('fontFamily.sans');
}
.heading-2 {
font-family: theme('fontFamily.serif');
}
.heading-3 {
font-family: theme('fontFamily.mono');
}
`

let output = css`
.heading-1 {
font-family: Inter;
}
.heading-2 {
font-family: Times, serif;
}
.heading-3 {
font-family: Menlo, monospace;
}
`

return run(input, {
theme: {
fontFamily: {
sans: ['Inter', { fontVariationSettings: '"opsz" 32' }],
serif: [['Times', 'serif'], { fontVariationSettings: '"opsz" 32' }],
mono: ['Menlo, monospace', { fontVariationSettings: '"opsz" 32' }],
},
},
}).then((result) => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

test('font-variation-settings values can be retrieved', () => {
let input = css`
.heading {
font-family: theme('fontFamily.sans');
font-variation-settings: theme('fontFamily.sans[1].fontVariationSettings');
}
`

let output = css`
.heading {
font-family: Inter;
font-variation-settings: 'opsz' 32;
}
`

return run(input, {
theme: {
fontFamily: {
sans: ['Inter', { fontVariationSettings: "'opsz' 32" }],
},
},
}).then((result) => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})

test('font-family values are joined when an array', () => {
let input = css`
.element {
Expand Down
40 changes: 40 additions & 0 deletions tests/plugins/fontFamily.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,43 @@ crosscheck(() => {
})
})
})

test('font-variation-settings can be provided when families are defined as a string', () => {
let config = {
content: [{ raw: html`<div class="font-sans"></div>` }],
theme: {
fontFamily: {
sans: ['Inter, sans-serif', { fontVariationSettings: '"opsz" 32' }],
},
},
}

return run('@tailwind utilities', config).then((result) => {
expect(result.css).toMatchCss(`
.font-sans {
font-family: Inter, sans-serif;
font-variation-settings: "opsz" 32;
}
`)
})
})

test('font-variation-settings can be provided when families are defined as an array', () => {
let config = {
content: [{ raw: html`<div class="font-sans"></div>` }],
theme: {
fontFamily: {
sans: [['Inter', 'sans-serif'], { fontVariationSettings: '"opsz" 32' }],
},
},
}

return run('@tailwind utilities', config).then((result) => {
expect(result.css).toMatchCss(`
.font-sans {
font-family: Inter, sans-serif;
font-variation-settings: "opsz" 32;
}
`)
})
})
Loading

0 comments on commit 8bd2846

Please sign in to comment.