-
Notifications
You must be signed in to change notification settings - Fork 22.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document Layout Instability API (#23007)
* Fix group, redirect overview page * Add layoutshift to general performance pages * Update LayoutShift pages * Apply suggestions from code review Co-authored-by: wbamberg <[email protected]> * Fix more nodes * Expand on why hadRecentInput is useful * Avoid CLS * Fix links * tags -> status * Fix spec list * tweaks as per wbamberg --------- Co-authored-by: wbamberg <[email protected]>
- Loading branch information
Showing
23 changed files
with
383 additions
and
102 deletions.
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
This file was deleted.
Oops, something went wrong.
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,56 @@ | ||
--- | ||
title: LayoutShift.hadRecentInput | ||
slug: Web/API/LayoutShift/hadRecentInput | ||
page-type: web-api-instance-property | ||
browser-compat: api.LayoutShift.hadRecentInput | ||
status: | ||
- experimental | ||
--- | ||
|
||
{{SeeCompatTable}}{{APIRef("Performance API")}} | ||
|
||
The **`hadRecentInput`** read-only property of the {{domxref("LayoutShift")}} interface returns `true` if {{domxref("LayoutShift.lastInputTime", "lastInputTime")}} is less than 500 milliseconds in the past. | ||
|
||
Layout shifts are only a problem if the user is not expecting them, so layout shifts that are the result of user interactions (such as a user expanding a UI element) are often not considered in layout shift metrics. The `hadRecentInput` property allows you to exclude these shifts. | ||
|
||
## Value | ||
|
||
A boolean returning `true` if {{domxref("LayoutShift.lastInputTime", "lastInputTime")}} is less than 500 milliseconds in the past; `false` otherwise. | ||
|
||
## Examples | ||
|
||
### Ignoring recent user input for layout shift scores | ||
|
||
The following example shows how the `hadRecentInput` property is used to only count layout shifts without recent user input. | ||
|
||
```js | ||
const observer = new PerformanceObserver((list) => { | ||
for (const entry of list.getEntries()) { | ||
// Count layout shifts without recent user input only | ||
if (!entry.hadRecentInput) { | ||
console.log("LayoutShift value:", entry.value); | ||
if (entry.sources) { | ||
for (const { node, currentRect, previousRect } of entry.sources) | ||
console.log("LayoutShift source:", node, { | ||
currentRect, | ||
previousRect, | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
observer.observe({ type: "layout-shift", buffered: true }); | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("LayoutShift.lastInputTime")}} |
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
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,54 @@ | ||
--- | ||
title: LayoutShift.lastInputTime | ||
slug: Web/API/LayoutShift/lastInputTime | ||
page-type: web-api-instance-property | ||
browser-compat: api.LayoutShift.lastInputTime | ||
status: | ||
- experimental | ||
--- | ||
|
||
{{SeeCompatTable}}{{APIRef("Performance API")}} | ||
|
||
The **`lastInputTime`** read-only property of the {{domxref("LayoutShift")}} interface returns the time of the most recent excluding input or `0` if no excluding input has occurred. | ||
|
||
Layout shifts are only bad if the user wasn't expecting them. Many layout shift metrics (like [Cumulative Layout Shift (CLS)](https://web.dev/cls/)) exclude shifts that occurred soon after certain user interactions. These interactions are called _excluding inputs_. Excluding inputs are: | ||
|
||
- Any events which signal a user's active interaction with the document: ([`mousedown`](/en-US/docs/Web/API/Element/mousedown_event), [`keydown`](/en-US/docs/Web/API/Element/keydown_event), and [`pointerdown`](/en-US/docs/Web/API/Element/pointerdown_event)) | ||
- Any events which directly changes the size of the viewport. | ||
- [`change`](/en-US/docs/Web/API/HTMLElement/change_event) events. | ||
|
||
The [`mousemove`](/en-US/docs/Web/API/Element/mousemove_event) and [`pointermove`](/en-US/docs/Web/API/Element/pointermove_event) events are **not** excluding inputs. | ||
|
||
## Value | ||
|
||
A {{domxref("DOMHighResTimeStamp")}} indicating the most recent excluding input time or `0` if no excluding input has occurred. | ||
|
||
## Examples | ||
|
||
### Logging last input times | ||
|
||
Log excluding input times if excluding input has occurred. | ||
|
||
```js | ||
const observer = new PerformanceObserver((list) => { | ||
list.getEntries().forEach((entry) => { | ||
if (entry.lastInputTime) { | ||
console.log(entry.lastInputTime); | ||
} | ||
}); | ||
}); | ||
|
||
observer.observe({ type: "layout-shift", buffered: true }); | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("LayoutShift.hadRecentInput")}} |
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,44 @@ | ||
--- | ||
title: LayoutShift.sources | ||
slug: Web/API/LayoutShift/sources | ||
page-type: web-api-instance-property | ||
browser-compat: api.LayoutShift.sources | ||
status: | ||
- experimental | ||
--- | ||
|
||
{{SeeCompatTable}}{{APIRef("Performance API")}} | ||
|
||
The **`sources`** read-only property of the {{domxref("LayoutShift")}} interface returns an array of {{domxref("LayoutShiftAttribution")}} objects that indicate the DOM elements that moved during the layout shift. | ||
|
||
## Value | ||
|
||
An {{jsxref("Array")}} of {{domxref("LayoutShiftAttribution")}} objects. This array will not contain more than five sources. If there are more than five elements impacted by the layout shift, the five most impactful elements are reported. | ||
|
||
## Examples | ||
|
||
### Logging layout shift sources | ||
|
||
```js | ||
const observer = new PerformanceObserver((list) => { | ||
list.getEntries().forEach((entry) => { | ||
entry.sources.forEach((source) => { | ||
console.log(source); | ||
}); | ||
}); | ||
}); | ||
|
||
observer.observe({ type: "layout-shift", buffered: true }); | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{domxref("LayoutShiftAttribution")}} |
Oops, something went wrong.