The composedPath()
method of the Event
interface returns the event’s path which is an array of the objects on which listeners will be invoked. This does not include nodes in shadow trees if the shadow root was created with its ShadowRoot.mode
closed.
var composed = Event.composedPath();
None.
An array of EventTarget
objects representing the objects on which an event listener will be invoked.
In our composed-composed-path example (see it live), we define two trivial custom elements, <open-shadow>
and <closed-shadow>
, both of which take the contents of their text attribute and insert them into the element's shadow DOM as the text content of a <p>
element. The only difference between the two is that their shadow roots are attached with their modes set to open
and closed
respectively.
The first definition looks like this, for example:
customElements.define('open-shadow', class extends HTMLElement { constructor() { super(); let pElem = document.createElement('p'); pElem.textContent = this.getAttribute('text'); let shadowRoot = this.attachShadow({mode: 'open'}) .appendChild(pElem); } });
We then insert one of each element into our page:
<open-shadow text="I have an open shadow root"></open-shadow> <closed-shadow text="I have a closed shadow root"></closed-shadow>
Then include a click event listener on the <html>
element:
document.querySelector('html').addEventListener('click',function(e) { console.log(e.composed); console.log(e.composedPath()); });
When you click on the <open-shadow>
element and then the <closed-shadow>
element, you'll notice two things. First, the composed
property returns true
beause the click
event is always able to propagate across shadow boundaries. Second, you'll notice a difference in the value of composedPath
for the two elements. The <open-shadow>
element's composed path is this:
Whereas the <closed-shadow>
element's composed path is a follows:
In the second case, the event listeners only propagate as far as the <closed-shadow>
element itself, but not to the nodes inside the shadow boundary.
Specification | Status | Comment |
---|---|---|
DOM The definition of 'composedPath()' in that specification. | Living Standard |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 53
|
No | 52 | No | 40
|
10 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 53
|
53
|
No | 52 | 40
|
10 | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath