Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/five-kiwis-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/tabs": patch
---

Fix vertical tabs to use correct aria-orientation and support Up/Down arrow navigation for accessibility.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please include the issue number at the end. i.e.

Fix vertical tabs to use correct aria-orientation and support Up/Down arrow navigation for accessibility. (#5810) 

63 changes: 63 additions & 0 deletions packages/components/tabs/__tests__/tabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,67 @@ describe("Tabs", () => {
expect(newTabButtons[2]).toHaveAttribute("aria-selected", "true");
});
});

it("should set vertical aria-orientation", () => {
const wrapper = render(
<Tabs isVertical aria-label="Tabs vertical test" data-testid="tabWrapper">
<Tab key="item1" title="Item 1">
<div>Content 1</div>
</Tab>
<Tab key="item2" title="Item 2">
<div>Content 2</div>
</Tab>
<Tab key="item3" title="Item 3">
<div>Content 3</div>
</Tab>
</Tabs>,
);

const tablist = wrapper.getByRole("tablist");

expect(tablist).toHaveAttribute("aria-orientation", "vertical");
});

it("should allow vertical tabs to navigate with up and down arrows", async () => {
const wrapper = render(
<Tabs isVertical aria-label="Tabs vertical test" data-testid="tabWrapper">
<Tab key="item1" title="Item 1">
<div>Content 1</div>
</Tab>
<Tab key="item2" title="Item 2">
<div>Content 2</div>
</Tab>
<Tab key="item3" title="Item 3">
<div>Content 3</div>
</Tab>
</Tabs>,
);

const tab1 = wrapper.getByRole("tab", {name: "Item 1"});
const tab2 = wrapper.getByRole("tab", {name: "Item 2"});
const tab3 = wrapper.getByRole("tab", {name: "Item 3"});

expect(tab1).toHaveAttribute("aria-selected", "true");
expect(tab2).toHaveAttribute("aria-selected", "false");
expect(tab3).toHaveAttribute("aria-selected", "false");

act(() => {
focus(tab1);
});

await user.keyboard("[ArrowDown]");
expect(tab1).toHaveAttribute("aria-selected", "false");
expect(tab2).toHaveAttribute("aria-selected", "true");
expect(tab3).toHaveAttribute("aria-selected", "false");

await user.keyboard("[ArrowDown]");
expect(tab1).toHaveAttribute("aria-selected", "false");
expect(tab2).toHaveAttribute("aria-selected", "false");
expect(tab3).toHaveAttribute("aria-selected", "true");

await user.keyboard("[ArrowUp]");
expect(tab1).toHaveAttribute("aria-selected", "false");
expect(tab2).toHaveAttribute("aria-selected", "true");
expect(tab3).toHaveAttribute("aria-selected", "false");
});
});
30 changes: 21 additions & 9 deletions packages/components/tabs/src/use-tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,19 @@ export function useTabs<T extends object>(originalProps: UseTabsProps<T>) {
const disableAnimation =
originalProps?.disableAnimation ?? globalContext?.disableAnimation ?? false;

const placement = (variantProps as Props).placement ?? (isVertical ? "start" : "top");
const orientation =
isVertical || placement === "start" || placement === "end" ? "vertical" : "horizontal";

const state = useTabListState<T>({
children: children as CollectionChildren<T>,
...otherProps,
});
const {tabListProps} = useTabList<T>(otherProps as AriaTabListProps<T>, state, domRef);
const {tabListProps} = useTabList<T>(
{...(otherProps as AriaTabListProps<T>), orientation},
state,
domRef,
);

const slots = useMemo(
() =>
Expand Down Expand Up @@ -161,7 +169,6 @@ export function useTabs<T extends object>(originalProps: UseTabsProps<T>) {
[baseStyles, otherProps, slots],
);

const placement = (variantProps as Props).placement ?? (isVertical ? "start" : "top");
const getWrapperProps: PropGetter = useCallback(
(props) => ({
"data-slot": "tabWrapper",
Expand All @@ -174,13 +181,18 @@ export function useTabs<T extends object>(originalProps: UseTabsProps<T>) {
);

const getTabListProps: PropGetter = useCallback(
(props) => ({
ref: domRef,
"data-slot": "tabList",
className: slots.tabList({class: clsx(classNames?.tabList, props?.className)}),
...mergeProps(tabListProps, props),
}),
[domRef, tabListProps, classNames, slots],
(props) =>
mergeProps(
tabListProps,
{
ref: domRef,
"data-slot": "tabList",
"aria-orientation": orientation,
className: slots.tabList({class: clsx(classNames?.tabList, props?.className)}),
},
props,
),
[domRef, tabListProps, classNames, slots, orientation],
Comment on lines +184 to +195
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to update this part. aria-orientation is already included in tabListProps. Also don't see the reason why we need to wrap everything in mergeProps.

);

const getTabCursorProps: PropGetter = useCallback(
Expand Down
Loading