Skip to content

Commit 0590bf4

Browse files
shaireznaorpeledigalklebanovthejackshelton
committed
feat(headless/tabs): add custom selected class
Co-authored-by: Naor Peled <[email protected]> Co-authored-by: Igal Klebanov <[email protected]> Co-authored-by: Jack Shelton <[email protected]>
1 parent 1e6fb49 commit 0590bf4

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
useVisibleTask$,
1010
QwikIntrinsicElements,
1111
type QwikMouseEvent,
12+
useComputed$,
1213
} from '@builder.io/qwik';
1314
import { tabsContextId } from './tabs-context-id';
1415
import { KeyCode } from '../../utils/key-code.type';
@@ -39,6 +40,10 @@ export const Tab = component$((props: TabProps) => {
3940
const elementRefSig = useSignal<HTMLElement | undefined>();
4041
const uniqueTabId = useId();
4142

43+
const selectedClassNameSig = useComputed$(() => {
44+
return props.selectedClassName || contextService.selectedClassName;
45+
});
46+
4247
useTask$(async function indexInitTask({ cleanup }) {
4348
if (isServer) {
4449
serverAssignedIndexSig.value =
@@ -112,14 +117,17 @@ export const Tab = component$((props: TabProps) => {
112117
tabIndex={isSelectedSig.value ? 0 : -1}
113118
aria-controls={'tabpanel-' + matchedTabPanelIdSig.value}
114119
class={`${
115-
isSelectedSig.value ? `selected ${props.selectedClassName || ''}` : ''
120+
isSelectedSig.value
121+
? `selected ${selectedClassNameSig.value || ''}`
122+
: ''
116123
}${props.class ? ` ${props.class}` : ''}`}
117124
onClick$={(event) => {
118125
contextService.selectTab$(uniqueTabId);
119126
if (props.onClick$) {
120127
props.onClick$(event);
121128
}
122129
}}
130+
style={props.style}
123131
>
124132
<Slot />
125133
</button>

packages/kit-headless/src/components/tabs/tabs-context.type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { QRL } from '@builder.io/qwik';
2-
import { Behavior } from './behavior.type';
32
import { TabInfo } from './tabs';
43
import { KeyCode } from '../../utils/key-code.type';
54

@@ -16,4 +15,5 @@ export interface TabsContext {
1615
isTabSelected$: QRL<(tabId: string) => boolean>;
1716
isPanelSelected$: QRL<(panelId: string) => boolean>;
1817
selectIfAutomatic$: QRL<(tabId: string) => void>;
18+
selectedClassName?: string;
1919
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export type TabsProps = {
4646
behavior?: Behavior;
4747
selectedIndex?: number;
4848
vertical?: boolean;
49+
selectedClassName?: string;
4950
onSelectedIndexChange$?: (index: number) => void;
5051
} & QwikIntrinsicElements['div'];
5152

@@ -214,6 +215,7 @@ export const Tabs = component$((props: TabsProps) => {
214215
reIndexTabs$,
215216
onTabKeyDown$,
216217
selectIfAutomatic$,
218+
selectedClassName: props.selectedClassName
217219
};
218220

219221
useContextProvider(tabsContextId, contextService);

0 commit comments

Comments
 (0)