Skip to content
This repository was archived by the owner on Apr 10, 2021. It is now read-only.

Commit 641833a

Browse files
committed
Version 1.0.0
Signed-off-by: Peter Neupauer <[email protected]>
1 parent a4d144d commit 641833a

File tree

4 files changed

+111
-70
lines changed

4 files changed

+111
-70
lines changed

README.md

+49-23
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Tailwind CSS Plugin – Filter & Backdrop Filter
22

3+
Utilities for controlling filter & backdrop-filter behaviour.
4+
35
## Install
46

57
1. Install the plugin:
68

79
```bash
810
# Using npm
9-
npm install @neupauer/tailwindcss-plugin-filter --save-dev
11+
npm install @neupauer/tailwindcss-plugin-filter
1012

1113
# Using Yarn
12-
yarn add @neupauer/tailwindcss-plugin-filter -D
14+
yarn add @neupauer/tailwindcss-plugin-filter
1315
```
1416

1517
2. Add it to your `tailwind.config.js` file:
@@ -29,69 +31,93 @@ module.exports = {
2931
module.exports = {
3032
theme: {
3133
// default
32-
filter: {
34+
filterFunctions: {
3335
blur: {},
3436
brightness: {},
3537
contrast: {},
36-
"drop-shadow": {},
38+
dropShadow: {},
3739
grayscale: {},
38-
"hue-rotate": {},
40+
hueRotate: {},
3941
invert: {},
40-
opacity: {},
4142
saturate: {},
4243
sepia: {},
4344
},
4445
},
4546
variants: {
4647
// default
47-
filter: ["responsive", "hover"],
48+
filterFunctions: ["responsive", "hover"],
4849
},
4950
};
5051
```
5152

5253
## Usage
5354

55+
### Filter & Backdrop Filter
56+
57+
To enable filters you have to add the `.filter` or `.backdrop` utility. _(Similar to the native Tailwind `transform` utility)_
58+
59+
### None
60+
61+
If you want to disable filters, then you can use the `.filter-none` or `.backdrop-none` utility.
62+
63+
### Responsive
64+
65+
To enable or disable filters at a specific breakpoint, add a `{screen}:` prefix to any of the filter utilities. For example, use `md:filter` or `md:backdrop` to apply the `filter` or `backdrop` utility at only medium screen sizes and above.
66+
67+
### Available Filter Functions
68+
69+
> By default, no values are provided for any filter
70+
71+
- `.blur-{value}`
72+
- `.brightness-{value}`
73+
- `.contrast-{value}`
74+
- `.drop-shadow-{value}`
75+
- `.grayscale-{value}`
76+
- `.hue-rotate-{value}`
77+
- `.invert-{value}`
78+
- `.saturate-{value}`
79+
- `.sepia-{value}`
80+
81+
> Utility for **filter: opacity()** or **backdrop-filter: opacity()** is not provided because [according to MDN](<https://developer.mozilla.org/en-US/docs/Web/CSS/filter-function/opacity()>) this function is similar to the more established **opacity** property. The difference is that with filters, some browsers provide hardware acceleration for better performance.
82+
>
83+
> So, you can use [Tailwind's native `opacity` utility](https://tailwindcss.com/docs/opacity).
84+
85+
### Example Usage
86+
5487
```js
5588
// tailwind.config.js
5689
module.exports = {
5790
theme: {
58-
filter: {
91+
filterFunctions: {
5992
blur: {
93+
1: "1px",
6094
2: "2px",
95+
3: "3px",
6196
},
6297
saturate: {
6398
40: "40%",
6499
},
65-
opacity: {
66-
20: "20%",
67-
},
68100
},
69101
},
70102
};
71103
```
72104

73-
> All filters have equivalent class name except `opacity`.
74-
> `.opacity-{x}` => `.filter-opacity-{x}`
75-
76105
```html
77-
<!-- backdrop-filter: blur(2px) saturate(40%); -->
78-
<div class="backdrop blur-2 saturate-40"></div>
79-
80106
<!-- filter: blur(2px) saturate(40%); -->
81107
<div class="filter blur-2 saturate-40"></div>
82108

109+
<!-- backdrop-filter: blur(2px) saturate(40%); -->
110+
<div class="backdrop blur-2 saturate-40"></div>
111+
83112
<!--
84113
filter: blur(2px) saturate(40%);
85114
backdrop-filter: blur(2px) saturate(40%);
86115
-->
87116
<div class="filter backdrop blur-2 saturate-40"></div>
88117

89-
<!-- backdrop-filter: none; -->
90-
<div class="no-backdrop"></div>
91-
92118
<!-- filter: none; -->
93-
<div class="no-filter"></div>
119+
<div class="filter-none"></div>
94120

95-
<!-- backdrop-filter: opacity(20%); -->
96-
<div class="backdrop filter-opacity-20"></div>
121+
<!-- backdrop-filter: none; -->
122+
<div class="backdrop-none"></div>
97123
```

package.json

+25-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
{
22
"name": "@neupauer/tailwindcss-plugin-filter",
3-
"version": "0.1.0",
4-
"description": "A Tailwind CSS plugin for the filter and backdrop-filter properties.",
3+
"version": "1.0.0",
4+
"description": "A Tailwind CSS plugin for controlling filter & backdrop-filter behaviour.",
55
"main": "src/index.js",
6-
"license": "MIT",
7-
"repository": "https://github.com/neupauer/tailwindcss-plugin-filter",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/neupauer/tailwindcss-plugin-filter"
9+
},
10+
"keywords": [
11+
"tailwindcss",
12+
"plugin",
13+
"filter",
14+
"backdrop-filter",
15+
"blur",
16+
"brightness",
17+
"contrast",
18+
"drop-shadow",
19+
"grayscale",
20+
"hue-rotate",
21+
"invert",
22+
"saturate",
23+
"sepia"
24+
],
825
"author": {
926
"name": "Peter Neupauer",
1027
"url": "https://neupauer.sk"
1128
},
29+
"license": "MIT",
1230
"publishConfig": {
1331
"access": "public"
1432
},
15-
"prettier": {
16-
"printWidth": 120
17-
},
1833
"scripts": {
1934
"prepublishOnly": "node scripts/build.js"
2035
},
@@ -23,5 +38,8 @@
2338
"clean-css": "^4.2.1",
2439
"postcss": "^8.2.2",
2540
"tailwindcss": "^2.0.2"
41+
},
42+
"prettier": {
43+
"printWidth": 120
2644
}
2745
}

scripts/build.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ const interpolationFunctions = [
77
"blur",
88
"brightness",
99
"contrast",
10-
"drop-shadow",
10+
"dropShadow",
1111
"grayscale",
12-
"hue-rotate",
12+
"hueRotate",
1313
"invert",
14-
"opacity",
1514
"saturate",
1615
"sepia",
1716
];
@@ -22,14 +21,12 @@ function buildDistFile(filename) {
2221
corePlugins: false,
2322
plugins: [require("../src/index.js")],
2423
theme: {
25-
filter: {
26-
...interpolationFunctions.reduce((acc, interpolationFunction) => {
27-
acc[interpolationFunction] = {
24+
filterFunctions: {
25+
...interpolationFunctions.reduce((accumulator, interpolationFunction) => {
26+
accumulator[interpolationFunction] = {
2827
1: "1",
29-
2: "2",
30-
3: "3",
3128
};
32-
return acc;
29+
return accumulator;
3330
}, {}),
3431
},
3532
},
@@ -56,6 +53,6 @@ function buildDistFile(filename) {
5653

5754
console.info("Building CSS...");
5855

59-
Promise.all([buildDistFile("filter")]).then(() => {
56+
Promise.all([buildDistFile("dist")]).then(() => {
6057
console.log("Finished building CSS.");
6158
});

src/index.js

+30-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const plugin = require("tailwindcss/plugin");
22

3+
const camelize = (s) => s.replace(/-./g, (x) => x.toUpperCase()[1]);
4+
35
const interpolationFunctions = [
46
"blur",
57
"brightness",
@@ -8,76 +10,74 @@ const interpolationFunctions = [
810
"grayscale",
911
"hue-rotate",
1012
"invert",
11-
"opacity",
1213
"saturate",
1314
"sepia",
1415
];
1516

16-
const interpolationFunctionsPublicApiNames = {
17-
opacity: "filter-opacity",
18-
};
19-
20-
const getPublicApiName = (key) => interpolationFunctionsPublicApiNames[key] || key;
21-
2217
const filterPlugin = plugin(
2318
function ({ addUtilities, theme, variants, e }) {
2419
addUtilities(
2520
[
2621
{
27-
".filter, .backdrop": {
28-
...interpolationFunctions.reduce((acc, interpolationFunction) => {
29-
acc[`--tw-filter-${interpolationFunction}`] = "var(--tw-empty, /*!*/ /*!*/)";
30-
return acc;
31-
}, {}),
22+
".filter,.backdrop": {
23+
"--tw-filter-functions": [
24+
"blur(var(--tw-filter-blur, 0))",
25+
"brightness(var(--tw-filter-brightness, 1))",
26+
"contrast(var(--tw-filter-contrast, 1))",
27+
"drop-shadow(var(--tw-filter-drop-shadow, 0 0))",
28+
"grayscale(var(--tw-filter-grayscale, 0))",
29+
"hue-rotate(var(--tw-filter-hue-rotate, 0))",
30+
"invert(var(--tw-filter-invert, 0))",
31+
"saturate(var(--tw-filter-saturate, 1))",
32+
"sepia(var(--tw-filter-sepia, 0))",
33+
].join(" "),
3234
},
3335
".filter": {
34-
filter: interpolationFunctions
35-
.map((interpolationFunction) => `var(--tw-filter-${interpolationFunction})`)
36-
.join(" "),
36+
filter: "var(--tw-filter-functions)",
3737
},
3838
".backdrop": {
39-
backdropFilter: interpolationFunctions
40-
.map((interpolationFunction) => `var(--tw-filter-${interpolationFunction})`)
41-
.join(" "),
39+
backdropFilter: "var(--tw-filter-functions)",
4240
},
43-
".no-filter": {
41+
".filter-none": {
4442
filter: "none",
4543
},
46-
".no-backdrop": {
44+
".backdrop-none": {
4745
backdropFilter: "none",
4846
},
4947
},
5048
...interpolationFunctions.map((interpolationFunction) => {
51-
const themeConfig = theme("filter") ? theme("filter")[interpolationFunction] || {} : {};
52-
return Object.entries(themeConfig).map(([key, value]) => {
49+
const themeFunctionConfig = theme("filterFunctions")
50+
? theme("filterFunctions")[camelize(interpolationFunction)] || {}
51+
: {};
52+
53+
return Object.entries(themeFunctionConfig).map(([key, value]) => {
5354
return {
54-
[`.${e(`${getPublicApiName(interpolationFunction)}-${key}`)}`]: {
55-
[`--tw-filter-${interpolationFunction}`]: `${interpolationFunction}(${value})`,
55+
[`.${e(`${interpolationFunction}-${key}`)}`]: {
56+
[`--tw-filter-${interpolationFunction}`]: `${value}`,
5657
},
5758
};
5859
});
5960
}),
6061
],
61-
variants("filter")
62+
variants("filterFunctions")
6263
);
6364
},
6465
{
6566
theme: {
66-
filter: {
67+
filterFunctions: {
6768
blur: {},
6869
brightness: {},
6970
contrast: {},
70-
"drop-shadow": {},
71+
dropShadow: {},
7172
grayscale: {},
72-
"hue-rotate": {},
73+
hueRotate: {},
7374
invert: {},
74-
opacity: {},
7575
saturate: {},
7676
sepia: {},
7777
},
7878
},
7979
variants: {
80-
filter: ["responsive", "hover"],
80+
filterFunctions: ["responsive", "hover"],
8181
},
8282
}
8383
);

0 commit comments

Comments
 (0)