Skip to content

Commit c6f5078

Browse files
authored
Merge pull request #10 from mrcelo/feat/add-customization-options
[Feature] Add customization options
2 parents 9bb8a28 + 5bc01c5 commit c6f5078

File tree

2 files changed

+85
-24
lines changed

2 files changed

+85
-24
lines changed

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,38 @@ module.exports = {
2727
// ...
2828
},
2929
plugins: [
30-
require('tailwindcss-question-mark'),
30+
require("tailwindcss-question-mark"),
3131
// ...
3232
],
33-
}
33+
};
3434
```
3535

3636
## Usage
3737

3838
Simply add the `?` utility class to any element that you'd like to highlight.
3939

40-
**Demo**: https://play.tailwindcss.com/fXhD65EpG4?layout=horizontal
40+
**Demo**: https://play.tailwindcss.com/fXhD65EpG4?layout=horizontal
41+
42+
## Customizing
43+
44+
Below is an example of how you can customize the plugin with the available configuration options and their defaults.
45+
46+
```js
47+
// tailwind.config.js
48+
module.exports = {
49+
theme: {
50+
// ...
51+
},
52+
plugins: [
53+
require("tailwindcss-question-mark")({
54+
animationDuration: "0.6s",
55+
enableAnimation: true,
56+
highlightColorStart: "#f16bc9",
57+
highlightColorEnd: "#f71fb6",
58+
widthStart: "8px",
59+
widthEnd: "12px",
60+
}),
61+
// ...
62+
],
63+
};
64+
```

src/index.js

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,59 @@
1-
const plugin = require('tailwindcss/plugin')
1+
const plugin = require("tailwindcss/plugin");
22

3-
module.exports = plugin(function({ addUtilities, e }) {
4-
addUtilities({
5-
[`.${e('?')}`]: {
6-
'outline-style': 'solid',
7-
'animation': `${e('?')}wobble 0.6s ease-in-out alternate infinite`
8-
},
9-
[`@keyframes ${e('?')}wobble`]: {
10-
'0%': {
11-
'outline-width': '4px',
12-
'outline-color': '#f16bc9',
13-
'box-shadow': 'inset 4px 4px #f16bc9, inset -4px -4px #f16bc9'
14-
},
15-
'100%': {
16-
'outline-width': '6px',
17-
'outline-color': '#f71fb6',
18-
'box-shadow': 'inset 6px 6px #f71fb6, inset -6px -6px #f71fb6'
19-
},
20-
}
21-
});
22-
});
3+
function parsePx(input, defaultValue) {
4+
let value = input.match(/\d+px/);
5+
if (value) {
6+
return parseInt(value[0], 10);
7+
}
8+
return defaultValue;
9+
}
10+
11+
module.exports = plugin.withOptions(
12+
({
13+
animationDuration = "0.6s",
14+
enableAnimation = true,
15+
highlightColorStart = "#f16bc9",
16+
highlightColorEnd = "#f71fb6",
17+
widthStart = "8px",
18+
widthEnd = "12px",
19+
} = {}) => {
20+
return function ({ addUtilities, e }) {
21+
const ANIMATION_NAME = "wobble";
22+
const OUTLINE_STYLE = "solid";
23+
24+
const widthStartPx = `${parsePx(widthStart, 8) / 2}px`;
25+
const widthEndPx = `${parsePx(widthEnd, 12) / 2}px`;
26+
27+
const boxShadowStart = `inset ${widthStartPx} ${widthStartPx} ${highlightColorStart}, inset -${widthStartPx} -${widthStartPx} ${highlightColorStart}`;
28+
const boxShadowEnd = `inset ${widthEndPx} ${widthEndPx} ${highlightColorEnd}, inset -${widthEndPx} -${widthEndPx} ${highlightColorEnd}`;
29+
30+
const animation = enableAnimation
31+
? `${e(
32+
"?"
33+
)}${ANIMATION_NAME} ${animationDuration} ease-in-out alternate infinite`
34+
: "none";
35+
36+
addUtilities({
37+
[`.${e("?")}`]: {
38+
"outline-style": OUTLINE_STYLE,
39+
"outline-width": widthStartPx,
40+
"outline-color": highlightColorStart,
41+
"box-shadow": boxShadowStart,
42+
animation: animation,
43+
},
44+
[`@keyframes ${e("?")}${ANIMATION_NAME}`]: {
45+
"0%": {
46+
"outline-width": widthStartPx,
47+
"outline-color": highlightColorStart,
48+
"box-shadow": boxShadowStart,
49+
},
50+
"100%": {
51+
"outline-width": widthEndPx,
52+
"outline-color": highlightColorEnd,
53+
"box-shadow": boxShadowEnd,
54+
},
55+
},
56+
});
57+
};
58+
}
59+
);

0 commit comments

Comments
 (0)