{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "image-cropper-08",
  "title": "Cropper with zoom slider",
  "description": "A slider and its read-out set the zoom of the image.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/cropper.json",
    "https://ui.ndekocode.dev/r/slider.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/image-cropper/image-cropper-08.tsx",
      "content": "'use client';\n\nimport {\n  type SpringOptions,\n  useMotionValueEvent,\n  useSpring,\n} from 'motion/react';\nimport { useState, useSyncExternalStore } from 'react';\n\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport {\n  Cropper,\n  CropperCropArea,\n  CropperDescription,\n  CropperImage,\n} from '@/registry/ndeko/ui/cropper';\nimport { Slider } from '@/registry/ndeko/ui/slider';\n\nconst PLACEHOLDER_WIDTH = 1200;\nconst PLACEHOLDER_HEIGHT = 800;\nconst WASH_ALPHA = 0.35;\nconst BRAND_CIRCLE = { x: 840, y: 280, radius: 150, alpha: 0.45 };\nconst INFO_CIRCLE = { x: 360, y: 640, radius: 260, alpha: 0.3 };\nconst FULL_TURN = Math.PI * 2;\nconst THEME_ATTRIBUTES = ['class', 'style', 'data-theme'];\n\ntype Circle = typeof BRAND_CIRCLE;\n\ntype DrawnImage = { key: string; url: string | null };\n\nlet drawnImage: DrawnImage | null = null;\n\nfunction tokenOf(styles: CSSStyleDeclaration, name: string) {\n  const value = styles.getPropertyValue(name);\n  return value.trim();\n}\n\nfunction fillCircle(\n  context: CanvasRenderingContext2D,\n  circle: Circle,\n  color: string\n) {\n  context.globalAlpha = circle.alpha;\n  context.fillStyle = color;\n  context.beginPath();\n  context.arc(circle.x, circle.y, circle.radius, 0, FULL_TURN);\n  context.fill();\n}\n\nfunction drawPlaceholderImage() {\n  const styles = getComputedStyle(document.documentElement);\n  const base = tokenOf(styles, '--card');\n  const brand = tokenOf(styles, '--primary');\n  const info = tokenOf(styles, '--info');\n  const canvas = document.createElement('canvas');\n  canvas.width = PLACEHOLDER_WIDTH;\n  canvas.height = PLACEHOLDER_HEIGHT;\n  const context = canvas.getContext('2d');\n  if (!context) {\n    return null;\n  }\n  context.fillStyle = base;\n  context.fillRect(0, 0, PLACEHOLDER_WIDTH, PLACEHOLDER_HEIGHT);\n  const wash = context.createLinearGradient(\n    0,\n    0,\n    PLACEHOLDER_WIDTH,\n    PLACEHOLDER_HEIGHT\n  );\n  wash.addColorStop(0, brand);\n  wash.addColorStop(1, info);\n  context.globalAlpha = WASH_ALPHA;\n  context.fillStyle = wash;\n  context.fillRect(0, 0, PLACEHOLDER_WIDTH, PLACEHOLDER_HEIGHT);\n  fillCircle(context, BRAND_CIRCLE, brand);\n  fillCircle(context, INFO_CIRCLE, info);\n  return canvas.toDataURL('image/png');\n}\n\nfunction placeholderSnapshot() {\n  const key = document.documentElement.className;\n  const cachedImage = drawnImage;\n  const isCurrent = cachedImage?.key === key;\n  if (cachedImage && isCurrent) {\n    return cachedImage.url;\n  }\n  const url = drawPlaceholderImage();\n  drawnImage = { key, url };\n  return url;\n}\n\nfunction serverSnapshot() {\n  return null;\n}\n\nfunction subscribeToTheme(onThemeChange: () => void) {\n  const observer = new MutationObserver(onThemeChange);\n  observer.observe(document.documentElement, {\n    attributes: true,\n    attributeFilter: THEME_ATTRIBUTES,\n  });\n  return () => observer.disconnect();\n}\n\nfunction usePlaceholderImage() {\n  return useSyncExternalStore(\n    subscribeToTheme,\n    placeholderSnapshot,\n    serverSnapshot\n  );\n}\n\nconst MIN_ZOOM = 1;\nconst MAX_ZOOM = 3;\nconst ZOOM_STEP = 0.1;\nconst ZOOM_LABELS = ['Zoom'];\nconst ZOOM_SPRING: SpringOptions = { stiffness: 400, damping: 40 };\n\nfunction firstValueOf(value: number | readonly number[]) {\n  if (typeof value === 'number') {\n    return value;\n  }\n  return value[0];\n}\n\nfunction zoomLabelOf(zoom: number) {\n  const rounded = zoom.toFixed(1);\n  const shortest = Number.parseFloat(rounded);\n  return `${shortest}x`;\n}\n\nexport default function ImageCropper08() {\n  const image = usePlaceholderImage();\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const [targetZoom, setTargetZoom] = useState(MIN_ZOOM);\n  const [zoom, setZoom] = useState(MIN_ZOOM);\n  const zoomSpring = useSpring(MIN_ZOOM, ZOOM_SPRING);\n\n  useMotionValueEvent(zoomSpring, 'change', setZoom);\n\n  function changeZoomFromSlider(value: number | readonly number[]) {\n    const nextZoom = firstValueOf(value);\n    setTargetZoom(nextZoom);\n    if (shouldReduceMotion) {\n      zoomSpring.jump(nextZoom);\n      setZoom(nextZoom);\n      return;\n    }\n    zoomSpring.set(nextZoom);\n  }\n\n  function followCropperZoom(nextZoom: number) {\n    setTargetZoom(nextZoom);\n    zoomSpring.jump(nextZoom);\n    setZoom(nextZoom);\n  }\n\n  return (\n    <div className='flex w-full flex-col gap-4'>\n      {image && (\n        <Cropper\n          className='h-80'\n          image={image}\n          zoom={zoom}\n          minZoom={MIN_ZOOM}\n          maxZoom={MAX_ZOOM}\n          onZoomChange={followCropperZoom}\n        >\n          <CropperDescription />\n          <CropperImage />\n          <CropperCropArea />\n        </Cropper>\n      )}\n      {!image && (\n        <div\n          aria-hidden='true'\n          className='bg-foreground/[0.04] h-80 w-full rounded-xl'\n        />\n      )}\n      <div className='mx-auto flex w-full max-w-80 items-center gap-3'>\n        <Slider\n          value={targetZoom}\n          min={MIN_ZOOM}\n          max={MAX_ZOOM}\n          step={ZOOM_STEP}\n          onValueChange={changeZoomFromSlider}\n          thumbLabels={ZOOM_LABELS}\n        />\n        <output className='w-10 shrink-0 text-right text-[13px] font-medium tabular-nums'>\n          {zoomLabelOf(targetZoom)}\n        </output>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "image-cropper"
  ],
  "type": "registry:component"
}