Skip to content

Commit 7be4eca

Browse files
committed
Add lineHeight & letterSpacing feature
It's not also possible to set lineHeight and / or letterSpacing like you know from the tailwind documentation.
1 parent 08207aa commit 7be4eca

File tree

3 files changed

+262
-69
lines changed

3 files changed

+262
-69
lines changed

README.md

Lines changed: 212 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# 👉🏻 tailwindcss-fluid-type
1+
# 👉🏻 tailwindcss-fluid-type
22

33
![Tailwincss Fluid Type](https://github.com/davidhellmann/tailwindcss-fluid-type/raw/main/tailwindcss-fluid-type.png)
44

5-
A plugin that makes the use of Fluid Type a breeze.
5+
A plugin that makes the use of Fluid Type a breeze.
66

77
## 👉🏻 Installation
8+
89
Install the plugin from npm:
10+
911
```bash
1012
# Using npm
1113
npm install tailwindcss-fluid-type
@@ -15,73 +17,232 @@ yarn add tailwindcss-fluid-type
1517
```
1618

1719
Then add the plugin to your tailwind.config.js file and do your settings if you're not happy with the defaults:
20+
1821
```js
1922

2023
// tailwind.config.js
2124
module.exports = {
22-
theme: {
23-
// ...
24-
},
25-
// You have to disable the fontSize core
26-
// plugins otherwise it doesn't work
27-
corePlugins: {
28-
fontSize: false,
29-
// ...
30-
},
31-
plugins: [
32-
require('tailwindcss-fluid-type'),
33-
// ...
34-
],
25+
theme: {
26+
// ...
27+
},
28+
// You have to disable the fontSize core
29+
// plugins otherwise it doesn't work
30+
corePlugins: {
31+
fontSize: false,
32+
// ...
33+
},
34+
plugins: [
35+
require('tailwindcss-fluid-type'),
36+
// ...
37+
],
3538
};
3639
```
3740

3841
## 👉🏻 Usage
42+
3943
Nothing changed here to the default tailwindcss configuration:
44+
4045
```html
46+
4147
<article>
42-
<h1 class="text-xl">Fluid type</h1>
48+
<h1 class="text-xl">Fluid type</h1>
4349
</article>
4450
```
4551

4652
## 👉🏻 Configuration
47-
The plugin comes with a default configuration (see below) but it's possible to customize this config for your project. As default, we use `rem` for better accessibility, but you can also use `px`:
53+
54+
The plugin comes with a default configuration (see below) but it's possible to customize this config for your project.
55+
As default, we use `rem` for better accessibility, but you can also use `px`.
56+
57+
### Default configuration
58+
4859
```js
4960
// tailwind.config.js
5061
module.exports = {
51-
theme: {
52-
// your fluid type settings
53-
// works only with unitless numbers
54-
// This numbers are the defaults settings
55-
fluidTypeSettings: {
56-
fontSizeMin: 1.125, // 1.125rem === 18px
57-
fontSizeMax: 1.25, // 1.25rem === 20px
58-
ratioMin: 1.125, // Multiplicator Min
59-
ratioMax: 1.2, // Multiplicator Max
60-
screenMin: 20, // 20rem === 320px
61-
screenMax: 96, // 96rem === 1536px
62-
unit: 'rem' // default is rem but it's also possible to use 'px'
62+
theme: {
63+
// your fluid type settings
64+
// works only with unitless numbers
65+
// This numbers are the defaults settings
66+
fluidTypeSettings: {
67+
fontSizeMin: 1.125, // 1.125rem === 18px
68+
fontSizeMax: 1.25, // 1.25rem === 20px
69+
ratioMin: 1.125, // Multiplicator Min
70+
ratioMax: 1.2, // Multiplicator Max
71+
screenMin: 20, // 20rem === 320px
72+
screenMax: 96, // 96rem === 1536px
73+
unit: 'rem' // default is rem but it's also possible to use 'px'
74+
},
75+
// Creates the text-xx classes
76+
// This are the default settings and analog to the tailwindcss defaults
77+
// Each `lineHeight` is set unitless and we think that's the way to go especially in context with fluid type.
78+
fluidType: {
79+
'xs': [-2, 1.6],
80+
'sm': [-1, 1.6],
81+
'base': [0, 1.6],
82+
'lg': [1, 1.6],
83+
'xl': [2, 1.2],
84+
'2xl': [3, 1.2],
85+
'3xl': [4, 1.2],
86+
'4xl': [5, 1.1],
87+
'5xl': [6, 1.1],
88+
'6xl': [7, 1.1],
89+
'7xl': [8, 1],
90+
'8xl': [9, 1],
91+
'9xl': [10, 1],
92+
},
6393
},
64-
// Creates the text-xx classes
65-
// This are the default settings and analog to the tailwindcss defaults
66-
// The values should be integer numbers
67-
fluidType: {
68-
'xs': -2,
69-
'sm': -1,
70-
'base': 0,
71-
'lg': 1,
72-
'xl': 2,
73-
'2xl': 3,
74-
'3xl': 4,
75-
'4xl': 5,
76-
'5xl': 6,
77-
'6xl': 7,
78-
'7xl': 8,
79-
'8xl': 9,
80-
'9xl': 10,
81-
},
82-
},
83-
variants: {
84-
fluidType: ['responsive']
85-
}
94+
variants: {
95+
fluidType: ['responsive']
96+
}
8697
};
8798
```
99+
100+
### Fluid type configuration without `lineHeight`
101+
102+
It is also possible to set just the `fontSize` without set the `lineHeight`
103+
104+
```js
105+
// tailwind.config.js
106+
module.exports = {
107+
theme: {
108+
fluidType: {
109+
// ...
110+
'base': 0,
111+
// ...
112+
}
113+
}
114+
};
115+
```
116+
117+
### Fluid type configuration with `lineHeight` & `letterSpacing`
118+
119+
And yes, you can also set the `letterSpacing` & `lineHeight` as you know from the tailwind
120+
documentation. `letterSpacing` can be all values that you like.
121+
122+
```js
123+
// tailwind.config.js
124+
module.exports = {
125+
theme: {
126+
fluidType: {
127+
// ...
128+
'base': [0,
129+
{
130+
lineHeight: 1.6,
131+
letterSpacing: '-0.1rem',
132+
}
133+
],
134+
// ...
135+
}
136+
}
137+
};
138+
```
139+
140+
## 👉🏻 Samples
141+
142+
### Just set the `fontSize` property
143+
144+
```js
145+
// tailwind.config.js
146+
module.exports = {
147+
theme: {
148+
fluidTypeSettings: {
149+
fontSizeMin: 1.125,
150+
fontSizeMax: 1.25,
151+
ratioMin: 1.125,
152+
ratioMax: 1.2,
153+
screenMin: 20,
154+
screenMax: 96,
155+
unit: 'rem'
156+
},
157+
fluidType: {
158+
// ...
159+
'base': 0,
160+
// ...
161+
}
162+
}
163+
};
164+
```
165+
166+
```html
167+
<p class="text-base">The quick brown fox jumps over the lazy dogs</p>
168+
```
169+
170+
```css
171+
.text-base {
172+
font-size: clamp(1.125rem, calc(1.125rem + (1.25 - 1.125) * ((100vw - 20rem) / (96 - 20))), 1.25rem);
173+
}
174+
```
175+
176+
### Set the `fontSize` & `lineHeight` property
177+
178+
```js
179+
// tailwind.config.js
180+
module.exports = {
181+
theme: {
182+
fluidTypeSettings: {
183+
fontSizeMin: 1.125,
184+
fontSizeMax: 1.25,
185+
ratioMin: 1.125,
186+
ratioMax: 1.2,
187+
screenMin: 20,
188+
screenMax: 96,
189+
unit: 'rem'
190+
},
191+
fluidType: {
192+
// ...
193+
'base': [0, 1.6],
194+
// ...
195+
}
196+
}
197+
};
198+
```
199+
200+
```html
201+
<p class="text-base">The quick brown fox jumps over the lazy dogs</p>
202+
```
203+
204+
```css
205+
.text-base {
206+
font-size: clamp(1.125rem, calc(1.125rem + (1.25 - 1.125) * ((100vw - 20rem) / (96 - 20))), 1.25rem);
207+
line-height: 1.6;
208+
}
209+
```
210+
211+
### Set the `fontSize`, `lineHeight` & `letterSpacing` property
212+
213+
```js
214+
// tailwind.config.js
215+
module.exports = {
216+
theme: {
217+
fluidTypeSettings: {
218+
fontSizeMin: 1.125,
219+
fontSizeMax: 1.25,
220+
ratioMin: 1.125,
221+
ratioMax: 1.2,
222+
screenMin: 20,
223+
screenMax: 96,
224+
unit: 'rem'
225+
},
226+
fluidType: {
227+
// ...
228+
'base': [0, {
229+
lineHeight: 1.6,
230+
letterSpacing: '-0.1rem',
231+
}],
232+
// ...
233+
}
234+
}
235+
};
236+
```
237+
238+
```html
239+
<p class="text-base">The quick brown fox jumps over the lazy dogs</p>
240+
```
241+
242+
```css
243+
.text-base {
244+
font-size: clamp(1.125rem, calc(1.125rem + (1.25 - 1.125) * ((100vw - 20rem) / (96 - 20))), 1.25rem);
245+
line-height: 1.6;
246+
letter-spacing: -0.1rem;
247+
}
248+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tailwindcss-fluid-type",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Bring fluid type to tailwindcss",
55
"main": "src/index.js",
66
"license": "MIT",

src/index.js

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const plugin = require('tailwindcss/plugin')
22

33
module.exports = plugin(
4-
function ({ addComponents, theme, variants, e }) {
4+
function ({addComponents, theme, variants, e}) {
55
const values = theme('fluidType');
66
const settingsAsArray = Object.entries(theme('fluidTypeSettings'));
77
const settingsAsArrayFiltered = settingsAsArray.filter(([key, value]) => key !== 'unit');
@@ -31,10 +31,42 @@ module.exports = plugin(
3131
addComponents(
3232
[
3333
Object.entries(values).map(([key, value]) => {
34+
let output = {};
35+
36+
// Check if value a number
37+
if (Number.isInteger(value)) {
38+
output.fontSize = Number.isInteger(value) ? calcModularScale(value) : value
39+
}
40+
41+
// Check if value is array with length 1
42+
if (Array.isArray(value) && value.length === 1) {
43+
output.fontSize = Number.isInteger(value[0]) ? calcModularScale(value[0]) : value[0]
44+
}
45+
46+
// Check if value is array with length 2
47+
if (Array.isArray(value) && value.length === 2) {
48+
49+
// Check if second value is an object
50+
if (typeof value[1] === 'object' && value[1] !== null) {
51+
output.fontSize = Number.isInteger(value[0]) ? calcModularScale(value[0]) : value[0]
52+
53+
// Check if key lineHeight exists
54+
if ("lineHeight" in value[1]) {
55+
output.lineHeight = value[1]['lineHeight']
56+
}
57+
58+
// Check if key letterSpacing exists
59+
if ("letterSpacing" in value[1]) {
60+
output.letterSpacing = value[1]['letterSpacing']
61+
}
62+
} else {
63+
output.fontSize = Number.isInteger(value[0]) ? calcModularScale(value[0]) : value[0]
64+
output.lineHeight = value[1]
65+
}
66+
}
67+
3468
return {
35-
[`.${e(`text-${key}`)}`]: {
36-
fontSize: Number.isInteger(value) ? calcModularScale(value) : value,
37-
},
69+
[`.${e(`text-${key}`)}`]: output,
3870
}
3971
}),
4072
],
@@ -53,19 +85,19 @@ module.exports = plugin(
5385
unit: 'rem'
5486
},
5587
fluidType: {
56-
'xs': -2,
57-
'sm': -1,
58-
'base': 0,
59-
'lg': 1,
60-
'xl': 2,
61-
'2xl': 3,
62-
'3xl': 4,
63-
'4xl': 5,
64-
'5xl': 6,
65-
'6xl': 7,
66-
'7xl': 8,
67-
'8xl': 9,
68-
'9xl': 10,
88+
'xs': [-2, 1.6],
89+
'sm': [-1, 1.6],
90+
'base': [0, 1.6],
91+
'lg': [1, 1.6],
92+
'xl': [2, 1.2],
93+
'2xl': [3, 1.2],
94+
'3xl': [4, 1.2],
95+
'4xl': [5, 1.1],
96+
'5xl': [6, 1.1],
97+
'6xl': [7, 1.1],
98+
'7xl': [8, 1],
99+
'8xl': [9, 1],
100+
'9xl': [10, 1],
69101
},
70102
},
71103
variants: {

0 commit comments

Comments
 (0)