Tailwind CSS Hocus Plugin

A few days after adding this snippet, I published this plugin as an npm package which you can now find under the name tailwindcss-hocus, and on GitHub here.

A plugin that's great for lazy people like me who make their :hover and :focus states basically they same. As anyone who's used Tailwind CSS for a while might expect, it adds the hocus: variant which will add the specificd class on hover and focus. It also adds hocus-within, group-hocus, and group-hocus-within which are similar but based of slightly different existing variants.

tailwind.config.js
javascript
const plugin = require('tailwindcss/plugin')
const hocusPlugin = plugin(({ addVariant }) => {
addVariant('hocus', ['&:hover', '&:focus'])
addVariant('hocus-within', ['&:hover', '&:focus-within'])
addVariant('group-hocus', [
':merge(.group):hover &',
':merge(.group):focus &',
])
addVariant('group-hocus-within', [
':merge(.group):hover &',
':merge(.group):focus-within &',
])
})
/**
* @type {import('tailwindcss').Config}
*/
module.exports = {
// ...the rest of your config
plugins: [hocusPlugin],
}

Usage & CSS Output Example

To use this plugin, you use it like any other variant - prefix an existing Tailwind class with hocus: or one of the other variants:

high-quality-code.html
html
<button className="hocus:text-red-700">Click Me!</button>

Here's what the Tailwind compiler outputs for the HTML above:

output-styles.css
css
.hocus\:text-red-700:hover,
.hocus\:text-red-700:focus {
--tw-text-opacity: 1;
color: rgb(185 28 28 / var(--tw-text-opacity));
}

Example

  • This website (in the past) - Inspect element on the link!

    Note: The latest version of the linked tailwind.config.js uses the npm package.

Created 09/19/22 • Updated 09/22/22
Found a mistake, or want to suggest an improvement? Edit on GitHub here
and see edit history here

License & Attribution

Attribution

You should place the following attribution (and probably the license below it) above your copy of the code, but 🤷‍♂️.

This snippet "Tailwind CSS Hocus Plugin" from "https://soorria.com/snippets/tailwindcss-hocus" 
was created by Soorria Saruva (https://soorria.com) and is covered under the 
MIT license found here: https://github.com/soorria/soorria.com/blob/master/LICENSE.

License

If you want, you can (and probably should) include the entire license below. I'm not a lawyer, though so idrk.

MIT License Copyright (c) 2022 Soorria Saruva

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.