Skip to content

Commit 3234dc3

Browse files
authored
Merge pull request #238 from rebeccaalpert/a11y-224
fix(MessageBox): MessageBox should have tabIndex, role, and aria-label
2 parents 2375d4b + 697dba0 commit 3234dc3

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

packages/module/src/MessageBox/MessageBox.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ import React from 'react';
55
import JumpButton from './JumpButton';
66

77
export interface MessageBoxProps extends React.HTMLProps<HTMLDivElement> {
8+
/** Content that can be announced, such as a new message, for screen readers */
9+
announcement?: string;
10+
/** Custom aria-label for scrollable portion of message box */
11+
ariaLabel?: string;
812
/** Content to be displayed in the message box */
913
children: React.ReactNode;
1014
/** Custom classname for the MessageBox component */
1115
className?: string;
12-
/** Content that can be announced, such as a new message, for screen readers */
13-
announcement?: string;
1416
}
1517

1618
const MessageBox: React.FunctionComponent<MessageBoxProps> = ({
1719
announcement,
20+
ariaLabel = 'Scrollable message log',
1821
children,
1922
className
2023
}: MessageBoxProps) => {
@@ -28,7 +31,6 @@ const MessageBox: React.FunctionComponent<MessageBoxProps> = ({
2831
const element = messageBoxRef.current;
2932
if (element) {
3033
const { scrollTop, scrollHeight, clientHeight } = element;
31-
3234
setAtTop(scrollTop === 0);
3335
setAtBottom(Math.round(scrollTop) + Math.round(clientHeight) >= Math.round(scrollHeight) - 1); // rounding means it could be within a pixel of the bottom
3436
}
@@ -76,7 +78,13 @@ const MessageBox: React.FunctionComponent<MessageBoxProps> = ({
7678
return (
7779
<>
7880
<JumpButton position="top" isHidden={isOverflowing && atTop} onClick={scrollToTop} />
79-
<div className={`pf-chatbot__messagebox ${className ?? ''}`} ref={messageBoxRef}>
81+
<div
82+
role="region"
83+
tabIndex={0}
84+
aria-label={ariaLabel}
85+
className={`pf-chatbot__messagebox ${className ?? ''}`}
86+
ref={messageBoxRef}
87+
>
8088
{children}
8189
<div className="pf-chatbot__messagebox-announcement" aria-live="polite">
8290
{announcement}

0 commit comments

Comments
 (0)