Skip to content

Commit

Permalink
Add support for configuring default font-feature-settings for a font-…
Browse files Browse the repository at this point in the history
…family
  • Loading branch information
adamwathan committed Aug 6, 2022
1 parent 99b53b4 commit 7b06509
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/corePlugins.js
Expand Up @@ -1604,9 +1604,32 @@ export let corePlugins = {
matchUtilities({ align: (value) => ({ 'vertical-align': value }) })
},

fontFamily: createUtilityPlugin('fontFamily', [['font', ['fontFamily']]], {
type: ['lookup', 'generic-name', 'family-name'],
}),
// 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(
Expand Down
98 changes: 98 additions & 0 deletions 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`<div class="font-sans"></div>` }],
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`<div class="font-sans"></div>` }],
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`<div class="font-sans"></div>` }],
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`<div class="font-sans"></div>` }],
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`<div class="font-sans"></div>` }],
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";
}
`)
})
})

0 comments on commit 7b06509

Please sign in to comment.