Capítulo 42 de 51

Chapter 42: Tabs

Core Idea

Standard single-panel-visible-at-a-time tabs with a separate Indicator part for an animated "active tab" highlight bar that tracks the selected Tab's position/size automatically.

Key Concepts

  • Anatomy: RootListTab (repeated), Indicator; Panel (repeated, one per Tab.value).
  • Indicator: a dedicated part (not just styling the active Tab) meant to be positioned via CSS using values it tracks internally — typically animated with a transform/width transition as the active tab changes. renderBeforeHydration avoids a flash-of-unpositioned-indicator on SSR before JS hydrates.
  • activateOnFocus (on List): whether moving focus to a tab (via arrow keys) also activates its panel immediately, vs. requiring a separate activation (Enter/Space) — the classic "automatic vs. manual" tab activation choice from the WAI-ARIA APG.
  • Panel.keepMounted: keep inactive panels in the DOM (hidden) instead of unmounting — useful for panels with expensive-to-recreate state (forms, iframes) or scroll-position preservation.
  • data-activation-direction: present on Root/List/Panel/Indicator/Tab, indicating which direction the newly active tab is relative to the previous one — drives directional slide transitions.

Reference Tables

PartNotable propsNotable data attributes
Rootvalue/defaultValue/onValueChange, orientationdata-orientation, data-activation-direction
ListactivateOnFocus, loopFocussame as Root
Tabvalue (required), nativeButton, disableddata-active, data-disabled, plus orientation/direction attrs
Panelvalue (required), keepMounteddata-hidden, data-index, data-starting-style/data-ending-style
IndicatorrenderBeforeHydrationorientation/direction attrs

Key Takeaways

  1. Choose activateOnFocus deliberately: true (automatic activation, default APG pattern) suits lightweight content-swap tabs; false (manual) suits tabs whose panels are expensive to render/fetch, so arrow-key browsing doesn't trigger unwanted loads.
  2. Use Indicator instead of hand-computing an active-tab underline's position — it's built to track the active Tab's geometry for you.
  3. Set keepMounted on Panel for any tab containing form state, video/iframe embeds, or scroll position that shouldn't reset when switching away and back.

Connects To

  • ch006 (Animation): data-starting-style/data-ending-style on Panel for panel-switch transitions; data-activation-direction for directional variants.