{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-safari",
  "title": "Safari",
  "description": "A macOS browser window drawn in CSS around a picture, a video or live UI, with a chrome that follows the theme.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/icons.json",
    "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/safari.tsx",
      "content": "'use client';\nimport { cn } from '@/lib/utils';\nimport { AnimatePresence, motion } from 'motion/react';\nimport { useEffect, useRef, type ComponentProps, type ReactNode } from 'react';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport {\n  ChevronLeftIcon,\n  ChevronRightIcon,\n  CopyIcon,\n  PadlockIcon,\n  PanelLeftIcon,\n  PlusIcon,\n  RefreshCwIcon,\n  ShareBoxIcon,\n} from '@/registry/ndeko/lib/icons';\n\nexport type SafariMode = 'simple' | 'full';\n\nexport type SafariProps = Omit<ComponentProps<'figure'>, 'children'> & {\n  url?: string;\n  imageSrc?: string;\n  videoSrc?: string;\n  alt?: string;\n  mode?: SafariMode;\n  aspectRatio?: number;\n  label?: string;\n  children?: ReactNode;\n};\n\nconst DEFAULT_URL = 'example.com';\n\nconst DEFAULT_ASPECT_RATIO = 16 / 10;\n\nconst PROTOCOL_PATTERN = /^[a-z][a-z0-9+.-]*:\\/\\//i;\n\nconst TRAILING_SLASH_PATTERN = /\\/$/;\n\nconst TRAFFIC_LIGHTS = [\n  { name: 'close', color: 'hsl(3 100% 67%)' },\n  { name: 'minimize', color: 'hsl(41 99% 59%)' },\n  { name: 'zoom', color: 'hsl(129 67% 47%)' },\n];\n\nconst MEDIA_FADE_SECONDS = 0.24;\n\nconst MEDIA_ENTER = {\n  duration: MEDIA_FADE_SECONDS,\n  ease: [0.23, 1, 0.32, 1],\n} as const;\n\nconst MEDIA_EXIT = { duration: 0, delay: MEDIA_FADE_SECONDS } as const;\n\nconst TOOLBAR_HEIGHT: Record<SafariMode, string> = {\n  simple: 'h-11',\n  full: 'h-[52px]',\n};\n\nfunction displayUrlOf(url: string) {\n  const withoutProtocol = url.replace(PROTOCOL_PATTERN, '');\n  return withoutProtocol.replace(TRAILING_SLASH_PATTERN, '');\n}\n\nfunction contentRatioOf(\n  aspectRatio: number | undefined,\n  hasMedia: boolean,\n  hasChildren: boolean\n) {\n  if (aspectRatio !== undefined) {\n    return aspectRatio;\n  }\n  if (hasMedia) {\n    return DEFAULT_ASPECT_RATIO;\n  }\n  if (hasChildren) {\n    return undefined;\n  }\n  return DEFAULT_ASPECT_RATIO;\n}\n\nfunction ignoreRefusedPlayback() {\n  return undefined;\n}\n\nfunction TrafficLights() {\n  return (\n    <span className='flex shrink-0 items-center gap-2'>\n      {TRAFFIC_LIGHTS.map((light) => (\n        <span\n          key={light.name}\n          className='size-3 rounded-full shadow-[inset_0_0_0_0.5px_rgb(0_0_0/0.14)]'\n          style={{ backgroundColor: light.color }}\n        />\n      ))}\n    </span>\n  );\n}\n\nfunction ToolbarGlyph({\n  children,\n  className,\n}: {\n  children: ReactNode;\n  className?: string;\n}) {\n  return (\n    <span\n      className={cn(\n        'flex size-7 shrink-0 items-center justify-center [&_svg]:size-4',\n        className\n      )}\n    >\n      {children}\n    </span>\n  );\n}\n\nfunction AddressField({ url }: { url: string }) {\n  return (\n    <span className='bg-foreground/[0.05] dark:bg-foreground/[0.08] flex h-[30px] min-w-0 items-center gap-1.5 rounded-lg px-2.5'>\n      {/* Balances the reload glyph so the address sits in the middle of the field, as Safari centres it. */}\n      <span className='size-3.5 shrink-0' />\n      <span className='flex min-w-0 flex-1 items-center justify-center gap-1'>\n        <PadlockIcon className='text-muted-foreground size-3 shrink-0' />\n        <span className='text-foreground truncate text-[13px] leading-none'>\n          {url}\n        </span>\n      </span>\n      <RefreshCwIcon className='text-muted-foreground size-3.5 shrink-0' />\n    </span>\n  );\n}\n\nfunction Toolbar({ url, mode }: { url: string; mode: SafariMode }) {\n  const isFull = mode === 'full';\n\n  return (\n    <div\n      aria-hidden='true'\n      className={cn(\n        'hairline-b border-border bg-foreground/[0.03] dark:bg-foreground/[0.05] grid grid-cols-[minmax(max-content,1fr)_minmax(0,30rem)_minmax(max-content,1fr)] items-center gap-3 pr-3 pl-5 select-none dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)]',\n        TOOLBAR_HEIGHT[mode]\n      )}\n    >\n      <span className='text-muted-foreground flex items-center gap-5'>\n        <TrafficLights />\n        {isFull && (\n          <span className='flex items-center'>\n            <ToolbarGlyph className='mr-2 @max-xl:hidden'>\n              <PanelLeftIcon />\n            </ToolbarGlyph>\n            <ToolbarGlyph>\n              <ChevronLeftIcon />\n            </ToolbarGlyph>\n            <ToolbarGlyph className='opacity-40 @max-md:hidden'>\n              <ChevronRightIcon />\n            </ToolbarGlyph>\n          </span>\n        )}\n      </span>\n      <AddressField url={url} />\n      <span className='text-muted-foreground flex items-center justify-end'>\n        {isFull && (\n          <>\n            <ToolbarGlyph className='@max-xl:hidden'>\n              <ShareBoxIcon />\n            </ToolbarGlyph>\n            <ToolbarGlyph className='@max-md:hidden'>\n              <PlusIcon />\n            </ToolbarGlyph>\n            <ToolbarGlyph className='@max-md:hidden'>\n              <CopyIcon />\n            </ToolbarGlyph>\n          </>\n        )}\n      </span>\n    </div>\n  );\n}\n\nfunction WindowVideo({ src, alt }: { src: string; alt: string }) {\n  const videoRef = useRef<HTMLVideoElement>(null);\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const isDecorative = alt === '';\n  const showsControls = Boolean(shouldReduceMotion);\n  const isHidden = isDecorative && !showsControls;\n\n  useEffect(() => {\n    const video = videoRef.current;\n    if (!video) {\n      return;\n    }\n    if (shouldReduceMotion) {\n      video.pause();\n      return;\n    }\n    const playback = video.play();\n    playback.catch(ignoreRefusedPlayback);\n  }, [shouldReduceMotion]);\n\n  return (\n    <video\n      ref={videoRef}\n      src={src}\n      muted\n      loop\n      playsInline\n      preload='metadata'\n      controls={showsControls}\n      aria-label={alt || undefined}\n      aria-hidden={isHidden || undefined}\n      className='size-full object-cover object-top'\n    />\n  );\n}\n\nfunction WindowMedia({\n  imageSrc,\n  videoSrc,\n  alt,\n}: {\n  imageSrc?: string;\n  videoSrc?: string;\n  alt: string;\n}) {\n  const mediaKey = videoSrc ?? imageSrc;\n  const hasVideo = videoSrc !== undefined;\n  const hasImage = imageSrc !== undefined;\n  const showsImage = hasImage && !hasVideo;\n\n  return (\n    <AnimatePresence initial={false}>\n      <motion.div\n        key={mediaKey}\n        className='absolute inset-0'\n        initial={{ opacity: 0 }}\n        animate={{ opacity: 1, transition: MEDIA_ENTER }}\n        exit={{ opacity: 0, transition: MEDIA_EXIT }}\n      >\n        {hasVideo && <WindowVideo src={videoSrc} alt={alt} />}\n        {showsImage && (\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={imageSrc}\n            alt={alt}\n            draggable={false}\n            className='size-full object-cover object-top'\n          />\n        )}\n      </motion.div>\n    </AnimatePresence>\n  );\n}\n\nexport function Safari({\n  url = DEFAULT_URL,\n  imageSrc,\n  videoSrc,\n  alt = '',\n  mode = 'full',\n  aspectRatio,\n  label,\n  className,\n  children,\n  ...props\n}: SafariProps) {\n  const displayUrl = displayUrlOf(url);\n  const figureLabel = label ?? `Browser window at ${displayUrl}`;\n  const hasMedia = Boolean(imageSrc || videoSrc);\n  const hasChildren = children !== undefined && children !== null;\n  const contentRatio = contentRatioOf(aspectRatio, hasMedia, hasChildren);\n  const hasFixedHeight = contentRatio !== undefined;\n\n  return (\n    <figure\n      aria-label={figureLabel}\n      className={cn(\n        'bg-card text-card-foreground hairline-ring ring-foreground/10 @container relative w-full overflow-hidden rounded-xl shadow-xl shadow-black/8',\n        className\n      )}\n      {...props}\n    >\n      <Toolbar url={displayUrl} mode={mode} />\n      <div\n        className='bg-background text-foreground relative'\n        style={{ aspectRatio: contentRatio }}\n      >\n        {hasMedia && (\n          <WindowMedia imageSrc={imageSrc} videoSrc={videoSrc} alt={alt} />\n        )}\n        {hasChildren && (\n          <div\n            className={cn(\n              'relative',\n              hasFixedHeight &&\n                'absolute inset-0 [scrollbar-width:thin] overflow-y-auto overscroll-contain'\n            )}\n          >\n            {children}\n          </div>\n        )}\n      </div>\n    </figure>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/core/safari.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}