Today we’re building one of the most common elements in web design — a lefthand sidebar for navigation — using Tailwind CSS and Stimulus.js.

Our sidebar will always take up 100% of the height of the page and we’ll be able to expand and collapse the sidebar by clicking a button. The whole thing will be accomplished with just a bit of Stimulus for the expand/collapse action and a small amount of Tailwind in our markup.

Here’s what it will look like when we’re finished.

To accomplish this, we’ll start with a plain HTML file, pull in Tailwind CSS to make things look nice, and use Stimulus for interactivity.

I’m writing this assuming a solid understanding of HTML and CSS, and some level of comfort with JavaScript.

If you’ve never seen Tailwind before, some of the classes we add for styling might feel a little odd. You don’t need any knowledge of how Stimulus works, but if you’re brand new you might want to read the Stimulus Handbook to help solidify some concepts as we go.

You can find the complete code for this project on Github.

Let’s dive in.

  • Project setup
  • Building a static sidebar
  • Creating the sidebar Stimulus controller
  • Connecting to the DOM
  • Wrapping up

#tailwind #tailwind-css #javascript #stimulusjs #stimulus

Building a collapsible sidebar with Stimulus and Tailwind CSS
5.80 GEEK