Skip to content
This repository was archived by the owner on Feb 22, 2022. It is now read-only.

Commit e9b50d4

Browse files
committed
Add support for [fontSize, lineHeight] tuples (fix #19)
1 parent 15d3414 commit e9b50d4

File tree

3 files changed

+143
-23
lines changed

3 files changed

+143
-23
lines changed

CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project mostly adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [Unreleased]
8+
## [3.1.0] - 2020-05-09
99

1010
### Added
1111
- Added `line-[style]` and `line-[color]` utilities to go with Tailwind’s text decoration utilities (`underline` and `line-through`); they can be customized with the `textDecorationStyle` and `textDecorationColor` theme keys
1212

13+
### Fixed
14+
- Fixed an issue when using a font size that includes a default line height in a text style (e.g. `['16px', '24px']`), which is supported since Tailwind 1.3
15+
1316
## [3.0.1] - 2020-02-13
1417

1518
### Fixed
@@ -96,7 +99,8 @@ and this project mostly adheres to [Semantic Versioning](https://semver.org/spec
9699

97100
Initial release
98101

99-
[Unreleased]: https://github.com/benface/tailwindcss-typography/compare/v3.0.1...HEAD
102+
[Unreleased]: https://github.com/benface/tailwindcss-typography/compare/v3.1.0...HEAD
103+
[3.1.0]: https://github.com/benface/tailwindcss-typography/compare/v3.0.1...v3.1.0
100104
[3.0.1]: https://github.com/benface/tailwindcss-typography/compare/v3.0.0...v3.0.1
101105
[3.0.0]: https://github.com/benface/tailwindcss-typography/compare/v2.2.0...v3.0.0
102106
[2.2.0]: https://github.com/benface/tailwindcss-typography/compare/v2.1.1...v2.2.0

index.js

+46-17
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,58 @@ module.exports = plugin.withOptions(function(options = {}) {
192192

193193
const textStylesTheme = theme('textStyles');
194194

195-
const resolveTextStyle = function(styles) {
196-
if (!_.isPlainObject(styles)) {
197-
return _.isArray(styles) ? styles.join(', ') : styles;
198-
}
199-
return _.transform(styles, function(result, value, key) {
200-
if (key === 'extends') {
201-
_.forEach(_.castArray(value), function(textStyleToExtend) {
202-
_.forEach(resolveTextStyle(textStylesTheme[textStyleToExtend]), function(extendedValue, extendedKey) {
203-
if (extendedKey === 'output') {
204-
return; // continue
205-
}
206-
result[extendedKey] = resolveTextStyle(extendedValue);
195+
const resolveTextStyle = function(name, styles, topLevel = false) {
196+
if (_.isPlainObject(styles)) {
197+
const resolvedStyles = _.reduce(styles, function(result, value, key) {
198+
if (key === 'extends') {
199+
_.forEach(_.castArray(value), function(textStyleToExtend) {
200+
_.forEach(resolveTextStyle(textStyleToExtend, textStylesTheme[textStyleToExtend], true), function(extendedValue, extendedKey) {
201+
if (extendedKey === 'output') {
202+
return; // continue
203+
}
204+
result = {
205+
...result,
206+
...resolveTextStyle(extendedKey, extendedValue),
207+
};
208+
});
207209
});
208-
});
209-
return;
210+
return result;
211+
}
212+
return {
213+
...result,
214+
...resolveTextStyle(key, value),
215+
};
216+
}, {});
217+
218+
if (topLevel) {
219+
return resolvedStyles;
210220
}
211-
result[key] = resolveTextStyle(value);
212-
});
221+
222+
return {
223+
[name]: resolvedStyles,
224+
};
225+
}
226+
227+
if (_.isArray(styles)) {
228+
if (name === 'fontSize' && styles.length === 2) {
229+
return {
230+
fontSize: styles[0],
231+
lineHeight: styles[1],
232+
};
233+
}
234+
return {
235+
[name]: styles.join(', '),
236+
};
237+
}
238+
239+
return {
240+
[name]: styles,
241+
};
213242
};
214243

215244
const textStyles = _.fromPairs(
216245
_.map(textStylesTheme, (componentStyles, componentName) => {
217-
componentStyles = resolveTextStyle(componentStyles);
246+
componentStyles = resolveTextStyle(componentName, componentStyles, true);
218247
if (componentStyles.output === false) {
219248
return [];
220249
}

test.js

+91-4
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ test('text styles can extend more than one other text style', () => {
901901
theme: {
902902
fontFamily: {
903903
'default': 'sans-serif',
904-
'heading': ['Helvetica', 'Arial', 'sans-serif'],
904+
'heading': 'Helvetica',
905905
},
906906
fontWeight: {
907907
'bold': '700',
@@ -956,29 +956,116 @@ test('text styles can extend more than one other text style', () => {
956956
}).then(css => {
957957
expect(css).toMatchCss(`
958958
.c-heading {
959-
font-family: Helvetica, Arial, sans-serif;
959+
font-family: Helvetica;
960960
font-weight: 700;
961961
}
962962
.c-large-heading {
963963
font-weight: 800;
964964
line-height: 1.25;
965965
}
966966
.c-h1 {
967-
font-family: Helvetica, Arial, sans-serif;
967+
font-family: Helvetica;
968968
font-weight: 800;
969969
line-height: 1.25;
970970
font-size: 64px;
971971
}
972972
.c-h2 {
973-
font-family: Helvetica, Arial, sans-serif;
973+
font-family: Helvetica;
974974
font-weight: 800;
975975
line-height: 1.25;
976976
font-size: 48px;
977977
}
978+
.c-h3 {
979+
font-family: Helvetica;
980+
font-weight: 700;
981+
font-size: 36px;
982+
}
983+
`);
984+
});
985+
});
986+
987+
test('text styles support arrays in fontFamily and fontSize properties', () => {
988+
return generatePluginCss({
989+
theme: {
990+
fontFamily: {
991+
'default': 'sans-serif',
992+
'heading': ['Helvetica', 'Arial', 'sans-serif'],
993+
},
994+
fontWeight: {
995+
'bold': '700',
996+
'extrabold': '800',
997+
},
998+
fontSize: {
999+
'heading-xs': ['24px', '36px'],
1000+
'heading-sm': ['30px', '45px'],
1001+
'heading': ['36px', '54px'],
1002+
'heading-lg': ['48px', '72px'],
1003+
'heading-xl': ['64px', '96px'],
1004+
},
1005+
lineHeight: {
1006+
'none': '1',
1007+
'tight': '1.25',
1008+
'normal': '1.5',
1009+
},
1010+
textStyles: theme => ({
1011+
heading: {
1012+
fontFamily: theme('fontFamily.heading'),
1013+
fontWeight: theme('fontWeight.bold'),
1014+
},
1015+
largeHeading: {
1016+
fontWeight: theme('fontWeight.extrabold'),
1017+
},
1018+
h1: {
1019+
extends: ['heading', 'largeHeading'],
1020+
fontSize: theme('fontSize.heading-xl'),
1021+
},
1022+
h2: {
1023+
extends: ['heading', 'largeHeading'],
1024+
fontSize: theme('fontSize.heading-lg'),
1025+
},
1026+
h3: {
1027+
extends: 'heading',
1028+
fontSize: theme('fontSize.heading'),
1029+
},
1030+
}),
1031+
textDecorationStyle: {},
1032+
textDecorationColor: {},
1033+
fontVariantCaps: {},
1034+
fontVariantNumeric: {},
1035+
fontVariantLigatures: {},
1036+
textRendering: {},
1037+
},
1038+
}, {
1039+
ellipsis: false,
1040+
hyphens: false,
1041+
kerning: false,
1042+
textUnset: false,
1043+
}).then(css => {
1044+
expect(css).toMatchCss(`
1045+
.c-heading {
1046+
font-family: Helvetica, Arial, sans-serif;
1047+
font-weight: 700;
1048+
}
1049+
.c-large-heading {
1050+
font-weight: 800;
1051+
}
1052+
.c-h1 {
1053+
font-family: Helvetica, Arial, sans-serif;
1054+
font-weight: 800;
1055+
font-size: 64px;
1056+
line-height: 96px;
1057+
}
1058+
.c-h2 {
1059+
font-family: Helvetica, Arial, sans-serif;
1060+
font-weight: 800;
1061+
font-size: 48px;
1062+
line-height: 72px;
1063+
}
9781064
.c-h3 {
9791065
font-family: Helvetica, Arial, sans-serif;
9801066
font-weight: 700;
9811067
font-size: 36px;
1068+
line-height: 54px;
9821069
}
9831070
`);
9841071
});

0 commit comments

Comments
 (0)