Skip to content

Commit

Permalink
fix: verify that it's running in the browser
Browse files Browse the repository at this point in the history
  • Loading branch information
llorca committed Sep 12, 2020
1 parent b747d68 commit 286b38f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/LayeredImage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react"
import { createRef, useEffect, useMemo, useRef, useState } from "react"

import { applyStyles, clamp, isFunction } from "./utils"
import { applyStyles, clamp, isBrowser, isFunction } from "./utils"

interface Size {
width: number
Expand Down Expand Up @@ -125,12 +125,24 @@ export const LayeredImage: React.FC<ILayeredImageProps> = ({
pageY?: number,
preventDefault = false,
) => {
if (!isBrowser) {
return
}

const { width, height } = _interaction === Interaction.Resize ? getDimensions() : size

const bodyScrollTop =
document.body.scrollTop || document.documentElement.scrollTop || document.scrollingElement.scrollTop
document.body.scrollTop ||
document.documentElement.scrollTop ||
document.scrollingElement.scrollTop ||
window.scrollY ||
window.pageYOffset
const bodyScrollLeft =
document.body.scrollLeft || document.documentElement.scrollLeft || document.scrollingElement.scrollLeft
document.body.scrollLeft ||
document.documentElement.scrollLeft ||
document.scrollingElement.scrollLeft ||
window.scrollX ||
window.pageXOffset
const containerRect = elementsRef.current.container.current.getBoundingClientRect()

const offsetX = (pageX - containerRect.left - bodyScrollLeft) / width
Expand Down
5 changes: 5 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export const clamp = (value: number, min: number, max: number) => {
return value != null ? Math.min(Math.max(value, min), maximum) : value
}

/**
* Check whether the code is running in the browser.
*/
export const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined"

/**
* Return `true` if the value is a `function`.
*/
Expand Down

0 comments on commit 286b38f

Please sign in to comment.