Skip to content

Commit 8e478c4

Browse files
committed
Release of version 2.0.0
1 parent 25bd5a1 commit 8e478c4

File tree

12 files changed

+57653
-330
lines changed

12 files changed

+57653
-330
lines changed

.eslintrc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"extends": ["wesbos"],
3+
"rules": {
4+
"no-console": 2,
5+
"class-methods-use-this": 0,
6+
"global-require": 0,
7+
"import/no-extraneous-dependencies": [
8+
"error",
9+
{
10+
"devDependencies": true,
11+
"optionalDependencies": false,
12+
"peerDependencies": false,
13+
"packageDir": "./"
14+
}
15+
],
16+
"prettier/prettier": [
17+
"error",
18+
{
19+
"trailingComma": "es5",
20+
"singleQuote": true,
21+
"printWidth": 200,
22+
"useTabs": false,
23+
"tabWidth": 4
24+
}
25+
]
26+
}
27+
}

README.md

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,137 @@
1-
# tailwindcss-rfs
2-
Tailwind CSS plugin to generate responsive font size utilities
1+
# RFS Plugin for Tailwind CSS
2+
3+
This is a Tailwind CSS plugin to generate utility classes for RFS.
4+
5+
**Important!**
6+
This plugin requires a working PostCSS setup of RFS in order to make use of the resizing engine of RFS. The plugin by itself only generates utility classes which then are transformed by RFS itself. You can check out the `webpack.mix.js` file for a working setup using Laravel Mix.
7+
8+
## Installation
9+
```
10+
yarn add tailwindcss-rfs
11+
```
12+
13+
## Usage
14+
Add the plugin to your Tailwind configuration:
15+
16+
```js
17+
// tailwind.config.js
18+
module.exports = {
19+
plugins: [
20+
require('tailwindcss-rfs')()
21+
]
22+
};
23+
```
24+
25+
### Options
26+
The plugin exposes the following options:
27+
28+
```js
29+
// tailwind.config.js
30+
module.exports = {
31+
plugins: [
32+
require('tailwindcss-rfs')({
33+
prefix: 'rfs-',
34+
suffix: '',
35+
fontSizeUtilities: true,
36+
paddingUtilities: true,
37+
marginUtilities: true,
38+
})
39+
]
40+
};
41+
```
42+
43+
#### Prefix
44+
By default all generated class names will start with `rfs-`. You can override this behaviour by changing the value of `prefix`:
45+
46+
```js
47+
prefix: '', // default: 'rfs-'
48+
```
49+
50+
#### Suffix
51+
You can also choose to use a `suffix` at the end of the generated class names:
52+
53+
```js
54+
suffix: '-rfs', // default: ''
55+
```
56+
57+
#### Utilities
58+
By default this plugin generates utility classes for `fontSize`, `padding` and `margin`. You can choose to disable the utilities that you don't need.
59+
60+
```js
61+
fontSizeUtilities: true, // default: true
62+
paddingUtilities: false, // default: true
63+
marginUtilities: false, // default: true
64+
```
65+
66+
### Variants
67+
You can also generate variants for the classes:
68+
69+
```js
70+
// tailwind.config.js
71+
module.exports = {
72+
variants: {
73+
rfsFontSize: ['responsive'], // default: []
74+
rfsPadding: ['responsive'], // default: []
75+
rfsMargin: ['responsive'], // default: []
76+
}
77+
};
78+
```
79+
80+
## Generated Utility Classes
81+
This plugin generates utility classes based on the keys of `fontSize`, `padding` and `margin` in the `theme` section of your `tailwind.config.js`. The generated class names follow the naming convention of Tailwind. By default all generated class names have a prefix of `rfs-`.
82+
83+
### Font Size
84+
```css
85+
.rfs-text-[key]
86+
```
87+
88+
### Padding
89+
```css
90+
.rfs-p-[key]
91+
.rfs-py-[key]
92+
.rfs-px-[key]
93+
.rfs-pt-[key]
94+
.rfs-pr-[key]
95+
.rfs-pb-[key]
96+
.rfs-pl-[key]
97+
```
98+
99+
### Margin
100+
```css
101+
.rfs-m-[key]
102+
.rfs-my-[key]
103+
.rfs-mx-[key]
104+
.rfs-mt-[key]
105+
.rfs-mr-[key]
106+
.rfs-mb-[key]
107+
.rfs-ml-[key]
108+
```
109+
110+
### Negative Margin
111+
```css
112+
.-rfs-m-[key]
113+
.-rfs-my-[key]
114+
.-rfs-mx-[key]
115+
.-rfs-mt-[key]
116+
.-rfs-mr-[key]
117+
.-rfs-mb-[key]
118+
.-rfs-ml-[key]
119+
```
120+
121+
**Note: You might want to disable the `corePlugins` for `fontSizes`, `padding` and `margin` in your Tailwind configuration.**
122+
123+
```js
124+
// tailwind.config.js
125+
module.exports = {
126+
corePlugins: {
127+
fontSize: false,
128+
padding: false,
129+
margin: false,
130+
}
131+
}
132+
```
133+
134+
## About RFS
135+
RFS is a unit resizing engine which automatically calculates the appropriate values based on the dimensions of the browser viewport.
136+
137+
[Learn more about RFS](https://github.com/twbs/rfs)

dist/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const _ = require('lodash');
2+
3+
const tailwindcssRfs = function(userConfig = {}) {
4+
return ({ addUtilities, e, theme, variants }) => {
5+
const defaultOptions = {
6+
prefix: 'rfs-',
7+
suffix: '',
8+
fontSizeUtilities: true,
9+
paddingUtilities: true,
10+
marginUtilities: true,
11+
};
12+
13+
const options = _.defaults({}, userConfig, defaultOptions);
14+
15+
const prefixNegativeModifiers = function(base, modifier) {
16+
if (modifier === '-') {
17+
return `-${base}`;
18+
}
19+
if (_.startsWith(modifier, '-')) {
20+
return `-${options.prefix}${base}-${modifier.slice(1)}${options.suffix}`;
21+
}
22+
return `${options.prefix}${base}-${modifier}${options.suffix}`;
23+
};
24+
25+
const fontSizeGenerator = (size, modifier) => ({
26+
[`.${e(`${options.prefix}text-${modifier}${options.suffix}`)}`]: { 'font-size': `rfs(${size})` },
27+
});
28+
29+
const paddingGenerator = (size, modifier) => ({
30+
[`.${e(`${options.prefix}p-${modifier}${options.suffix}`)}`]: { padding: `rfs(${size})` },
31+
[`.${e(`${options.prefix}py-${modifier}${options.suffix}`)}`]: { 'padding-top': `rfs(${size})`, 'padding-bottom': `rfs(${size})` },
32+
[`.${e(`${options.prefix}px-${modifier}${options.suffix}`)}`]: { 'padding-left': `rfs(${size})`, 'padding-right': `rfs(${size})` },
33+
[`.${e(`${options.prefix}pt-${modifier}${options.suffix}`)}`]: { 'padding-top': `rfs(${size})` },
34+
[`.${e(`${options.prefix}pr-${modifier}${options.suffix}`)}`]: { 'padding-right': `rfs(${size})` },
35+
[`.${e(`${options.prefix}pb-${modifier}${options.suffix}`)}`]: { 'padding-bottom': `rfs(${size})` },
36+
[`.${e(`${options.prefix}pl-${modifier}${options.suffix}`)}`]: { 'padding-left': `rfs(${size})` },
37+
});
38+
39+
const marginGenerator = (size, modifier) => ({
40+
[`.${e(prefixNegativeModifiers('m', modifier))}`]: { margin: `rfs(${size})` },
41+
[`.${e(prefixNegativeModifiers('my', modifier))}`]: { 'margin-top': `rfs(${size})`, 'margin-bottom': `rfs(${size})` },
42+
[`.${e(prefixNegativeModifiers('mx', modifier))}`]: { 'margin-left': `rfs(${size})`, 'margin-right': `rfs(${size})` },
43+
[`.${e(prefixNegativeModifiers('mt', modifier))}`]: { 'margin-top': `rfs(${size})` },
44+
[`.${e(prefixNegativeModifiers('mr', modifier))}`]: { 'margin-right': `rfs(${size})` },
45+
[`.${e(prefixNegativeModifiers('mb', modifier))}`]: { 'margin-bottom': `rfs(${size})` },
46+
[`.${e(prefixNegativeModifiers('ml', modifier))}`]: { 'margin-left': `rfs(${size})` },
47+
});
48+
49+
const fontSizeUtilities = _.flatMap(theme('fontSize'), fontSizeGenerator);
50+
const paddingUtilities = _.flatMap(theme('padding'), paddingGenerator);
51+
const marginUtilities = _.flatMap(theme('margin'), marginGenerator);
52+
53+
if (options.fontSizeUtilities) addUtilities(fontSizeUtilities, variants('rfsFontSize'));
54+
if (options.paddingUtilities) addUtilities(paddingUtilities, variants('rfsPadding'));
55+
if (options.marginUtilities) addUtilities(marginUtilities, variants('rfsMargin'));
56+
};
57+
};
58+
59+
module.exports = tailwindcssRfs;

0 commit comments

Comments
 (0)