-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fontSize second parameters object
- Loading branch information
Showing
3 changed files
with
59 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,60 @@ | ||
const plugin = require('tailwindcss/plugin') | ||
|
||
function addFamilies(fontFamilies, e, basePrefix, baseStyle){ | ||
const memo = {} | ||
memo[`.${e(basePrefix)}`] = baseStyle | ||
fontFamilies.forEach(([familyKey, familyValue]) => { | ||
memo[`.${e(`${basePrefix}-${familyKey}`)}`] = { | ||
...baseStyle, | ||
fontFamily: Array.isArray(familyValue) ? familyValue.join(', ') : familyValue | ||
} | ||
}) | ||
return memo | ||
function addFamilies(fontFamilies, e, basePrefix, baseStyle) { | ||
const memo = {} | ||
memo[`.${e(basePrefix)}`] = baseStyle | ||
fontFamilies.forEach(([familyKey, familyValue]) => { | ||
memo[`.${e(`${basePrefix}-${familyKey}`)}`] = { | ||
...baseStyle, | ||
fontFamily: Array.isArray(familyValue) | ||
? familyValue.join(', ') | ||
: familyValue, | ||
} | ||
}) | ||
return memo | ||
} | ||
|
||
module.exports = plugin(function ({ addComponents, e, config }) { | ||
const prefix = "typog" | ||
const fontSizes = Object.entries(config('theme.fontSize')) | ||
const fontWeights = Object.entries(config('theme.fontWeight')) | ||
const fontFamilies = Object.entries(config('theme.fontFamily')) | ||
const letterSpacing = Object.entries(config('theme.letterSpacing')) | ||
const rules = fontSizes.reduce((memo, [sizeKey, sizeValue]) => { | ||
fontWeights.forEach(([weightKey, weightValue]) => { | ||
const basePrefix = `${prefix}-${sizeKey}-${weightKey}`; | ||
const baseStyle = { | ||
fontSize: Array.isArray(sizeValue) ? sizeValue[0] : sizeValue, | ||
fontWeight: weightValue | ||
} | ||
if (Array.isArray(sizeValue)) { | ||
baseStyle.lineHeight = sizeValue[1] | ||
} | ||
memo = { | ||
...memo, | ||
...addFamilies(fontFamilies, e, basePrefix, baseStyle) | ||
} | ||
letterSpacing.forEach(([letterSpacingKey, letterSpacingValue]) => { | ||
const innerBasePrefix = `${basePrefix}-${letterSpacingKey}`; | ||
const innerBaseStyle = { | ||
...baseStyle, | ||
letterSpacing: letterSpacingValue | ||
} | ||
memo = { | ||
...memo, | ||
...addFamilies(fontFamilies, e, innerBasePrefix, innerBaseStyle) | ||
} | ||
}) | ||
}) | ||
return memo | ||
}, {}) | ||
addComponents(rules) | ||
}) | ||
const prefix = 'typog' | ||
const fontSizes = Object.entries(config('theme.fontSize')) | ||
const fontWeights = Object.entries(config('theme.fontWeight')) | ||
const fontFamilies = Object.entries(config('theme.fontFamily')) | ||
const letterSpacing = Object.entries(config('theme.letterSpacing')) | ||
const rules = fontSizes.reduce((memo, [sizeKey, sizeValue]) => { | ||
fontWeights.forEach(([weightKey, weightValue]) => { | ||
const basePrefix = `${prefix}-${sizeKey}-${weightKey}` | ||
let baseStyle = { | ||
fontSize: Array.isArray(sizeValue) ? sizeValue[0] : sizeValue, | ||
fontWeight: weightValue, | ||
} | ||
if (Array.isArray(sizeValue)) { | ||
if (typeof sizeValue[1] !== 'object') { | ||
sizeValue[1] = { | ||
lineHeight: sizeValue[1], | ||
} | ||
} | ||
baseStyle = { | ||
...baseStyle, | ||
...sizeValue[1], | ||
} | ||
} | ||
memo = { | ||
...memo, | ||
...addFamilies(fontFamilies, e, basePrefix, baseStyle), | ||
} | ||
letterSpacing.forEach(([letterSpacingKey, letterSpacingValue]) => { | ||
const innerBasePrefix = `${basePrefix}-${letterSpacingKey}` | ||
const innerBaseStyle = { | ||
...baseStyle, | ||
letterSpacing: letterSpacingValue, | ||
} | ||
memo = { | ||
...memo, | ||
...addFamilies(fontFamilies, e, innerBasePrefix, innerBaseStyle), | ||
} | ||
}) | ||
}) | ||
return memo | ||
}, {}) | ||
addComponents(rules) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters