Converting Laravel Spark navigation to Tailwind CSS
- I find it a bit clunky and restricting, personally
- I'd built a sales site for MemberScore (at memberscore.io), and I wanted the actual app (at app.memberscore.io) to match the sales site appearance.
I haven't converted the entire app to use Tailwind (yet?), but the nav is now converted and fully functional.
Here's how I did it
Essentially, we're going to recreate all the nav-related Spark views, and swap out the various css classes to take advantage of Tailwind, removing Bootstrap-related ones and instead using Tailwind classes to get the nav looking how we want.
The first step is to install Tailwind. If you're not sure whether you'll want to make this change permanently, I highly recommend including Tailwind via CDN as referenced in the docs, rather than going through the relative hassle of including it via npm.
If you're ready to commit, you'll need to include
tailwindcss in your
package.json file. I also needed to bump the version of
laravel-mix I was loading, from
^2.0. I also included laravel-mix-tailwind.
webpack.mix.js file, I was then able to follow the instructions provided by laravel-mix-tailwind, and get Tailwind compiling ok after adding the tailwind directives to the end of my
You'll notice the
bootstrap-overrides.less file there; I had to make a couple of additions to override some default Bootstrap files that didn't play well with Tailwind:
That may not be the Right Way™ to do this, but it's working ok for me.
Next, I had to figure out how to get two things working:
- The toggling of the main nav menu on small screens
- The account switcher/settings/kiosk etc. dropdown in the nav
I ended up creating a single file Vue template called
MainNav.vue and using it as an inline template; this let me still use blade helpers within it. Here's the template:
And where it's used in
dropdownOpen props, and associated
toggleDropdown() methods; those handle showing/hiding the two items mentioned previously. You can see there's a button that calls
toggleNav(), and the main div containing the nav items applies a class, either
hidden, depending on the current value of
navOpen (which is what
The dropdown toggling is done exactly the same; you can see
dropdown-toggle.blade.php is included in the main nav file. For some reason, I ended up with
dropdown-toggle.blade.php instead other way around, but the names don't really matter.
That's the general gist of how I did this. I'm sure this isn't all done as perfectly as it could be, but it's functional, looks how I want it, and I understand it – and those three things go a long way in production.
If you have any questions, or suggestions, shoot me an email at firstname.lastname@example.org. I'd love to hear from you.