Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: a11y rule "interactive-supports-focus" fails with a warning #4159

Open
wants to merge 4 commits into
base: beta
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions components/TabList/tab-list.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from "react";

import { useRouter } from "next/router";
import Link from "next/link";
import clsx from "clsx";
Expand All @@ -25,6 +24,7 @@ interface SubTabsListProps extends TabListProps {
const TabList = ({ tabList, pageId, selectedTab }: TabListProps) => {
const router = useRouter();
const range = router.query.range ? Number(router.query.range) : 30;

return (
<nav
role="tablist"
Expand All @@ -37,13 +37,13 @@ const TabList = ({ tabList, pageId, selectedTab }: TabListProps) => {
role="tab"
aria-selected={selectedTab === tab.name.toLowerCase() ? "true" : "false"}
data-state={selectedTab === tab.name.toLowerCase() ? "active" : "inactive"}
tabIndex={-1}
key={index}
className={`tool-list-item border-b-2 transition-all ease-in-out ${
className={clsx(
Copy link
Member

Choose a reason for hiding this comment

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

After reviewing the pull request, this really is just links in a navigation element that look like tabs, but they really are just. links.

The role="tablist" can be removed from the <nav /> and the role="tab" on the <div /> can be removed.

"tool-list-item border-b-2 transition-all ease-in-out",
nickytonline marked this conversation as resolved.
Show resolved Hide resolved
selectedTab === tab.name.toLowerCase()
? "border-orange-500"
: "border-transparent hover:border-light-slate-8"
}`}
)}
>
<TabListItem
tab={tab}
Expand Down Expand Up @@ -77,6 +77,11 @@ export const SubTabsList = ({ tabList, pageId, selectedTab, label, textSize, onS
key={index}
className={clsx(isSelected && "bg-white shadow", "rounded py-1 px-2", !isSelected && "text-light-slate-11")}
onClick={onSelect ? () => onSelect(tab) : undefined}
onKeyDown={(e) => {
Copy link
Member

Choose a reason for hiding this comment

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

For the SubTabsList, it's not a tab list if there is no onSelect, it's just links. But if the onSelect is present, it means it is a tab list. We can rework the SubTabsList component to this.

export const SubTabsList = ({ tabList, pageId, selectedTab, label, textSize, onSelect }: SubTabsListProps) => {
  const ContainerComponent = onSelect ? "div" : "nav";
  const extraProps = onSelect ? { role: "tablist" } : {};

  return (
    <ContainerComponent
      {...extraProps}
      aria-label={label}
      className={clsx(
        "flex items-center w-max overflow-x-auto overflow-y-hidden gap-2 bg-light-slate-3 p-1 rounded",
        textSize === "small" && "text-sm"
      )}
    >
      {tabList.map((tab, index) => {
        const isSelected = selectedTab === tab.name.toLowerCase();

        return (
          <div
            key={index}
            className={clsx(isSelected && "bg-white shadow", "rounded py-1 px-2", !isSelected && "text-light-slate-11")}
          >
            {onSelect ? (
              <button role="tab" onClick={() => onSelect(tab)}>
                tab.name
              </button>
            ) : (
              <Link href={`${pageId ? `${pageId}/` : ""}${tab.path}`}>{tab.name}</Link>
            )}
          </div>
        );
      })}
    </ContainerComponent>
  );
};

This could be improved, but I don't want to introduced too much refactoring for the moment as the goal of the issue was to sort out some accessibility issues.

Note that because we've introduced a button when the onSelect exists, i.e.

            {onSelect ? (
              <button role="tab" onClick={() => onSelect(tab)}>
                tab.name
              </button>
            ) : (
              <Link href={`${pageId ? `${pageId}/` : ""}${tab.path}`}>{tab.name}</Link>
            )}

we no longer need the onKeyDown to handle ENTER as this is handled natively with a button.

if (e.key === "Enter" || e.key === " ") {
onSelect?.(tab);
}
}} // Added keyboard interaction for Enter/Space keys
>
{onSelect ? tab.name : <Link href={`${pageId ? `${pageId}/` : ""}${tab.path}`}>{tab.name}</Link>}
</div>
Expand Down
Loading