From c7f627dd68e32c716e0262c6a9fcf08941a10428 Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Mon, 8 Aug 2022 15:02:08 -0400 Subject: [PATCH] Add support for configuring default font-feature-settings for a font-family (#9039) * Add support for configuring default font-feature-settings for a font-family * Update changelog Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com> --- CHANGELOG.md | 4 ++ src/corePlugins.js | 25 +++++++- tests/plugins/fontFamily.test.js | 98 ++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 tests/plugins/fontFamily.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f8fbbacfe69..6e40bf5beac2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add support fo configuring default `font-feature-settings` for a font family ([#9039](https://github.com/tailwindlabs/tailwindcss/pull/9039)) + ### Fixed - Use absolute paths when resolving changed files for resilience against working directory changes ([#9032](https://github.com/tailwindlabs/tailwindcss/pull/9032)) diff --git a/src/corePlugins.js b/src/corePlugins.js index 62f9d24aa8f0..1a19be3f9d8c 100644 --- a/src/corePlugins.js +++ b/src/corePlugins.js @@ -1604,9 +1604,28 @@ export let corePlugins = { matchUtilities({ align: (value) => ({ 'vertical-align': value }) }) }, - fontFamily: createUtilityPlugin('fontFamily', [['font', ['fontFamily']]], { - type: ['lookup', 'generic-name', 'family-name'], - }), + fontFamily: ({ matchUtilities, theme }) => { + matchUtilities( + { + font: (value) => { + let [families, options = {}] = + Array.isArray(value) && isPlainObject(value[1]) ? value : [value] + let { fontFeatureSettings } = options + + return { + 'font-family': Array.isArray(families) ? families.join(', ') : families, + ...(fontFeatureSettings === undefined + ? {} + : { 'font-feature-settings': fontFeatureSettings }), + } + }, + }, + { + values: theme('fontFamily'), + type: ['lookup', 'generic-name', 'family-name'], + } + ) + }, fontSize: ({ matchUtilities, theme }) => { matchUtilities( diff --git a/tests/plugins/fontFamily.test.js b/tests/plugins/fontFamily.test.js new file mode 100644 index 000000000000..961689cf111a --- /dev/null +++ b/tests/plugins/fontFamily.test.js @@ -0,0 +1,98 @@ +import { run, html, css } from '../util/run' + +test('font-family utilities can be defined as a string', () => { + let config = { + content: [{ raw: html`
` }], + theme: { + fontFamily: { + sans: 'Helvetica, Arial, sans-serif', + }, + }, + } + + return run('@tailwind utilities', config).then((result) => { + expect(result.css).toMatchCss(css` + .font-sans { + font-family: Helvetica, Arial, sans-serif; + } + `) + }) +}) + +test('font-family utilities can be defined as an array', () => { + let config = { + content: [{ raw: html`
` }], + theme: { + fontFamily: { + sans: ['Helvetica', 'Arial', 'sans-serif'], + }, + }, + } + + return run('@tailwind utilities', config).then((result) => { + expect(result.css).toMatchCss(css` + .font-sans { + font-family: Helvetica, Arial, sans-serif; + } + `) + }) +}) + +test('font-family values are not automatically escaped', () => { + let config = { + content: [{ raw: html`
` }], + theme: { + fontFamily: { + sans: ["'Exo 2'", 'sans-serif'], + }, + }, + } + + return run('@tailwind utilities', config).then((result) => { + expect(result.css).toMatchCss(css` + .font-sans { + font-family: 'Exo 2', sans-serif; + } + `) + }) +}) + +test('font-feature-settings can be provided when families are defined as a string', () => { + let config = { + content: [{ raw: html`
` }], + theme: { + fontFamily: { + sans: ['Helvetica, Arial, sans-serif', { fontFeatureSettings: '"cv11", "ss01"' }], + }, + }, + } + + return run('@tailwind utilities', config).then((result) => { + expect(result.css).toMatchCss(` + .font-sans { + font-family: Helvetica, Arial, sans-serif; + font-feature-settings: "cv11", "ss01"; + } + `) + }) +}) + +test('font-feature-settings can be provided when families are defined as an array', () => { + let config = { + content: [{ raw: html`
` }], + theme: { + fontFamily: { + sans: [['Helvetica', 'Arial', 'sans-serif'], { fontFeatureSettings: '"cv11", "ss01"' }], + }, + }, + } + + return run('@tailwind utilities', config).then((result) => { + expect(result.css).toMatchCss(` + .font-sans { + font-family: Helvetica, Arial, sans-serif; + font-feature-settings: "cv11", "ss01"; + } + `) + }) +})