Skip to content

Commit

Permalink
fix: contextDocument scope for SSR bug
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinVandy committed Jan 12, 2024
1 parent b8604a7 commit 0c0a947
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/table-core/src/features/ColumnSizing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ export const ColumnSizing: TableFeature = {
return 0
}
header.getResizeHandler = _contextDocument => {
const contextDocument = _contextDocument || document
const column = table.getColumn(header.column.id)
const canResize = column?.getCanResize()

Expand Down Expand Up @@ -409,14 +408,17 @@ export const ColumnSizing: TableFeature = {
}))
}

const contextDocument =
_contextDocument || typeof document !== 'undefined' ? document : null

const mouseEvents = {
moveHandler: (e: MouseEvent) => onMove(e.clientX),
upHandler: (e: MouseEvent) => {
contextDocument.removeEventListener(
contextDocument?.removeEventListener(
'mousemove',
mouseEvents.moveHandler
)
contextDocument.removeEventListener(
contextDocument?.removeEventListener(
'mouseup',
mouseEvents.upHandler
)
Expand All @@ -434,11 +436,11 @@ export const ColumnSizing: TableFeature = {
return false
},
upHandler: (e: TouchEvent) => {
contextDocument.removeEventListener(
contextDocument?.removeEventListener(
'touchmove',
touchEvents.moveHandler
)
contextDocument.removeEventListener(
contextDocument?.removeEventListener(
'touchend',
touchEvents.upHandler
)
Expand All @@ -455,23 +457,23 @@ export const ColumnSizing: TableFeature = {
: false

if (isTouchStartEvent(e)) {
contextDocument.addEventListener(
contextDocument?.addEventListener(
'touchmove',
touchEvents.moveHandler,
passiveIfSupported
)
contextDocument.addEventListener(
contextDocument?.addEventListener(
'touchend',
touchEvents.upHandler,
passiveIfSupported
)
} else {
contextDocument.addEventListener(
contextDocument?.addEventListener(
'mousemove',
mouseEvents.moveHandler,
passiveIfSupported
)
contextDocument.addEventListener(
contextDocument?.addEventListener(
'mouseup',
mouseEvents.upHandler,
passiveIfSupported
Expand Down

0 comments on commit 0c0a947

Please sign in to comment.