Mayank RSS

Simple tabs

If you make websites, chances are you might need tabs (aka tabpanels) every now and then. I know I do.

So I decided to create a simple implementation of tabs in vanilla JavaScript. The goal? Styleable and accessible.

30 minutes later, this is what I ended up with. (Yes, I placed the code for these tabs within said tabs.)

<the-tabs>
	<div role='tablist'>
		<button role='tab' aria-selected='true'></button>
		<button role='tab'></button>
		<button role='tab'></button>
	</div>

	<div role='tabpanel'></div>
	<div role='tabpanel' hidden></div>
	<div role='tabpanel' hidden></div>
</the-tabs>

That’s less than 100 lines of simple, readable JavaScript, with no extra API, and it reuses semantic selectors for styling. Pretty neat, eh?

One day I might expand this post to explain some of the design decisions, but honestly there isn’t much to explain if you’re familiar with the APG Tabs pattern. The most interesting bit in my code is perhaps the use of a setter to automatically perform any side effects (updating attributes, moving focus, etc).

If you want something installable, you can find it on npm in all its glory.

Preview

Assuming you’re not reading this in an RSS reader, here’s some quick and dirty screen recordings showing the tabs in action: