├── LICENSE ├── README.md ├── index.js ├── package-lock.json └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2010-2020 Google, Inc. http://angularjs.org 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > Automatic alpha variants for your Tailwind CSS colors based on your opacity config 2 | 3 | ## Why? 4 | 5 | Tailwind only supports opacity but sometimes you need to apply semi-transparent background/border or text. 6 | 7 | You could tweak your Tailwind configuration like this 8 | 9 | ```javascript 10 | module.exports = { 11 | theme: { 12 | colors: { 13 | primary: "#2b2e4a", 14 | "primary-10": "rgba(43,46,74, 0.1)", 15 | "primary-20": "rgba(43,46,74, 0.2)", 16 | "primary-75": "rgba(43,46,74, 0.75)", 17 | // ... 18 | }, 19 | // ... 20 | }, 21 | }; 22 | ``` 23 | 24 | But it's repetitive, confusing and error prone. We can do better. 25 | 26 | ## Install 27 | 28 | `npm install --save-dev tailwind-color-alpha` 29 | 30 | ```javascript 31 | module.exports = { 32 | // ... 33 | plugins: [require("tailwind-color-alpha")()], 34 | }; 35 | ``` 36 | 37 | The plugin will automatically use your colors and opacity config to generate all the corresponding rgba values for your text, background, border, fill and stroke utility classes respecting your variant settings for each. 38 | 39 | ## Examples 40 | 41 | ```javascript 42 | module.exports = { 43 | theme: { 44 | colors: { 45 | primary: "#2b2e4a", 46 | }, 47 | opacity: { 48 | "25": ".25", 49 | "50": "0.5", 50 | }, 51 | }, 52 | }; 53 | ``` 54 | 55 | The configuration above yields the following utilities: 56 | 57 | ```css 58 | .bg-primary-alpha-25 { 59 | background-color: rgba(43, 46, 74, 0.25); 60 | } 61 | .bg-primary-alpha-50 { 62 | background-color: rgba(43, 46, 74, 0.5); 63 | } 64 | .text-primary-alpha-25 { 65 | color: rgba(43, 46, 74, 0.25); 66 | } 67 | .text-primary-alpha-50 { 68 | color: rgba(43, 46, 74, 0.5); 69 | } 70 | .border-primary-alpha-25 { 71 | border-color: rgba(43, 46, 74, 0.25); 72 | } 73 | .border-primary-alpha-25 { 74 | border-color: rgba(43, 46, 74, 0.5); 75 | } 76 | .hover\:text-primary-alpha-25:hover { 77 | color: rgba(43, 46, 74, 0.25); 78 | } 79 | .focus\:text-primary-alpha-50:focus { 80 | color: rgba(43, 46, 74, 0.5); 81 | } 82 | 83 | /* and more... */ 84 | ``` 85 | 86 | Notice that a color named `red.default` will end up generating `text-red` and so we follow Tailwind's convention with `bg-red-alpha-${opacity}` 87 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const Color = require("color"); 2 | 3 | module.exports = function() { 4 | const PREFIXES = { 5 | backgroundColor: ["bg"], 6 | borderColor: ["border", "border-t", "border-r", "border-b", "border-l"], 7 | fill: ["fill"], 8 | stroke: ["stroke"], 9 | textColor: ["text"] 10 | }; 11 | 12 | const PROPERTIES = { 13 | backgroundColor: ["backgroundColor"], 14 | borderColor: [ 15 | "borderColor", 16 | "borderTopColor", 17 | "borderRightColor", 18 | "borderBottomColor", 19 | "borderLeftColor" 20 | ], 21 | fill: ["fill"], 22 | stroke: ["stroke"], 23 | textColor: ["color"] 24 | }; 25 | 26 | return function({ addUtilities, theme, variants }) { 27 | let colors = theme("colors", []); 28 | let opacities = theme("opacity", []); 29 | for (const [key, value] of Object.entries(colors)) { 30 | const colorGroup = typeof value === "string" ? { [key]: value } : value; 31 | for (const [colorName, colorValue] of Object.entries(colorGroup)) { 32 | for (const o in opacities) { 33 | const colorVariant = 34 | typeof value === "string" || colorName === "default" 35 | ? key 36 | : `${key}-${colorName}`; 37 | for (const [variant, properties] of Object.entries(PREFIXES)) { 38 | const newColors = {}; 39 | properties.forEach((property, index) => { 40 | newColors[`.${property}-${colorVariant}-alpha-${o}`] = { 41 | [`${PROPERTIES[variant][index]}`]: Color(colorValue) 42 | .alpha(opacities[o]) 43 | .string() 44 | }; 45 | }); 46 | addUtilities(newColors, variants(variant)); 47 | } 48 | } 49 | } 50 | } 51 | }; 52 | }; 53 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tailwind-color-alpha", 3 | "version": "1.0.2", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "color": { 8 | "version": "3.1.2", 9 | "resolved": "https://registry.npmjs.org/color/-/color-3.1.2.tgz", 10 | "integrity": "sha512-vXTJhHebByxZn3lDvDJYw4lR5+uB3vuoHsuYA5AKuxRVn5wzzIfQKGLBmgdVRHKTJYeK5rvJcHnrd0Li49CFpg==", 11 | "requires": { 12 | "color-convert": "^1.9.1", 13 | "color-string": "^1.5.2" 14 | } 15 | }, 16 | "color-convert": { 17 | "version": "1.9.3", 18 | "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", 19 | "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", 20 | "requires": { 21 | "color-name": "1.1.3" 22 | } 23 | }, 24 | "color-name": { 25 | "version": "1.1.3", 26 | "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", 27 | "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" 28 | }, 29 | "color-string": { 30 | "version": "1.5.3", 31 | "resolved": "https://registry.npmjs.org/color-string/-/color-string-1.5.3.tgz", 32 | "integrity": "sha512-dC2C5qeWoYkxki5UAXapdjqO672AM4vZuPGRQfO8b5HKuKGBbKWpITyDYN7TOFKvRW7kOgAn3746clDBMDJyQw==", 33 | "requires": { 34 | "color-name": "^1.0.0", 35 | "simple-swizzle": "^0.2.2" 36 | } 37 | }, 38 | "is-arrayish": { 39 | "version": "0.3.2", 40 | "resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz", 41 | "integrity": "sha512-eVRqCvVlZbuw3GrM63ovNSNAeA1K16kaR/LRY/92w0zxQ5/1YzwblUX652i4Xs9RwAGjW9d9y6X88t8OaAJfWQ==" 42 | }, 43 | "simple-swizzle": { 44 | "version": "0.2.2", 45 | "resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz", 46 | "integrity": "sha1-pNprY1/8zMoz9w0Xy5JZLeleVXo=", 47 | "requires": { 48 | "is-arrayish": "^0.3.1" 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tailwind-color-alpha", 3 | "version": "1.0.4", 4 | "description": "Automatic alpha variants for your Tailwind CSS colors based on your opacity config", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/soueuls/tailwind-color-alpha" 12 | }, 13 | "keywords": [ 14 | "tailwind", 15 | "tailwindcss", 16 | "plugin", 17 | "tailwindcss-plugin", 18 | "css", 19 | "alpha" 20 | ], 21 | "author": "Swann Polydor ", 22 | "license": "MIT", 23 | "dependencies": { 24 | "color": "^3.1.2" 25 | } 26 | } 27 | --------------------------------------------------------------------------------