Skip to content

Commit da58e8c

Browse files
committed
Init
0 parents  commit da58e8c

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
yarn.lock
3+
package-lock.json

dist/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!.gitignore
3+
!.npmignore

dist/.npmignore

Whitespace-only changes.

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "@tailwindcss/aspect-ratio",
3+
"version": "0.1.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"repository": "https://github.com/tailwindlabs/tailwindcss-aspect-ratio",
7+
"publishConfig": {
8+
"access": "public"
9+
},
10+
"prettier": {
11+
"printWidth": 100,
12+
"semi": false,
13+
"singleQuote": true,
14+
"trailingComma": "es5"
15+
},
16+
"scripts": {
17+
"prepublishOnly": "node scripts/build.js"
18+
},
19+
"peerDependencies": {
20+
"tailwindcss": "^1.0.0 || ^2.0.0"
21+
},
22+
"devDependencies": {
23+
"autoprefixer": "^9.6.1",
24+
"clean-css": "^4.2.1",
25+
"postcss": "^7.0.17",
26+
"tailwindcss": "^2.0.0-alpha.8"
27+
}
28+
}

scripts/build.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const fs = require('fs')
2+
const postcss = require('postcss')
3+
const tailwind = require('tailwindcss')
4+
const CleanCSS = require('clean-css')
5+
6+
function buildDistFile(filename) {
7+
return postcss([
8+
tailwind({
9+
corePlugins: false,
10+
plugins: [require('../src/index.js')],
11+
}),
12+
require('autoprefixer'),
13+
])
14+
.process('@tailwind components', {
15+
from: null,
16+
to: `./dist/${filename}.css`,
17+
map: false,
18+
})
19+
.then((result) => {
20+
fs.writeFileSync(`./dist/${filename}.css`, result.css)
21+
return result
22+
})
23+
.then((result) => {
24+
const minified = new CleanCSS().minify(result.css)
25+
fs.writeFileSync(`./dist/${filename}.min.css`, minified.styles)
26+
})
27+
.catch((error) => {
28+
console.log(error)
29+
})
30+
}
31+
32+
console.info('Building CSS...')
33+
34+
Promise.all([buildDistFile('aspect-ratio')]).then(() => {
35+
console.log('Finished building CSS.')
36+
})

src/index.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const plugin = require('tailwindcss/plugin')
2+
3+
const aspectRatio = plugin(
4+
function ({ addComponents, theme, variants, e }) {
5+
const values = theme('aspectRatio')
6+
7+
const baseSelectors = Object.entries(values)
8+
.map(([key, value]) => {
9+
return `.${e(`aspect-w-${key}`)}`
10+
})
11+
.join(',\n')
12+
13+
const childSelectors = Object.entries(values)
14+
.map(([key, value]) => {
15+
return `.${e(`aspect-w-${key}`)} > *`
16+
})
17+
.join(',\n')
18+
19+
console.log(baseSelectors, childSelectors)
20+
21+
addComponents(
22+
[
23+
{
24+
[baseSelectors]: {
25+
position: 'relative',
26+
paddingBottom: `calc(var(--aspect-h) / var(--aspect-w) * 100%)`,
27+
},
28+
},
29+
{
30+
[childSelectors]: {
31+
position: 'absolute',
32+
height: '100%',
33+
width: '100%',
34+
top: '0',
35+
right: '0',
36+
bottom: '0',
37+
left: '0',
38+
},
39+
},
40+
Object.entries(values).map(([key, value]) => {
41+
return {
42+
[`.${e(`aspect-w-${key}`)}`]: {
43+
'--aspect-w': value,
44+
},
45+
}
46+
}),
47+
Object.entries(values).map(([key, value]) => {
48+
return {
49+
[`.${e(`aspect-h-${key}`)}`]: {
50+
'--aspect-h': value,
51+
},
52+
}
53+
}),
54+
],
55+
variants('aspectRatio')
56+
)
57+
},
58+
{
59+
theme: {
60+
aspectRatio: {
61+
1: '1',
62+
2: '2',
63+
3: '3',
64+
4: '4',
65+
5: '5',
66+
6: '6',
67+
7: '7',
68+
8: '8',
69+
9: '9',
70+
10: '10',
71+
11: '11',
72+
12: '12',
73+
13: '13',
74+
14: '14',
75+
15: '15',
76+
16: '16',
77+
},
78+
},
79+
variants: {
80+
aspectRatio: ['responsive'],
81+
},
82+
}
83+
)
84+
85+
module.exports = aspectRatio

0 commit comments

Comments
 (0)