|
| 1 | +module.exports = function ({ addUtilities, theme }) { |
| 2 | + const breakpoints = theme('screens'); |
| 3 | + const userColors = theme('backgroundColorsByBreakpoint', {}); |
| 4 | + const allColors = theme('colors'); |
| 5 | + |
| 6 | + // Function to resolve Tailwind color classes to actual color codes |
| 7 | + // Without this, only rgb values would have to be used instead of |
| 8 | + // tailwind colors like 'red-500' or 'gray-100', etc |
| 9 | + const resolveColor = (colorName) => { |
| 10 | + const [color, shade] = colorName.split('-'); |
| 11 | + return allColors[color] && allColors[color][shade] |
| 12 | + ? allColors[color][shade] |
| 13 | + : colorName; |
| 14 | + }; |
| 15 | + |
| 16 | + const defaultColors = { |
| 17 | + DEFAULT: 'black', |
| 18 | + sm: '#a3e635', |
| 19 | + md: '#f59e0b', |
| 20 | + lg: '#ea580c', |
| 21 | + xl: '#991b1b', |
| 22 | + '2xl': '#881337', |
| 23 | + }; |
| 24 | + |
| 25 | + // Merge user-defined colors with defaults, in order to override |
| 26 | + // default colors with user-defined colors. |
| 27 | + const finalColors = { ...defaultColors, ...userColors }; |
| 28 | + |
| 29 | + let newUtilities = { |
| 30 | + '.bg-breakpoint': { |
| 31 | + backgroundColor: finalColors.DEFAULT, |
| 32 | + }, |
| 33 | + }; |
| 34 | + |
| 35 | + Object.entries(breakpoints).forEach(([breakpoint]) => { |
| 36 | + if (finalColors[breakpoint]) { |
| 37 | + const colorCode = resolveColor(finalColors[breakpoint]); |
| 38 | + if (!newUtilities[`@screen ${breakpoint}`]) { |
| 39 | + newUtilities[`@screen ${breakpoint}`] = {}; |
| 40 | + } |
| 41 | + newUtilities[`@screen ${breakpoint}`]['.bg-breakpoint'] = { |
| 42 | + backgroundColor: colorCode, |
| 43 | + }; |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + addUtilities(newUtilities, ['responsive']); |
| 48 | +}; |
0 commit comments