Skip to content
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(side-panel): close with esc key #5814

16 changes: 15 additions & 1 deletion packages/ibm-products/src/components/SidePanel/SidePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ export let SidePanel = React.forwardRef(
// Title animation on scroll related state
const [labelTextHeight, setLabelTextHeight] = useState<any>(0);

const handleEscapeKey = (event) => {
if (event.key === 'Escape' && open) {
onRequestClose?.();
}
};

useEffect(() => {
if (open && !titleRef?.current) {
setDoAnimateTitle(false);
Expand Down Expand Up @@ -728,6 +734,7 @@ export let SidePanel = React.forwardRef(
className={`${blockClass}__close-button`}
label={closeIconDescription}
onClick={onRequestClose}
onKeyDown={slideIn ? undefined : handleEscapeKey}
title={closeIconDescription}
ariellalgilmore marked this conversation as resolved.
Show resolved Hide resolved
aria-label={closeIconDescription}
ref={closeRef}
Expand Down Expand Up @@ -825,6 +832,13 @@ export let SidePanel = React.forwardRef(
);
};

const handleKeyDown = (event) => {
if (!slideIn) {
handleEscapeKey(event);
keyDownListener(event);
}
};

return (
<AnimatePresence>
{open && (
Expand All @@ -844,7 +858,7 @@ export let SidePanel = React.forwardRef(
animate="visible"
exit="exit"
custom={{ placement, shouldReduceMotion }}
onKeyDown={slideIn ? undefined : keyDownListener}
onKeyDown={handleKeyDown}
>
<>
{/* header */}
Expand Down
Loading