Performance: Creating custom utilities with addUtilities
MEDIUM IMPACT
This affects the CSS bundle size and how quickly styles are applied during page load.
module.exports = {
plugins: [
function({ addUtilities, e, theme, variants }) {
const shadows = theme('boxShadow')
const utilities = Object.entries(shadows).map(([name, value]) => ({
[`.${e(`custom-shadow-${name}`)}`]: { 'box-shadow': value }
}))
addUtilities(utilities, variants('boxShadow'))
}
]
}module.exports = {
plugins: [
function({ addUtilities }) {
addUtilities({
'.custom-shadow': {
'box-shadow': '0 4px 6px rgba(0,0,0,0.1)',
},
'.custom-shadow-lg': {
'box-shadow': '0 10px 15px rgba(0,0,0,0.2)',
},
'.custom-shadow-xl': {
'box-shadow': '0 20px 25px rgba(0,0,0,0.3)',
},
// ... many more similar utilities
})
}
]
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual many custom utilities | No extra DOM nodes | 0 reflows | High paint cost due to large CSS | [X] Bad |
| Dynamic utilities from theme | No extra DOM nodes | 0 reflows | Lower paint cost with smaller CSS | [OK] Good |