{"name":"zoom","type":"registry:ui","files":[{"path":"zoom.tsx","type":"registry:ui","content":"import { DataZoomInsideComponent, type DataZoomComponentOption } from \"echarts/components\";\nimport {\n  Children,\n  isValidElement,\n  useEffect,\n  type FC,\n  type ReactNode,\n  type RefObject,\n} from \"react\";\nimport * as echarts from \"echarts/core\";\n\necharts.use([DataZoomInsideComponent]);\n\n/**\n * Zoom and pan inside the plot. By default a pinch on a trackpad or touch\n * screen, or a scroll with Ctrl held, zooms; a plain scroll keeps scrolling\n * the page. Dragging pans a zoomed chart. Double-click, or the\n * Reset button that appears while zoomed, returns to the full range.\n */\nexport interface ZoomProps {\n  /**\n   * What a scroll wheel does: `modifier` zooms only with Ctrl held (and on\n   * pinch), `always` zooms on every scroll, `false` turns wheel zoom off.\n   */\n  wheel?: \"modifier\" | \"always\" | false;\n  /** Drag to pan while zoomed. Defaults to true. */\n  pan?: boolean;\n  /** The smallest window, as a percentage of the full range. Defaults to 5. */\n  minSpan?: number;\n  /** Show a Reset button while zoomed. Defaults to true. */\n  showReset?: boolean;\n  onChange?: (range: { startIndex: number; endIndex: number }) => void;\n}\n\nexport const Zoom: FC<ZoomProps> = () => null;\nZoom.displayName = \"Zoom\";\n\nexport type ZoomSlot = {\n  present: boolean;\n  wheel: \"modifier\" | \"always\" | false;\n  pan: boolean;\n  minSpan: number;\n  showReset: boolean;\n  onChange?: (range: { startIndex: number; endIndex: number }) => void;\n};\n\nexport const NO_ZOOM: ZoomSlot = {\n  present: false,\n  wheel: \"modifier\",\n  pan: true,\n  minSpan: 5,\n  showReset: true,\n};\n\nexport function collectZoom(children: ReactNode): ZoomSlot {\n  let slot = NO_ZOOM;\n  Children.forEach(children, (child) => {\n    if (!isValidElement(child)) return;\n    const type = child.type as FC | string;\n    const name = typeof type === \"function\" ? (type.displayName ?? type.name) : \"\";\n    if (type !== Zoom && name !== \"Zoom\" && !String(name).endsWith(\".Zoom\")) return;\n    const props = child.props as ZoomProps;\n    slot = {\n      present: true,\n      wheel: props.wheel ?? \"modifier\",\n      pan: props.pan ?? true,\n      minSpan: props.minSpan ?? 5,\n      showReset: props.showReset ?? true,\n      onChange: props.onChange,\n    };\n  });\n  return slot;\n}\n\n/** The inside data zoom for one axis, sharing its window with any Brush. */\nexport function buildInsideZoom(params: {\n  zoom: ZoomSlot;\n  range: { start: number; end: number };\n  axis?: { xAxisIndex: number[] } | { yAxisIndex: number[] };\n  filterMode?: \"filter\" | \"none\";\n}): DataZoomComponentOption {\n  const { zoom, range, axis = { xAxisIndex: [0] }, filterMode } = params;\n  return {\n    type: \"inside\",\n    ...axis,\n    start: range.start,\n    end: range.end,\n    minSpan: zoom.minSpan,\n    ...(filterMode ? { filterMode } : {}),\n    // ECharts reports a pinch as a wheel event with Ctrl held, so the\n    // modifier setting zooms on pinch and leaves plain scrolling alone.\n    zoomOnMouseWheel: zoom.wheel === \"always\" ? true : zoom.wheel === \"modifier\" ? \"ctrl\" : false,\n    moveOnMouseWheel: false,\n    moveOnMouseMove: zoom.pan,\n    preventDefaultMouseMove: zoom.pan,\n  } as DataZoomComponentOption;\n}\n\n/**\n * ECharts' inside zoom takes every wheel event over the plot and cancels its\n * default, even one it does not act on, so a plain scroll would stop\n * scrolling the page. Wheel events that should not zoom are stopped at the\n * chart's container, before ECharts sees them, and the page scrolls.\n */\nexport function useZoomWheelGuard(ref: RefObject<HTMLElement | null>, zoom: ZoomSlot) {\n  const { present, wheel } = zoom;\n  useEffect(() => {\n    const element = ref.current;\n    if (!element || !present || wheel === \"always\") return;\n    const onWheel = (event: WheelEvent) => {\n      if (wheel === \"modifier\" && event.ctrlKey) return;\n      event.stopPropagation();\n    };\n    element.addEventListener(\"wheel\", onWheel, { capture: true });\n    return () => element.removeEventListener(\"wheel\", onWheel, { capture: true });\n  }, [ref, present, wheel]);\n}\n\n/** Whether a 0–100 window is narrower than the full range. */\nexport function isZoomed(range: { start: number; end: number }) {\n  return range.start > 0.01 || range.end < 99.99;\n}\n\nexport const ZOOM_RESET_CLASS =\n  \"bg-background text-muted-foreground hover:text-foreground border-border absolute z-10 rounded-md border px-2 py-0.5 text-xs shadow-xs transition-colors focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring\";\n","target":"components/ui/zoom.tsx"}],"dependencies":["echarts"]}