Tailwind provides a plugin
function as an exposed API for customizing reusable styles. The plugin
function can be imported from the tailwindcss/plugin
module and invoked in the plugins
array in the tailwind.config.js
.
In this note, we will create a reusable neon effect style that can be easily applied using the classname neon-*
with different colors. For example, the div box below implements a vibrant box-shadow
style by simply using the classname neon-amber
or by using neon-primary
if you declare the primary color in the theme > extends > colors
object in tailwind.config.js
.
Implementation
The plugin function receives an argument that can be destructured into several helper functions. In our example, we are going to use only two of those functions, namely theme
and addUtilities
. You can view all the helper functions in the Tailwind official documentation.
theme()
retrieves styles from Tailwind’s default theme. In our case, we are going to retrieve all the colors from the default theme.addUtilities()
can be used to register our own static styles.
Now, if you are using the Tailwind CSS IntelliSense extension in your VSCode, you will see our neon-*
classnames appearing in the auto-complete suggestions.
References