From b4aeb770dafaf4455d8c13228d037a5df66d0e97 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Wed, 11 Sep 2024 00:50:41 +0200 Subject: [PATCH] feat: add support for tabs, details and accordions --- index.html | 67 ++++++++++++++++++++++++++++++++++++ src/_typography.scss | 4 ++- src/components/_buttons.scss | 40 +++++++++++++++++++-- src/components/_well.scss | 41 +++++++++++++++++++++- 4 files changed, 147 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 1ae9d43..fd79642 100644 --- a/index.html +++ b/index.html @@ -133,6 +133,73 @@ +
+
+ Graduation Requirements +

+ Requires 40 credits, including a passing grade in health, geography, + history, economics, and wood shop. +

+
+
+ System Requirements +

+ Requires a computer running an operating system. The computer must have some + memory and ideally some kind of long-term storage. An input device as well + as some form of output device is recommended. +

+
+
+ Job Requirements +

+ Requires knowledge of HTML, CSS, JavaScript, accessibility, web performance, + privacy, security, and internationalization, as well as a dislike of + broccoli. +

+
+
+ +
+
+ Tag +
+

Imagine an accordion-like element with native keyboard support that doesn't require javascript for control.

+

Enter the details tag.

+
+ The details tag +

This is a details tag with content

+
+

The details tag gives us a simple accordion with no scripts—that's great! But it feels like it could be something more...imagine if it could do tabs.

+
+
+
+ Tabs ? +
+

Tabs and accordions work in technically the same way, although tabs typically require a shared parent container and containers to build out the tabs and content separate from each other.

+

Interactivity is another difference: the ways accordions or tabs are toggled. With accordions it's common to be able to open and close them individually, but with tabs there must always be one, and only one, open.

+

At first glance there doesn't seem to be a way to make this work. The structure isn't right. The layout isn't right. The functionality isn't quite right. But each of those problems can be solved if we look a bit closer.

+
+
+
+ CSS +
+

With a little help from the display: contents; property, we can transform a group of discrete details tags with tab and content area elements as children, into a group of the tab and content area elements from all the details elements mixed together.

+

Say you had 3 small buckets of red and blue balls. The buckets can interact with each other or bang together, but the balls inside each bucket can't interact with the balls from the other buckets.

+

You could think of what we are doing like pouring all of the small buckets into a larger bucket. Now all of the balls can interact with each other and we can organize them by color in the bigger bucket. With display: contents; it's as if the details tag isn't there at all—at least visually.

+

Now that all the tab and content area elements can interact with each other in the flow of the page, we can use display: flex; and order to make the tabs flow nicely but also be separate from the content area.

+

And that's what you see here! Open up the inspector and have a look.

+
+
+
+ Javascript +
+

So you might have thought we could get by without any JS, but we need it a little. Luckily it's the type of script that you can write once and reuse without reconfiguration.

+

The default functionality of the details tab means that it can be toggled open or closed. That's not what we want for tabs. Each details tag must be manually closed and then another opened. We want a tab to stay open and then close when another is picked.

+

By watching for attribute changes and blocking keyboard and mouse input from toggling a details tab when a tab is open, we can emulate the expected tab behavior.

+

Then it's just a matter of toggling the sibling details tags when another is selected.

+
+
+