Skip to content

Commit

Permalink
Avoid TypeError in PageLayout checking document.body (#5552)
Browse files Browse the repository at this point in the history
* Avoid TypeError in PageLayout checking document.body

* Make eslint happy
  • Loading branch information
camertron authored Jan 16, 2025
1 parent c0360db commit c87e80c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-jars-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Avoid accessing properties of potentially null document.body in PageLayout
9 changes: 6 additions & 3 deletions packages/react/src/PageLayout/PageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -364,21 +364,24 @@ const VerticalDivider: React.FC<React.PropsWithChildren<DividerProps & Draggable
window.addEventListener('keydown', handleKeyDrag)
window.addEventListener('mouseup', handleDragEnd)
window.addEventListener('keyup', handleKeyDragEnd)
document.body.setAttribute('data-page-layout-dragging', 'true')
const body = document.body as HTMLElement | undefined
body?.setAttribute('data-page-layout-dragging', 'true')
} else {
window.removeEventListener('mousemove', handleDrag)
window.removeEventListener('mouseup', handleDragEnd)
window.removeEventListener('keydown', handleKeyDrag)
window.removeEventListener('keyup', handleKeyDragEnd)
document.body.removeAttribute('data-page-layout-dragging')
const body = document.body as HTMLElement | undefined
body?.removeAttribute('data-page-layout-dragging')
}

return () => {
window.removeEventListener('mousemove', handleDrag)
window.removeEventListener('mouseup', handleDragEnd)
window.removeEventListener('keydown', handleKeyDrag)
window.removeEventListener('keyup', handleKeyDragEnd)
document.body.removeAttribute('data-page-layout-dragging')
const body = document.body as HTMLElement | undefined
body?.removeAttribute('data-page-layout-dragging')
}
}, [isDragging, isKeyboardDrag, currentWidth, minWidth, maxWidth])

Expand Down

0 comments on commit c87e80c

Please sign in to comment.