Skip to content

Determine event target in Shadow DOM correctly #12163

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fixtures/dom/src/components/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Header extends React.Component {
<option value="/event-pooling">Event Pooling</option>
<option value="/custom-elements">Custom Elements</option>
<option value="/media-events">Media Events</option>
<option value="/shadow-dom">Shadow DOM</option>
</select>
</label>
<label htmlFor="react_version">
Expand Down
3 changes: 3 additions & 0 deletions fixtures/dom/src/components/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ErrorHandling from './error-handling';
import EventPooling from './event-pooling';
import CustomElementFixtures from './custom-elements';
import MediaEventsFixtures from './media-events';
import ShadowDom from './shadow-dom';

const React = window.React;

Expand Down Expand Up @@ -46,6 +47,8 @@ function FixturesPage() {
return <CustomElementFixtures />;
case '/media-events':
return <MediaEventsFixtures />;
case '/shadow-dom':
return <ShadowDom />;
default:
return <p>Please select a test fixture.</p>;
}
Expand Down
67 changes: 67 additions & 0 deletions fixtures/dom/src/components/fixtures/shadow-dom/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import FixtureSet from '../../FixtureSet';
import TestCase from '../../TestCase';

const SUPPORTS_SHADOW_DOM = 'attachShadow' in HTMLElement.prototype;

const React = window.React;
const ReactDOM = window.ReactDOM;

class SelectFixture extends React.Component {
render() {
if (!SUPPORTS_SHADOW_DOM) {
return (
<div>Browser does not support Shadow DOM, no tests to execute.</div>
);
}

return (
<FixtureSet title="Shadow DOM" description="">
<TestCase title="Event listeners in shadow-dom" relatedIssues="4963">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test case fail without this PR? My understanding is the event would still fire, but the target would be wrong, the TestCase doesn't assert anything about the target, only that the event was seen.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test case fails without the other changes in this PR

<TestCase.Steps>
<li>Click on the orange box</li>
</TestCase.Steps>
<TestCase.ExpectedResult>
The box should turn green
</TestCase.ExpectedResult>
<Shadow>
<Box />
</Shadow>
</TestCase>
</FixtureSet>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably also include a closed version that ensures documents/asserts the behavior in that case as well

);
}
}

class Shadow extends React.Component {
componentDidMount() {
this.ref.attachShadow({mode: 'open'});
const el = document.createElement('div');
this.ref.shadowRoot.appendChild(el);
ReactDOM.render(this.props.children, el);
}

render() {
return <div ref={ref => (this.ref = ref)} />;
}
}

class Box extends React.Component {
state = {active: false};

render() {
const style = {
height: 100,
background: this.state.active ? 'green' : 'orange',
color: 'white',
marginBottom: 20,
};
return (
<div
onClick={() => this.setState({active: !this.state.active})}
style={style}
/>
);
}
}

export default SelectFixture;
6 changes: 6 additions & 0 deletions packages/react-dom/src/events/getEventTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import {TEXT_NODE} from '../shared/HTMLNodeType';
function getEventTarget(nativeEvent) {
let target = nativeEvent.target || window;

// If composed / inside open shadow-dom use first item of composed path #9242
if (nativeEvent.composed && typeof nativeEvent.composedPath === 'function') {
const path = nativeEvent.composedPath();
target = path[0];
}

// Normalize SVG <use> element events #4963
if (target.correspondingUseElement) {
target = target.correspondingUseElement;
Expand Down