Skip to content

Event not invoked in ShadowDom's non-root node. #15759

Closed

Description

@Jack-Works

Do you want to request a feature or report a bug?
Bug
What is the current behavior?
The second h1 onClick not responding

Reproduce:

<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>

<body>
    <div id="1"></div>
    <div id="2"></div>
</body>
<script>
    function Component() {
        return React.createElement('h1', {onClick() { alert('click!') }}, 'Click me!')
    }
    const shadow1 = document.getElementById('1').attachShadow({mode: 'closed'})
    const shadow2 = document.getElementById('2').attachShadow({mode: 'closed'})
    const inner = document.createElement('div')
    shadow2.appendChild(inner)

    ReactDOM.render(React.createElement(Component), shadow1)
    ReactDOM.render(React.createElement(Component), inner)
</script>

What is the expected behavior?
The first h1 works, the second h1 works too.

Activity

  1. Jack-Works commented on Jun 14, 2019

    @Jack-Works
    ContributorAuthor

    If anyone interested, here is a hack that can resolve problems by event lost due to ShadowRoot.
    Full example at mui/material-ui#16223

    {
            // ? Hack for React, let event go through ShadowDom
            const hackingEvents = new WeakMap()
            function hack(eventName, shadowRoot) {
                shadowRoot.addEventListener(eventName, e => {
                    if (hackingEvents.has(e)) return
                    const path = e.composedPath()
                    var e2 = new e.constructor(e.type, e)
                    hackingEvents.set(e2, path)
                    shadowRoot.dispatchEvent(e2)
                    e.stopPropagation()
                    e.stopImmediatePropagation()
                })
            }
            // call hack with parameter1 = Event name you want to make it work with ShadowRoot
            // parameter2 = the shadow root
            hack('click', portal)
            var nativeTarget = Object.getOwnPropertyDescriptor(Event.prototype, 'target').get
            Object.defineProperty(Event.prototype, 'target', {
                get() {
                    if (hackingEvents.has(this)) return hackingEvents.get(this)[0]
                    return nativeTarget.call(this)
                }
            })
        }
  2. stale commented on Jan 10, 2020

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution.

  3. added
    Resolution: Stale
    on Jan 10, 2020
  4. Jack-Works commented on Jan 10, 2020

    @Jack-Works
    ContributorAuthor

    Wait. This bug is not fixed yet. Don't close it.

  5. removed
    Resolution: Stale
    on Jan 10, 2020
  6. stale commented on Apr 9, 2020

    @stale

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any additional information, please include with in your comment!

  7. added
    Resolution: Stale
    on Apr 9, 2020
  8. Jack-Works commented on Apr 9, 2020

    @Jack-Works
    ContributorAuthor

    Don't close

  9. removed
    Resolution: Stale
    on Apr 9, 2020
  10. stale commented on Jul 11, 2020

    @stale

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

  11. added
    Resolution: Stale
    on Jul 11, 2020
  12. Jack-Works commented on Jul 11, 2020

    @Jack-Works
    ContributorAuthor

    Bump

  13. removed
    Resolution: Stale
    on Jul 11, 2020
  14. stale commented on Oct 12, 2020

    @stale

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

  15. added
    Resolution: Stale
    on Oct 12, 2020
  16. Jack-Works commented on Oct 12, 2020

    @Jack-Works
    ContributorAuthor

    Let me try if it is fixed in react 17 later

  17. removed
    Resolution: Stale
    on Oct 12, 2020
  18. Jack-Works commented on Oct 12, 2020

    @Jack-Works
    ContributorAuthor

    resolved in 17.0.0-rc.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions