{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-magnified-doc",
  "title": "Magnified doc",
  "description": "A dock of icons that magnify near the pointer and switch the content of the stage above them.",
  "dependencies": [
    "@base-ui/react",
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "components/core/magnified-doc.tsx",
      "content": "'use client';\nimport { Tabs as TabsPrimitive } from '@base-ui/react/tabs';\nimport {\n  motion,\n  useMotionValue,\n  useSpring,\n  useTransform,\n  type MotionValue,\n} from 'motion/react';\nimport {\n  Children,\n  createContext,\n  useContext,\n  useEffect,\n  useRef,\n  type PointerEvent,\n  type ReactNode,\n  type RefObject,\n} from 'react';\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport {\n  ImageTabs,\n  ImageTabsFrame,\n  ImageTabsImage,\n  ImageTabsPanel,\n  type ImageTabsProps,\n} from './image-tabs';\n\ntype DockLayout = {\n  centers: number[];\n  sizes: number[];\n};\n\ntype DockMetrics = {\n  pointerX: number;\n  presence: number;\n  layout: DockLayout;\n  magnification: number;\n  distance: number;\n};\n\ntype DockContextValue = {\n  pointer: MotionValue<number>;\n  presence: MotionValue<number>;\n  layoutRef: RefObject<DockLayout>;\n  magnification: number;\n  distance: number;\n};\n\nconst POINTER_SPRING = { stiffness: 520, damping: 46 };\n\nconst PRESENCE_SPRING = { stiffness: 320, damping: 36 };\n\nconst RESTING_PRESENCE = 0.05;\n\nconst DockContext = createContext<DockContextValue | undefined>(undefined);\n\nconst DockItemIndexContext = createContext(0);\n\nfunction useDock() {\n  const context = useContext(DockContext);\n  if (!context) {\n    throw new Error('MagnifiedDocItem must be used within MagnifiedDocDock');\n  }\n  return context;\n}\n\nfunction measureLayout(list: HTMLElement): DockLayout {\n  const items = list.querySelectorAll<HTMLElement>('[data-dock-item]');\n  const centers: number[] = [];\n  const sizes: number[] = [];\n  items.forEach((item) => {\n    const center = item.offsetLeft + item.offsetWidth / 2;\n    centers.push(center);\n    sizes.push(item.offsetWidth);\n  });\n  return { centers, sizes };\n}\n\nfunction weightOf(offsetFromPointer: number, distance: number) {\n  const ratio = Math.abs(offsetFromPointer) / distance;\n  if (ratio >= 1) {\n    return 0;\n  }\n  const wave = Math.cos(ratio * Math.PI);\n  return (wave + 1) / 2;\n}\n\nfunction growthOf(index: number, metrics: DockMetrics) {\n  const center = metrics.layout.centers[index] ?? 0;\n  const size = metrics.layout.sizes[index] ?? 0;\n  const weight = weightOf(metrics.pointerX - center, metrics.distance);\n  const maximumGrowth = (metrics.magnification - 1) * size;\n  return weight * metrics.presence * maximumGrowth;\n}\n\nfunction scaleOf(index: number, metrics: DockMetrics) {\n  const size = metrics.layout.sizes[index];\n  if (!size) {\n    return 1;\n  }\n  const growth = growthOf(index, metrics);\n  return 1 + growth / size;\n}\n\n/** Translates each item as if its neighbours had grown in width, so the row spreads like a dock without animating layout. */\nfunction shiftOf(index: number, metrics: DockMetrics) {\n  let growthBefore = 0;\n  let totalGrowth = 0;\n  metrics.layout.centers.forEach((_, otherIndex) => {\n    const growth = growthOf(otherIndex, metrics);\n    totalGrowth += growth;\n    if (otherIndex < index) {\n      growthBefore += growth;\n    }\n  });\n  const ownGrowth = growthOf(index, metrics);\n  return growthBefore + ownGrowth / 2 - totalGrowth / 2;\n}\n\nfunction liftOf(index: number, metrics: DockMetrics) {\n  const growth = growthOf(index, metrics);\n  return -growth;\n}\n\nexport type MagnifiedDocProps = Omit<ImageTabsProps, 'orientation'>;\n\nfunction MagnifiedDoc({ className, ...props }: MagnifiedDocProps) {\n  return (\n    <ImageTabs\n      orientation='horizontal'\n      className={cn('relative', className)}\n      {...props}\n    />\n  );\n}\n\nexport type MagnifiedDocDockProps = {\n  children: ReactNode;\n  className?: string;\n  magnification?: number;\n  distance?: number;\n  'aria-label'?: string;\n};\n\nfunction MagnifiedDocDock({\n  children,\n  className,\n  magnification = 1.6,\n  distance = 120,\n  'aria-label': ariaLabel,\n}: MagnifiedDocDockProps) {\n  const listRef = useRef<HTMLDivElement>(null);\n  const layoutRef = useRef<DockLayout>({ centers: [], sizes: [] });\n  const pointerTarget = useMotionValue(0);\n  const presenceTarget = useMotionValue(0);\n  const pointer = useSpring(pointerTarget, POINTER_SPRING);\n  const presence = useSpring(presenceTarget, PRESENCE_SPRING);\n  const prefersReducedMotion = usePrefersReducedMotion();\n\n  useEffect(() => {\n    const list = listRef.current;\n    if (!list) {\n      return;\n    }\n    const remeasure = () => {\n      layoutRef.current = measureLayout(list);\n    };\n    remeasure();\n    const observer = new ResizeObserver(remeasure);\n    observer.observe(list);\n    return () => observer.disconnect();\n  }, []);\n\n  const handlePointerMove = (event: PointerEvent<HTMLDivElement>) => {\n    if (prefersReducedMotion) {\n      return;\n    }\n    if (event.pointerType === 'touch') {\n      return;\n    }\n    const listLeft = event.currentTarget.getBoundingClientRect().left;\n    const pointerX = event.clientX - listLeft;\n    const isResting = presence.get() < RESTING_PRESENCE;\n    pointerTarget.set(pointerX);\n    if (isResting) {\n      pointer.jump(pointerX);\n    }\n    presenceTarget.set(1);\n  };\n\n  const handlePointerLeave = () => {\n    presenceTarget.set(0);\n  };\n\n  const items = Children.toArray(children);\n\n  return (\n    <DockContext.Provider\n      value={{ pointer, presence, layoutRef, magnification, distance }}\n    >\n      <TabsPrimitive.List\n        ref={listRef}\n        activateOnFocus\n        aria-label={ariaLabel}\n        onPointerMove={handlePointerMove}\n        onPointerLeave={handlePointerLeave}\n        className={cn(\n          'surface-raised absolute bottom-3 left-1/2 z-10 flex w-fit -translate-x-1/2 items-end gap-2 rounded-2xl px-2 pt-2 pb-1',\n          className\n        )}\n      >\n        {items.map((item, index) => (\n          <DockItemIndexContext.Provider key={index} value={index}>\n            {item}\n          </DockItemIndexContext.Provider>\n        ))}\n      </TabsPrimitive.List>\n    </DockContext.Provider>\n  );\n}\n\nexport type MagnifiedDocItemProps = {\n  value: string;\n  label: string;\n  children: ReactNode;\n  className?: string;\n};\n\nfunction MagnifiedDocItem({\n  value,\n  label,\n  children,\n  className,\n}: MagnifiedDocItemProps) {\n  const index = useContext(DockItemIndexContext);\n  const { pointer, presence, layoutRef, magnification, distance } = useDock();\n\n  const metricsOf = (): DockMetrics => ({\n    pointerX: pointer.get(),\n    presence: presence.get(),\n    layout: layoutRef.current,\n    magnification,\n    distance,\n  });\n\n  const scale = useTransform(() => scaleOf(index, metricsOf()));\n  const x = useTransform(() => shiftOf(index, metricsOf()));\n  const labelY = useTransform(() => liftOf(index, metricsOf()));\n\n  return (\n    <motion.div\n      data-dock-item=''\n      style={{ x }}\n      className='group/dock-item relative flex flex-col items-center gap-1'\n    >\n      <motion.span\n        aria-hidden\n        style={{ y: labelY }}\n        className='surface-raised pointer-events-none absolute bottom-full left-1/2 mb-2 -translate-x-1/2 rounded-lg px-2 py-0.5 text-xs font-medium whitespace-nowrap opacity-0 transition-opacity duration-150 group-hover/dock-item:opacity-100 group-has-[:focus-visible]/dock-item:opacity-100'\n      >\n        {label}\n      </motion.span>\n      <TabsPrimitive.Tab\n        value={value}\n        aria-label={label}\n        render={<motion.button style={{ scale, originY: 1 }} />}\n        className={cn(\n          'keycap-3d text-foreground focus-visible:ring-ring focus-visible:ring-offset-background grid size-10 cursor-pointer place-items-center overflow-hidden rounded-xl outline-none focus-visible:ring-2 focus-visible:ring-offset-2 [&_svg]:size-5',\n          className\n        )}\n      >\n        {children}\n      </TabsPrimitive.Tab>\n      <span\n        aria-hidden\n        className='bg-foreground size-1 rounded-full opacity-0 transition-opacity duration-150 group-has-[[data-active]]/dock-item:opacity-100'\n      />\n    </motion.div>\n  );\n}\n\nexport {\n  MagnifiedDoc,\n  MagnifiedDocDock,\n  MagnifiedDocItem,\n  ImageTabsFrame as MagnifiedDocStage,\n  ImageTabsPanel as MagnifiedDocPanel,\n  ImageTabsImage as MagnifiedDocImage,\n};\n",
      "type": "registry:component",
      "target": "components/core/magnified-doc.tsx"
    },
    {
      "path": "components/core/image-tabs.tsx",
      "content": "'use client';\nimport { Tabs as TabsPrimitive } from '@base-ui/react/tabs';\nimport {\n  Children,\n  createContext,\n  isValidElement,\n  useContext,\n  useState,\n  type ComponentProps,\n  type CSSProperties,\n  type ReactNode,\n} from 'react';\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\n\nexport type ImageTabsOrientation = 'horizontal' | 'vertical';\n\nexport type ImageTabsListVariant = 'list' | 'thumbnails';\n\nexport type ImageTabsTransition = 'fade' | 'clip';\n\ntype PanelState = 'active' | 'previous' | 'idle';\n\ntype PanelSide = 'before' | 'after';\n\ntype ImageTabsContextValue = {\n  value: string;\n  previousValue: string | null;\n  orientation: ImageTabsOrientation;\n};\n\ntype ImageTabsFrameContextValue = {\n  values: string[];\n  transition: ImageTabsTransition;\n};\n\nconst ImageTabsContext = createContext<ImageTabsContextValue | undefined>(\n  undefined\n);\n\nconst ImageTabsListContext = createContext<ImageTabsListVariant>('list');\n\nconst ImageTabsFrameContext = createContext<\n  ImageTabsFrameContextValue | undefined\n>(undefined);\n\nfunction useImageTabs() {\n  const context = useContext(ImageTabsContext);\n  if (!context) {\n    throw new Error('useImageTabs must be used within ImageTabs');\n  }\n  return context;\n}\n\nfunction useImageTabsFrame() {\n  const context = useContext(ImageTabsFrameContext);\n  if (!context) {\n    throw new Error('ImageTabsPanel must be used within ImageTabsFrame');\n  }\n  return context;\n}\n\nfunction selectedValueOf(\n  controlledValue: string | undefined,\n  internal: string\n) {\n  if (controlledValue === undefined) {\n    return internal;\n  }\n  return controlledValue;\n}\n\nexport type ImageTabsProps = {\n  children: ReactNode;\n  className?: string;\n  defaultValue: string;\n  value?: string;\n  onValueChange?: (value: string) => void;\n  orientation?: ImageTabsOrientation;\n};\n\nfunction ImageTabs({\n  children,\n  className,\n  defaultValue,\n  value: controlledValue,\n  onValueChange,\n  orientation = 'vertical',\n}: ImageTabsProps) {\n  const [internalValue, setInternalValue] = useState(defaultValue);\n  const value = selectedValueOf(controlledValue, internalValue);\n  const [shownValue, setShownValue] = useState(value);\n  const [previousValue, setPreviousValue] = useState<string | null>(null);\n\n  const hasNewValue = value !== shownValue;\n  if (hasNewValue) {\n    setPreviousValue(shownValue);\n    setShownValue(value);\n  }\n\n  const handleValueChange = (nextValue: unknown) => {\n    const next = String(nextValue);\n    setInternalValue(next);\n    onValueChange?.(next);\n  };\n\n  return (\n    <ImageTabsContext.Provider value={{ value, previousValue, orientation }}>\n      <TabsPrimitive.Root\n        value={value}\n        onValueChange={handleValueChange}\n        orientation={orientation}\n        className={cn('grid gap-4', className)}\n      >\n        {children}\n      </TabsPrimitive.Root>\n    </ImageTabsContext.Provider>\n  );\n}\n\nconst LIST_CLASS: Record<ImageTabsListVariant, string> = {\n  list: 'flex gap-1 data-[orientation=vertical]:flex-col',\n  thumbnails: 'flex flex-wrap gap-2 p-1',\n};\n\nconst INDICATOR_CLASS: Record<ImageTabsListVariant, string> = {\n  list: 'bg-card hairline-ring ring-foreground/10 dark:bg-foreground/[0.06] dark:ring-foreground/15 z-0 rounded-lg shadow-[0_1px_2px_rgb(0_0_0/0.08)]',\n  thumbnails: 'outline-primary z-20 rounded-lg outline-2 outline-offset-2',\n};\n\nconst INDICATOR_MOTION =\n  'pointer-events-none absolute top-0 left-0 h-(--active-tab-height) w-(--active-tab-width) translate-x-(--active-tab-left) translate-y-(--active-tab-top) transition-[translate,width,height] duration-250 ease-(--ease-out-strong)';\n\nexport type ImageTabsListProps = {\n  children: ReactNode;\n  className?: string;\n  variant?: ImageTabsListVariant;\n  activateOnFocus?: boolean;\n  'aria-label'?: string;\n};\n\nfunction ImageTabsList({\n  children,\n  className,\n  variant = 'list',\n  activateOnFocus = true,\n  'aria-label': ariaLabel,\n}: ImageTabsListProps) {\n  return (\n    <ImageTabsListContext.Provider value={variant}>\n      <TabsPrimitive.List\n        data-variant={variant}\n        activateOnFocus={activateOnFocus}\n        aria-label={ariaLabel}\n        className={cn('relative', LIST_CLASS[variant], className)}\n      >\n        {children}\n        <TabsPrimitive.Indicator\n          className={cn(INDICATOR_MOTION, INDICATOR_CLASS[variant])}\n        />\n      </TabsPrimitive.List>\n    </ImageTabsListContext.Provider>\n  );\n}\n\nconst TAB_BASE =\n  'relative z-10 cursor-pointer text-left outline-none select-none focus-visible:ring-[3px] focus-visible:ring-ring/25';\n\nconst TAB_CLASS: Record<ImageTabsListVariant, string> = {\n  list: 'text-muted-foreground hover:text-foreground data-active:text-foreground flex flex-col gap-0.5 rounded-lg px-3 py-2.5 transition-colors duration-150',\n  thumbnails:\n    'size-14 overflow-hidden rounded-lg opacity-60 transition-opacity duration-150 hover:opacity-100 data-active:opacity-100',\n};\n\nexport type ImageTabsTabProps = {\n  value: string;\n  children: ReactNode;\n  className?: string;\n  'aria-label'?: string;\n  disabled?: boolean;\n};\n\nfunction ImageTabsTab({\n  value,\n  children,\n  className,\n  'aria-label': ariaLabel,\n  disabled,\n}: ImageTabsTabProps) {\n  const variant = useContext(ImageTabsListContext);\n\n  return (\n    <TabsPrimitive.Tab\n      value={value}\n      aria-label={ariaLabel}\n      disabled={disabled}\n      className={cn(TAB_BASE, TAB_CLASS[variant], className)}\n    >\n      {children}\n    </TabsPrimitive.Tab>\n  );\n}\n\nexport type ImageTabsTextProps = {\n  children: ReactNode;\n  className?: string;\n};\n\nfunction ImageTabsTitle({ children, className }: ImageTabsTextProps) {\n  return (\n    <span className={cn('text-sm font-medium text-balance', className)}>\n      {children}\n    </span>\n  );\n}\n\nfunction ImageTabsDescription({ children, className }: ImageTabsTextProps) {\n  return (\n    <span\n      className={cn(\n        'text-muted-foreground text-[13px] leading-relaxed',\n        className\n      )}\n    >\n      {children}\n    </span>\n  );\n}\n\nfunction valuesOf(children: ReactNode) {\n  const values: string[] = [];\n  Children.forEach(children, (child) => {\n    if (!isValidElement<{ value?: string }>(child)) {\n      return;\n    }\n    const value = child.props.value;\n    if (value !== undefined) {\n      values.push(value);\n    }\n  });\n  return values;\n}\n\nfunction transitionOf(\n  transition: ImageTabsTransition,\n  prefersReducedMotion: boolean | null\n): ImageTabsTransition {\n  if (prefersReducedMotion) {\n    return 'fade';\n  }\n  return transition;\n}\n\nexport type ImageTabsFrameProps = {\n  children: ReactNode;\n  className?: string;\n  transition?: ImageTabsTransition;\n};\n\nfunction ImageTabsFrame({\n  children,\n  className,\n  transition = 'fade',\n}: ImageTabsFrameProps) {\n  const prefersReducedMotion = usePrefersReducedMotion();\n  const values = valuesOf(children);\n  const activeTransition = transitionOf(transition, prefersReducedMotion);\n\n  return (\n    <ImageTabsFrameContext.Provider\n      value={{ values, transition: activeTransition }}\n    >\n      <div\n        className={cn(\n          'bg-foreground/[0.04] relative isolate aspect-[3/2] overflow-hidden rounded-xl',\n          className\n        )}\n      >\n        {children}\n      </div>\n    </ImageTabsFrameContext.Provider>\n  );\n}\n\nconst FADE_DURATION = 200;\n\nconst CLIP_DURATION = 320;\n\nconst FADE_IN = `opacity ${FADE_DURATION}ms var(--ease-out-strong)`;\n\nconst CLIP_IN = `clip-path ${CLIP_DURATION}ms var(--ease-in-out-strong)`;\n\nconst HIDE_AFTER_FADE = `opacity 0s linear ${FADE_DURATION}ms`;\n\nconst HIDE_AFTER_CLIP = `clip-path 0s linear ${CLIP_DURATION}ms`;\n\nconst SHOWN_CLIP = 'inset(0 0 0 0)';\n\nfunction panelStateOf(\n  value: string,\n  activeValue: string,\n  previousValue: string | null\n): PanelState {\n  if (value === activeValue) {\n    return 'active';\n  }\n  if (value === previousValue) {\n    return 'previous';\n  }\n  return 'idle';\n}\n\nfunction sideOf(index: number, activeIndex: number): PanelSide {\n  if (index < activeIndex) {\n    return 'before';\n  }\n  return 'after';\n}\n\nfunction hiddenClipOf(side: PanelSide, orientation: ImageTabsOrientation) {\n  const isAfter = side === 'after';\n  if (orientation === 'vertical') {\n    if (isAfter) {\n      return 'inset(100% 0 0 0)';\n    }\n    return 'inset(0 0 100% 0)';\n  }\n  if (isAfter) {\n    return 'inset(0 0 0 100%)';\n  }\n  return 'inset(0 100% 0 0)';\n}\n\nfunction stackOrderOf(state: PanelState) {\n  if (state === 'active') {\n    return 2;\n  }\n  if (state === 'previous') {\n    return 1;\n  }\n  return 0;\n}\n\nfunction fadeStyleOf(state: PanelState): CSSProperties {\n  const zIndex = stackOrderOf(state);\n  if (state === 'idle') {\n    return {\n      opacity: 0,\n      clipPath: 'none',\n      zIndex,\n      transition: HIDE_AFTER_FADE,\n    };\n  }\n  return { opacity: 1, clipPath: 'none', zIndex, transition: FADE_IN };\n}\n\nfunction clipStyleOf(\n  state: PanelState,\n  side: PanelSide,\n  orientation: ImageTabsOrientation\n): CSSProperties {\n  const zIndex = stackOrderOf(state);\n  if (state === 'idle') {\n    const clipPath = hiddenClipOf(side, orientation);\n    return { opacity: 1, clipPath, zIndex, transition: HIDE_AFTER_CLIP };\n  }\n  return { opacity: 1, clipPath: SHOWN_CLIP, zIndex, transition: CLIP_IN };\n}\n\nfunction renderVisiblePanel(props: ComponentProps<'div'>) {\n  return <div {...props} hidden={false} />;\n}\n\nexport type ImageTabsPanelProps = {\n  value: string;\n  children: ReactNode;\n  className?: string;\n};\n\nfunction ImageTabsPanel({ value, children, className }: ImageTabsPanelProps) {\n  const { value: activeValue, previousValue, orientation } = useImageTabs();\n  const { values, transition } = useImageTabsFrame();\n  const state = panelStateOf(value, activeValue, previousValue);\n  const index = values.indexOf(value);\n  const activeIndex = values.indexOf(activeValue);\n  const side = sideOf(index, activeIndex);\n  let style = fadeStyleOf(state);\n  if (transition === 'clip') {\n    style = clipStyleOf(state, side, orientation);\n  }\n\n  return (\n    <TabsPrimitive.Panel\n      value={value}\n      keepMounted\n      render={renderVisiblePanel}\n      data-state={state}\n      style={style}\n      className={cn(\n        'focus-visible:outline-ring absolute inset-0 focus-visible:outline-2 focus-visible:-outline-offset-2',\n        className\n      )}\n    >\n      {children}\n    </TabsPrimitive.Panel>\n  );\n}\n\nexport type ImageTabsImageProps = {\n  value: string;\n  src: string;\n  alt: string;\n  className?: string;\n};\n\nfunction ImageTabsImage({ value, src, alt, className }: ImageTabsImageProps) {\n  return (\n    <ImageTabsPanel value={value}>\n      {/* eslint-disable-next-line @next/next/no-img-element -- registry items land in any React app, where next/image may not exist */}\n      <img\n        src={src}\n        alt={alt}\n        draggable={false}\n        decoding='async'\n        className={cn('size-full object-cover', className)}\n      />\n    </ImageTabsPanel>\n  );\n}\n\nexport {\n  ImageTabs,\n  ImageTabsDescription,\n  ImageTabsFrame,\n  ImageTabsImage,\n  ImageTabsList,\n  ImageTabsPanel,\n  ImageTabsTab,\n  ImageTabsTitle,\n  useImageTabs,\n};\n",
      "type": "registry:component",
      "target": "components/core/image-tabs.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}