-
Notifications
You must be signed in to change notification settings - Fork 47
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): Add keyboard support to highlight comment flow #690
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ import './PopupHighlight.scss'; | |
|
||
export type Props = { | ||
onCancel?: () => void; | ||
onClick?: (event: React.MouseEvent) => void; | ||
onSubmit?: () => void; | ||
shape: Shape; | ||
}; | ||
|
||
|
@@ -45,7 +45,7 @@ const options: Partial<Options> = { | |
placement: 'bottom', | ||
}; | ||
|
||
export default function PopupHighlight({ onCancel = noop, onClick = noop, shape }: Props): JSX.Element { | ||
export default function PopupHighlight({ onCancel = noop, onSubmit = noop, shape }: Props): JSX.Element { | ||
const buttonRef = React.useRef<HTMLButtonElement>(null); | ||
const { height, width, x, y } = shape; | ||
|
||
|
@@ -60,19 +60,33 @@ export default function PopupHighlight({ onCancel = noop, onClick = noop, shape | |
}), | ||
}; | ||
|
||
const handleClick = (event: React.MouseEvent): void => { | ||
onClick(event); | ||
const handleKeydownOutside = (event: Event): void => { | ||
if (!(event instanceof KeyboardEvent) || event.key !== 'Enter' || event.ctrlKey || event.metaKey) { | ||
return; | ||
} | ||
|
||
event.preventDefault(); // Consume any enter keydown event if the promoter is open | ||
event.stopPropagation(); // Prevent the enter keydown event from adding a new line to the textarea | ||
|
||
onSubmit(); | ||
}; | ||
|
||
useOutsideEvent('keydown', buttonRef, handleKeydownOutside); | ||
useOutsideEvent('mousedown', buttonRef, onCancel); | ||
|
||
return ( | ||
<PopupBase className="ba-PopupHighlight" options={options} reference={reference}> | ||
<PopupBase | ||
aria-live="polite" | ||
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. Does 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. It seems to work well. I found that omitting
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. Should we have some 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. I don't think so. It's a button, so it should be the same interaction as any other interactive element. |
||
className="ba-PopupHighlight" | ||
options={options} | ||
reference={reference} | ||
role="dialog" | ||
> | ||
<button | ||
ref={buttonRef} | ||
className="ba-PopupHighlight-button" | ||
data-testid="ba-PopupHighlight-button" | ||
onClick={handleClick} | ||
onClick={onSubmit} | ||
type="button" | ||
> | ||
<IconHighlightTextAnnotation className="ba-PopupHighlight-icon" /> | ||
|
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.
Is the approach of setting focus to the button on mount viable?
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.
I would rather avoid messing with focus, if possible, since I would need to set it back to the originating element if the popup is closed.