-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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(Tag): Add keyboard accessibility #7060
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: fix | ||
fix: | ||
description: 'fix(Tag): Add keyboard accessibility' | ||
links: | ||
- https://github.com/palantir/blueprint/pull/7060 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* ! | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
*/ | ||
|
||
import * as React from "react"; | ||
|
||
import { mergeRefs, Utils } from "../common"; | ||
|
||
type InteractiveHTMLAttributes<E extends HTMLElement> = Pick< | ||
React.HTMLAttributes<E>, | ||
"onBlur" | "onClick" | "onFocus" | "onKeyDown" | "onKeyUp" | "tabIndex" | ||
>; | ||
|
||
interface InteractiveComponentProps extends InteractiveHTMLAttributes<HTMLElement> { | ||
active?: boolean | undefined; | ||
} | ||
|
||
interface InteractiveAttributes<E extends HTMLElement> extends InteractiveHTMLAttributes<E> { | ||
ref: React.Ref<E>; | ||
} | ||
|
||
export function useInteractiveAttributes<E extends HTMLElement>( | ||
interactive: boolean, | ||
props: InteractiveComponentProps, | ||
ref: React.Ref<E>, | ||
defaultTabIndex?: number, | ||
): [active: boolean, interactiveProps: InteractiveAttributes<E>] { | ||
const { active, onClick, onFocus, onKeyDown, onKeyUp, onBlur, tabIndex = defaultTabIndex } = props; | ||
// the current key being pressed | ||
const [currentKeyPressed, setCurrentKeyPressed] = React.useState<string | undefined>(); | ||
// whether the button is in "active" state | ||
const [isActive, setIsActive] = React.useState(false); | ||
// our local ref for the interactive element, merged with the consumer's own ref in this hook's return value | ||
const elementRef = React.useRef<E | null>(null); | ||
|
||
const handleBlur = React.useCallback( | ||
(e: React.FocusEvent<E>) => { | ||
if (isActive) { | ||
setIsActive(false); | ||
} | ||
|
||
onBlur?.(e); | ||
}, | ||
[isActive, onBlur], | ||
); | ||
|
||
const handleKeyDown = React.useCallback( | ||
(e: React.KeyboardEvent<E>) => { | ||
if (Utils.isKeyboardClick(e)) { | ||
e.preventDefault(); | ||
if (e.key !== currentKeyPressed) { | ||
setIsActive(true); | ||
} | ||
} | ||
|
||
setCurrentKeyPressed(e.key); | ||
onKeyDown?.(e); | ||
}, | ||
[currentKeyPressed, onKeyDown], | ||
); | ||
|
||
const handleKeyUp = React.useCallback( | ||
(e: React.KeyboardEvent<E>) => { | ||
if (Utils.isKeyboardClick(e)) { | ||
setIsActive(false); | ||
elementRef.current?.click(); | ||
} | ||
setCurrentKeyPressed(undefined); | ||
onKeyUp?.(e); | ||
}, | ||
[onKeyUp, elementRef], | ||
); | ||
|
||
const resolvedActive = interactive && (active || isActive); | ||
|
||
return [ | ||
resolvedActive, | ||
{ | ||
onBlur: handleBlur, | ||
onClick: interactive ? onClick : undefined, | ||
onFocus: interactive ? onFocus : undefined, | ||
onKeyDown: handleKeyDown, | ||
onKeyUp: handleKeyUp, | ||
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. Would this function the same if we were to conditionally render the inclusion of all the handlers based on the const handlers = {
onBlur: handleBlur,
onClick,
onFocus,
onKeyDown: handleKeyDown,
onKeyUp: handleKeyUp,
};
return [
resolvedActive,
{
...(interactive ? handlers : {}),
ref: mergeRefs(elementRef, ref),
tabIndex: interactive ? tabIndex : -1,
},
]; 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. Very likely I think, but I wanted to quite safe to avoid any unintentional side effects so I wanted to match the button behavior as close as possible. |
||
ref: mergeRefs(elementRef, ref), | ||
tabIndex: interactive ? tabIndex : -1, | ||
}, | ||
]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we add tests for this hook with
@testing-library/react-hooks
in a follow-up PR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, sgtm