In this episode, we tackle a bit of code duplication in our app. Right now, our mentionable functionality (you know, the little @-mention autocompletion) is sprinkled in multiple places—like the main input and the edit section. The problem? Whenever we tweak how mentions work, we need to change it everywhere, and things fall out of sync.
So, our solution: let's create a simple Alpine plugin! You'll see how we extract this shared logic, move it into a dedicated mentionable
plugin under a new plugins
directory, and then register it with Alpine using Alpine.plugin()
. There’s a little trickiness because we’re using Livewire, so we have to manually bundle and import Alpine & Livewire, making sure the plugin registration runs before Livewire starts.
Once the plugin is set up, we move the core logic (previously inside x-init
) into a custom Alpine directive—x-mentionable
. This means we can now just slap x-mentionable
on any <textarea>
that needs mentions, and boom—no more duplicate logic! Plus, any tweaks to how mentioning works only need to be made in the plugin, instantly updating everywhere the directive is used.
By the end, we’ve refactored our mentionable logic to be DRY (Don’t Repeat Yourself). It’s tidier, easier to maintain, and sets the stage for future improvements. Nice!