mui / mui/material-ui

[FocusTrap] Option to `preventScroll` when focusing

Open
#36,508 2 comments 4 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

scope: focus trap type: new feature waiting for 👍
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

Duplicates
  • I have searched the existing issues
Latest version
  • I have tested the latest version
Summary 💡

The FocusTrap component should be able to focus without scrolling to the selected element. The default behavior could be set to scroll to the element, and the user can optionally choose to preventScroll={true}.

Examples 🌈

Here is a specific case where FocusTrap scrolls to an element outside the viewport and triggers a layout shift creating a non-actionable white space at the bottom of the page. Note that body has overflow: hidden, and the element which is being scrolled to has position: absolute.

https://codesandbox.io/s/joyride-qwed1r?file=/src/App.tsx

Motivation 🔦

In the example I gave I tried to replicate a specific case where I'm using react-joyride to go through the steps of a tutorial. In such cases the positioning can be dynamic:
For example, I have a dynamically created list of elements and I'm rendering the Popper at the very bottom of that list which can sometimes overflow the window. My guess is that since react-joyride uses a portal-like approach the scroll event triggered by default when we focus causes a layout shift as it has position absolute against html or document. In such circumstances (especially for smaller screens) it might be valuable to provide the option to preventFocus

element.focus({
 preventFocus:true
})

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.