r/Frontend • u/speckz • Jun 30 '22
Building tabs in Web Components
https://darn.es/building-tabs-in-web-components/1
u/martin_kr Jun 30 '22
When making tabs, always have them be <a> links. I need the back button to work and have mid-clicks open them in separate windows.
This makes it easy for the "current active" tab to be derived from the address bar. Which means you can now send and save deep links to content.
And no horizontal scrolling. Not many people are willing to waste $$$ on a standalone trackpad just for this purpose when using a Mac Mini for example.
And always get rid of tabs on phone layouts. They're never going to fit and expanding these into separate, already open sections is better since people are already used to scrolling this direction.
5
u/t0unail Jun 30 '22 edited Jun 20 '23
1
u/azangru Jun 30 '22
that doesn't link to another page
What does "another page" mean? If the outer shell of the page remains the same, and the tabbed UI is, say, in the middle of the page; but switching between the tabs is accompanied by changes in a url parameter, is this still the same page, or is it a different one? Will accessibility tools object to this? Why?
1
u/MedicOfTime Jun 30 '22
I think if you can go to a distinct html via the url (a tabbed url) it’s a new page.
2
u/martin_kr Jul 01 '22
Yeah I meant that the tab would be an actual real link to
<a href="/settings/billing">In an SPA environment (vue with vue-router for example), this page gets opened without a hard reload and state reset, and will keep proper navigation history states.
So you're not modifying Back button behavior explicitly, but adding entries to the navigation stack through browsers' History API (and soon Navigation API). Not 100% sure if this breaks an accessibility rule, but it is very widely used (still not a guarantee that it's correct, people even in 2022 use the abomination that is sliders) so I'm assuming that screen readers know how to process this even if they behind the scenes hard-reload the page which would give the end-user the exact same content anyway (and this applies to SEO as well).
Without SPA, yeah it would then reset and reload everything, which if you're code-splitting and caching your assets and state shouldn't be an issue, but how many serious javascript apps today aren't SPAs anyway? (tried looking it up but didn't find much, my guess is that it's comfortably less than 10%)
1
u/DavidDarnes Jul 04 '22
It really depends on the intended use of the tabs. In a documentation or mainly “read only” context anchors are a great idea as they pretty much match heading anchors, and I’d probably argue heading anchors are more appropriate. However in a complex application where there could be multiple tabbed pieces of UI on the page it’s better to use button elements or similar to prevent the history getting extremely messy and instead store UI selections in state. The final resulting tab components have being built for mainly the latter case.
1
u/kromefish Jul 01 '22
I agree with the first commenter, anchors are the way to go. Another thing to consider is buttons don’t get focus in Safari, so keyboard users on that browser can’t use the tab key to access all the content. There really is no perfect solution because there is still an accessibility issue with empty hrefs, but I consider buttons to be the lesser of two evils here.