From c9fbef6438131cf358e2eaff3d866cf4398e622c Mon Sep 17 00:00:00 2001 From: Siddharth Kshetrapal Date: Tue, 13 Feb 2024 17:13:08 +0100 Subject: [PATCH] ActionList: Prevent scroll when an item is selected with Space (#4259) * prevent default on Space * immediately reset defaultPrevented * improve comment * Create slow-owls-report.md --- .changeset/slow-owls-report.md | 5 +++++ packages/react/src/ActionList/Item.tsx | 6 ++++++ 2 files changed, 11 insertions(+) create mode 100644 .changeset/slow-owls-report.md diff --git a/.changeset/slow-owls-report.md b/.changeset/slow-owls-report.md new file mode 100644 index 00000000000..c572b98f1c2 --- /dev/null +++ b/.changeset/slow-owls-report.md @@ -0,0 +1,5 @@ +--- +"@primer/react": patch +--- + +ActionList: Prevent scroll when an item is selected with `Space` diff --git a/packages/react/src/ActionList/Item.tsx b/packages/react/src/ActionList/Item.tsx index a65a10372c6..ee6592210d7 100644 --- a/packages/react/src/ActionList/Item.tsx +++ b/packages/react/src/ActionList/Item.tsx @@ -229,6 +229,12 @@ export const Item = React.forwardRef( (event: React.KeyboardEvent) => { if (disabled || inactive) return if ([' ', 'Enter'].includes(event.key)) { + if (event.key === ' ') { + event.preventDefault() // prevent scrolling on Space + // immediately reset defaultPrevented once it's job is done + // so as to not disturb the functions that use that event after this + event.defaultPrevented = false + } onSelect(event, afterSelect) } },