Skip to content

Commit

Permalink
Merge pull request #30 from crswll/support-fontfamily
Browse files Browse the repository at this point in the history
  • Loading branch information
crswll authored Dec 18, 2023
2 parents 4433836 + db4edf0 commit df0479a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tailwindcss-theme-swapper",
"version": "0.8.0",
"version": "0.9.0",
"main": "src/index.js",
"license": "MIT",
"devDependencies": {
Expand Down
14 changes: 13 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ function defaultCustomPropValueTransformer (keys, value) {
}
}

if (keys[0] === 'fontSize' && typeof value !== 'string') {
return value[0]
}

if (Array.isArray(value)) {
return value.join(', ')
}
Expand All @@ -89,6 +93,14 @@ function defaultConfigValueTransformer (keys, value) {
}
}

if (keys[0] === 'fontSize' && typeof value === 'object') {
if (process.env.NODE_ENV !== 'test') {
console.warn(`tailwindcss-theme-swapper: Only using the font size defined at ${keys.join('.')}. Support for this may come if enough people complain about it.`)
}

return `var(--${getTailwindKeyName(keys)}, ${value[0]})`
}

return `var(--${getTailwindKeyName(keys)}, ${value})`
}

Expand Down Expand Up @@ -116,7 +128,7 @@ function resolveThemeConfig (
const keyPath = [ ...previousKeys, key ]
return {
...acc,
[key]: typeof value === "object"
[key]: typeof value === 'object' && !Array.isArray(value)
? resolveThemeConfig(value, keyPath)
: valueTransformer(keyPath, value)
}
Expand Down
12 changes: 10 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ const defaultTheme = {
borderRadius: {
default: '5px',
},
fontFamily: ['a', 'b', '"C 4"']
fontFamily: {
sans: ['Font A', 'Font B', 'Font C']
},
fontSize: {
sm: '12px',
complex: ['22px', { lineHeight: '1.2rem' }]
},
}

const darkTheme = {
Expand Down Expand Up @@ -92,7 +98,9 @@ postcss(tailwindcss({
--colors-primary-darker: 68 0 0;
--spacing-fart: 69px;
--border-radius: 5px;
--font-family: a, b, "C 4"
--font-family-sans: Font A, Font B, Font C;
--font-size-sm: 12px;
--font-size-complex: 22px;
}
@media (prefers-color-scheme: dark) {
Expand Down
18 changes: 16 additions & 2 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,21 @@ describe('getThemeAsCustomProps', () => {
})
})

describe('defaultCustomVarTransformer', () => {
describe('defaultCustomPropValueTransformer', () => {
test('should return a string in rgb for colors', () => {
expect(defaultCustomPropValueTransformer(['colors'], 'rgb(255, 0, 0)')).toEqual('255 0 0')
expect(defaultCustomPropValueTransformer(['backgroundColor'], 'rgb(255, 0, 0)')).toEqual('255 0 0')
expect(defaultCustomPropValueTransformer(['borderColor'], 'rgb(255, 0, 0)')).toEqual('255 0 0')
expect(defaultCustomPropValueTransformer(['textColor'], 'rgb(255, 0, 0)')).toEqual('255 0 0')
})

test('should return a joined string when array', () => {
expect(defaultCustomPropValueTransformer(['fontFamily'], [1, 2, 3])).toEqual('1, 2, 3')
})

test('should just return the value when it is not a color', () => {
expect(defaultCustomPropValueTransformer(['fontSize'], '16px')).toEqual('16px')
expect(defaultCustomPropValueTransformer(['fontSize'], ['16px', '1'])).toEqual('16px')
})
})

Expand All @@ -170,8 +175,17 @@ describe('getThemeAsCustomProps', () => {
expect(defaultConfigValueTransformer(['colors', 'primary'], 'rgb(255, 0, 0)')()).toEqual('rgb(var(--colors-primary))')
})

test('should return a joined string when an array', () => {
expect(defaultConfigValueTransformer(['fontFamily', 'sans'], ['font a', 'font b'])).toEqual('var(--font-family-sans, font a,font b)')
})

test('should just use the font-size when using a more complex value for fontSize', () => {
expect(defaultConfigValueTransformer(['fontSize', 'complex'], ['24px', { lineHeight: '1.2' }])).toEqual('var(--font-size-complex, 24px)')
expect(defaultConfigValueTransformer(['fontSize', 'complex'], ['22px', '1.2'])).toEqual('var(--font-size-complex, 22px)')
})

test('should just return the value when it is not a color', () => {
expect(defaultConfigValueTransformer(['fontSize'], '16px')).toEqual('var(--font-size, 16px)')
expect(defaultConfigValueTransformer(['fontSize', 'base'], '16px')).toEqual('var(--font-size-base, 16px)')
})
})

Expand Down

0 comments on commit df0479a

Please sign in to comment.