Skip to content

Commit

Permalink
fix: fontSize second parameters object
Browse files Browse the repository at this point in the history
  • Loading branch information
Se7en-IT committed Nov 22, 2023
1 parent b7b8980 commit 4ad6387
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16.x'
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
100 changes: 55 additions & 45 deletions index.js
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)
})
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tailwindcss-typography-shorthand",
"version": "1.1.1",
"version": "1.1.2",
"description": "Tailwindcss Typography Shorthand",
"main": "index.js",
"repository": {
Expand All @@ -13,4 +13,4 @@
"url": "https://github.com/Se7en-IT/tailwindcss-typography-shorthand/issues"
},
"homepage": "https://github.com/Se7en-IT/tailwindcss-typography-shorthand#readme"
}
}

0 comments on commit 4ad6387

Please sign in to comment.