{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "station-dialog",
  "title": "Station Dialog",
  "description": "A detail dialog that opens from a point on a map or list, can grow out of it, and sends focus back to it on close.",
  "dependencies": [
    "lucide-react",
    "motion",
    "radix-ui"
  ],
  "registryDependencies": [
    "utils",
    "https://miseui.junsu4621.workers.dev/r/motion.json"
  ],
  "files": [
    {
      "path": "registry/miseui/ui/station-dialog.tsx",
      "content": "import type * as React from \"react\"\nimport { useCallback, useMemo, useState } from \"react\"\nimport { Dialog as DialogPrimitive, Slot } from \"radix-ui\"\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\"\nimport { SPRING_DEFAULT } from \"@/registry/miseui/lib/motion\"\nimport {\n  StationDialogContext,\n  useStationDialog,\n  type StationDialogEffect,\n  type StationDialogVariant,\n} from \"@/registry/miseui/ui/station-dialog-context\"\nimport { StationSurface, type StationDialogContentProps } from \"@/registry/miseui/ui/station-dialog-surface\"\nimport { cn } from \"@/registry/miseui/lib/utils\"\n\ninterface StationDialogProps {\n  /** The open station, or null when closed. Several triggers can share one dialog. */\n  value?: string | null\n  defaultValue?: string | null\n  onValueChange?: (value: string | null) => void\n  /** Where it opened from when no StationDialogTrigger was pressed, such as a point on a map you draw yourself. */\n  origin?: HTMLElement | null\n  /** `card` a centred surface, `side` a panel that leaves the map in view, `pin` a small note beside the point itself. */\n  variant?: StationDialogVariant\n  /** `morph` grows the surface out of the point it opened from and shrinks it back there. */\n  effect?: StationDialogEffect\n  children?: React.ReactNode\n}\n\nfunction StationDialog({ value, defaultValue = null, onValueChange, origin, variant = \"card\", effect, children }: StationDialogProps) {\n  const [uncontrolled, setUncontrolled] = useState(defaultValue)\n  const [pressed, setPressed] = useState<HTMLElement | null>(null)\n  const current = value === undefined ? uncontrolled : value\n  const choose = useCallback(\n    (next: string | null, element?: HTMLElement) => {\n      if (element) setPressed(element)\n      setUncontrolled(next)\n      onValueChange?.(next)\n    },\n    [onValueChange],\n  )\n  const context = useMemo(\n    () => ({ value: current, variant, effect, origin: origin ?? pressed, choose }),\n    [current, variant, effect, origin, pressed, choose],\n  )\n  return (\n    <StationDialogContext.Provider value={context}>\n      <DialogPrimitive.Root open={current !== null} onOpenChange={(open) => (open ? undefined : choose(null))}>\n        {children}\n      </DialogPrimitive.Root>\n    </StationDialogContext.Provider>\n  )\n}\n\ntype StationDialogTriggerProps = React.ComponentProps<\"button\"> & { value: string; asChild?: boolean }\n\nfunction StationDialogTrigger({ value, asChild = false, onClick, ...props }: StationDialogTriggerProps) {\n  const { value: current, choose } = useStationDialog()\n  const Comp = asChild ? Slot.Root : \"button\"\n  return (\n    <Comp\n      data-slot=\"station-dialog-trigger\"\n      type={asChild ? undefined : \"button\"}\n      aria-haspopup=\"dialog\"\n      aria-expanded={current === value}\n      onClick={(event: React.MouseEvent<HTMLButtonElement>) => {\n        onClick?.(event)\n        if (!event.defaultPrevented) choose(value, event.currentTarget)\n      }}\n      {...props}\n    />\n  )\n}\n\nconst OVERLAY_CLASS: Record<StationDialogVariant, string> = {\n  card: \"bg-background/70 backdrop-blur-sm [@media(prefers-reduced-transparency:reduce)]:bg-background/90 [@media(prefers-reduced-transparency:reduce)]:backdrop-blur-none\",\n  side: \"bg-background/40\",\n  // The map is the point of a pin, so nothing covers it; the layer is still there to catch the click that closes it.\n  pin: \"bg-transparent\",\n}\n\nfunction StationDialogContent(props: StationDialogContentProps) {\n  const { value, variant } = useStationDialog()\n  const reduceMotion = useReducedMotion() ?? false\n  return (\n    <AnimatePresence>\n      {value !== null ? (\n        <DialogPrimitive.Portal forceMount key=\"station-dialog\">\n          <DialogPrimitive.Overlay forceMount asChild>\n            <motion.div\n              data-slot=\"station-dialog-overlay\"\n              className={cn(\"fixed inset-0 z-50\", OVERLAY_CLASS[variant])}\n              initial={{ opacity: 0 }}\n              animate={{ opacity: 1 }}\n              exit={{ opacity: 0 }}\n              transition={reduceMotion ? { duration: 0 } : SPRING_DEFAULT}\n            />\n          </DialogPrimitive.Overlay>\n          {/* A full-screen grid centres a surface of any height, which also gives morph a known place to travel to. */}\n          <div className={cn(\"pointer-events-none fixed inset-0 z-50\", variant === \"card\" && \"grid place-items-center p-4\")}>\n            <StationSurface {...props} />\n          </div>\n        </DialogPrimitive.Portal>\n      ) : null}\n    </AnimatePresence>\n  )\n}\n\nexport {\n  StationDialog,\n  StationDialogTrigger,\n  StationDialogContent,\n  type StationDialogProps,\n  type StationDialogTriggerProps,\n  type StationDialogContentProps,\n  type StationDialogVariant,\n  type StationDialogEffect,\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/station-dialog-context.ts",
      "content": "import { createContext, useContext } from \"react\"\n\nexport type StationDialogVariant = \"card\" | \"side\" | \"pin\"\nexport type StationDialogEffect = \"morph\"\n\ninterface StationDialogContextValue {\n  value: string | null\n  variant: StationDialogVariant\n  effect: StationDialogEffect | undefined\n  origin: HTMLElement | null\n  choose: (next: string | null, element?: HTMLElement) => void\n}\n\nexport const StationDialogContext = createContext<StationDialogContextValue | null>(null)\n\nexport function useStationDialog() {\n  const context = useContext(StationDialogContext)\n  if (!context) throw new Error(\"StationDialog parts must be used inside <StationDialog>\")\n  return context\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/station-dialog-geometry.ts",
      "content": "export interface OriginBox {\n  left: number\n  top: number\n  right: number\n  bottom: number\n  width: number\n  height: number\n}\n\ninterface Size {\n  width: number\n  height: number\n}\n\nconst PIN_GAP_PX = 12\nconst VIEWPORT_MARGIN_PX = 16\n\nfunction clamp(value: number, low: number, high: number) {\n  return Math.max(low, Math.min(Math.max(low, high), value))\n}\n\n/** How far a centred surface has to travel to start on top of the point it opened from. */\nexport function morphOffset(origin: OriginBox, viewport: Size) {\n  return {\n    x: origin.left + origin.width / 2 - viewport.width / 2,\n    y: origin.top + origin.height / 2 - viewport.height / 2,\n  }\n}\n\n/** Beside the point if there is room on either side, otherwise under or over it, and always inside the screen. */\nexport function pinPosition(origin: OriginBox, size: Size, viewport: Size) {\n  const centerX = origin.left + origin.width / 2\n  const centerY = origin.top + origin.height / 2\n  const maxLeft = viewport.width - size.width - VIEWPORT_MARGIN_PX\n  const maxTop = viewport.height - size.height - VIEWPORT_MARGIN_PX\n  const besideTop = clamp(centerY - size.height / 2, VIEWPORT_MARGIN_PX, maxTop)\n\n  const rightOf = origin.right + PIN_GAP_PX\n  if (rightOf <= maxLeft) return { left: rightOf, top: besideTop, originX: 0, originY: centerY - besideTop }\n  const leftOf = origin.left - PIN_GAP_PX - size.width\n  if (leftOf >= VIEWPORT_MARGIN_PX) return { left: leftOf, top: besideTop, originX: size.width, originY: centerY - besideTop }\n\n  const left = clamp(centerX - size.width / 2, VIEWPORT_MARGIN_PX, maxLeft)\n  const under = origin.bottom + PIN_GAP_PX\n  if (under <= maxTop) return { left, top: under, originX: centerX - left, originY: 0 }\n  const top = clamp(origin.top - PIN_GAP_PX - size.height, VIEWPORT_MARGIN_PX, maxTop)\n  return { left, top, originX: centerX - left, originY: size.height }\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/station-dialog-surface.tsx",
      "content": "import type * as React from \"react\"\nimport { useCallback, useState } from \"react\"\nimport { Dialog as DialogPrimitive } from \"radix-ui\"\nimport { motion, useReducedMotion, type TargetAndTransition } from \"motion/react\"\nimport { X } from \"lucide-react\"\nimport { SPRING_DEFAULT } from \"@/registry/miseui/lib/motion\"\nimport { useStationDialog, type StationDialogVariant } from \"@/registry/miseui/ui/station-dialog-context\"\nimport { morphOffset, pinPosition, type OriginBox } from \"@/registry/miseui/ui/station-dialog-geometry\"\nimport { cn } from \"@/registry/miseui/lib/utils\"\n\nconst SURFACE_CLASS: Record<StationDialogVariant, string> = {\n  card: \"pointer-events-auto grid max-h-[86svh] w-full max-w-md overflow-y-auto overscroll-contain rounded-xl border bg-background shadow-xl\",\n  side: \"pointer-events-auto fixed inset-y-0 right-0 grid w-[min(24rem,calc(100%-2rem))] content-start overflow-y-auto overscroll-contain border-l bg-background shadow-xl\",\n  pin: \"pointer-events-auto fixed grid max-h-[60svh] w-[min(20rem,calc(100vw-2rem))] overflow-y-auto overscroll-contain rounded-lg border bg-popover text-popover-foreground shadow-lg\",\n}\n\nconst MORPH_START_SCALE = 0.2\nconst PIN_MORPH_START_SCALE = 0.6\nconst PLAIN_START_SCALE = 0.96\n\nfunction hiddenPose(variant: StationDialogVariant, morph: boolean, reduceMotion: boolean, box: OriginBox | null): TargetAndTransition {\n  if (reduceMotion) return { opacity: 0 }\n  if (variant === \"side\") return { x: \"100%\", opacity: 1 }\n  if (morph && box && variant === \"card\") {\n    return { ...morphOffset(box, { width: window.innerWidth, height: window.innerHeight }), scale: MORPH_START_SCALE, opacity: 0 }\n  }\n  return { scale: morph ? PIN_MORPH_START_SCALE : PLAIN_START_SCALE, opacity: 0 }\n}\n\nexport type StationDialogContentProps = Omit<React.ComponentProps<typeof DialogPrimitive.Content>, \"title\" | \"asChild\" | \"forceMount\"> & {\n  title: React.ReactNode\n  /** A short line above the title, such as the line and time. */\n  meta?: React.ReactNode\n  closeLabel?: string\n}\n\nfunction usePinPlacement(variant: StationDialogVariant, box: OriginBox | null) {\n  // A callback ref because the portal mounts after the first layout effect. Written to the style directly so there is no frame in the wrong place.\n  return useCallback(\n    (element: HTMLElement | null) => {\n      if (!element || variant !== \"pin\" || !box) return\n      const size = { width: element.offsetWidth, height: element.offsetHeight }\n      const place = pinPosition(box, size, { width: window.innerWidth, height: window.innerHeight })\n      element.style.left = `${place.left}px`\n      element.style.top = `${place.top}px`\n      element.style.transformOrigin = `${place.originX}px ${place.originY}px`\n    },\n    [box, variant],\n  )\n}\n\nfunction StationHeader({ title, meta, closeLabel, compact }: { title: React.ReactNode; meta: React.ReactNode; closeLabel: string; compact: boolean }) {\n  return (\n    <header className=\"flex items-start justify-between gap-4\">\n      <div className=\"grid min-w-0 gap-1\">\n        {meta ? <p className=\"text-[13px] text-muted-foreground\">{meta}</p> : null}\n        <DialogPrimitive.Title className={cn(\"font-semibold tracking-tight text-balance\", compact ? \"text-[15px]\" : \"text-xl\")}>\n          {title}\n        </DialogPrimitive.Title>\n      </div>\n      <DialogPrimitive.Close\n        aria-label={closeLabel}\n        className=\"-mt-2 -mr-2 inline-grid size-10 shrink-0 place-items-center rounded-full text-muted-foreground outline-none transition-colors duration-(--duration-fast) ease-(--ease-mise) hover:bg-muted hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50\"\n      >\n        <X aria-hidden=\"true\" className=\"size-4\" />\n      </DialogPrimitive.Close>\n    </header>\n  )\n}\n\nexport function StationSurface({ title, meta, closeLabel = \"Close\", className, children, onCloseAutoFocus, ...props }: StationDialogContentProps) {\n  const { variant, effect, origin } = useStationDialog()\n  const reduceMotion = useReducedMotion() ?? false\n  // Read once when the surface mounts: this is where it opened from, even if the page moves underneath it later.\n  const [box] = useState<OriginBox | null>(() => origin?.getBoundingClientRect() ?? null)\n  const hidden = hiddenPose(variant, effect === \"morph\", reduceMotion, box)\n  const placePin = usePinPlacement(variant, box)\n  const compact = variant === \"pin\"\n  return (\n    <DialogPrimitive.Content\n      forceMount\n      asChild\n      aria-describedby={undefined}\n      onCloseAutoFocus={(event) => {\n        onCloseAutoFocus?.(event)\n        if (event.defaultPrevented) return\n        // These triggers are not Radix triggers, so Radix does not know where to send focus back.\n        event.preventDefault()\n        if (origin?.isConnected) origin.focus()\n      }}\n      {...props}\n    >\n      <motion.div\n        ref={placePin}\n        data-slot=\"station-dialog\"\n        data-variant={variant}\n        data-effect={effect}\n        className={cn(SURFACE_CLASS[variant], className)}\n        initial={hidden}\n        animate={{ x: 0, y: 0, scale: 1, opacity: 1 }}\n        exit={hidden}\n        transition={SPRING_DEFAULT}\n      >\n        <div className={cn(\"grid min-w-0\", compact ? \"gap-2 p-4\" : \"gap-4 p-6\")}>\n          <StationHeader title={title} meta={meta} closeLabel={closeLabel} compact={compact} />\n          <div className=\"min-w-0 text-sm leading-relaxed\">{children}</div>\n        </div>\n      </motion.div>\n    </DialogPrimitive.Content>\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "overlays"
  ],
  "type": "registry:ui"
}