Skip to content

Commit 5e76c34

Browse files
authored
Use theme and variants keys of config + add tests (#7)
Use theme and variants keys of config + add tests
2 parents 3ab2a8b + 8a7b029 commit 5e76c34

File tree

5 files changed

+5411
-66
lines changed

5 files changed

+5411
-66
lines changed

README.md

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,29 @@ npm install --save-dev tailwindcss-aspect-ratio
1111

1212
## Usage
1313

14-
The aspect ratio plugin exposes options for you to use. Here is the example for adding it to your project plugins
14+
This plugin uses the `aspectRatio` key in your Tailwind config’s `theme` and `variants` objects to generate aspect ratio utilities. Here is an example:
1515

1616
```js
17-
require('tailwindcss-aspect-ratio')({
18-
ratios: {
19-
'square': [1, 1],
20-
'16/9': [16, 9],
21-
'4/3': [4, 3],
22-
'21/9': [21, 9],
23-
}
24-
})
17+
// tailwind.config.js
18+
{
19+
theme: {
20+
aspectRatio: { // defaults to {}
21+
'square': [1, 1],
22+
'16/9': [16, 9],
23+
'4/3': [4, 3],
24+
'21/9': [21, 9],
25+
},
26+
},
27+
variants: {
28+
aspectRatio: ['responsive'], // defaults to ['responsive']
29+
},
30+
plugins: [
31+
require('tailwindcss-aspect-ratio')(),
32+
],
33+
}
2534
```
2635

27-
This configuration would create the following classes:
36+
This configuration would create the following classes, as well as their responsive variants:
2837

2938
```css
3039
.aspect-ratio-square { padding-top: 100%; }
@@ -33,41 +42,6 @@ This configuration would create the following classes:
3342
.aspect-ratio-21/9 { padding-top: 42.86%; }
3443
```
3544

36-
The plugin accepts an object where the key is the suffix of the class name and the value is an array of width and height `[{width}, {height}]`.
45+
The `aspectRatio` theme is an object where the key is the suffix of the class name and the value is an array of width and height `[{width}, {height}]`.
3746

3847
In the example above you can see that the key does not have to replicate the values, so if you prefer "nice names" you could have some like `'cinema': [21, 9]` or `'letterbox': [16,9]`.
39-
40-
As per the [tailwind plugin docs](https://tailwindcss.com/docs/plugins/) you are able to pass variants (responsive, hover, etc.) as a parameter.
41-
42-
```js
43-
require('tailwindcss-aspect-ratio')({
44-
ratios: {
45-
'square': [1, 1],
46-
'16/9': [16, 9],
47-
'4/3': [4, 3],
48-
'21/9': [21, 9],
49-
},
50-
variants: ['responsive', 'hover'],
51-
})
52-
```
53-
54-
Using the above should mean your plugins config looks something like this:
55-
56-
```js
57-
// example plugins section of tailwind.js config file
58-
59-
plugins: [
60-
require('tailwindcss/plugins/container')({
61-
//center: true,
62-
// padding: '1rem',
63-
}),
64-
require('tailwindcss-aspect-ratio')({
65-
ratios: {
66-
'square': [1, 1],
67-
'16/9': [16, 9],
68-
'4/3': [4, 3],
69-
'21/9': [21, 9],
70-
}
71-
}),
72-
],
73-
```

index.js

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
const _ = require('lodash');
22

3-
module.exports = function({ ratios, variants }) {
4-
return function({ addUtilities, e }) {
5-
const utilities = _.map(ratios, ([width, height], name) => ({
6-
[`.${e(`aspect-ratio-${name}`)}`]: {
7-
paddingTop: `${((Math.round(height) / Math.round(width)) * 100).toFixed(2)}%`
8-
}
9-
}))
10-
11-
addUtilities(utilities, variants)
12-
}
13-
}
3+
module.exports = function() {
4+
return ({ theme, variants, e, addUtilities }) => {
5+
const defaultAspectRatioTheme = {};
6+
const defaultAspectRatioVariants = ['responsive'];
7+
8+
const aspectRatioTheme = theme('aspectRatio', defaultAspectRatioTheme);
9+
const aspectRatioVariants = variants('aspectRatio', defaultAspectRatioVariants);
10+
11+
const aspectRatioUtilities = _.fromPairs(
12+
_.map(aspectRatioTheme, (value, modifier) => {
13+
const aspectRatio = _.isArray(value) ? value[0] / value[1] : value;
14+
return [
15+
`.${e(`aspect-ratio-${modifier}`)}`,
16+
{
17+
paddingBottom: `${1 / aspectRatio * 100}%`,
18+
},
19+
];
20+
})
21+
);
22+
23+
addUtilities(aspectRatioUtilities, aspectRatioVariants);
24+
};
25+
};

0 commit comments

Comments
 (0)