๐ŸŽ›๏ธ Chapter 40: Interactive Semantic Elements

Custom Toggle Components with details

Modern exclusive accordions via the native `name` attribute, `beforematch` search integration, and zero-JS accordion groups.

LEARNING OBJECTIVES โŒต
  • Implement native, zero-JavaScript exclusive accordions using the HTML name attribute on <details>.
  • Understand the browser-level mutual exclusivity mechanics of named disclosure groups.
  • Master in-page searchability (Ctrl+F) integration with the beforematch event and hidden="until-found".
  • Design robust, responsive accordion architectures for enterprise design systems.
๐ŸŽฌ INTERACTIVE VISUAL PIPELINE Core Architecture Simulation
๐ŸŒ
1. Input
Directives & Tags
โš™๏ธ
2. Parse
Tokenizer & AST
๐ŸŒณ
3. Layout
Box Model & Flow
๐ŸŽจ
4. Render
GPU Paint & Composite
PHASE 1: INPUT & DIRECTIVES
Browser receives declarative markup stream, parsing tag tokens and initializing component state.

๐Ÿ“– The Mental Model & Story (Intuitive Foundation)

Imagine a physical car radio dashboard from the 1980s with mechanical preset buttons.

  • When you press button 1 (Jazz 89.5), it pushes in and locks in place.
  • When you later press button 2 (Rock 101.1), a mechanical lever inside the radio automatically pops button 1 back out. Only one radio station can ever be tuned at a time.
  • You didn't need an electronic computer or external wiring to pop button 1 out; the physical mechanical linkage inside the radio housing handled the mutual exclusivity automatically.

For years in web development, creating an "exclusive accordion" (where opening one accordion section automatically closes any currently open section) required JavaScript event listeners, looping through sibling elements with querySelectorAll, and removing attributes manually.

The modern HTML Living Standard introduced the name attribute for <details>. By simply assigning the same name="faq-group" to multiple <details> elements, the browser acts as that mechanical radio linkage: opening one details element automatically closes all other sibling details sharing that name with zero JavaScript.

+-----------------------------------------------------------------------------+
|                     NATIVE EXCLUSIVE ACCORDION (name="faq")                  |
+-----------------------------------------------------------------------------+
|                                                                             |
|  <details name="faq" open>                                                  |
|  โ–ผ What is your SLA uptime?                                                 |
|    "We guarantee 99.99% multi-region uptime."                               |
|                                                                             |
|  <details name="faq">                                                       |
|  โ–ถ How does data replication work?  <-- User clicks this                    |
|                                                                             |
|  <details name="faq">                                                       |
|  โ–ถ Can I cancel anytime?                                                    |
|                                                                             |
+-----------------------------------------------------------------------------+
                                       |
                   (Browser automatically pops previous item closed)
                                       v
+-----------------------------------------------------------------------------+
|  <details name="faq">                                                       |
|  โ–ถ What is your SLA uptime?         <-- Automatically closed by browser!    |
|                                                                             |
|  <details name="faq" open>                                                  |
|  โ–ผ How does data replication work?  <-- Now open                            |
|    "Data is synchronously mirrored across 3 availability zones."            |
|                                                                             |
|  <details name="faq">                                                       |
|  โ–ถ Can I cancel anytime?                                                    |
+-----------------------------------------------------------------------------+

Technical Deep Dive & Specifications

The name Attribute on <details>

The WHATWG HTML Living Standard specifies:

The name content attribute gives the name of the group of details elements that this element belongs to. If multiple <details> elements in the same tree have the same name attribute value, at most one of them can have the open attribute set at any given time.

interface HTMLDetailsElement : HTMLElement {
  [CEReactions] attribute boolean open;
  [CEReactions] attribute DOMString name; // Group name property
};
<!-- Native Exclusive Accordion Group (Zero JavaScript) -->
<details name="account-settings">
  <summary>Personal Information</summary>
  <p>Update your email address and profile photo.</p>
</details>

<details name="account-settings">
  <summary>Security & Password</summary>
  <p>Manage two-factor authentication and passwords.</p>
</details>

<details name="account-settings">
  <summary>Billing & Subscriptions</summary>
  <p>Download recent invoices and payment receipts.</p>
</details>

In-Page Find (Ctrl+F) & the beforematch Event

A major flaw of legacy JavaScript accordions (display: none or CSS height clipping) is that when users press Ctrl+F (Cmd+F on macOS) to search for text on a page, words inside closed accordions are invisible to the browser's find tool.

Modern browser rendering engines solve this with hidden="until-found" and native <details> integration:

  1. When a user searches for text contained inside a collapsed <details> element, the browser engine detects the text match.
  2. The browser dispatches a native beforematch event to the <details> element.
  3. The browser automatically sets open="true" on the matching <details> element, smoothly revealing the text and scrolling the user directly to the highlighted match.
const detailsItem = document.querySelector('details');

detailsItem.addEventListener('beforematch', () => {
  console.log('Search match detected! Browser is auto-expanding this widget.');
});

Pure HTML vs Legacy JavaScript Accordion Comparison

Feature Modern HTML (<details name="...">) Legacy JavaScript Accordion
JavaScript Dependency Zero JavaScript Requires event listeners & query loops
Mutual Exclusivity Built-in via name="group-name" Handled by custom JS state management
SSR / No-JS Resilience 100% functional before scripts execute Non-functional or broken until JS hydrates
Find-in-Page (Ctrl+F) Auto-expands on match (beforematch) Matches are completely missed or hidden
Keyboard Accessibility Native Tab, Enter, Space Requires manual tabindex and aria-expanded

SYS: ACTIVE
HULL: 98%
CORE: STABLE
NET: ONLINE
HTML STARSHIP CODE TERMINAL example.html
LIVE RENDER & DIAGNOSTICS CORE TEMP: 45ยฐC
INSPECTING DOM: VALID
TAGS: SCANNING...

๐Ÿ’ป Interactive Code Playground

Starter Code

Line-by-Line Code Breakdown

  • Lines 73, 87, 101: Applies name="cloud-faq" to all three <details> elements. This binds them into an exclusive group governed by the browser engine.
  • Line 73: The first item specifies open. On page load, this item starts open while the others remain closed.
  • Lines 49โ€“59: Combines the native name attribute with the CSS Grid 0fr to 1fr transition for a zero-JS animated exclusive accordion.
  • No JavaScript Needed: When the user clicks Item 2 or 3, the browser automatically closes the currently open item without a single line of script.

Expected Browser Render Output

  1. Initial Load: Item 1 is expanded with its text visible; Items 2 and 3 are collapsed.
  2. Clicking Item 2: Item 1 smoothly collapses down to 0px, and Item 2 simultaneously expands to reveal its DDoS mitigation description.

SYS: ACTIVE
HULL: 98%
CORE: STABLE
NET: ONLINE
HTML STARSHIP CODE TERMINAL playground.html
LIVE RENDER & DIAGNOSTICS CORE TEMP: 45ยฐC
INSPECTING DOM: VALID
TAGS: SCANNING...

๐Ÿ‹๏ธ Hands-On Exercise

๐ŸŽฏ The Challenge: Build an Enterprise Product Feature Matrix

Construct an enterprise product comparison matrix with two distinct, independent exclusive accordion groups:

  1. Group 1: Infrastructure Architecture (name="infra-group"):
    • Section A: "Global CDN Distribution" (Open by default)
    • Section B: "Serverless Compute Runners"
  2. Group 2: Security & Compliance (name="security-group"):
    • Section A: "SOC 2 Type II Certification" (Open by default)
    • Section B: "HIPAA & GDPR Compliance"
  3. Verify that opening an item in Group 1 does not close items in Group 2, but closes other items within Group 1.

๐Ÿ Starter Code Sandbox

SYS: ACTIVE
HULL: 98%
CORE: STABLE
NET: ONLINE
STARTER CODE SANDBOX exercise.html
LIVE RENDER & DIAGNOSTICS CORE TEMP: 45ยฐC
INSPECTING DOM: VALID
TAGS: SCANNING...

โš ๏ธ Common Pitfalls

  1. Mismatched Group Names: Accordion exclusivity relies on strict string matching of the name attribute. A typo like name="faq" on one and name="faqs" on another will break exclusivity.
  2. Using Non-Standard JavaScript Click Handlers on Named Details: If you attach custom click handlers that call e.preventDefault(), you may interfere with the browser's native mutual-exclusion state updates.
  3. Assuming name Prevents All Items From Closing: Unlike radio buttons (<input type="radio">) which must have one item checked, <details name="..."> allows the user to close all items in the group if desired.

๐Ÿ’ก Pro Tips

  1. Find-in-Page Integration: Because <details> natively supports beforematch, users searching with Ctrl+F will automatically expand the correct accordion pane, delivering superior UX over custom React/Vue accordion libraries.
  2. Check Browser Baseline: The name attribute on <details> is supported across modern baseline browsers (Chrome 120+, Safari 17.2+, Firefox 125+). For legacy browsers, a tiny 5-line JS polyfill can query [name] attributes on toggle events.

๐Ÿ“Œ Key Takeaways

  • The name attribute on <details> creates native exclusive accordions with zero JavaScript.
  • Opening one named <details> automatically collapses any other open <details> sharing that same name.
  • Multiple independent accordion groups can coexist on the same page by using different name values.
  • Native <details> automatically expands when browser search matches text inside (beforematch event).
  • Users can still collapse all items in a group if they wish.
  • --
โญ LEARN: HTML ๐ŸŒŸ โš”๏ธ QUIZ BATTLE ARENA // ACTIVE
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 1 / 3

How do you create an exclusive accordion (where opening one item automatically closes others) in modern standards-compliant HTML without JavaScript?

Question 1 / 3 Topic: HTML Fundamentals
00:45 REMAINING
XP REWARD
+250 XP
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 2 / 3

What native browser event is fired when a user's in-page search (Ctrl+F) discovers text inside a collapsed <details> element?

Question 2 / 3 Topic: HTML Fundamentals
00:45 REMAINING
XP REWARD
+250 XP
3x
STREAK!
BONUS ACTIVE
COMBO
? Question 3 / 3

How does <details name="faq"> differ from <input type="radio" name="faq"> regarding closure?

Question 3 / 3 Topic: HTML Fundamentals
00:45 REMAINING
XP REWARD
+250 XP