Skip to content

Commit c87e80c

Browse files
authored
Avoid TypeError in PageLayout checking document.body (#5552)
* Avoid TypeError in PageLayout checking document.body * Make eslint happy
1 parent c0360db commit c87e80c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

.changeset/lazy-jars-leave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": patch
3+
---
4+
5+
Avoid accessing properties of potentially null document.body in PageLayout

packages/react/src/PageLayout/PageLayout.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,21 +364,24 @@ const VerticalDivider: React.FC<React.PropsWithChildren<DividerProps & Draggable
364364
window.addEventListener('keydown', handleKeyDrag)
365365
window.addEventListener('mouseup', handleDragEnd)
366366
window.addEventListener('keyup', handleKeyDragEnd)
367-
document.body.setAttribute('data-page-layout-dragging', 'true')
367+
const body = document.body as HTMLElement | undefined
368+
body?.setAttribute('data-page-layout-dragging', 'true')
368369
} else {
369370
window.removeEventListener('mousemove', handleDrag)
370371
window.removeEventListener('mouseup', handleDragEnd)
371372
window.removeEventListener('keydown', handleKeyDrag)
372373
window.removeEventListener('keyup', handleKeyDragEnd)
373-
document.body.removeAttribute('data-page-layout-dragging')
374+
const body = document.body as HTMLElement | undefined
375+
body?.removeAttribute('data-page-layout-dragging')
374376
}
375377

376378
return () => {
377379
window.removeEventListener('mousemove', handleDrag)
378380
window.removeEventListener('mouseup', handleDragEnd)
379381
window.removeEventListener('keydown', handleKeyDrag)
380382
window.removeEventListener('keyup', handleKeyDragEnd)
381-
document.body.removeAttribute('data-page-layout-dragging')
383+
const body = document.body as HTMLElement | undefined
384+
body?.removeAttribute('data-page-layout-dragging')
382385
}
383386
}, [isDragging, isKeyboardDrag, currentWidth, minWidth, maxWidth])
384387

0 commit comments

Comments
 (0)