import { component$, useSignal, useComputed$, useVisibleTask$ } from "@builder.io/qwik"
import {
useZoomImageWheel,
useZoomImageHover,
useZoomImageMove,
useZoomImageClick
} from "@zoom-image/qwik"
type Tab = {
name: string
current: boolean
value: "wheel" | "hover" | "move" | "click"
}
export default component$(() => {
const tabs = useSignal<Tab[]>([
{ name: "Wheel", current: true, value: "wheel" },
{ name: "Hover", current: false, value: "hover" },
{ name: "Move", current: false, value: "move" },
{ name: "Click", current: false, value: "click" }
])
const imageWheelContainerRef = useSignal<HTMLDivElement>()
const imageHoverContainerRef = useSignal<HTMLDivElement>()
const imageMoveContainerRef = useSignal<HTMLDivElement>()
const imageClickContainerRef = useSignal<HTMLDivElement>()
const zoomTargetRef = useSignal<HTMLDivElement>()
const { createZoomImage: createZoomImageWheel } = useZoomImageWheel()
const { createZoomImage: createZoomImageHover } = useZoomImageHover()
const { createZoomImage: createZoomImageMove } = useZoomImageMove()
const { createZoomImage: createZoomImageClick } = useZoomImageClick()
const zoomType = useComputed$(() => {
return tabs.value.find(tab => tab.current)?.value || "wheel"
})
useVisibleTask$(({ track }) => {
track(() => zoomType.value)
if (zoomType.value === "wheel" && imageWheelContainerRef.value) {
createZoomImageWheel(imageWheelContainerRef.value)
}
if (zoomType.value === "hover" && imageHoverContainerRef.value && zoomTargetRef.value) {
createZoomImageHover(imageHoverContainerRef.value, {
zoomImageSource: "/image-large.jpg",
customZoom: { width: 300, height: 500 },
zoomTarget: zoomTargetRef.value,
scale: 2
})
}
if (zoomType.value === "move" && imageMoveContainerRef.value) {
createZoomImageMove(imageMoveContainerRef.value, {
zoomImageSource: "/image-large.jpg"
})
}
if (zoomType.value === "click" && imageClickContainerRef.value) {
createZoomImageClick(imageClickContainerRef.value, {
zoomImageSource: "/image-large.jpg"
})
}
})
return (
<div>
<nav class="flex gap-4 mb-4">
{tabs.value.map((tab) => (
<button
key={tab.name}
onClick$={() => {
tabs.value = tabs.value.map(t => ({
...t,
current: t.name === tab.name
}))
}}
class={tab.current ? "active" : ""}
>
{tab.name}
</button>
))}
</nav>
{zoomType.value === "wheel" && (
<div ref={imageWheelContainerRef} class="h-[300px] w-[200px]">
<img src="/image.jpg" alt="Wheel zoom" class="h-full w-full" />
</div>
)}
{zoomType.value === "hover" && (
<div class="flex gap-4">
<div ref={imageHoverContainerRef} class="h-[300px] w-[200px]">
<img src="/image.jpg" alt="Hover zoom" class="h-full w-full" />
</div>
<div ref={zoomTargetRef}></div>
</div>
)}
{zoomType.value === "move" && (
<div ref={imageMoveContainerRef} class="h-[300px] w-[200px] overflow-hidden">
<img src="/image.jpg" alt="Move zoom" class="h-full w-full" />
</div>
)}
{zoomType.value === "click" && (
<div ref={imageClickContainerRef} class="h-[300px] w-[200px] overflow-hidden">
<img src="/image.jpg" alt="Click zoom" class="h-full w-full" />
</div>
)}
</div>
)
})