-
Notifications
You must be signed in to change notification settings - Fork 23k
Editorial review: Add docs for the HighlightRegistry.highlightsFromPoint() method #40390
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
Changes from 7 commits
d2d14a5
fc68b5a
228f652
47f07c1
b0ebafe
3867bbb
620fa1d
6037da2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,195 @@ | ||||||||||||||
| --- | ||||||||||||||
| title: "HighlightRegistry: highlightsFromPoint() method" | ||||||||||||||
| short-title: highlightsFromPoint() | ||||||||||||||
| slug: Web/API/HighlightRegistry/highlightsFromPoint | ||||||||||||||
| page-type: web-api-instance-method | ||||||||||||||
| browser-compat: api.HighlightRegistry.highlightsFromPoint | ||||||||||||||
| --- | ||||||||||||||
|
|
||||||||||||||
| {{APIRef("CSS Custom Highlight API")}} | ||||||||||||||
|
|
||||||||||||||
| The **`highlightsFromPoint()`** method of the {{domxref("HighlightRegistry")}} interface returns an array of objects representing the custom highlights applied at a specific point within the viewport. | ||||||||||||||
|
|
||||||||||||||
| ## Syntax | ||||||||||||||
|
|
||||||||||||||
| ```js-nolint | ||||||||||||||
| highlightsFromPoint(x, y) | ||||||||||||||
| highlightsFromPoint(x, y, options) | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| ### Parameters | ||||||||||||||
|
|
||||||||||||||
| - `x` | ||||||||||||||
| - : The x-coordinate of the point within the viewport from which to return custom highlight information. | ||||||||||||||
| - `y` | ||||||||||||||
| - : The y-coordinate of the point within the viewport from which to return custom highlight information. | ||||||||||||||
| - `options` {{optional_inline}} | ||||||||||||||
| - : An object containing options, which can include: | ||||||||||||||
| - `shadowRoots` | ||||||||||||||
| - : An array of {{domxref("ShadowRoot")}} objects. Custom highlights that exist at the specified point inside shadow roots in the array will also be included in the return value, in addition to those present in the light DOM. By default, highlights inside shadow roots are not returned. | ||||||||||||||
|
|
||||||||||||||
| ### Return value | ||||||||||||||
|
|
||||||||||||||
| An array of `HighlightHitResult` objects representing the custom highlights applied at the point in the viewport specified by the `x` and `y` parameters. | ||||||||||||||
|
|
||||||||||||||
| Each `HighlightHitResult` object contains the following properties: | ||||||||||||||
|
|
||||||||||||||
| - `highlight` | ||||||||||||||
| - : A {{domxref("Highlight")}} object representing the applied custom highlight. | ||||||||||||||
| - `ranges` | ||||||||||||||
| - : An array of {{domxref("AbstractRange")}} objects representing the ranges to which the custom highlight is applied. | ||||||||||||||
|
|
||||||||||||||
| If no custom highlights are applied at the specified point, or the specified point is outside the viewport, the method returns an empty array. | ||||||||||||||
|
|
||||||||||||||
| ## Examples | ||||||||||||||
|
|
||||||||||||||
| ### Output custom highlights applied at the mouse pointer position | ||||||||||||||
|
|
||||||||||||||
| In this example, you can apply custom highlights to a paragraph of text. These custom highlights can be overlapping. When the user double-clicks the paragraph, we use the `highlightsFromPoint()` method to return the content of any custom highlights located at the mouse pointer coordinates of the double-click. | ||||||||||||||
|
|
||||||||||||||
| #### HTML | ||||||||||||||
|
|
||||||||||||||
| The markup includes a {{htmlelement("p")}} element containing text to which you can apply custom highlights, and a {{htmlelement("section")}} element into which we will output the highlighted text fragments. | ||||||||||||||
|
|
||||||||||||||
| ```html live-sample___highlights-from-point-example | ||||||||||||||
| <h1>highlightsFromPoint() demo</h1> | ||||||||||||||
| <p class="highlightable-text"> | ||||||||||||||
| When you select a section of text then press "h" on the keyboard, the text you | ||||||||||||||
| selected will be given a custom highlight. Multiple highlights will be colored | ||||||||||||||
| yellow, red, and blue, in that order. When you double-click on a highlighted | ||||||||||||||
| section of text, that section will be outputted at the bottom of the UI. If | ||||||||||||||
| multiple highlights overlap the section, you'll see multiple text sections | ||||||||||||||
| outputted. | ||||||||||||||
| </p> | ||||||||||||||
| <h2>Highlighted text at point</h2> | ||||||||||||||
| <section></section> | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| #### CSS | ||||||||||||||
|
|
||||||||||||||
| In the CSS, we define styling for three custom highlights named `highlight1`, `highlight2`, and `highlight3`. We select each custom highlight by passing its name into the {{cssxref("::highlight()")}} pseudo-element, giving them yellow, red, and blue background colors respectively. | ||||||||||||||
|
|
||||||||||||||
| ```css hidden live-sample___highlights-from-point-example | ||||||||||||||
| * { | ||||||||||||||
| box-sizing: border-box; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| body { | ||||||||||||||
| background-color: #fff; | ||||||||||||||
| color: #333; | ||||||||||||||
| font: | ||||||||||||||
| 1em / 1.4 Helvetica Neue, | ||||||||||||||
| Helvetica, | ||||||||||||||
| Arial, | ||||||||||||||
| sans-serif; | ||||||||||||||
| padding: 1em; | ||||||||||||||
| max-width: 800px; | ||||||||||||||
| margin: 0 auto; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| section { | ||||||||||||||
| display: flex; | ||||||||||||||
| gap: 10px; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| .highlightable-text, | ||||||||||||||
| article { | ||||||||||||||
| padding: 10px; | ||||||||||||||
| background-color: #eee; | ||||||||||||||
| border: 2px solid #ddd; | ||||||||||||||
| border-radius: 5px; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| .instructions { | ||||||||||||||
| font-size: 0.8rem; | ||||||||||||||
| } | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| ```css live-sample___highlights-from-point-example | ||||||||||||||
| :root::highlight(highlight1) { | ||||||||||||||
| background-color: rgb(255 255 0 / 0.5); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| :root::highlight(highlight2) { | ||||||||||||||
| background-color: rgb(255 0 0 / 0.5); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| :root::highlight(highlight3) { | ||||||||||||||
| background-color: rgb(0 0 255 / 0.75); | ||||||||||||||
| color: white; | ||||||||||||||
| } | ||||||||||||||
| ``` | ||||||||||||||
|
|
||||||||||||||
| #### JavaScript | ||||||||||||||
|
|
||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||||||||||||||
| We start by grabbing references to the `<p>` element, its contained text node, and our `<section>` element. We then create a variable called `highlightCount`, initially set to `1`, which will be used to specify which custom highlight to apply later on. | ||||||||||||||
|
||||||||||||||
| We start by grabbing references to the `<p>` element, its contained text node, and our `<section>` element. We then create a variable called `highlightCount`, initially set to `1`, which will be used to specify which custom highlight to apply later on. | |
| ##### Create custom highlights | |
| To create custom highlights, we start by grabbing references to the `<p>` element, its contained text node, and our `<section>` element. We then create a variable called `highlightCount`, initially set to `1`, which will be used to specify which custom highlight to apply later on. |
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.
Yeah, this is quite a nice suggestion — see my next commit. I have also moved the section global definition into the second section, so it is right by the point where it gets used.
chrisdavidmills marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
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.
| Next, we define a [`dblclick`](/en-US/docs/Web/API/Element/dblclick_event) event handler function to handle outputting the highlighted text at the mouse cursor position when the event fires. We pass the current mouse coordinates into a `highlightsFromPoint()` call, clear the contents of the `<section>` element, then loop through each highlight in the `highlights` array. | |
| ##### Returning custom highlights from a point | |
| Now that we have the ability to create custom highlights, we can use the highlightsFromPoint() method to return custom highlights from a defined point. | |
| We define a [`dblclick`](/en-US/docs/Web/API/Element/dblclick_event) event handler function to handle outputting the highlighted text at the mouse cursor position when the event fires. We pass the current mouse coordinates into a `highlightsFromPoint()` call, clear the contents of the `<section>` element, then loop through each highlight in the `highlights` array. |
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.
updated
Uh oh!
There was an error while loading. Please reload this page.