From c24c100738399c19cc3ac0d2494fa1982369c4ab Mon Sep 17 00:00:00 2001 From: Adam Wathan <4323180+adamwathan@users.noreply.github.com> Date: Sat, 6 Aug 2022 11:34:39 -0400 Subject: [PATCH] Add support for configuring default font-feature-settings for a font-family --- src/corePlugins.js | 25 +++++++- tests/plugins/fontFamily.test.js | 98 ++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 tests/plugins/fontFamily.test.js 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"; + } + `) + }) +})