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

New pages: ShadowRoot.elementFromPoint() #37115

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

New pages: ShadowRoot.elementFromPoint() #37115

wants to merge 5 commits into from

Conversation

estelle
Copy link
Member

@estelle estelle commented Dec 6, 2024

@estelle estelle requested a review from a team as a code owner December 6, 2024 02:43
@estelle estelle requested review from wbamberg and removed request for a team December 6, 2024 02:43
@github-actions github-actions bot added Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed labels Dec 6, 2024
Copy link
Contributor

github-actions bot commented Dec 6, 2024

Preview URLs

Flaws (1)

Note! 1 document with no flaws that don't need to be listed. 🎉

URL: /en-US/docs/Web/API/ShadowRoot/elementsFromPoint
Title: ShadowRoot: elementsFromPoint() method
Flaw count: 1

  • macros:
    • /en-US/docs/Glossary/shadow_DOM does not exist

(comment last updated: 2025-01-12 03:01:47)


{{APIRef("DOM")}}{{Non-standard_Header}}

The **`elementFromPoint()`** method, available on the {{domxref("ShadowRoot")}} object, returns the topmost shadowroot element at the specified coordinates relative to the viewport that does not have {{cssxref("pointer-events")}} set to `none`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does "topmost" mean? Is it in the topmost layer, as in "top layer"? Or first in the DOM?


{{APIRef("DOM")}}{{Non-standard_Header}}

The **`elementFromPoint()`** method, available on the {{domxref("ShadowRoot")}} object, returns the topmost shadowroot element at the specified coordinates relative to the viewport that does not have {{cssxref("pointer-events")}} set to `none`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This has "shadowroot", the next para has "shadowRoot", and the other page has "shadow root". I think "shadow root" is probably best. This also ought to link to somewhere shadow root is defined. Maybe https://developer.mozilla.org/en-US/docs/Glossary/Shadow_tree ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the "does not have {{cssxref("pointer-events")}} set to none." is confusing, it's not obvious what it refers to. I assume it is the shadow root element? So it would be clearer to make this a separate sentence, like "Shadow root elements which have {{cssxref("pointer-events")}} set to none are ignored."

Comment on lines 38 to 53
```js
customElements.define(
"my-custom-element",
class extends HTMLElement {
constructor() {
super();
let template = document.getElementById("my-custom-element-template");
let templateContent = template.content;
const sRoot = this.attachShadow({ mode: "open" });
sRoot.appendChild(templateContent.cloneNode(true));
let srElement = this.shadowRoot.elementFromPoint(0, 0);
srElement.style.border = "1px dashed red";
}
},
);
```
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure I really understand the use case this example is serving. As a specific code comment, all these let declarations could be const.

The **`elementsFromPoint()`** method of the {{domxref("ShadowRoot")}} interface returns an array of all the shadow root elements
at the specified coordinates (relative to the viewport). The elements are ordered from the topmost descendant to the bottommost element.

It operates in a similar way to the {{domxref("ShadowRoot.elementFromPoint", "elementFromPoint()")}} method. Some browsers return only the shadow root elements present at that location. Other browsers include elements outside of the {{glossary("shadow DOM")}}, from the topmost shadow DOM element to the document root node, such as the {{htmlelement("html")}} or {{SVGElement("svg")}} root element. In these browsers, it operates similar to the {{domxref("Document.elementsFromPoint", "elementFromPoint()")}} method, but with the ability to cross the [shadow boundary](/en-US/docs/Glossary/Shadow_tree).
Copy link
Collaborator

Choose a reason for hiding this comment

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

This paragraph is confusing because the link text elementFromPoint() is the same although the actual linked page is different.

Comment on lines +24 to +27
- `x`
- : The horizontal coordinate of a point.
- `y`
- : The vertical coordinate of a point.
Copy link
Collaborator

Choose a reason for hiding this comment

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

The other page specified the origin for these, but this doesn't.

Comment on lines +38 to +45
let msg = "";
let elements = shadow.elementsFromPoint(20, 20);
elements.forEach((el, i) => {
msg += el.localName;
if (i < elements.length - 1) {
msg += " < ";
}
});
Copy link
Collaborator

@wbamberg wbamberg Jan 8, 2025

Choose a reason for hiding this comment

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

Suggested change
let msg = "";
let elements = shadow.elementsFromPoint(20, 20);
elements.forEach((el, i) => {
msg += el.localName;
if (i < elements.length - 1) {
msg += " < ";
}
});
const elements = shadow.elementsFromPoint(20, 20);
const msg = elements.map(el => el.localName).join(" < ");

sometimes JavaScript is nice.

files/en-us/web/api/shadowroot/elementsfrompoint/index.md Outdated Show resolved Hide resolved
@estelle estelle requested a review from wbamberg January 12, 2025 03:00
Comment on lines +11 to +12
The **`elementsFromPoint()`** method of the {{domxref("ShadowRoot")}} interface returns an array of all the shadow root elements
at the specified coordinates (relative to the viewport). The elements are ordered from the element in the top most layer, to the bottommost element.
Copy link
Collaborator

@hamishwillee hamishwillee Jan 20, 2025

Choose a reason for hiding this comment

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

Edited. Topmost and bottommost are words. Who would have thunk. Merriam Webster hmmmfff

Suggested change
The **`elementsFromPoint()`** method of the {{domxref("ShadowRoot")}} interface returns an array of all the shadow root elements
at the specified coordinates (relative to the viewport). The elements are ordered from the element in the top most layer, to the bottommost element.
The **`elementsFromPoint()`** method of the {{domxref("ShadowRoot")}} interface returns an array of all the shadow root elements at the specified coordinates (relative to the viewport). The elements are ordered from the element in the topmost layer, to the bottommost element.

The **`elementsFromPoint()`** method of the {{domxref("ShadowRoot")}} interface returns an array of all the shadow root elements
at the specified coordinates (relative to the viewport). The elements are ordered from the element in the top most layer, to the bottommost element.

It operates in a similar way to the {{domxref("ShadowRoot.elementFromPoint")}} method. Some browsers return only the shadow root elements present at that location. Other browsers include elements outside of the {{glossary("shadow DOM")}}, from the shadow DOM element in the topmost layer to the document root node, such as the {{htmlelement("html")}} or {{SVGElement("svg")}} root element. In these browsers, it operates similar to the {{domxref("Document.elementsFromPoint")}} method, but with the ability to cross the [shadow boundary](/en-US/docs/Glossary/Shadow_tree).
Copy link
Collaborator

Choose a reason for hiding this comment

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

{{glossary("shadow DOM")}} is a red link. Normally this points to the shadow tree.

The **`elementsFromPoint()`** method of the {{domxref("ShadowRoot")}} interface returns an array of all the shadow root elements
at the specified coordinates (relative to the viewport). The elements are ordered from the element in the top most layer, to the bottommost element.

It operates in a similar way to the {{domxref("ShadowRoot.elementFromPoint")}} method. Some browsers return only the shadow root elements present at that location. Other browsers include elements outside of the {{glossary("shadow DOM")}}, from the shadow DOM element in the topmost layer to the document root node, such as the {{htmlelement("html")}} or {{SVGElement("svg")}} root element. In these browsers, it operates similar to the {{domxref("Document.elementsFromPoint")}} method, but with the ability to cross the [shadow boundary](/en-US/docs/Glossary/Shadow_tree).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Some browsers return only the shadow root elements present at that location. Other browsers include elements outside of

Sounds like a compatibility issue. Should it be in the bcd? Appreciate there is no spec, so there is not "right" way, but there are still behavioural differences.


{{APIRef("DOM")}}{{Non-standard_Header}}

The **`elementFromPoint()`** method, available on the {{domxref("ShadowRoot")}} object, returns the element at the topmost shadow root layer at the specified coordinates relative to the viewport. Shadow root elements that have {{cssxref("pointer-events")}} set to `none` are ignored.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I only just worked out (what I think) this means by reading the following document (interface returns an array of all the shadow root elements at the specified coordinates (relative to the viewport).

So what is happening here is that you might have multiple elements located at a point, some visible, some invisible, some in the HTML dom and some in the shadow dom. There are layered via the z-order, and the element that is returned is the one in the topmost layer - i.e. the one that would receive the point event (unless the shadow root doesn't accept pointer events).

I mistook this "topmost" think to be about the nesting of elements within the shadow root, and to mean the shadow root itself. That might be my lack of knowledge, but you might want to word this to avoid the possible.

You could for example, do something like

Suggested change
The **`elementFromPoint()`** method, available on the {{domxref("ShadowRoot")}} object, returns the element at the topmost shadow root layer at the specified coordinates relative to the viewport. Shadow root elements that have {{cssxref("pointer-events")}} set to `none` are ignored.
The **`elementFromPoint()`** method, available on the {{domxref("ShadowRoot")}} object, returns the element at the topmost shadow root layer at the specified coordinates relative to the viewport (the shadow root highest in the display z-order, that is able to receive pointer events). Shadow root elements that have {{cssxref("pointer-events")}} set to `none` are ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants