Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tailwind): Add font-size aliases for text utilities #5

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions formats/tailwind/get-font-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ module.exports.getFontSize = (dictionary) => {
const secondaryFontSizes = getSecondaryFontSizesTokens(dictionary);
const textFontSizes = getTextFontSizesTokens(dictionary);

return [...primaryFontSizes, ...secondaryFontSizes, ...textFontSizes].reduce(
(acc, token) => ({
...acc,
[token.name.replace('typography-', '')]: [
token.value.fontSize,
{ lineHeight: token.value.lineHeight, fontWeight: token.value.fontWeight },
],
}),
{},
);
return [...primaryFontSizes, ...secondaryFontSizes, ...textFontSizes].reduce((acc, token) => {
let tokenKey = token.name.replace('typography-', '');

// Only remove the `text-` prefix for Tailwind built-ins
// Note: In the future, we should consider replacing `text-` prefixes for other tokens as well to avoid redundancy.
if (['text-sm', 'text-base', 'text-lg', 'text-xl'].some((utility) => token.name.includes(utility))) {
tokenKey = tokenKey.replace('text-', '');
}

acc[tokenKey] = [token.value.fontSize, { lineHeight: token.value.lineHeight, fontWeight: token.value.fontWeight }];

return acc;
}, {});
};
14 changes: 14 additions & 0 deletions tests/tailwind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ describe('Tailwind Config specs', () => {
expect(tailwindConfig.theme.fontFamily.primary).toHaveLength(3);
expect(tailwindConfig.theme.fontFamily.secondary).toHaveLength(3);
});

it('Font-size aliases are properly generated for text-* utilities', () => {
expect(tailwindConfig.theme.fontSize.sm).toEqual(tailwindConfig.theme.fontSize['text-4']);
expect(tailwindConfig.theme.fontSize.base).toEqual(tailwindConfig.theme.fontSize['text-3']);
expect(tailwindConfig.theme.fontSize.lg).toEqual(tailwindConfig.theme.fontSize['text-2']);
expect(tailwindConfig.theme.fontSize.xl).toEqual(tailwindConfig.theme.fontSize['text-1']);
});

it('Font-size <number> utilities are still available for backward compatibility', () => {
expect(tailwindConfig.theme.fontSize['text-1'][0]).toMatch(/rem$/);
expect(tailwindConfig.theme.fontSize['text-2'][0]).toMatch(/rem$/);
expect(tailwindConfig.theme.fontSize['text-3'][0]).toMatch(/rem$/);
expect(tailwindConfig.theme.fontSize['text-4'][0]).toMatch(/rem$/);
});
});
36 changes: 36 additions & 0 deletions tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,42 @@
"breakpoints-base-xs": {
"value": 0
},
"typography-text-xl": {
"value": {
"fontFamily": "Source Sans Pro, Arial, sans-serif",
"fontWeight": 400,
"fontSize": "20px",
"lineHeight": 1.5
},
"type": "typography"
},
"typography-text-lg": {
"value": {
"fontFamily": "Source Sans Pro, Arial, sans-serif",
"fontWeight": 400,
"fontSize": "18px",
"lineHeight": 1.5
},
"type": "typography"
},
"typography-text-sm": {
"value": {
"fontFamily": "Source Sans Pro, Arial, sans-serif",
"fontWeight": 400,
"fontSize": "14px",
"lineHeight": 1.214
},
"type": "typography"
},
"typography-text-base": {
"value": {
"fontFamily": "Source Sans Pro, Arial, sans-serif",
"fontWeight": 400,
"fontSize": "16px",
"lineHeight": 1.5
},
"type": "typography"
},
"typography-text-4": {
"value": {
"fontFamily": "Source Sans Pro, Arial, sans-serif",
Expand Down
36 changes: 36 additions & 0 deletions tokens/globals/typography/text.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,42 @@
"lineHeight": "{typography.line-height.8}"
},
"type": "typography"
},
"base": {
"value": {
"fontFamily": "{typography.font-family.primary}, {typography.font-family.primary-fallback-1}, {typography.font-family.primary-fallback-2}",
"fontWeight": 400,
"fontSize": "{typography.font-size.2}",
"lineHeight": "{typography.line-height.10}"
},
"type": "typography"
},
"sm": {
"value": {
"fontFamily": "{typography.font-family.primary}, {typography.font-family.primary-fallback-1}, {typography.font-family.primary-fallback-2}",
"fontWeight": 400,
"fontSize": "{typography.font-size.1}",
"lineHeight": "{typography.line-height.8}"
},
"type": "typography"
},
"lg": {
"value": {
"fontFamily": "{typography.font-family.primary}, {typography.font-family.primary-fallback-1}, {typography.font-family.primary-fallback-2}",
"fontWeight": 400,
"fontSize": "{typography.font-size.3}",
"lineHeight": "{typography.line-height.10}"
},
"type": "typography"
},
"xl": {
"value": {
"fontFamily": "{typography.font-family.primary}, {typography.font-family.primary-fallback-1}, {typography.font-family.primary-fallback-2}",
"fontWeight": 400,
"fontSize": "{typography.font-size.4}",
"lineHeight": "{typography.line-height.10}"
},
"type": "typography"
}
}
}
Expand Down
Loading