Skip to content

Commit f15a1ff

Browse files
authored
Ability to change color of outline using inline styles (#11)
* Initial commit post-fork * Add function to use semantic colors to distinguish between different elements * Update README.md * Delete package-lock.json * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Add comments before PR * Formatting * - Removed comments - Reverted default widthStart to 8px and widthEnd to 12px
1 parent b4a8587 commit f15a1ff

File tree

2 files changed

+64
-33
lines changed

2 files changed

+64
-33
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A plugin that provides a helpful `?` dev time utility.
66

77
![example](https://user-images.githubusercontent.com/2526/248293688-da86d4e7-0955-40fb-8fb2-f892b270a9a8.gif)
88

9-
## Installation
9+
## [Installation](#installation "Goto Installation")
1010

1111
Install the plugin from npm:
1212

@@ -33,13 +33,19 @@ module.exports = {
3333
};
3434
```
3535

36-
## Usage
36+
## [Usage](#usage "Goto Usage")
3737

3838
Simply add the `?` utility class to any element that you'd like to highlight.
39+
By default, the utility will animate the element with a pink highlight.
40+
41+
Optionally, you can specify a color by using the `?-{color}` utility class with `{color}` being the semantic name. For
42+
example, `?-blue` will highlight the element with a blue color.
43+
44+
![example](https://i.ibb.co/LvXtxLG/twcssqm-colors.gif)
3945

4046
**Demo**: https://play.tailwindcss.com/fXhD65EpG4?layout=horizontal
4147

42-
## Customizing
48+
## [Customizing](#customizing "Goto Customizing")
4349

4450
Here's an example of how you can customize the plugin with the available configuration options and their defaults.
4551

src/index.js

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,68 @@ module.exports = plugin.withOptions(
1717
widthStart = "8px",
1818
widthEnd = "12px",
1919
} = {}) => {
20-
return function ({ addUtilities, e }) {
20+
return function ({ addUtilities, e, theme }) {
2121
const ANIMATION_NAME = "wobble";
2222
const OUTLINE_STYLE = "solid";
2323

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%": {
24+
const createUtilitiesForColor = (colorStart, colorEnd, colorName) => {
25+
const className = colorName ? `?-${colorName}` : "?";
26+
const widthStartPx = `${parsePx(widthStart, 8) / 2}px`;
27+
const widthEndPx = `${parsePx(widthEnd, 12) / 2}px`;
28+
29+
const boxShadowStart = `inset ${widthStartPx} ${widthStartPx} ${colorStart}, inset -${widthStartPx} -${widthStartPx} ${colorStart}`;
30+
const boxShadowEnd = `inset ${widthEndPx} ${widthEndPx} ${colorEnd}, inset -${widthEndPx} -${widthEndPx} ${colorEnd}`;
31+
32+
const animation = enableAnimation
33+
? `${e(className)}${ANIMATION_NAME} ${animationDuration} ease-in-out alternate infinite`
34+
: "none";
35+
36+
return {
37+
[`.${e(className)}`]: {
38+
"outline-style": OUTLINE_STYLE,
4639
"outline-width": widthStartPx,
47-
"outline-color": highlightColorStart,
40+
"outline-color": colorStart,
4841
"box-shadow": boxShadowStart,
42+
animation: animation,
4943
},
50-
"100%": {
51-
"outline-width": widthEndPx,
52-
"outline-color": highlightColorEnd,
53-
"box-shadow": boxShadowEnd,
44+
[`@keyframes ${e(className)}${ANIMATION_NAME}`]: {
45+
"0%": {
46+
"outline-width": widthStartPx,
47+
"outline-color": colorStart,
48+
"box-shadow": boxShadowStart,
49+
},
50+
"100%": {
51+
"outline-width": widthEndPx,
52+
"outline-color": colorEnd,
53+
"box-shadow": boxShadowEnd,
54+
},
5455
},
56+
};
57+
};
58+
59+
const defaultUtilities = createUtilitiesForColor(
60+
highlightColorStart,
61+
highlightColorEnd,
62+
);
63+
64+
const customColors = theme("colors");
65+
const customUtilities = Object.keys(customColors).reduce(
66+
(acc, colorName) => {
67+
const colorValueStart =
68+
customColors[colorName][400] || customColors[colorName];
69+
const colorValueEnd =
70+
customColors[colorName][600] || customColors[colorName];
71+
const utilities = createUtilitiesForColor(
72+
colorValueStart,
73+
colorValueEnd,
74+
colorName,
75+
);
76+
return { ...acc, ...utilities };
5577
},
56-
});
78+
{},
79+
);
80+
81+
addUtilities({ ...defaultUtilities, ...customUtilities });
5782
};
58-
}
83+
},
5984
);

0 commit comments

Comments
 (0)