Skip to content

Instantly share code, notes, and snippets.

@Accudio
Created November 18, 2021 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Accudio/f1fbda4e1fa76239baf06583138c1217 to your computer and use it in GitHub Desktop.
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
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
module.exports = {
plugins: [
require('./1-plugin.js')
]
}
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