From ec994c3e1f4fc7e3336212a3b1ddc98f58327f71 Mon Sep 17 00:00:00 2001 From: Uchechukwu Obasi Date: Thu, 2 Dec 2021 15:13:44 +0100 Subject: [PATCH] Drawer: apply react-aria for a11y support (#42571) * Drawer: adds focus trapping to widget * add useOverlay hook to drawer * fixes drawer closing when clicked within --- .../src/components/Drawer/Drawer.tsx | 79 +++++++++++-------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/packages/grafana-ui/src/components/Drawer/Drawer.tsx b/packages/grafana-ui/src/components/Drawer/Drawer.tsx index 674470cff95..4502d7ddad2 100644 --- a/packages/grafana-ui/src/components/Drawer/Drawer.tsx +++ b/packages/grafana-ui/src/components/Drawer/Drawer.tsx @@ -7,6 +7,8 @@ import { selectors } from '@grafana/e2e-selectors'; import { CustomScrollbar } from '../CustomScrollbar/CustomScrollbar'; import { IconButton } from '../IconButton/IconButton'; import { stylesFactory, useTheme2 } from '../../themes'; +import { FocusScope } from '@react-aria/focus'; +import { useOverlay } from '@react-aria/overlays'; export interface Props { children: ReactNode; @@ -98,6 +100,13 @@ export const Drawer: FC = ({ const [isExpanded, setIsExpanded] = useState(false); const [isOpen, setIsOpen] = useState(false); const currentWidth = isExpanded ? '100%' : width; + const overlayRef = React.useRef(null); + const { overlayProps } = useOverlay( + { + isDismissable: true, + }, + overlayRef + ); // RcDrawer v4.x needs to be mounted in advance for animations to play. useEffect(() => { @@ -122,46 +131,48 @@ export const Drawer: FC = ({ : selectors.components.Drawer.General.title('no title') } > - {typeof title === 'string' && ( -
-
- {expandable && !isExpanded && ( - setIsExpanded(true)} - surface="header" - aria-label={selectors.components.Drawer.General.expand} - /> - )} - {expandable && isExpanded && ( + + {typeof title === 'string' && ( +
+
+ {expandable && !isExpanded && ( + setIsExpanded(true)} + surface="header" + aria-label={selectors.components.Drawer.General.expand} + /> + )} + {expandable && isExpanded && ( + setIsExpanded(false)} + surface="header" + aria-label={selectors.components.Drawer.General.contract} + /> + )} setIsExpanded(false)} + onClick={onClose} surface="header" - aria-label={selectors.components.Drawer.General.contract} + aria-label={selectors.components.Drawer.General.close} /> - )} - -
-
-

{title}

- {typeof subtitle === 'string' &&
{subtitle}
} - {typeof subtitle !== 'string' && subtitle} +
+
+

{title}

+ {typeof subtitle === 'string' &&
{subtitle}
} + {typeof subtitle !== 'string' && subtitle} +
+ )} + {typeof title !== 'string' && title} +
+ {!scrollableContent ? children : {children}}
- )} - {typeof title !== 'string' && title} -
- {!scrollableContent ? children : {children}} -
+
); };