Skip to content

Commit e642246

Browse files
authored
Merge pull request #330 from shairez/pr-tabs-final
2 parents 596fa12 + 983a7ad commit e642246

File tree

14 files changed

+757
-151
lines changed

14 files changed

+757
-151
lines changed

apps/website/src/components/keyboard-interaction-table/keyboard-interaction-table.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export const KeyboardInteractionTable = component$(
2323
</thead>
2424
<tbody>
2525
{props.keyDescriptors.map((descriptor) => {
26-
console.log('descriptor', descriptor);
2726
return (
2827
<KBInteractionTableRow
2928
key={descriptor.keyTitle}

apps/website/src/routes/docs/headless/(components)/tabs/index.mdx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,46 @@ import { APITable } from '../../../../../components/api-table/api-table';
8888
keyDescriptors={[
8989
{
9090
keyTitle: 'Tab',
91-
description: 'Moves focus to next tab.',
91+
description: 'Moves focus to the selected panel.',
9292
},
9393
{
9494
keyTitle: 'Shift + Tab',
95+
description: 'Moves focus to the selected tab.',
96+
},
97+
{
98+
keyTitle: 'ArrowRight',
99+
description: 'Moves focus to the next tab.',
100+
},
101+
{
102+
keyTitle: 'ArrowLeft',
95103
description: 'Moves focus to the previous tab.',
96104
},
105+
{
106+
keyTitle: 'ArrowDown',
107+
description: 'In "vertical mode", moves focus to the next tab.',
108+
},
109+
{
110+
keyTitle: 'ArrowUp',
111+
description: 'In "vertical mode", moves focus to the previous tab.',
112+
},
113+
{
114+
/* {
115+
keyTitle: 'Home',
116+
description: 'Moves focus to the first tab.',
117+
},
118+
{
119+
keyTitle: 'PageUp',
120+
description: 'Moves focus to the first tab.',
121+
},
122+
{
123+
keyTitle: 'End',
124+
description: 'Moves focus to the last tab.',
125+
},
126+
{
127+
keyTitle: 'PageDown',
128+
description: 'Moves focus to the first tab.',
129+
}, */
130+
},
97131
]}
98132
/>
99133

@@ -109,6 +143,12 @@ import { APITable } from '../../../../../components/api-table/api-table';
109143
description:
110144
'Toggle between automatic or manual. The automatic behavior moves between tabs when hover. The manual behavior moves between tabs on click.',
111145
},
146+
{
147+
name: 'vertical',
148+
type: 'boolean',
149+
description:
150+
'If set to true, the behavior of UpArrow and DownArrow will navigate between tabs vertically instead of horizontally.',
151+
},
112152
]}
113153
/>
114154

cypress.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from 'cypress';
2+
3+
export default defineConfig({
4+
e2e: {
5+
setupNodeEvents(on, config) {
6+
// implement node event listeners here
7+
},
8+
},
9+
});

cypress/fixtures/example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "[email protected]",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}

cypress/support/commands.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************
3+
// This example commands.ts shows you how to
4+
// create various custom commands and overwrite
5+
// existing commands.
6+
//
7+
// For more comprehensive examples of custom
8+
// commands please read more here:
9+
// https://on.cypress.io/custom-commands
10+
// ***********************************************
11+
//
12+
//
13+
// -- This is a parent command --
14+
// Cypress.Commands.add('login', (email, password) => { ... })
15+
//
16+
//
17+
// -- This is a child command --
18+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
19+
//
20+
//
21+
// -- This is a dual command --
22+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
23+
//
24+
//
25+
// -- This will overwrite an existing command --
26+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
27+
//
28+
// declare global {
29+
// namespace Cypress {
30+
// interface Chainable {
31+
// login(email: string, password: string): Chainable<void>
32+
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
33+
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
34+
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
35+
// }
36+
// }
37+
// }

cypress/support/e2e.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/e2e.ts is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands';
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

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

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,55 @@ import {
77
useComputed$,
88
useTask$,
99
$,
10+
useSignal,
1011
} from '@builder.io/qwik';
1112
import { tabsContextId } from './tabs-context-id';
13+
import { KeyCode } from '../../utils/key-code.type';
14+
import { isBrowser, isServer } from '@builder.io/qwik/build';
1215

1316
export interface TabProps {
1417
onClick?: PropFunction<() => void>;
1518
class?: string;
1619
selectedClassName?: string;
20+
disabled?: boolean;
1721
}
1822

1923
export const Tab = component$((props: TabProps) => {
2024
const contextService = useContext(tabsContextId);
2125

26+
const serverAssignedIndexSig = useSignal<number | undefined>(undefined);
2227
const uniqueId = useId();
2328

2429
useTask$(({ cleanup }) => {
25-
contextService.tabsChanged$();
26-
30+
if (isServer) {
31+
serverAssignedIndexSig.value =
32+
contextService.lastAssignedTabIndexSig.value;
33+
contextService.lastAssignedTabIndexSig.value++;
34+
}
35+
if (isBrowser) {
36+
contextService.onTabsChanged$();
37+
}
2738
cleanup(() => {
28-
contextService.tabsChanged$();
39+
contextService.onTabsChanged$();
2940
});
3041
});
3142

43+
useTask$(({ track }) => {
44+
track(() => props.disabled);
45+
46+
if (props.disabled && contextService.tabsMap[uniqueId]) {
47+
contextService.tabsMap[uniqueId].disabled = true;
48+
}
49+
});
50+
3251
const isSelectedSignal = useComputed$(() => {
52+
if (isServer) {
53+
return (
54+
serverAssignedIndexSig.value === contextService.selectedIndexSig.value
55+
);
56+
}
3357
return (
34-
contextService.selectedIndex.value ===
58+
contextService.selectedIndexSig.value ===
3559
contextService.tabsMap[uniqueId]?.index
3660
);
3761
});
@@ -40,17 +64,13 @@ export const Tab = component$((props: TabProps) => {
4064
() => contextService.tabsMap[uniqueId]?.tabPanelId
4165
);
4266

43-
// TODO: Figure out a way to fix this shitty hack :)
44-
useTask$(({ track }) => {
45-
track(() => isSelectedSignal.value);
67+
const selectTab$ = $(() => {
68+
// TODO: try to move this to the Tabs component
4669

47-
if (isSelectedSignal.value) {
48-
contextService.showTabs$();
70+
if (props.disabled) {
71+
return;
4972
}
50-
});
51-
52-
const selectTab$ = $(() => {
53-
contextService.selectedIndex.value =
73+
contextService.selectedIndexSig.value =
5474
contextService.tabsMap[uniqueId]?.index || 0;
5575

5676
contextService.selectTab$(uniqueId);
@@ -68,6 +88,8 @@ export const Tab = component$((props: TabProps) => {
6888
data-tab-id={uniqueId}
6989
type="button"
7090
role="tab"
91+
disabled={props.disabled}
92+
aria-disabled={props.disabled}
7193
onFocus$={selectIfAutomatic$}
7294
onMouseEnter$={selectIfAutomatic$}
7395
aria-selected={isSelectedSignal.value}
@@ -84,6 +106,12 @@ export const Tab = component$((props: TabProps) => {
84106
props.onClick();
85107
}
86108
}}
109+
onKeyDown$={(e) => {
110+
contextService.onTabKeyDown$(
111+
e.key as KeyCode,
112+
(e.target as any).getAttribute('data-tab-id')
113+
);
114+
}}
87115
>
88116
<Slot />
89117
</button>
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { Signal, QRL } from '@builder.io/qwik';
22
import { Behavior } from './behavior.type';
33
import { TabInfo } from './tabs';
4+
import { KeyCode } from '../../utils/key-code.type';
45

56
export interface TabsContext {
6-
selectedIndex: Signal<number>;
7-
selectedTabId: Signal<string>;
87
selectTab$: QRL<(tabId: string) => void>;
98
showTabs$: QRL<() => void>;
10-
tabsChanged$: QRL<() => void>;
9+
onTabsChanged$: QRL<() => void>;
10+
onTabKeyDown$: QRL<(key: KeyCode, tabId: string) => void>;
11+
selectedIndexSig: Signal<number>;
12+
selectedTabIdSig: Signal<string>;
1113
tabsMap: { [key: string]: TabInfo };
1214
tabPanelsMap: { [key: string]: TabInfo };
1315
behavior: Behavior;
16+
17+
lastAssignedTabIndexSig: Signal<number>;
18+
lastAssignedPanelIndexSig: Signal<number>;
1419
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { component$, Slot } from '@builder.io/qwik';
1+
import { component$, QwikIntrinsicElements, Slot } from '@builder.io/qwik';
22
import { Behavior } from './behavior.type';
33

4-
export interface TabListProps {
4+
export type TabListProps = QwikIntrinsicElements['div'] & {
55
labelledBy?: string;
66
behavior?: Behavior;
7-
class?: string;
8-
}
7+
};
98

109
// List of tabs that can be clicked to show different content.
1110
export const TabList = component$((props: TabListProps) => {

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,49 @@ import {
55
Slot,
66
useTask$,
77
useComputed$,
8+
useSignal,
89
} from '@builder.io/qwik';
910
import { tabsContextId } from './tabs-context-id';
11+
import { isBrowser, isServer } from '@builder.io/qwik/build';
1012

1113
export interface TabPanelProps {
1214
class?: string;
1315
}
1416

1517
export const TabPanel = component$(({ ...props }: TabPanelProps) => {
1618
const contextService = useContext(tabsContextId);
19+
20+
const serverAssignedIndexSig = useSignal<number | undefined>(undefined);
21+
1722
const panelUID = useId();
1823

1924
const matchedTabId = useComputed$(
2025
() => contextService.tabPanelsMap[panelUID]?.tabId
2126
);
2227

2328
useTask$(({ cleanup }) => {
24-
contextService.tabsChanged$();
25-
29+
if (isServer) {
30+
serverAssignedIndexSig.value =
31+
contextService.lastAssignedPanelIndexSig.value;
32+
contextService.lastAssignedPanelIndexSig.value++;
33+
}
34+
if (isBrowser) {
35+
contextService.onTabsChanged$();
36+
}
2637
cleanup(() => {
27-
contextService.tabsChanged$();
38+
contextService.onTabsChanged$();
2839
});
2940
});
3041

3142
const isSelectedSignal = useComputed$(() => {
43+
if (isServer) {
44+
return (
45+
serverAssignedIndexSig.value === contextService.selectedIndexSig.value
46+
);
47+
}
48+
3249
return (
33-
contextService.selectedIndex.value ===
50+
contextService.selectedIndexSig.value ===
3451
contextService.tabPanelsMap[panelUID]?.index
3552
);
3653
});

0 commit comments

Comments
 (0)