-
Notifications
You must be signed in to change notification settings - Fork 22.6k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
908f4c1
New pages: ShadowRoot.elementFromPoint()
estelle c5bea04
return value
estelle 3968337
Merge branch 'main' into shadowroot
estelle 61acb3d
Merge branch 'main' into shadowroot
estelle 087a658
edits based on review
estelle c480c75
Update files/en-us/web/api/shadowroot/elementsfrompoint/index.md
estelle a804380
Update files/en-us/web/api/shadowroot/elementfrompoint/index.md
estelle 2fcc30f
Update files/en-us/web/api/shadowroot/elementsfrompoint/index.md
estelle 79b3739
Update files/en-us/web/api/shadowroot/elementsfrompoint/index.md
estelle 223b5d2
Update index.md
estelle 0014b3f
Update files/en-us/web/api/shadowroot/elementsfrompoint/index.md
estelle d951c46
Update files/en-us/web/api/shadowroot/elementsfrompoint/index.md
estelle fc931f2
Update files/en-us/web/api/shadowroot/elementfrompoint/index.md
wbamberg ab29701
Update files/en-us/web/api/shadowroot/elementsfrompoint/index.md
wbamberg 44a4457
Merge branch 'main' into shadowroot
wbamberg 7f3ec2b
Merge branch 'main' into shadowroot
wbamberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
title: "ShadowRoot: elementFromPoint() method" | ||
short-title: elementFromPoint() | ||
slug: Web/API/ShadowRoot/elementFromPoint | ||
page-type: web-api-instance-method | ||
browser-compat: api.ShadowRoot.elementFromPoint | ||
--- | ||
|
||
{{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 (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. | ||
|
||
If the specified point is outside the bounds of the shadow root, the result is `undefined`. | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
elementFromPoint(x, y) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `x` | ||
- : The horizontal coordinate of a point, relative to the left edge of the current {{Glossary("viewport")}}. | ||
- `y` | ||
- : The vertical coordinate of a point, relative to the top edge of the current viewport. | ||
|
||
### Return value | ||
|
||
An {{domxref("Element")}}; the topmost shadow root element located at the specified coordinates, if any. | ||
|
||
## Examples | ||
|
||
In this example, assuming the existence of a {{htmlelement("template")}} in the HTML, we define a `<my-custom-element>`. If the appended custom element abuts the top-left corner of the viewport, or any portion of it overlaps that corner, the element that is the topmost layer at that point in the custom element will have a thin, dashed red border. | ||
|
||
```js | ||
customElements.define( | ||
"my-custom-element", | ||
class extends HTMLElement { | ||
constructor() { | ||
super(); | ||
const templateContent = document.getElementById( | ||
"my-custom-element-template", | ||
).content; | ||
const sRoot = this.attachShadow({ mode: "open" }); | ||
sRoot.appendChild(templateContent.cloneNode(true)); | ||
|
||
// get the topmost element in the top left corner of the viewport | ||
const srElement = this.shadowRoot.elementFromPoint(0, 0); | ||
// apply a border to that element | ||
srElement.style.border = "1px dashed red"; | ||
} | ||
}, | ||
); | ||
``` | ||
|
||
## Specifications | ||
|
||
Not part of any standard. | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("ShadowRoot.elementsFromPoint()")}} | ||
- {{domxref("Document.elementFromPoint()")}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
--- | ||
title: "ShadowRoot: elementsFromPoint() method" | ||
short-title: elementsFromPoint() | ||
slug: Web/API/ShadowRoot/elementsFromPoint | ||
page-type: web-api-instance-method | ||
browser-compat: api.ShadowRoot.elementsFromPoint | ||
--- | ||
|
||
{{APIRef("DOM")}}{{Non-standard_Header}} | ||
|
||
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 element (highest in the display z-order), 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 [shadow DOM](/en-US/docs/Web/API/Web_components/Using_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). | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
elementsFromPoint(x, y) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `x` | ||
- : The horizontal coordinate of a point, relative to the left edge of the current {{Glossary("viewport")}}. | ||
- `y` | ||
- : The vertical coordinate of a point, relative to the top edge of the current viewport. | ||
|
||
### Return value | ||
|
||
An array of {{domxref('Element')}} objects. | ||
|
||
## Examples | ||
|
||
```js | ||
const customElem = document.querySelector("my-custom-element"); | ||
const shadow = customElem.shadowRoot; | ||
const elements = shadow.elementsFromPoint(20, 20); | ||
const msg = elements.map((el) => el.localName).join(" < "); | ||
if (msg) { | ||
console.log(msg); | ||
} else { | ||
console.log("The custom element had no descendants at x: 20, y: 20."); | ||
} | ||
``` | ||
|
||
If `<my-custom-element>` is near the top left corner of the viewport, and contains a single `<div>`, the above may return either of the following, depending on the browser implementation: | ||
|
||
```plain | ||
div | ||
div < my-custom-element < body < html | ||
``` | ||
|
||
## Specifications | ||
|
||
Not part of any standard. | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{DOMxRef("ShadowRoot.elementFromPoint()")}} | ||
- {{DOMxRef("Document.elementsFromPoint()")}} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI it is now common, but not mandatory (AFAIK) to always have a descriptive level 3 heading as well as the main heading. This is a good thing to do, but up to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the example is just a code example, not a rendered example. I generally add the subheading on live examples or when there are multiple. here is would be overkill.