Created
November 18, 2021 18:11
-
-
Save Accudio/f1fbda4e1fa76239baf06583138c1217 to your computer and use it in GitHub Desktop.
Custom Property colour plugin for Tailwind. Used in this case where `.focus-pink` would set `--focus-colour` but could be anything
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const plugin = require('tailwindcss/plugin') | |
const baseClass = '.focus' | |
const property = '--focus-colour' | |
const getColours = (colours, prefix, e) => { | |
let utils = {} | |
for (const key in colours) { | |
if (typeof colours[key] === 'object') { | |
const subColours = getColours(colours[key], `-${key}`, e) | |
utils = { | |
...utils, | |
...subColours | |
} | |
continue | |
} | |
let classSuffix = prefix | |
if (key !== 'DEFAULT') classSuffix += `-${key}` | |
utils[baseClass + e(classSuffix)] = { | |
[property]: colours[key] | |
} | |
} | |
return utils | |
} | |
const focusColour = plugin(({ addUtilities, theme, e }) => { | |
addUtilities(getColours(theme('colors', {}), '', e)) | |
}) | |
module.exports = focusColour |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
plugins: [ | |
require('./1-plugin.js') | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const plugin = require('tailwindcss/plugin') | |
module.exports = { | |
plugins: [ | |
plugin(function({ addUtilities, theme, e }) { | |
const baseClass = '.focus' | |
const property = '--focus-colour' | |
function getColours(colours, prefix = '') { | |
let utils = {} | |
for (const key in colours) { | |
if (typeof colours[key] === 'object') { | |
const subColours = getColours(colours[key], `-${key}`) | |
utils = { | |
...utils, | |
...subColours | |
} | |
continue | |
} | |
let classSuffix = prefix | |
if (key !== 'DEFAULT') classSuffix += `-${key}` | |
utils[baseClass + e(classSuffix)] = { | |
[property]: colours[key] | |
} | |
} | |
return utils | |
} | |
addUtilities(getColours(theme('colors', {}))) | |
}) | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment