Skip to content

Commit 037416a

Browse files
authored
Merge pull request #2144 from tailwindlabs/fix-font-size-theme-bug
Fix font size theme bug
2 parents cb4ff81 + a84deb4 commit 037416a

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

__tests__/themeFunction.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,48 @@ test('an unquoted list is valid as a default value', () => {
109109
expect(result.warnings().length).toBe(0)
110110
})
111111
})
112+
113+
test('array values are joined by default', () => {
114+
const input = `
115+
.heading { font-family: theme('fontFamily.sans'); }
116+
`
117+
118+
const output = `
119+
.heading { font-family: Inter, Helvetica, sans-serif; }
120+
`
121+
122+
return run(input, {
123+
theme: {
124+
fontFamily: {
125+
sans: ['Inter', 'Helvetica', 'sans-serif'],
126+
},
127+
},
128+
}).then(result => {
129+
expect(result.css).toEqual(output)
130+
expect(result.warnings().length).toBe(0)
131+
})
132+
})
133+
134+
test('font sizes are retrieved without default line-heights or letter-spacing', () => {
135+
const input = `
136+
.heading-1 { font-size: theme('fontSize.lg'); }
137+
.heading-2 { font-size: theme('fontSize.xl'); }
138+
`
139+
140+
const output = `
141+
.heading-1 { font-size: 20px; }
142+
.heading-2 { font-size: 24px; }
143+
`
144+
145+
return run(input, {
146+
theme: {
147+
fontSize: {
148+
lg: ['20px', '28px'],
149+
xl: ['24px', { lineHeight: '32px', letterSpacing: '-0.01em' }],
150+
},
151+
},
152+
}).then(result => {
153+
expect(result.css).toEqual(output)
154+
expect(result.warnings().length).toBe(0)
155+
})
156+
})

src/lib/evaluateTailwindFunctions.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
import _ from 'lodash'
22
import functions from 'postcss-functions'
33

4+
const themeTransforms = {
5+
fontSize(value) {
6+
return Array.isArray(value) ? value[0] : value
7+
},
8+
}
9+
10+
function defaultTransform(value) {
11+
return Array.isArray(value) ? value.join(', ') : value
12+
}
13+
414
export default function(config) {
515
return functions({
616
functions: {
717
theme: (path, ...defaultValue) => {
8-
return _.thru(_.get(config.theme, _.trim(path, `'"`), defaultValue), value => {
9-
return Array.isArray(value) ? value.join(', ') : value
18+
const trimmedPath = _.trim(path, `'"`)
19+
return _.thru(_.get(config.theme, trimmedPath, defaultValue), value => {
20+
const [themeSection] = trimmedPath.split('.')
21+
22+
return _.get(themeTransforms, themeSection, defaultTransform)(value)
1023
})
1124
},
1225
},

0 commit comments

Comments
 (0)