Skip to content

Commit

Permalink
Add support to style zoom image wrapper on hover 💞 (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
willnguyen1312 authored Sep 30, 2023
1 parent 02781f5 commit 8348edd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-tools-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zoom-image/core": minor
---

Add support to style zoom image wrapper on hover 💞
14 changes: 13 additions & 1 deletion packages/core/src/createZoomImageHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type ZoomImageHoverOptions = {
zoomLensClass?: string
zoomLensScale?: number
zoomTarget: HTMLElement
zoomTargetClass?: string
scale?: number
}

Expand All @@ -32,13 +33,14 @@ export function createZoomImageHover(container: HTMLElement, options: ZoomImageH
const finalOptions: Required<ZoomImageHoverOptions> = {
zoomImageSource: options.zoomImageSource || sourceImgElement.src,
zoomLensClass: options.zoomLensClass || "",
zoomTargetClass: options.zoomTargetClass || "",
customZoom: options.customZoom,
scale: options.scale || 2,
zoomTarget: options.zoomTarget,
zoomLensScale: options.zoomLensScale || 1,
}

const { scale, zoomImageSource, customZoom, zoomLensClass, zoomTarget, zoomLensScale } = finalOptions
const { scale, zoomImageSource, customZoom, zoomLensClass, zoomTarget, zoomLensScale, zoomTargetClass } = finalOptions

const store = createStore<ZoomImageHoverState>({
zoomedImgStatus: imageCache.checkImageLoaded(zoomImageSource) ? "loaded" : "idle",
Expand Down Expand Up @@ -152,12 +154,22 @@ export function createZoomImageHover(container: HTMLElement, options: ZoomImageH
disableScroll()
zoomedImg.style.display = "block"
zoomLens.style.display = "block"

if (zoomTargetClass) {
const classes = zoomTargetClass.split(" ")
classes.forEach((className) => zoomTarget.classList.add(className))
}
}

function handlePointerLeave() {
enableScroll()
zoomedImg.style.display = "none"
zoomLens.style.display = "none"

if (zoomTargetClass) {
const classes = zoomTargetClass.split(" ")
classes.forEach((className) => zoomTarget.classList.remove(className))
}
}

function handleScroll() {
Expand Down
5 changes: 4 additions & 1 deletion packages/docs/src/api/createZoomImageHover.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ZoomImageHoverOptions = {
// The source of zoomed image, default is the same as the original image
zoomImageSource?: string

// The css class will be added to zoom lens element
// The css classes will be added to zoom lens element
zoomLensClass?: string

// By default, zoomLensScale have a value of 1
Expand All @@ -45,6 +45,9 @@ type ZoomImageHoverOptions = {
// The container of zoomed image
zoomTarget: HTMLElement

// The css classes will be added to zoomed image container on zoom
zoomTargetClass?: string

// The scale of zoomed image, default is 2
scale?: number
}
Expand Down

0 comments on commit 8348edd

Please sign in to comment.