Skip to content

Commit 286b38f

Browse files
committed
fix: verify that it's running in the browser
1 parent b747d68 commit 286b38f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/LayeredImage.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from "react"
22
import { createRef, useEffect, useMemo, useRef, useState } from "react"
33

4-
import { applyStyles, clamp, isFunction } from "./utils"
4+
import { applyStyles, clamp, isBrowser, isFunction } from "./utils"
55

66
interface Size {
77
width: number
@@ -125,12 +125,24 @@ export const LayeredImage: React.FC<ILayeredImageProps> = ({
125125
pageY?: number,
126126
preventDefault = false,
127127
) => {
128+
if (!isBrowser) {
129+
return
130+
}
131+
128132
const { width, height } = _interaction === Interaction.Resize ? getDimensions() : size
129133

130134
const bodyScrollTop =
131-
document.body.scrollTop || document.documentElement.scrollTop || document.scrollingElement.scrollTop
135+
document.body.scrollTop ||
136+
document.documentElement.scrollTop ||
137+
document.scrollingElement.scrollTop ||
138+
window.scrollY ||
139+
window.pageYOffset
132140
const bodyScrollLeft =
133-
document.body.scrollLeft || document.documentElement.scrollLeft || document.scrollingElement.scrollLeft
141+
document.body.scrollLeft ||
142+
document.documentElement.scrollLeft ||
143+
document.scrollingElement.scrollLeft ||
144+
window.scrollX ||
145+
window.pageXOffset
134146
const containerRect = elementsRef.current.container.current.getBoundingClientRect()
135147

136148
const offsetX = (pageX - containerRect.left - bodyScrollLeft) / width

lib/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export const clamp = (value: number, min: number, max: number) => {
1616
return value != null ? Math.min(Math.max(value, min), maximum) : value
1717
}
1818

19+
/**
20+
* Check whether the code is running in the browser.
21+
*/
22+
export const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"
23+
1924
/**
2025
* Return `true` if the value is a `function`.
2126
*/

0 commit comments

Comments
 (0)