question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[TrapFocus] Can't focus input attached under shadow-DOM

See original GitHub issue

Whenever I click on a TextField to start editing it, thus the focus will be lost immediately.

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

chrome-capture

Expected Behavior 🤔

On clicking, the Textfield can get focus normally.

Steps to Reproduce 🕹

Example: https://codesandbox.io/s/material-demo-forked-bi51w?file=/index.tsx

Steps:

  1. Click the TextField at the bottom

Context 🔦

class RTWebComponent extends HTMLElement {
  connectedCallback() {
    const shadowRoot = this.attachShadow({ mode: "open" });
    const mountPoint = document.createElement("div");
    mountPoint.id = "ResourceTimeline";
    const reactRoot = shadowRoot.appendChild(mountPoint);

    const jss = create({
      ...jssPreset(),
      insertionPoint: reactRoot
    });

    ReactDOM.render(
      <div>
        <StylesProvider jss={jss}>
          <TemporaryDrawerLoseFocus />
        </StylesProvider>
        {/* <TemporaryDrawerWithoutStlyle /> */}
      </div>,
      mountPoint
    );
  }
}

customElements.define("resource-timeline", RTWebComponent);
export default class TemporaryDrawer extends React.Component {
  render() {
    return (
      <div>
        {/* <ThemeProvider theme={this.getTheme()}> */}
        <Drawer anchor={"top"} open={true}>
          <TextField
            id="111111"
            label="Flight Number"
            variant="outlined"
            margin="dense"
            fullWidth
            defaultValue={"1231"}
          />
        </Drawer>
        {/* </ThemeProvider> */}
      </div>
    );
  }
}

Your Environment 🌎

`npx @material-ui/envinfo`
  System:
    OS: Windows 10 10.0.19041
  Binaries:
    Node: 12.18.3 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.5 - C:\opt\Yarn\bin\yarn.CMD
    npm: 6.14.6 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 87.0.4280.88
    Edge: Spartan (44.19041.423.0), Chromium (87.0.664.60)
  npmPackages:
    @material-ui/core: 4.11.2 => 4.11.2
    @material-ui/icons: 4.11.2 => 4.11.2
    @material-ui/styles:  4.11.2
    @material-ui/system:  4.11.2
    @material-ui/types:  5.1.0
    @material-ui/utils:  4.11.2
    @types/react: 17.0.0 => 17.0.0
    react: 17.0.1 => 17.0.1
    react-dom: 17.0.1 => 17.0.1
    typescript: 4.1.3 => 4.1.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
sag1vcommented, Jan 7, 2021

@natezhengbne When passing the Drawer the modal prop ModalProps={{disableEnforceFocus: true}} it seems to work.

You can do that globally via the theme as well

2reactions
oliviertassinaricommented, Dec 17, 2020

We have fixed this issue along the way in v5. I believe we fixed it when we changed the logic of the TrapFocus to only restore the focus when escaping and landing on the body. Proof: https://codesandbox.io/s/material-demo-forked-6ln7y?file=/TemporaryDrawerLoseFocus.tsx.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[TrapFocus] Can't focus input attached under shadow-DOM
Whenever I click on a TextField to start editing it, thus the focus will be lost immediately. The issue is present in the...
Read more >
shadowElement.focus() — impossible? - Stack Overflow
I've found that it seems impossible to focus on any element in the shadow dom. we have shadow options like delegatesFocus , and...
Read more >
Managing focus in the shadow DOM | Read the Tea Leaves
So the first challenge is to emulate whatever the browser normally does when you press Tab or Shift + Tab . In this...
Read more >
focus-trap demo - GitHub Pages
Here is a focus trap in an open Shadow DOM with some focusable parts. deactivate trap. View demo source >>. with shadow dom....
Read more >
Accessibility - Angular Material
The cdkTrapFocus directive traps Tab key focus within an element. This is intended to be used to create accessible experience for components like...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found