Skip to content

ActionList: Prevent scroll when an item is selected with Space - #4259

Merged
siddharthkp merged 5 commits into
mainfrom
actionlist-avoid-scroll-on-selection
Feb 13, 2024
Merged

ActionList: Prevent scroll when an item is selected with Space#4259
siddharthkp merged 5 commits into
mainfrom
actionlist-avoid-scroll-on-selection

Conversation

@siddharthkp

@siddharthkpsiddharthkp commented Feb 12, 2024

Copy link
Copy Markdown
Member
BeforeAfter
Pressing space selects the focused item but also scrolls the listPressing space selects the focused item but does not scrolls the list, nice!
Pressing space selects the focused item but also scrolls the listPressing space selects the focused item but does not scrolls the list, nice!

But is it safe??

The way to prevent unwanted scroll is to event.preventDefault on ActionList keypress.

const itemKeyPressHandler = (event: React.KeyboardEvent<HTMLLIElement>) => {
+ if (event.key === 'Space') {+ 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;+ }; 
// Enter and Space both select item
if (event.key === 'Enter' || event.key === 'Space') onSelect(event, afterSelect)
}

We don't want this to have any unwanted side effects. This is the code block that is affected, simplified for readability:

constonSelect=(event: React.KeyboardEvent<HTMLLIElement>,afterSelect?: Function,)=>{if(typeofprops.onSelect==='function')props.onSelect(event)// ↑ we always call props.onSelect, so that's safe 🟢 // product developers can still call event.preventDefault() on this event if(event.defaultPrevented)return;// 👀 // ↓ afterSelect would still be called because we reset defaultPrevented 🟢// this is an internal function that is passed by ActionMenu and SelectPanelif(typeofafterSelect==='function')afterSelect()}
  • afterSelect is called when it should, closing the menu automatically 🟢
  • ActionList.LinkItem: Links don't respond to Space, so this is unaffected 🟢
  • Wanted to check if there are any onSelect in dotcom that check for event.defaultPrevented, There are none, so this breaks nothing 🟢

@siddharthkpsiddharthkp self-assigned this Feb 12, 2024
@changeset-bot

changeset-botBot commented Feb 12, 2024

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 85e4290

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@primer/reactPatch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actionsBot commented Feb 12, 2024

Copy link
Copy Markdown
Contributor

size-limit report 📦

PathSize
packages/react/dist/browser.esm.js113.38 KB (+0.01% 🔺)
packages/react/dist/browser.umd.js114.03 KB (+0.02% 🔺)

@github-actions
github-actionsBottemporarily deployed to storybook-preview-4259 February 12, 2024 16:34 Inactive
@siddharthkpsiddharthkp added patch release bug fixes, docs, housekeeping backward-compatible labels Feb 13, 2024
@siddharthkp
siddharthkp added this pull request to the merge queueFeb 13, 2024
Merged via the queue into main with commit c9fbef6Feb 13, 2024
@siddharthkp
siddharthkp deleted the actionlist-avoid-scroll-on-selection branch February 13, 2024 16:22
@primerprimerBot mentioned this pull request Feb 13, 2024
lukasoppermann pushed a commit that referenced this pull request Apr 16, 2024
* prevent default on Space
* immediately reset defaultPrevented
* improve comment
* Create slow-owls-report.md
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

patch releasebug fixes, docs, housekeeping

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@siddharthkp@joshblack