Skip to content

Commit

Permalink
fix(side-panel): close with esc key (carbon-design-system#5814)
Browse files Browse the repository at this point in the history
* fix(side-panel): close with esc key

* fix(side-panel): ignore esc slide in

* fix(side-panel): pass event

* fix(side-panel): ensure that slide-in esc doesnt work

* fix(side-panel): cleanup
  • Loading branch information
ariellalgilmore authored Aug 13, 2024
1 parent bd91747 commit 49b0227
Showing 1 changed file with 15 additions and 1 deletion.
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 @@ -287,6 +287,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 @@ -725,6 +731,7 @@ export let SidePanel = React.forwardRef(
className={`${blockClass}__close-button`}
label={closeIconDescription}
onClick={onRequestClose}
onKeyDown={slideIn ? undefined : handleEscapeKey}
title={closeIconDescription}
aria-label={closeIconDescription}
ref={closeRef}
Expand Down Expand Up @@ -822,6 +829,13 @@ export let SidePanel = React.forwardRef(
);
};

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

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

0 comments on commit 49b0227

Please sign in to comment.