Merge pull request #2144 from tailwindlabs/fix-font-size-theme-bug

Fix font size theme bug
This commit is contained in:
Adam Wathan 2020-08-07 14:40:01 -04:00 committed by GitHub
commit 037416ac70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 2 deletions

View File

@ -109,3 +109,48 @@ test('an unquoted list is valid as a default value', () => {
expect(result.warnings().length).toBe(0)
})
})
test('array values are joined by default', () => {
const input = `
.heading { font-family: theme('fontFamily.sans'); }
`
const output = `
.heading { font-family: Inter, Helvetica, sans-serif; }
`
return run(input, {
theme: {
fontFamily: {
sans: ['Inter', 'Helvetica', 'sans-serif'],
},
},
}).then(result => {
expect(result.css).toEqual(output)
expect(result.warnings().length).toBe(0)
})
})
test('font sizes are retrieved without default line-heights or letter-spacing', () => {
const input = `
.heading-1 { font-size: theme('fontSize.lg'); }
.heading-2 { font-size: theme('fontSize.xl'); }
`
const output = `
.heading-1 { font-size: 20px; }
.heading-2 { font-size: 24px; }
`
return run(input, {
theme: {
fontSize: {
lg: ['20px', '28px'],
xl: ['24px', { lineHeight: '32px', letterSpacing: '-0.01em' }],
},
},
}).then(result => {
expect(result.css).toEqual(output)
expect(result.warnings().length).toBe(0)
})
})

View File

@ -1,12 +1,25 @@
import _ from 'lodash'
import functions from 'postcss-functions'
const themeTransforms = {
fontSize(value) {
return Array.isArray(value) ? value[0] : value
},
}
function defaultTransform(value) {
return Array.isArray(value) ? value.join(', ') : value
}
export default function(config) {
return functions({
functions: {
theme: (path, ...defaultValue) => {
return _.thru(_.get(config.theme, _.trim(path, `'"`), defaultValue), value => {
return Array.isArray(value) ? value.join(', ') : value
const trimmedPath = _.trim(path, `'"`)
return _.thru(_.get(config.theme, trimmedPath, defaultValue), value => {
const [themeSection] = trimmedPath.split('.')
return _.get(themeTransforms, themeSection, defaultTransform)(value)
})
},
},