-
Notifications
You must be signed in to change notification settings - Fork 22.8k
Feat: Caret Position Web APIs #40527
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
Merged
+177
−0
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,65 @@ | ||
--- | ||
title: "CaretPosition: getClientRect() method" | ||
short-title: getClientRect() | ||
slug: Web/API/CaretPosition/getClientRect | ||
page-type: web-api-instance-method | ||
browser-compat: api.CaretPosition.getClientRect | ||
--- | ||
|
||
The `getClientRect()` method of the {{domxref("CaretPosition")}} interface returns the client rectangle for the caret range. | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
getClientRect() | ||
``` | ||
|
||
### Parameters | ||
|
||
None. | ||
|
||
### Return value | ||
|
||
A {{domxref("DOMRect")}} object. | ||
|
||
## Examples | ||
|
||
### Get the caret's screen position | ||
|
||
```html | ||
<input | ||
aria-label="text field" | ||
value="Click inside this input field" | ||
style="width: 100%; padding: 10px; font-size: 16px; box-sizing: border-box" /> | ||
``` | ||
|
||
```js | ||
document.querySelector("input").addEventListener("click", (event) => { | ||
const x = event.clientX; | ||
const y = event.clientY; | ||
|
||
const caret = document.caretPositionFromPoint?.(x, y); | ||
if (!caret) return; | ||
|
||
const rect = caret.getClientRect(); | ||
|
||
console.dir("Caret bounding rect:", rect); | ||
console.log(`Caret is at (${rect.x.toFixed(2)}, ${rect.y.toFixed(2)})`); | ||
}); | ||
``` | ||
|
||
{{EmbedLiveSample("get_client_rect", "", 300)}} | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("DOMRect")}} | ||
- {{domxref("Document.caretPositionFromPoint()")}} | ||
- {{domxref("Element.getBoundingClientRect()")}} |
This file contains hidden or 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,57 @@ | ||
--- | ||
title: "CaretPosition: offset property" | ||
short-title: offset | ||
slug: Web/API/CaretPosition/offset | ||
page-type: web-api-instance-property | ||
browser-compat: api.CaretPosition.offset | ||
--- | ||
|
||
The **`offset`** property of the {{domxref("CaretPosition")}} interface returns a `long` representing the offset of the selection in the caret position node. | ||
|
||
This will be the character offset in a text node or the selected child node's index in an element node. | ||
|
||
## Value | ||
|
||
A long. | ||
sideshowbarker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Examples | ||
|
||
This example logs the `offsetNode` and `offset` of the caret position when clicking inside the input field | ||
|
||
```html | ||
<input | ||
aria-label="text field" | ||
value="Click inside this input field" | ||
style="width: 100%; padding: 10px; font-size: 16px; box-sizing: border-box" /> | ||
``` | ||
|
||
```js | ||
document.querySelector("input").addEventListener("click", (event) => { | ||
const x = event.clientX; | ||
const y = event.clientY; | ||
|
||
const caret = document.caretPositionFromPoint?.(x, y); | ||
if (!caret) return; | ||
|
||
const node = caret.offsetNode; | ||
const offset = caret.offset; | ||
|
||
console.dir("offsetNode:", node); | ||
console.log("offset:", offset); | ||
}); | ||
``` | ||
|
||
{{EmbedLiveSample("offset", "", 300)}} | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("Node")}} | ||
- {{domxref("Document.caretPositionFromPoint()")}} |
This file contains hidden or 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,55 @@ | ||
--- | ||
title: "CaretPosition: offsetNode property" | ||
short-title: offsetNode | ||
slug: Web/API/CaretPosition/offsetNode | ||
page-type: web-api-instance-property | ||
browser-compat: api.CaretPosition.offsetNode | ||
--- | ||
|
||
The **`offsetNode`** property of the {{domxref("CaretPosition")}} interface returns a {{domxref("Node")}} containing the found node at the caret's position. | ||
|
||
## Value | ||
|
||
A {{domxref("Node")}}. | ||
|
||
## Examples | ||
|
||
This example logs the `offsetNode` and `offset` of the caret position when clicking inside the input field | ||
|
||
```html | ||
<input | ||
aria-label="text field" | ||
value="Click inside this input field" | ||
style="width: 100%; padding: 10px; font-size: 16px; box-sizing: border-box" /> | ||
``` | ||
|
||
```js | ||
document.querySelector("input").addEventListener("click", (event) => { | ||
const x = event.clientX; | ||
const y = event.clientY; | ||
|
||
const caret = document.caretPositionFromPoint?.(x, y); | ||
if (!caret) return; | ||
|
||
const node = caret.offsetNode; | ||
const offset = caret.offset; | ||
|
||
console.dir("offsetNode:", node); | ||
console.log("offset:", offset); | ||
}); | ||
``` | ||
|
||
{{EmbedLiveSample("offsetnode", "", 300)}} | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("Node")}} | ||
- {{domxref("Document.caretPositionFromPoint()")}} |
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.
Uh oh!
There was an error while loading. Please reload this page.