Skip to content

Commit e115051

Browse files
committed
feat: implement tabs indicator disabled state
1 parent 16f5993 commit e115051

File tree

7 files changed

+37
-2
lines changed

7 files changed

+37
-2
lines changed

examples/next-ts/pages/tabs.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function Page() {
1111

1212
const service = useMachine(tabs.machine, {
1313
id: useId(),
14-
defaultValue: "nils",
14+
defaultValue: "evelyn",
1515
...controls.context,
1616
})
1717

@@ -24,7 +24,11 @@ export default function Page() {
2424
<div {...api.getIndicatorProps()} />
2525
<div {...api.getListProps()}>
2626
{tabsData.map((data) => (
27-
<button {...api.getTriggerProps({ value: data.id })} key={data.id} data-testid={`${data.id}-tab`}>
27+
<button
28+
{...api.getTriggerProps({ value: data.id, disabled: data.disabled })}
29+
key={data.id}
30+
data-testid={`${data.id}-tab`}
31+
>
2832
{data.label}
2933
</button>
3034
))}

packages/machines/tabs/src/tabs.connect.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,14 @@ export function connect<T extends PropTypes>(service: Service<TabsSchema>, norma
185185
getIndicatorProps() {
186186
const indicatorRect = context.get("indicatorRect")
187187
const indicatorTransition = context.get("indicatorTransition")
188+
const disabled = context.get("indicatorDisabled")
189+
188190
return normalize.element({
189191
id: dom.getIndicatorId(scope),
190192
...parts.indicator.attrs,
191193
dir: prop("dir"),
192194
"data-orientation": prop("orientation"),
195+
"data-disabled": dataAttr(disabled),
193196
style: {
194197
"--transition-property": "left, right, top, bottom, width, height",
195198
"--left": indicatorRect.left,

packages/machines/tabs/src/tabs.dom.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,8 @@ export const resolveRect = (rect: Record<"width" | "height" | "left" | "top", nu
4646
left: `${rect.left}px`,
4747
top: `${rect.top}px`,
4848
})
49+
50+
export const getIsTriggerDisabled = (ctx: Scope, id: string) => {
51+
const el = getTriggerEl(ctx, id)
52+
return el?.hasAttribute("disabled") || el?.getAttribute("aria-disabled") === "true"
53+
}

packages/machines/tabs/src/tabs.machine.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const machine = createMachine({
4646
indicatorRect: bindable(() => ({
4747
defaultValue: { left: "0px", top: "0px", width: "0px", height: "0px" },
4848
})),
49+
indicatorDisabled: bindable(() => ({ defaultValue: false })),
4950
}
5051
},
5152

@@ -260,6 +261,7 @@ export const machine = createMachine({
260261
if (!triggerEl) return
261262

262263
context.set("indicatorRect", dom.getRectById(scope, value))
264+
context.set("indicatorDisabled", dom.getIsTriggerDisabled(scope, value))
263265

264266
nextTick(() => {
265267
context.set("indicatorTransition", false)
@@ -289,6 +291,7 @@ export const machine = createMachine({
289291
onEntry({ rects }) {
290292
const [rect] = rects
291293
context.set("indicatorRect", dom.resolveRect(rect))
294+
context.set("indicatorDisabled", dom.getIsTriggerDisabled(scope, value))
292295
},
293296
})
294297

packages/machines/tabs/src/tabs.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export type TabsSchema = {
108108
focusedValue: string | null
109109
indicatorTransition: boolean
110110
indicatorRect: { left: string; top: string; width: string; height: string }
111+
indicatorDisabled: boolean
111112
}
112113
refs: {
113114
indicatorCleanup: VoidFunction | null | undefined

shared/src/css/tabs.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
background: blue;
2727
color: white;
2828
}
29+
&[disabled] {
30+
opacity: 0.5;
31+
pointer-events: none;
32+
}
2933
}
3034

3135
[data-scope="tabs"][data-part="content"] {
@@ -38,6 +42,10 @@
3842
[data-scope="tabs"][data-part="indicator"] {
3943
background-color: red;
4044
z-index: 10;
45+
46+
&[data-disabled] {
47+
opacity: 0.5;
48+
}
4149
}
4250

4351
[data-scope="tabs"][data-part="indicator"][data-orientation="horizontal"] {

shared/src/data.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,17 @@ export const comboboxData = [
5858
]
5959

6060
export const tabsData = [
61+
{
62+
id: "evelyn",
63+
label: "Evelyn Glennie",
64+
content: `
65+
Evelyn Glennie is a Scottish percussionist and composer who has been profoundly deaf since the age of 12. She
66+
is known for her ability to feel music through vibrations and has performed internationally, becoming one
67+
of the world's leading solo percussionists. Her work has inspired many and challenged perceptions of
68+
disability in music.
69+
`,
70+
disabled: true,
71+
},
6172
{
6273
id: "nils",
6374
label: "Nils Frahm",

0 commit comments

Comments
 (0)