- Notifications
You must be signed in to change notification settings - Fork928
fix(site): add tests and improve accessibility for useClickable#12218
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -114,6 +114,7 @@ | ||
"Signup", | ||
"slogtest", | ||
"sourcemapped", | ||
"spinbutton", | ||
"Srcs", | ||
"stdbuf", | ||
"stretchr", | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { | ||
type ElementType, | ||
type FC, | ||
type MouseEventHandler, | ||
type PropsWithChildren, | ||
} from "react"; | ||
import { type ClickableAriaRole, useClickable } from "./useClickable"; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
/** | ||
* Since the point of the hook is to take a traditionally non-interactive HTML | ||
* element and make it interactive, it made the most sense to test against an | ||
* element directly. | ||
*/ | ||
type NonNativeButtonProps<TElement extends HTMLElement = HTMLElement> = | ||
Readonly< | ||
PropsWithChildren<{ | ||
as?: Exclude<ElementType, "button">; | ||
role?: ClickableAriaRole; | ||
onInteraction: MouseEventHandler<TElement>; | ||
}> | ||
>; | ||
const NonNativeButton: FC<NonNativeButtonProps<HTMLElement>> = ({ | ||
as, | ||
onInteraction, | ||
children, | ||
role = "button", | ||
}) => { | ||
const clickableProps = useClickable(onInteraction, role); | ||
const Component = as ?? "div"; | ||
return <Component {...clickableProps}>{children}</Component>; | ||
}; | ||
describe(useClickable.name, () => { | ||
it("Always defaults to role 'button'", () => { | ||
render(<NonNativeButton onInteraction={jest.fn()} />); | ||
expect(() => screen.getByRole("button")).not.toThrow(); | ||
}); | ||
it("Overrides the native role of any element that receives the hook result (be very careful with this behavior)", () => { | ||
const anchorText = "I'm a button that's secretly a link!"; | ||
render( | ||
<NonNativeButton as="a" role="button" onInteraction={jest.fn()}> | ||
{anchorText} | ||
</NonNativeButton>, | ||
); | ||
const linkButton = screen.getByRole("button", { | ||
name: anchorText, | ||
}); | ||
expect(linkButton).toBeInstanceOf(HTMLAnchorElement); | ||
}); | ||
it("Always returns out the same role override received via arguments", () => { | ||
const placeholderCallback = jest.fn(); | ||
const roles = [ | ||
"button", | ||
"switch", | ||
] as const satisfies readonly ClickableAriaRole[]; | ||
for (const role of roles) { | ||
const { unmount } = render( | ||
<NonNativeButton role={role} onInteraction={placeholderCallback} />, | ||
); | ||
expect(() => screen.getByRole(role)).not.toThrow(); | ||
unmount(); | ||
} | ||
}); | ||
it("Allows an element to receive keyboard focus", async () => { | ||
const user = userEvent.setup(); | ||
const mockCallback = jest.fn(); | ||
render(<NonNativeButton role="button" onInteraction={mockCallback} />, { | ||
wrapper: ({ children }) => ( | ||
<> | ||
<button>Native button</button> | ||
{children} | ||
</> | ||
), | ||
}); | ||
await user.keyboard("[Tab][Tab]"); | ||
await user.keyboard(" "); | ||
expect(mockCallback).toBeCalledTimes(1); | ||
}); | ||
it("Allows an element to respond to clicks and Space/Enter, following all rules for native Button element interactions", async () => { | ||
const mockCallback = jest.fn(); | ||
const user = userEvent.setup(); | ||
render(<NonNativeButton role="button" onInteraction={mockCallback} />); | ||
await user.click(document.body); | ||
await user.keyboard(" "); | ||
await user.keyboard("[Enter]"); | ||
expect(mockCallback).not.toBeCalled(); | ||
const button = screen.getByRole("button"); | ||
await user.click(button); | ||
await user.keyboard(" "); | ||
await user.keyboard("[Enter]"); | ||
expect(mockCallback).toBeCalledTimes(3); | ||
}); | ||
it("Will keep firing events if the Enter key is held down", async () => { | ||
const mockCallback = jest.fn(); | ||
const user = userEvent.setup(); | ||
render(<NonNativeButton role="button" onInteraction={mockCallback} />); | ||
// Focus over to element, hold down Enter for specified keydown period | ||
// (count determined by browser/library), and then release the Enter key | ||
const keydownCount = 5; | ||
await user.keyboard(`[Tab]{Enter>${keydownCount}}{/Enter}`); | ||
expect(mockCallback).toBeCalledTimes(keydownCount); | ||
}); | ||
it("Will NOT keep firing events if the Space key is held down", async () => { | ||
const mockCallback = jest.fn(); | ||
const user = userEvent.setup(); | ||
render(<NonNativeButton role="button" onInteraction={mockCallback} />); | ||
// Focus over to element, and then hold down Space for 100 keydown cycles | ||
await user.keyboard("[Tab]{ >100}"); | ||
expect(mockCallback).not.toBeCalled(); | ||
// Then explicitly release the space bar | ||
await user.keyboard(`{/ }`); | ||
expect(mockCallback).toBeCalledTimes(1); | ||
}); | ||
test("If focus is lost while Space is held down, then releasing the key will do nothing", async () => { | ||
const mockCallback = jest.fn(); | ||
const user = userEvent.setup(); | ||
render(<NonNativeButton role="button" onInteraction={mockCallback} />, { | ||
wrapper: ({ children }) => ( | ||
<> | ||
{children} | ||
<button>Native button</button> | ||
</> | ||
), | ||
}); | ||
// Focus over to element, hold down Space for an indefinite amount of time, | ||
// move focus away from element, and then release Space | ||
await user.keyboard("[Tab]{ >}[Tab]{/ }"); | ||
expect(mockCallback).not.toBeCalled(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,60 @@ | ||
/** | ||
* @file 2024-02-19 - MES - Sadly, even though this hook aims to make elements | ||
* more accessible, it's doing the opposite right now. Per axe audits, the | ||
* current implementation will create a bunch of critical-level accessibility | ||
* violations: | ||
* | ||
* 1. Nesting interactive elements (e.g., workspace table rows having checkboxes | ||
* inside them) | ||
* 2. Overriding the native element's role (in this case, turning a native table | ||
* row into a button, which means that screen readers lose the ability to | ||
* announce the row's data as part of a larger table) | ||
* | ||
* It might not make sense to test this hook until the underlying design | ||
* problems are fixed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. WDYT about moving this into a ticket as well as a note in the milestone? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yeah, just for documentation/visibility, I think that'd be best. | ||
*/ | ||
import { type MouseEventHandler } from "react"; | ||
import { type CSSObject, useTheme } from "@emotion/react"; | ||
import { | ||
type ClickableAriaRole, | ||
type UseClickableResult, | ||
useClickable, | ||
} from "./useClickable"; | ||
import { type TableRowProps } from "@mui/material/TableRow"; | ||
type UseClickableTableRowResult< | ||
TRole extends ClickableAriaRole = ClickableAriaRole, | ||
> = UseClickableResult<HTMLTableRowElement, TRole> & | ||
TableRowProps & { | ||
css: CSSObject; | ||
hover: true; | ||
onAuxClick: MouseEventHandler<HTMLTableRowElement>; | ||
}; | ||
// Awkward type definition (the hover preview in VS Code isn't great, either), | ||
// but this basicallyextracts all click props from TableRowProps, but makes | ||
// onClick required, and addsadditional optionalprops (notablyonMiddleClick) | ||
type UseClickableTableRowConfig<TRole extends ClickableAriaRole> = { | ||
[Key in keyof TableRowProps as Key extends `on${string}Click` | ||
? Key | ||
: never]: UseClickableTableRowResult<TRole>[Key]; | ||
} & { | ||
role?: TRole; | ||
onClick: MouseEventHandler<HTMLTableRowElement>; | ||
onMiddleClick?: MouseEventHandler<HTMLTableRowElement>; | ||
}; | ||
export const useClickableTableRow = < | ||
TRole extends ClickableAriaRole = ClickableAriaRole, | ||
>({ | ||
role, | ||
onClick, | ||
onDoubleClick, | ||
onMiddleClick, | ||
onAuxClick: externalOnAuxClick, | ||
}: UseClickableTableRowConfig<TRole>): UseClickableTableRowResult<TRole> => { | ||
const clickableProps = useClickable(onClick, (role ?? "button") as TRole); | ||
const theme = useTheme(); | ||
return { | ||
@@ -49,12 +75,14 @@ export const useClickableTableRow = ({ | ||
hover: true, | ||
onDoubleClick, | ||
onAuxClick: (event) => { | ||
// Regardless of which callback gets called, the hook won't stop the event | ||
// from bubbling further up the DOM | ||
const isMiddleMouseButton = event.button === 1; | ||
if (isMiddleMouseButton) { | ||
onMiddleClick?.(event); | ||
} else { | ||
externalOnAuxClick?.(event); | ||
} | ||
}, | ||
}; | ||
}; |