-
-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor TabPanels to compound Tab children pattern #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,8 +1,14 @@ | ||||||
| import { useState } from 'react'; | ||||||
| import React, { useState } from 'react'; | ||||||
|
|
||||||
| export function TabPanels({ tabs = [] }) { | ||||||
| const [active, setActive] = useState(tabs[0]?.id); | ||||||
| const activeTab = tabs.find((tab) => tab.id === active) || tabs[0]; | ||||||
| export function Tab({ children }) { | ||||||
| return children; | ||||||
| } | ||||||
|
|
||||||
| export function TabPanels({ children }) { | ||||||
| const tabs = React.Children.toArray(children).filter( | ||||||
| (child) => React.isValidElement(child) && child.props.title != null, | ||||||
| ); | ||||||
| const [activeIndex, setActiveIndex] = useState(0); | ||||||
|
|
||||||
|
Comment on lines
+7
to
12
|
||||||
| if (!tabs.length) { | ||||||
| return null; | ||||||
|
|
@@ -11,22 +17,24 @@ export function TabPanels({ tabs = [] }) { | |||||
| return ( | ||||||
| <div className="my-6 overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm dark:border-slate-800 dark:bg-slate-900"> | ||||||
| <div className="flex flex-wrap gap-1 border-b border-slate-200 p-2 dark:border-slate-800"> | ||||||
| {tabs.map((tab) => ( | ||||||
| {tabs.map((tab, index) => ( | ||||||
| <button | ||||||
| key={tab.id} | ||||||
| key={tab.props.title} | ||||||
|
||||||
| key={tab.props.title} | |
| key={tab.key ?? index} |
Copilot
AI
Mar 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tab UI is rendered as a set of buttons without tab semantics. For accessibility, consider adding role="tablist" on the container, role="tab" + aria-selected on each button, and role="tabpanel" on the content region (with proper id/aria-controls wiring) so screen readers can interpret this as a tab interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file now uses a default
Reactimport mainly forReact.Children/React.isValidElement, but other components in this repo typically import named APIs fromreact(e.g.,components/Accordion.js:1). To stay consistent and enable tree-shaking, consider importingChildren/isValidElement/useStateas named imports instead ofimport React, ....