Skip to content

Commit 6685382

Browse files
committed
Add support for other units beside rem
1 parent 24daee8 commit 6685382

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Nothing changed here to the default tailwindcss configuration:
4444
```
4545

4646
## 👉🏻 Configuration
47-
The plugin comes with a default configuration (see below) but it's possible to customize this config for your project:
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`:
4848
```js
4949
// tailwind.config.js
5050
module.exports = {
@@ -58,7 +58,8 @@ module.exports = {
5858
ratioMin: 1.125, // Multiplicator Min
5959
ratioMax: 1.2, // Multiplicator Max
6060
screenMin: 20, // 20rem === 320px
61-
screenMax: 96 // 96rem === 1536px
61+
screenMax: 96, // 96rem === 1536px
62+
unit: 'rem' // default is rem but it's also possible to use 'px'
6263
},
6364
// Creates the text-xx classes
6465
// This are the default settings and analog to the tailwindcss defaults

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.0.0",
3+
"version": "1.1.0",
44
"description": "Bring fluid type to tailwindcss",
55
"main": "src/index.js",
66
"license": "MIT",

src/index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ const plugin = require('tailwindcss/plugin')
33
module.exports = plugin(
44
function ({ addComponents, theme, variants, e }) {
55
const values = theme('fluidType');
6-
const settingsAreNumbers = Object.values(theme('fluidTypeSettings')).every(value => typeof value === 'number')
6+
const settingsAsArray = Object.entries(theme('fluidTypeSettings'));
7+
const settingsAsArrayFiltered = settingsAsArray.filter(([key, value]) => key !== 'unit');
8+
const finalSettings = Object.fromEntries(settingsAsArrayFiltered);
9+
const settingsAreNumbers = Object
10+
.values(finalSettings)
11+
.every(value => typeof value === 'number')
712

813
const calcModularScale = (value = 0) => {
914
if (settingsAreNumbers) {
@@ -13,11 +18,12 @@ module.exports = plugin(
1318
const sFtRMax = theme('fluidTypeSettings.ratioMax');
1419
const sFtSMin = theme('fluidTypeSettings.screenMin');
1520
const sFtSMax = theme('fluidTypeSettings.screenMax');
21+
const sFtUnit = typeof theme('fluidTypeSettings.unit') === 'string' ? theme('fluidTypeSettings.unit') : 'rem';
1622
const ftMin = sFtMin * Math.pow(sFtRMin, value);
1723
const ftMax = sFtMax * Math.pow(sFtRMax, value);
18-
return `clamp(${ftMin}rem,
19-
calc(${ftMin}rem + (${ftMax} - ${ftMin}) * ((100vw - ${sFtSMin}rem) / (${sFtSMax} - ${sFtSMin}))),
20-
${ftMax}rem)`;
24+
return `clamp(${ftMin}${sFtUnit},
25+
calc(${ftMin}${sFtUnit} + (${ftMax} - ${ftMin}) * ((100vw - ${sFtSMin}${sFtUnit}) / (${sFtSMax} - ${sFtSMin}))),
26+
${ftMax}${sFtUnit})`;
2127
}
2228
return value;
2329
};
@@ -43,7 +49,8 @@ module.exports = plugin(
4349
ratioMin: 1.125,
4450
ratioMax: 1.2,
4551
screenMin: 20,
46-
screenMax: 96
52+
screenMax: 96,
53+
unit: 'rem'
4754
},
4855
fluidType: {
4956
'xs': -2,
@@ -65,4 +72,4 @@ module.exports = plugin(
6572
fluidType: ['responsive'],
6673
},
6774
}
68-
)
75+
)

0 commit comments

Comments
 (0)