{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "video-player-plyr",
  "title": "Video Player (Plyr)",
  "author": "AlgorizeTech",
  "description": "Ask the user to pick one of 4 video player variants: https://avaraui.com/view/video-player. This is the Plyr player. Skip the question if the user already named one. A hero video player built on Plyr — one accessible, skinnable control bar in front of HTML5 files, YouTube and Vimeo alike.",
  "dependencies": [
    "plyr"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "src/components/custom/blocks/VideoPlayerPlyrBlock.tsx",
      "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport type PlyrInstance from \"plyr\";\nimport React, { useEffect, useRef, useState } from \"react\";\n\nimport \"plyr/dist/plyr.css\";\n\n/** Where the media comes from. Plyr wraps all three behind one control bar. */\nexport type PlyrProvider = \"html5\" | \"youtube\" | \"vimeo\";\n\nexport interface PlyrPlayerProps\n  extends React.HTMLAttributes<HTMLDivElement> {\n  /**\n   * For `provider=\"html5\"` a media file URL. For `youtube` / `vimeo` either a\n   * watch URL or the bare video id — Plyr accepts both.\n   */\n  src?: string;\n  provider?: PlyrProvider;\n  /** `video` renders a `<video>`, `audio` an `<audio>`. HTML5 only. */\n  mediaType?: \"video\" | \"audio\";\n  /** MIME type for the HTML5 source, e.g. `video/mp4`. */\n  srcType?: string;\n  posterUrl?: string;\n  title?: string;\n  subtitle?: string;\n  aspectRatio?: \"16/9\" | \"21/9\" | \"4/3\" | \"1/1\";\n  /**\n   * Drives Plyr's `--plyr-color-main`, which is what actually skins the\n   * control bar, the progress fill and the focus ring. Defaults to the theme's\n   * primary colour.\n   */\n  accentColor?: string;\n  /** Plyr control names in bar order. Omit for Plyr's own default set. */\n  controls?: string[];\n  enableGlow?: boolean;\n  /**\n   * The title/subtitle laid over the frame: at the foot until the video is\n   * first played, then a hover-only bar at the top. Turn it off for a bare\n   * player with no copy on it.\n   */\n  showOverlay?: boolean;\n  autoPlay?: boolean;\n  muted?: boolean;\n  loop?: boolean;\n  /**\n   * Where Plyr fetches its control icons. Defaults to Plyr's own CDN sprite —\n   * point it at a self-hosted copy of `plyr.svg` if the page must not reach\n   * out to a third-party host.\n   */\n  iconUrl?: string;\n  onReady?: () => void;\n}\n\nconst aspectRatios = {\n  \"16/9\": \"aspect-video\",\n  \"21/9\": \"aspect-[21/9]\",\n  \"4/3\": \"aspect-[4/3]\",\n  \"1/1\": \"aspect-square\",\n};\n\n/** Plyr's `ratio` option wants `w:h`, not the CSS `w/h` this component takes. */\nconst plyrRatios: Record<string, string> = {\n  \"16/9\": \"16:9\",\n  \"21/9\": \"21:9\",\n  \"4/3\": \"4:3\",\n  \"1/1\": \"1:1\",\n};\n\n/**\n * A YouTube or Vimeo embed id.\n *\n * Plyr reads the id out of `data-plyr-embed-id`, and while it does accept a\n * full URL there, handing it the id keeps the attribute stable across the\n * half-dozen URL shapes YouTube serves (`watch?v=`, `youtu.be/`, `/embed/`,\n * `/shorts/`) rather than depending on Plyr's own parsing.\n */\nfunction embedId(src: string, provider: PlyrProvider): string {\n  if (provider === \"html5\" || !/^https?:\\/\\//i.test(src)) return src;\n  try {\n    const url = new URL(src);\n    const fromQuery = url.searchParams.get(\"v\");\n    if (fromQuery) return fromQuery;\n    const last = url.pathname.split(\"/\").filter(Boolean).pop();\n    return last || src;\n  } catch {\n    return src;\n  }\n}\n\n/**\n * Title and subtitle laid over the frame, following the convention YouTube set.\n *\n * Before anything has played the copy sits at the FOOT of the frame and stays\n * there: nothing is happening yet, so it is free to introduce the video, and a\n * viewer should not have to hover to find out what they are about to watch.\n * Its bottom padding keeps it clear of Plyr's control bar, so the gradient\n * reaches the bottom edge but the text stops above the scrubber.\n *\n * Once playback has started that is no longer true — the picture is the point,\n * and a permanent caption is in the way. So the copy moves to the TOP of the\n * frame and only appears on hover, where it overlaps nothing that matters.\n *\n * `pointer-events-none` keeps every click going through to Plyr in both\n * positions.\n */\nfunction MediaOverlay({\n  title,\n  subtitle,\n  hasPlayed,\n}: {\n  title?: string;\n  subtitle?: string;\n  /** Whether the viewer has started watching. Drives the whole treatment. */\n  hasPlayed?: boolean;\n}) {\n  if (!title && !subtitle) return null;\n\n  const copy = (\n    <>\n      {title && (\n        <h3 className=\"line-clamp-1 text-base font-semibold tracking-tight text-white drop-shadow-sm sm:text-xl md:text-2xl\">\n          {title}\n        </h3>\n      )}\n      {subtitle && (\n        <p className=\"mt-0.5 line-clamp-1 text-[11px] text-zinc-300 sm:text-xs md:text-sm\">\n          {subtitle}\n        </p>\n      )}\n    </>\n  );\n\n  if (hasPlayed) {\n    return (\n      <div className=\"pointer-events-none absolute inset-x-0 top-0 z-10 bg-gradient-to-b from-black/85 via-black/40 to-transparent px-4 pt-4 pb-16 opacity-0 transition-opacity duration-300 group-hover/frame:opacity-100 group-focus-within/frame:opacity-100 sm:px-6 sm:pt-5 sm:pb-20\">\n        {copy}\n      </div>\n    );\n  }\n\n  return (\n    <div className=\"pointer-events-none absolute inset-x-0 bottom-0 z-10 bg-gradient-to-t from-black/90 via-black/50 to-transparent px-4 pt-16 pb-14 transition-opacity duration-300 sm:px-6 sm:pt-24 sm:pb-16\">\n      {copy}\n    </div>\n  );\n}\n\nexport const PlyrPlayer = React.forwardRef<\n  HTMLDivElement,\n  PlyrPlayerProps\n>(\n  (\n    {\n      src = \"https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-576p.mp4\",\n      provider = \"html5\",\n      mediaType = \"video\",\n      srcType = \"video/mp4\",\n      posterUrl = \"https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-HD.jpg\",\n      title = \"View From A Blue Moon\",\n      subtitle = \"An accessible, customizable HTML5, YouTube and Vimeo player.\",\n      aspectRatio = \"16/9\",\n      accentColor,\n      controls,\n      enableGlow = true,\n      showOverlay = true,\n      autoPlay = false,\n      muted = false,\n      loop = false,\n      iconUrl,\n      onReady,\n      className,\n      ...props\n    },\n    ref,\n  ) => {\n    const mediaRef = useRef<HTMLElement | null>(null);\n    const playerRef = useRef<PlyrInstance | null>(null);\n    const [isReady, setIsReady] = useState(false);\n    // Latches on the first play and never goes back: the title treatment keys\n    // off \"has this viewer started watching\", not the current paused state, so\n    // pausing must not bring the bottom caption back over the picture.\n    const [hasPlayed, setHasPlayed] = useState(false);\n    const onReadyRef = useRef(onReady);\n    useEffect(() => {\n      onReadyRef.current = onReady;\n    });\n\n    const isEmbed = provider !== \"html5\";\n    // `controls` is an array literal, so it is a new reference on every parent\n    // render. The effect keys off its serialized value and reads the live array\n    // through a ref — depending on the array itself would rebuild the player on\n    // every render and restart playback under the viewer.\n    const controlsKey = controls ? JSON.stringify(controls) : \"\";\n    const controlsRef = useRef(controls);\n    useEffect(() => {\n      controlsRef.current = controls;\n    });\n\n    useEffect(() => {\n      const element = mediaRef.current;\n      if (!element) return;\n\n      let cancelled = false;\n      let player: PlyrInstance | null = null;\n      setHasPlayed(false);\n\n      // Plyr touches `document` at construction, so it is imported here rather\n      // than at module scope: the component then renders on the server and\n      // hydrates without the library ever loading during SSR.\n      void import(\"plyr\").then(({ default: Plyr }) => {\n        if (cancelled) return;\n        const barControls = controlsRef.current;\n        player = new Plyr(element, {\n          autoplay: autoPlay,\n          muted: muted || autoPlay,\n          loop: { active: loop },\n          ratio: plyrRatios[aspectRatio],\n          ...(barControls ? { controls: barControls } : {}),\n          ...(iconUrl ? { iconUrl } : {}),\n        });\n        player.on(\"play\", () => setHasPlayed(true));\n        playerRef.current = player;\n        setIsReady(true);\n        onReadyRef.current?.();\n      });\n\n      return () => {\n        cancelled = true;\n        setIsReady(false);\n        playerRef.current = null;\n        // A YouTube/Vimeo teardown can race the provider iframe and throw after\n        // the node is already gone. Nothing useful is left to do at that point,\n        // and letting it escape breaks the unmount of everything above it.\n        try {\n          player?.destroy();\n        } catch {\n          /* already torn down */\n        }\n      };\n    }, [\n      src,\n      provider,\n      mediaType,\n      aspectRatio,\n      autoPlay,\n      muted,\n      loop,\n      iconUrl,\n      controlsKey,\n    ]);\n\n    const setMedia = (el: HTMLElement | null) => {\n      mediaRef.current = el;\n    };\n\n    const media = isEmbed ? (\n      <div\n        ref={setMedia}\n        data-plyr-provider={provider}\n        data-plyr-embed-id={embedId(src, provider)}\n      />\n    ) : mediaType === \"audio\" ? (\n      <audio ref={setMedia} controls>\n        <source src={src} type={srcType} />\n      </audio>\n    ) : (\n      <video ref={setMedia} controls playsInline poster={posterUrl}>\n        <source src={src} type={srcType} />\n      </video>\n    );\n\n    const isAudio = !isEmbed && mediaType === \"audio\";\n\n    return (\n      <div\n        ref={ref}\n        className={cn(\"relative w-full\", className)}\n        // Plyr is skinned entirely through CSS custom properties, so the accent\n        // is set on the wrapper rather than through a stylesheet override.\n        style={\n          {\n            \"--plyr-color-main\": accentColor ?? \"var(--primary)\",\n          } as React.CSSProperties\n        }\n        {...props}\n      >\n        {enableGlow && (\n          <div className=\"pointer-events-none absolute -inset-4 z-0 rounded-3xl bg-gradient-to-tr from-primary/20 via-primary/20 to-primary/10 opacity-80 blur-3xl transition-opacity duration-500 dark:opacity-50\" />\n        )}\n\n        <div className=\"relative z-10 flex w-full flex-col overflow-hidden rounded-xl border border-border/80 bg-zinc-900 shadow-2xl sm:rounded-2xl md:rounded-3xl\">\n          <div\n            className={cn(\n              \"group/frame relative w-full bg-black\",\n              isAudio ? \"p-4 sm:p-6\" : aspectRatios[aspectRatio],\n            )}\n          >\n            {/* An audio player has no picture to lay copy over, so its title\n                sits above the control bar in normal flow instead. */}\n            {isAudio && showOverlay && (title || subtitle) && (\n              <div className=\"mb-3\">\n                {title && (\n                  <h3 className=\"line-clamp-1 text-base font-semibold tracking-tight text-white sm:text-xl\">\n                    {title}\n                  </h3>\n                )}\n                {subtitle && (\n                  <p className=\"mt-0.5 line-clamp-1 text-[11px] text-zinc-400 sm:text-xs\">\n                    {subtitle}\n                  </p>\n                )}\n              </div>\n            )}\n\n            {/* Plyr rewrites the media element into its own markup on init.\n                Until that happens the raw <video> would flash at its intrinsic\n                size, so it is held invisible behind a matching placeholder. */}\n            <div\n              className={cn(\n                \"h-full w-full\",\n                !isReady && \"invisible\",\n                !isAudio && \"[&_.plyr]:h-full [&_.plyr]:w-full\",\n              )}\n            >\n              {media}\n            </div>\n\n            {!isReady && (\n              <div className=\"absolute inset-0 flex items-center justify-center bg-zinc-950\">\n                <span className=\"h-8 w-8 animate-spin rounded-full border-2 border-white/20 border-t-white/80\" />\n              </div>\n            )}\n\n            {!isAudio && isReady && showOverlay && (\n              <MediaOverlay\n                title={title}\n                subtitle={subtitle}\n                hasPlayed={hasPlayed}\n              />\n            )}\n          </div>\n        </div>\n      </div>\n    );\n  },\n);\n\nPlyrPlayer.displayName = \"PlyrPlayer\";\n",
      "type": "registry:component",
      "target": "components/custom/blocks/VideoPlayerPlyrBlock.tsx"
    }
  ],
  "docs": "Named export: PlyrPlayer. Requires the `plyr` package, which the CLI installs. `provider` is html5 | youtube | vimeo and `mediaType` is video | audio; `accentColor` sets Plyr's `--plyr-color-main`, and `controls` takes Plyr's control-name array. Plyr is imported dynamically inside an effect, so the component server-renders cleanly. Its control icons come from Plyr's CDN sprite by default — set `iconUrl` to a self-hosted `plyr.svg` if the page must not reach a third-party host.",
  "categories": [
    "media"
  ],
  "type": "registry:component"
}
