Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
natezhengbne opened this issue Dec 17, 2020 · 12 comments
Closed
2 tasks done

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

natezhengbne opened this issue Dec 17, 2020 · 12 comments
Labels
bug 🐛 Something doesn't work component: FocusTrap The React component.

Comments

@natezhengbne
Copy link

natezhengbne commented Dec 17, 2020

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
@natezhengbne natezhengbne added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Dec 17, 2020
@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work component: FocusTrap The React component. and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Dec 17, 2020
@oliviertassinari
Copy link
Member

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.

@oliviertassinari oliviertassinari changed the title TexField lose focus on click in the Drawer attached under shadow-DOM [TrapFocus] Can't focus input attached under shadow-DOM Dec 17, 2020
@sag1v
Copy link

sag1v commented Jan 5, 2021

@oliviertassinari What about version 4.11.0?

@natezhengbne
Copy link
Author

@oliviertassinari What about version 4.11.0?

I tried here but still not work on version 4.11.0

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

@oliviertassinari
Copy link
Member

We have no plan to backport fixes from v5 to v4.

@sag1v
Copy link

sag1v commented Jan 5, 2021

@natezhengbne the problem is the drawer not the text field itself.

@oliviertassinari Anyway to workaround this issue? as migrating from v4 to v5 is painful.
We actually have some more issues with shadow-dom, like making all popover components (modals tooltips etc) to be contained by default.

@sag1v
Copy link

sag1v commented 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

@natezhengbne
Copy link
Author

@sag1v it works, big thanks
We also have tricky issues with the shadow-dom, same as you have.
Meanwhile, I also use version 4. It is more stable.

@natezhengbne
Copy link
Author

Hi @oliviertassinari , could you have a look at this issue?
The popper also couldn't be included in shadow-root.
Sandbox: https://codesandbox.io/s/material-demo-forked-nwh3s?file=/theme-provider.tsx:791-806
Local snaphot:
image

@oliviertassinari
Copy link
Member

See #17473 for the support of shadow DOM

@natezhengbne
Copy link
Author

@sag1v
If the mount point can be pass thought the popover components to the inner popper,
the popper component will be contained by the shadow-DOM.

@petermikitsh
Copy link
Contributor

If you're using Shadow DOM, and have a TextField inside of a Menu, in v4 you can set the disableEnforceFocus prop:

<Menu disableEnforceFocus>
  <TextField />
</Menu>

@hata6502
Copy link

hata6502 commented Sep 6, 2021

If you're using Shadow DOM, and have a TextField inside of a Menu, in v4 you can set the disableEnforceFocus prop:

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: FocusTrap The React component.
Projects
None yet
Development

No branches or pull requests

5 participants