Skip to content

Commit a6a74be

Browse files
committed
chore(docs): For fullscreen and embedded, change focus
For fullscreen and embedded, put focus on first element in section instead
1 parent b9e68ca commit a6a74be

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

packages/module/patternfly-docs/content/extensions/virtual-assistant/examples/demos/Chatbot.tsx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export const ChatbotDemo: React.FunctionComponent = () => {
171171
const scrollToBottomRef = React.useRef<HTMLDivElement>(null);
172172
const toggleRef = React.useRef<HTMLButtonElement>(null);
173173
const chatbotRef = React.useRef<HTMLDivElement>(null);
174+
const historyRef = React.useRef<HTMLButtonElement>(null);
174175

175176
// Autu-scrolls to the latest message
176177
React.useEffect(() => {
@@ -285,18 +286,29 @@ export const ChatbotDemo: React.FunctionComponent = () => {
285286

286287
const handleSkipToContent = (e) => {
287288
e.preventDefault();
288-
if (displayMode === ChatbotDisplayMode.default) {
289-
if (!chatbotVisible && toggleRef.current) {
290-
toggleRef.current.focus();
291-
}
292-
if (chatbotVisible && chatbotRef.current) {
293-
chatbotRef.current.focus();
294-
}
295-
} else {
296-
if (chatbotRef.current) {
297-
chatbotRef.current.focus();
298-
}
289+
/* eslint-disable indent */
290+
switch (displayMode) {
291+
case ChatbotDisplayMode.default:
292+
if (!chatbotVisible && toggleRef.current) {
293+
toggleRef.current.focus();
294+
}
295+
if (chatbotVisible && chatbotRef.current) {
296+
chatbotRef.current.focus();
297+
}
298+
break;
299+
300+
case ChatbotDisplayMode.docked:
301+
if (chatbotRef.current) {
302+
chatbotRef.current.focus();
303+
}
304+
break;
305+
default:
306+
if (historyRef.current) {
307+
historyRef.current.focus();
308+
}
309+
break;
299310
}
311+
/* eslint-enable indent */
300312
};
301313

302314
return (
@@ -342,7 +354,11 @@ export const ChatbotDemo: React.FunctionComponent = () => {
342354
<>
343355
<ChatbotHeader>
344356
<ChatbotHeaderMain>
345-
<ChatbotHeaderMenu aria-expanded={isDrawerOpen} onMenuToggle={() => setIsDrawerOpen(!isDrawerOpen)} />
357+
<ChatbotHeaderMenu
358+
ref={historyRef}
359+
aria-expanded={isDrawerOpen}
360+
onMenuToggle={() => setIsDrawerOpen(!isDrawerOpen)}
361+
/>
346362
<ChatbotHeaderTitle
347363
displayMode={displayMode}
348364
showOnFullScreen={horizontalLogo}

packages/module/patternfly-docs/content/extensions/virtual-assistant/examples/demos/EmbeddedChatbot.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ export const EmbeddedChatbotDemo: React.FunctionComponent = () => {
176176
const [isSidebarOpen, setIsSidebarOpen] = React.useState(false);
177177
const [announcement, setAnnouncement] = React.useState<string>();
178178
const scrollToBottomRef = React.useRef<HTMLDivElement>(null);
179-
const chatbotRef = React.useRef<HTMLDivElement>(null);
179+
const historyRef = React.useRef<HTMLButtonElement>(null);
180180

181181
const displayMode = ChatbotDisplayMode.embedded;
182182
// Autu-scrolls to the latest message
@@ -307,7 +307,9 @@ export const EmbeddedChatbotDemo: React.FunctionComponent = () => {
307307

308308
const skipToChatbot = (event: React.MouseEvent) => {
309309
event.preventDefault();
310-
chatbotRef.current?.focus();
310+
if (historyRef.current) {
311+
historyRef.current.focus();
312+
}
311313
};
312314

313315
const skipToContent = (
@@ -319,7 +321,7 @@ export const EmbeddedChatbotDemo: React.FunctionComponent = () => {
319321

320322
return (
321323
<Page skipToContent={skipToContent} masthead={masthead} sidebar={sidebar} isContentFilled>
322-
<Chatbot displayMode={displayMode} ref={chatbotRef}>
324+
<Chatbot displayMode={displayMode}>
323325
<ChatbotConversationHistoryNav
324326
displayMode={displayMode}
325327
onDrawerToggle={() => {
@@ -350,7 +352,11 @@ export const EmbeddedChatbotDemo: React.FunctionComponent = () => {
350352
<>
351353
<ChatbotHeader>
352354
<ChatbotHeaderMain>
353-
<ChatbotHeaderMenu aria-expanded={isDrawerOpen} onMenuToggle={() => setIsDrawerOpen(!isDrawerOpen)} />
355+
<ChatbotHeaderMenu
356+
ref={historyRef}
357+
aria-expanded={isDrawerOpen}
358+
onMenuToggle={() => setIsDrawerOpen(!isDrawerOpen)}
359+
/>
354360
<ChatbotHeaderTitle>{horizontalLogo}</ChatbotHeaderTitle>
355361
</ChatbotHeaderMain>
356362
<ChatbotHeaderActions>

packages/module/src/Chatbot/Chatbot.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export interface ChatbotProps {
1515
className?: string;
1616
/** Ref applied to chatbot */
1717
innerRef?: React.Ref<HTMLDivElement>;
18+
/** Custom aria label applied to focusable container */
19+
ariaLabel?: string;
1820
}
1921

2022
export enum ChatbotDisplayMode {
@@ -30,6 +32,7 @@ const ChatbotBase: React.FunctionComponent<ChatbotProps> = ({
3032
isVisible = true,
3133
className,
3234
innerRef,
35+
ariaLabel,
3336
...props
3437
}: ChatbotProps) => {
3538
// Configure docked mode
@@ -59,7 +62,7 @@ const ChatbotBase: React.FunctionComponent<ChatbotProps> = ({
5962
{/* Motion.div does not accept refs */}
6063
{isVisible ? (
6164
<section
62-
aria-label={props['aria-label'] ?? 'Chatbot'}
65+
aria-label={ariaLabel ?? 'Chatbot'}
6366
className={`pf-chatbot-container pf-chatbot-container--${displayMode} ${!isVisible ? 'pf-chatbot-container--hidden' : ''}`}
6467
tabIndex={-1}
6568
ref={innerRef}

packages/module/src/ChatbotHeader/ChatbotHeaderMenu.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ export interface ChatbotHeaderMenuProps {
1212
tooltipProps?: TooltipProps;
1313
/** Aria label for menu */
1414
menuAriaLabel?: string;
15+
/** Ref applied to menu */
16+
innerRef?: React.Ref<HTMLButtonElement>;
1517
}
1618

17-
export const ChatbotHeaderMenu: React.FunctionComponent<ChatbotHeaderMenuProps> = ({
19+
const ChatbotHeaderMenuBase: React.FunctionComponent<ChatbotHeaderMenuProps> = ({
1820
className,
1921
onMenuToggle,
2022
tooltipProps,
21-
menuAriaLabel = 'Toggle menu'
23+
menuAriaLabel = 'Toggle menu',
24+
innerRef
2225
}: ChatbotHeaderMenuProps) => (
2326
<div className={`pf-chatbot__menu ${className}`}>
2427
<Tooltip content="Menu" position="bottom" {...tooltipProps}>
@@ -27,6 +30,7 @@ export const ChatbotHeaderMenu: React.FunctionComponent<ChatbotHeaderMenuProps>
2730
variant="plain"
2831
onClick={onMenuToggle}
2932
aria-label={menuAriaLabel}
33+
ref={innerRef}
3034
icon={
3135
<Icon size="xl" isInline>
3236
<BarsIcon />
@@ -37,4 +41,8 @@ export const ChatbotHeaderMenu: React.FunctionComponent<ChatbotHeaderMenuProps>
3741
</div>
3842
);
3943

40-
export default ChatbotHeaderMenu;
44+
export const ChatbotHeaderMenu = React.forwardRef(
45+
(props: ChatbotHeaderMenuProps, ref: React.Ref<HTMLButtonElement>) => (
46+
<ChatbotHeaderMenuBase innerRef={ref} {...props} />
47+
)
48+
);

0 commit comments

Comments
 (0)