Skip to content

Commit

Permalink
Add utils file 🤞 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
willnguyen1312 authored May 1, 2023
1 parent 36cc206 commit 09def4a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
6 changes: 1 addition & 5 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { enableScroll, disableScroll } from "./scroll"
import { enableScroll, disableScroll, clamp } from "./utils"

export type ZoomImageOptions = {
customZoom?: { width: number; height: number }
Expand All @@ -18,10 +18,6 @@ type ZoomImageStore = {
zoomLens: HTMLDivElement
}

function clamp(value: number, min: number, max: number) {
return Math.max(min, Math.min(max, value))
}

function createZoomImage(container: HTMLElement, options: ZoomImageOptions = {}) {
if (!container) {
throw new Error("Please specify a container for the zoom image")
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/scroll.ts → packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
const keySet = new Set(["ArrowUp", "ArrowRight", "ArrowDown", "ArrowLeft"])
export function clamp(value: number, min: number, max: number) {
return Math.max(min, Math.min(max, value))
}

function preventDefault(event: Event) {
event.preventDefault()
}

const keySet = new Set(["ArrowUp", "ArrowRight", "ArrowDown", "ArrowLeft"])
function preventDefaultForScrollKeys(event: KeyboardEvent) {
if (keySet.has(event.key)) {
preventDefault(event)
return false
}
}

const wheelOpt = { passive: false }

export function disableScroll() {
window.addEventListener("DOMMouseScroll", preventDefault)
window.addEventListener("wheel", preventDefault, wheelOpt)
window.addEventListener("touchmove", preventDefault, wheelOpt)
window.addEventListener("wheel", preventDefault, { passive: false })
window.addEventListener("touchmove", preventDefault, { passive: false })
window.addEventListener("keydown", preventDefaultForScrollKeys)
}

Expand Down

0 comments on commit 09def4a

Please sign in to comment.