Skip to content

Commit a609823

Browse files
shairezwmertens
andcommitted
refactor(headless/tabs): renamed internal things
Co-authored-by: Wout Mertens <[email protected]>
1 parent 6909794 commit a609823

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

packages/kit-headless/src/components/tabs/tab-panel.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,15 @@ export const TabPanel = component$(
3838
return (
3939
<div
4040
{...props}
41-
data-tabpanel-id={fullPanelElementId}
4241
id={fullPanelElementId}
42+
aria-labelledby={fullTabElementId}
4343
role="tabpanel"
4444
tabIndex={0}
45-
aria-labelledby={fullTabElementId}
4645
class={[
4746
(props.class as Signal<string>)?.value ?? (props.class as string),
48-
(_extraClass as Signal<string>)?.value ?? (_extraClass as string),
49-
// TODO hiddenClass
50-
isSelectedSig.value && 'is-hidden'
47+
(_extraClass as Signal<string>)?.value ?? (_extraClass as string)
5148
]}
52-
// We need to use null so a previous hidden attribute is removed.
53-
hidden={isSelectedSig.value ? (null as unknown as undefined) : true}
49+
hidden={!isSelectedSig.value}
5450
>
5551
<Slot />
5652
</div>

packages/kit-headless/src/components/tabs/tab.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,28 @@ export const Tab = component$(
7373
contextService.selectIfAutomatic$(_tabId!);
7474
});
7575

76+
const classNamesSig = useComputed$(() => [
77+
(_extraClass as Signal<string>)?.value ?? (_extraClass as string),
78+
// TODO only given class if selected
79+
isSelectedSig.value && ['selected', selectedClassNameSig.value]
80+
]);
81+
7682
return (
7783
<button
7884
{...props}
7985
type="button"
8086
role="tab"
8187
id={fullTabElementId}
82-
data-tab-id={fullTabElementId}
88+
aria-controls={fullPanelElementId}
8389
ref={elementRefSig}
8490
aria-disabled={props.disabled}
8591
onFocus$={[selectIfAutomatic$, props.onFocus$]}
8692
onMouseEnter$={[selectIfAutomatic$, props.onMouseEnter$]}
8793
aria-selected={isSelectedSig.value}
8894
tabIndex={isSelectedSig.value ? 0 : -1}
89-
aria-controls={fullPanelElementId}
9095
class={[
9196
(props.class as Signal<string>)?.value ?? (props.class as string),
92-
(_extraClass as Signal<string>)?.value ?? (_extraClass as string),
93-
// TODO only given class if selected
94-
isSelectedSig.value && ['selected', selectedClassNameSig.value]
97+
classNamesSig.value
9598
]}
9699
onClick$={[$(() => contextService.selectTab$(_tabId!)), props.onClick$]}
97100
>

packages/kit-headless/src/components/tabs/tabs.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from '@builder.io/qwik';
1414
import { KeyCode } from '../../utils/key-code.type';
1515
import { Behavior } from './behavior.type';
16-
import { TAB_ID_PREFIX, Tab, TabProps } from './tab';
16+
import { Tab, TabProps } from './tab';
1717
import { TabPanel, TabPanelProps } from './tab-panel';
1818
import { tabsContextId } from './tabs-context-id';
1919
import { TabsContext } from './tabs-context.type';
@@ -40,8 +40,7 @@ import { TabList } from './tabs-list';
4040
- do we keep all current props (tabId callbacks?)
4141
- shorthand for tab: "label" or "tab"?
4242
- the TODOs
43-
- why id and data-tab-id? we don't need any of them
44-
*
43+
*
4544
*/
4645

4746
export type TabsProps = {
@@ -120,7 +119,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
120119
let tabListElement: JSXNode | undefined;
121120
const tabComponents: JSXNode[] = [];
122121
const panelComponents: JSXNode[] = [];
123-
const tabs: TabInfo[] = [];
122+
const tabsInfo: TabInfo[] = [];
124123
let panelIndex = 0;
125124
let selectedIndex;
126125

@@ -156,7 +155,8 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
156155
case TabPanel: {
157156
const { label, selected } = child.props;
158157
// The consumer must provide a key if they change the order
159-
const tabId = tabComponents[panelIndex]?.key || child.key || `${panelIndex}`;
158+
const tabIdFromTabMaybe = tabComponents[panelIndex]?.key;
159+
const tabId = tabIdFromTabMaybe || child.key || `${panelIndex}`;
160160

161161
if (label) {
162162
tabComponents.push(<Tab>{label}</Tab>);
@@ -173,7 +173,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
173173
child.props._extraClass = panelClass;
174174

175175
panelComponents.push(child);
176-
tabs.push({
176+
tabsInfo.push({
177177
tabId,
178178
index: panelIndex,
179179
panelProps: child.props
@@ -195,11 +195,11 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
195195
}
196196

197197
tabComponents.forEach((tab, index) => {
198-
const tabId = tabs[index]?.tabId;
198+
const tabId = tabsInfo[index]?.tabId;
199199
tab.key = tabId;
200200
tab.props._tabId = tabId;
201201
tab.props._extraClass = tabClass;
202-
tabs[index].tabProps = tab.props;
202+
tabsInfo[index].tabProps = tab.props;
203203
});
204204

205205
if (tabListElement) {
@@ -215,7 +215,7 @@ export const Tabs: FunctionComponent<TabsProps> = (props) => {
215215
}
216216

217217
return (
218-
<TabsImpl tabs={tabs} {...rest}>
218+
<TabsImpl tabs={tabsInfo} {...rest}>
219219
{tabListElement}
220220
{panelComponents}
221221
</TabsImpl>
@@ -276,6 +276,8 @@ export const TabsImpl = component$((props: TabsProps & { tabs: TabInfo[] }) => {
276276
const selectedTabIdSig = givenTabIdSig || initialSelectedTabIdSig;
277277

278278
useTask$(function syncTabsTask({ track }) {
279+
// Possible optimizer bug: tracking only works with props.tabs
280+
// TODO: Write a test in Qwik optimizer to prove this bug
279281
const tabs = track(() => props.tabs);
280282
const tabId = selectedTabIdSig.value;
281283
updateSignals(tabs, selectedIndexSig, selectedTabIdSig, { tabId }, true);
@@ -350,15 +352,11 @@ export const TabsImpl = component$((props: TabsProps & { tabs: TabInfo[] }) => {
350352
tab = findPrevEnabledTab(props.tabs, props.tabs.length);
351353
}
352354
if (tab) {
353-
focusOnTab(tab.tabId);
355+
focusOnTab(tab.index);
354356
}
355357

356-
function focusOnTab(tabId: string) {
357-
const fullTabElementId = tabsPrefix + TAB_ID_PREFIX + tabId;
358-
// TODO use the index
359-
tabsRootElement
360-
?.querySelector<HTMLElement>(`[data-tab-id='${fullTabElementId}']`)
361-
?.focus();
358+
function focusOnTab(index: number) {
359+
tabsRootElement?.children[0]?.children[index]?.focus();
362360
}
363361
});
364362

0 commit comments

Comments
 (0)