Skip to content

Commit 7c99ee9

Browse files
committed
Create shapes plugin for tailwind
1 parent e641700 commit 7c99ee9

File tree

6 files changed

+203
-0
lines changed

6 files changed

+203
-0
lines changed

.gitignore

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

README.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# tailwindcss-shapes
2+
3+
A tailwind plugin that provides a simple way to create a square or a circle.
4+
5+
## Installation
6+
7+
Install the plugin from npm:
8+
9+
```sh
10+
npm install -D tailwindcss-shapes
11+
```
12+
13+
Then add the plugin to your `tailwind.config.js` file
14+
15+
```js
16+
// tailwind.config.js
17+
module.exports = {
18+
theme: {
19+
// ...
20+
},
21+
plugins: [
22+
require('tailwindcss-shapes'),
23+
// ...
24+
],
25+
}
26+
```
27+
28+
## Usage
29+
30+
Simply use `sq-{n}` or `circle-{n}` to set both the width and height of an element. The circle will additionally apply a border-radius of 9999px.
31+
32+
```html
33+
<div class="sq-24">...contents</div>
34+
```
35+
36+
Works with arbitrary values:
37+
38+
```html
39+
<div class="sq-[53px]">
40+
<!-- ... -->
41+
</div>
42+
```
43+
44+
As well as with modifiers
45+
46+
```html
47+
<div class="sq-12 lg:sq-16"></div>
48+
```
49+
50+
By default all the default spacing values are available.
51+
52+
```text
53+
sq-px: 1px
54+
sq-0: 0
55+
sq-0.5: 0.125rem
56+
sq-1: 0.25rem
57+
sq-1.5: 0.375rem
58+
sq-2: 0.5rem
59+
sq-2.5: 0.625rem
60+
sq-3: 0.75rem
61+
sq-3.5: 0.875rem
62+
sq-4: 1rem
63+
sq-5: 1.25rem
64+
sq-6: 1.5rem
65+
sq-7: 1.75rem
66+
sq-8: 2rem
67+
sq-9: 2.25rem
68+
sq-10: 2.5rem
69+
sq-11: 2.75rem
70+
sq-12: 3rem
71+
sq-14: 3.5rem
72+
sq-16: 4rem
73+
sq-20: 5rem
74+
sq-24: 6rem
75+
sq-28: 7rem
76+
sq-32: 8rem
77+
sq-36: 9rem
78+
sq-40: 10rem
79+
sq-44: 11rem
80+
sq-48: 12rem
81+
sq-52: 13rem
82+
sq-56: 14rem
83+
sq-60: 15rem
84+
sq-64: 16rem
85+
sq-72: 18rem
86+
sq-80: 20rem
87+
sq-96: 24rem
88+
```

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "tailwindcss-shapes",
3+
"version": "1.0.0",
4+
"main": "src/index.js",
5+
"types": "src/index.d.ts",
6+
"license": "MIT",
7+
"repository": "https://github.com/mezzomind/tailwindcss-shapes",
8+
"publishConfig": {
9+
"access": "public"
10+
},
11+
"prettier": {
12+
"printWidth": 100,
13+
"semi": false,
14+
"singleQuote": true,
15+
"trailingComma": "es5"
16+
},
17+
"scripts": {
18+
"test": "jest"
19+
},
20+
"peerDependencies": {
21+
"tailwindcss": ">=3.0.0"
22+
},
23+
"devDependencies": {
24+
"jest": "^29.2.2",
25+
"postcss": "^8.2.4",
26+
"tailwindcss": "^3.2.1"
27+
}
28+
}

src/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const plugin: { handler: () => void }
2+
export = plugin

src/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const plugin = require('tailwindcss/plugin')
2+
3+
const shapes = plugin(function ({ matchUtilities, theme }) {
4+
matchUtilities(
5+
{
6+
sq: (value) => ({
7+
width: value,
8+
height: value,
9+
}),
10+
circle: (value) => ({
11+
width: value,
12+
height: value,
13+
borderRadius: '9999px',
14+
}),
15+
},
16+
{ values: theme('spacing') }
17+
)
18+
})
19+
20+
module.exports = shapes

tests/test.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const postcss = require('postcss')
2+
3+
let expected = `
4+
.sq-px {
5+
width: 1px;
6+
height: 1px
7+
}
8+
.sq-6 {
9+
width: 1.5rem;
10+
height: 1.5rem
11+
}
12+
.sq-\\[11px\\] {
13+
width: 11px;
14+
height: 11px
15+
}
16+
.sq-\\[var\\(--box\\)\\] {
17+
width: var(--box);
18+
height: var(--box)
19+
}
20+
.circle-px {
21+
width: 1px;
22+
height: 1px;
23+
border-radius: 9999px
24+
}
25+
.circle-6 {
26+
width: 1.5rem;
27+
height: 1.5rem;
28+
border-radius: 9999px
29+
}
30+
.circle-\\[11px\\] {
31+
width: 11px;
32+
height: 11px;
33+
border-radius: 9999px
34+
}
35+
@media (min-width: 1024px) {
36+
.lg\\:sq-8 {
37+
width: 2rem;
38+
height: 2rem
39+
}
40+
.lg\\:circle-8 {
41+
width: 2rem;
42+
height: 2rem;
43+
border-radius: 9999px
44+
}
45+
}
46+
`
47+
48+
let css = postcss([
49+
require('tailwindcss')({
50+
content: [
51+
{
52+
raw: 'sq-px sq-6 sq-[11px] sq-[var(--box)] lg:sq-8 circle-px circle-6 circle-[11px] lg:circle-8',
53+
},
54+
],
55+
plugins: [require('../')],
56+
}),
57+
]).process('@tailwind utilities').css
58+
59+
test('sq values', () => {
60+
expect(css).toBe(expected.trim())
61+
})

0 commit comments

Comments
 (0)