Skip to content

Commit ca926de

Browse files
Copilotmattcosta7jonrohan
authored
Defer --dialog-scrollgutter computation to avoid synchronous reflow on page load (#3950)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mattcosta7 <8616962+mattcosta7@users.noreply.github.com> Co-authored-by: Matthew Costabile <mattcosta7@github.com> Co-authored-by: Jon Rohan <rohan@github.com>
1 parent 16a2d75 commit ca926de

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/view-components": patch
3+
---
4+
5+
Defer `--dialog-scrollgutter` computation in `DialogHelperElement` to the moment a dialog is first opened, avoiding a forced synchronous layout reflow during page load.

app/components/primer/dialog_helper.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
function setScrollGutter(doc: Document) {
2+
if (doc.body.style.getPropertyValue('--dialog-scrollgutter')) return
3+
doc.body.style.setProperty('--dialog-scrollgutter', `${window.innerWidth - doc.body.clientWidth}px`)
4+
}
5+
16
function dialogInvokerButtonHandler(event: Event) {
27
const target = event.target as HTMLElement
38
const button = target?.closest('button')
@@ -9,6 +14,7 @@ function dialogInvokerButtonHandler(event: Event) {
914
if (dialogId) {
1015
const dialog = document.getElementById(dialogId)
1116
if (dialog instanceof HTMLDialogElement) {
17+
setScrollGutter(dialog.ownerDocument)
1218
dialog.showModal()
1319
// A buttons default behaviour in some browsers it to send a pointer event
1420
// If the behaviour is allowed through the dialog will be shown but then
@@ -85,10 +91,6 @@ export class DialogHelperElement extends HTMLElement {
8591
const {signal} = (this.#abortController = new AbortController())
8692
document.addEventListener('click', dialogInvokerButtonHandler, true)
8793
document.addEventListener('click', this, {signal})
88-
this.ownerDocument.body.style.setProperty(
89-
'--dialog-scrollgutter',
90-
`${window.innerWidth - this.ownerDocument.body.clientWidth}px`,
91-
)
9294
new MutationObserver(records => {
9395
for (const record of records) {
9496
if (record.target === this.dialog) {
@@ -110,6 +112,7 @@ export class DialogHelperElement extends HTMLElement {
110112
// eslint-disable-next-line no-restricted-syntax
111113
this.dialog.addEventListener('close', e => e.stopImmediatePropagation(), {once: true})
112114
this.dialog.close()
115+
setScrollGutter(this.dialog.ownerDocument)
113116
this.dialog.showModal()
114117
}
115118
}

0 commit comments

Comments
 (0)