{
  "name": "scrollbar",
  "type": "registry:ui",
  "framework": "react",
  "title": "Scrollbar Utility",
  "description": "Discord-styled scrollbars for any scrollable container, with an overlay thumb, auto-hide and an optional keyboard-focusable viewport.",
  "files": [
    {
      "path": "ui/scrollbar.tsx",
      "content": "/*\n * No \"use client\" here: a Server Component imports the class strings as\n * strings. Only `ScrollArea`, which runs hooks, is a client component.\n */\nexport {\n  ScrollArea,\n  ScrollArea as Scrollbar,\n  type ScrollAreaProps,\n} from \"@/registry/bases/newt/ui/scroll-area\"\nexport {\n  scrollbarAutoHideClassName,\n  scrollbarClassName,\n} from \"@/registry/bases/newt/ui/scrollbar-classes\"\n",
      "type": "registry:ui"
    },
    {
      "path": "ui/scroll-area.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\n\nimport { cn } from \"@/lib/utils\"\nimport {\n  scrollbarAutoHideClassName,\n  scrollbarClassName,\n} from \"@/registry/bases/newt/ui/scrollbar-classes\"\n\n/*\n * A focusable region shows its focus inside its own box, where an ancestor\n * with `overflow: hidden` cannot clip it.\n */\nconst focusRingClassName =\n  \"focus-visible:outline focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-newt-text-link\"\n\n/** How long an auto-hiding thumb stays after the last scroll, in ms. */\nconst HIDE_DELAY = 800\n\n/**\n * Wires the behaviour CSS cannot express and returns its teardown. The\n * viewport scrolls natively, so the keyboard, the wheel and touch need nothing\n * from here. `root` is the element that carries `data-scrolling`.\n */\nfunction attachScrollbar(\n  root: HTMLElement,\n  viewport: HTMLElement,\n  autoHide: boolean,\n  overlay?: { track: HTMLElement; thumb: HTMLElement }\n): () => void {\n  const abort = new AbortController()\n  const { signal } = abort\n  let drag: { y: number; scrollTop: number; ratio: number } | undefined\n  let hideTimer: ReturnType<typeof setTimeout> | undefined\n\n  /*\n   * Hover and focus are the stylesheet's. This covers a scroll that neither\n   * explains, such as a new message or a touch.\n   */\n  function showWhileScrolling() {\n    if (!autoHide) return\n    root.setAttribute(\"data-scrolling\", \"\")\n    clearTimeout(hideTimer)\n    if (drag) return\n    hideTimer = setTimeout(\n      () => root.removeAttribute(\"data-scrolling\"),\n      HIDE_DELAY\n    )\n  }\n\n  viewport.addEventListener(\"scroll\", showWhileScrolling, {\n    passive: true,\n    signal,\n  })\n\n  if (overlay) {\n    const { track, thumb } = overlay\n\n    const draw = () => {\n      const overflow = viewport.scrollHeight - viewport.clientHeight\n      track.hidden = overflow < 1\n      if (track.hidden) return\n      const length = track.clientHeight\n      thumb.style.height = `${(viewport.clientHeight / viewport.scrollHeight) * length}px`\n      // The classes cap the height, so the travel is read back from the box.\n      const travel = Math.max(0, length - thumb.offsetHeight)\n      thumb.style.transform = `translateY(${(viewport.scrollTop / overflow) * travel}px)`\n    }\n\n    viewport.addEventListener(\"scroll\", draw, { passive: true, signal })\n\n    /*\n     * The content has no wrapper to measure, so each child is watched:\n     * content that grows or shrinks changes the size of one of them.\n     */\n    const resize = new ResizeObserver(draw)\n    const watch = () => {\n      resize.disconnect()\n      for (const element of [viewport, track, ...viewport.children]) {\n        resize.observe(element)\n      }\n    }\n    /*\n     * A new child needs watching. A changed text node resizes nothing that is\n     * watched when it sits straight in the viewport, so it redraws.\n     */\n    const mutation = new MutationObserver((records) => {\n      if (\n        records.some((r) => r.type === \"childList\" && r.target === viewport)\n      ) {\n        watch()\n      } else {\n        draw()\n      }\n    })\n    mutation.observe(viewport, {\n      childList: true,\n      characterData: true,\n      subtree: true,\n    })\n    watch()\n    signal.addEventListener(\"abort\", () => {\n      resize.disconnect()\n      mutation.disconnect()\n    })\n\n    /*\n     * Each pixel the thumb moves is worth as much scroll as the content is\n     * longer than the thumb's travel, which is how a native bar drags.\n     */\n    thumb.addEventListener(\n      \"pointerdown\",\n      (event) => {\n        if (event.button !== 0) return\n        event.preventDefault()\n        const travel = track.clientHeight - thumb.offsetHeight\n        if (travel <= 0) return\n        thumb.setPointerCapture(event.pointerId)\n        drag = {\n          y: event.clientY,\n          scrollTop: viewport.scrollTop,\n          ratio: (viewport.scrollHeight - viewport.clientHeight) / travel,\n        }\n        showWhileScrolling()\n      },\n      { signal }\n    )\n    thumb.addEventListener(\n      \"pointermove\",\n      (event) => {\n        if (!drag) return\n        viewport.scrollTop =\n          drag.scrollTop + (event.clientY - drag.y) * drag.ratio\n      },\n      { signal }\n    )\n    thumb.addEventListener(\n      \"lostpointercapture\",\n      () => {\n        drag = undefined\n        showWhileScrolling()\n      },\n      { signal }\n    )\n  }\n\n  return () => {\n    abort.abort()\n    clearTimeout(hideTimer)\n    root.removeAttribute(\"data-scrolling\")\n  }\n}\n\nexport interface ScrollAreaProps extends React.ComponentProps<\"div\"> {\n  /**\n   * `native` styles the browser's scrollbar, which keeps a lane beside the\n   * content. `overlay` draws a thumb over the content instead, so the box\n   * keeps its full width. It scrolls on the block axis only, and renders an\n   * outer box around the viewport: `className`, `style` and `dir` land on the\n   * box, every other attribute and the ref on the viewport, which is the\n   * element that scrolls.\n   */\n  variant?: \"native\" | \"overlay\"\n  /**\n   * Hide the thumb until the pointer is over the area, keyboard focus is\n   * inside it, or it scrolls.\n   */\n  autoHide?: boolean\n  /**\n   * Put the viewport in the tab order so it can be scrolled from the keyboard\n   * (WCAG 2.1.1). Off by default: a focusable region only helps when the area\n   * actually scrolls and holds no other focusable content.\n   */\n  focusable?: boolean\n  /** Accessible name of the focusable viewport. */\n  viewportLabel?: string\n  /**\n   * Classes for the element that scrolls. With `overlay`, padding that should\n   * scroll with the content belongs here.\n   */\n  viewportClassName?: string\n}\n\nconst ScrollArea = React.forwardRef<HTMLDivElement, ScrollAreaProps>(\n  (\n    {\n      className,\n      variant = \"native\",\n      autoHide = false,\n      focusable = false,\n      viewportLabel = \"Scrollable region\",\n      viewportClassName,\n      style,\n      dir,\n      ...props\n    },\n    ref\n  ) => {\n    const overlay = variant === \"overlay\"\n    const root = React.useRef<HTMLDivElement | null>(null)\n    const viewport = React.useRef<HTMLDivElement | null>(null)\n    const track = React.useRef<HTMLDivElement | null>(null)\n    const thumb = React.useRef<HTMLDivElement | null>(null)\n    React.useImperativeHandle<HTMLDivElement | null, HTMLDivElement | null>(\n      ref,\n      () => viewport.current\n    )\n\n    React.useEffect(() => {\n      if (!viewport.current || (!overlay && !autoHide)) return\n      return attachScrollbar(\n        root.current ?? viewport.current,\n        viewport.current,\n        autoHide,\n        track.current && thumb.current\n          ? { track: track.current, thumb: thumb.current }\n          : undefined\n      )\n    }, [overlay, autoHide])\n\n    if (!overlay) {\n      return (\n        <div\n          ref={viewport}\n          tabIndex={focusable ? 0 : undefined}\n          role={focusable ? \"group\" : undefined}\n          aria-label={focusable ? viewportLabel : undefined}\n          dir={dir}\n          style={style}\n          className={cn(\n            \"overflow-auto\",\n            scrollbarClassName,\n            autoHide && scrollbarAutoHideClassName,\n            focusRingClassName,\n            viewportClassName,\n            className\n          )}\n          {...props}\n        />\n      )\n    }\n\n    return (\n      <div\n        ref={root}\n        dir={dir}\n        style={style}\n        className={cn(\n          \"relative flex flex-col\",\n          autoHide &&\n            \"[&:not(:hover):not(:has(:focus-visible)):not([data-scrolling])>[data-scrollbar-track]]:invisible\",\n          className\n        )}\n      >\n        <div\n          ref={viewport}\n          tabIndex={focusable ? 0 : undefined}\n          role={focusable ? \"group\" : undefined}\n          aria-label={focusable ? viewportLabel : undefined}\n          className={cn(\n            \"min-h-0 flex-1 overflow-y-auto overflow-x-hidden [scrollbar-width:none] [&::-webkit-scrollbar]:hidden\",\n            focusRingClassName,\n            viewportClassName\n          )}\n          {...props}\n        />\n        {/*\n         * Only the thumb takes the pointer, so the content under the track\n         * stays clickable. Hidden until the content overflows.\n         */}\n        <div\n          ref={track}\n          data-scrollbar-track=\"\"\n          aria-hidden=\"true\"\n          hidden\n          className=\"pointer-events-none absolute bottom-[var(--newt-scrollbar-inset-end,4px)] end-1 top-[var(--newt-scrollbar-inset-start,4px)]\"\n        >\n          <div\n            ref={thumb}\n            className=\"pointer-events-auto max-h-[var(--newt-scrollbar-thumb-max-height,none)] min-h-[min(24px,100%)] w-[var(--newt-scrollbar-thumb-width,8px)] touch-none rounded-full bg-[var(--newt-scrollbar-thumb,var(--newt-bg-active))] hover:bg-[var(--newt-scrollbar-thumb-hover,var(--newt-text-muted))] active:bg-[var(--newt-scrollbar-thumb-hover,var(--newt-text-muted))]\"\n          />\n        </div>\n      </div>\n    )\n  }\n)\nScrollArea.displayName = \"ScrollArea\"\n\nexport { ScrollArea }\n",
      "type": "registry:ui"
    },
    {
      "path": "ui/scrollbar-classes.ts",
      "content": "/**\n * Tailwind classes that give any scrollable container Discord-styled\n * scrollbars (thin, rounded thumb, transparent track). Apply via\n * `<ScrollArea>` or spread `scrollbarClassName` on your own element.\n *\n * The thumb reads `--newt-scrollbar-thumb` and, under the pointer,\n * `--newt-scrollbar-thumb-hover`. Set either on the element or an ancestor.\n */\nexport const scrollbarClassName = [\n  \"[scrollbar-width:thin] [scrollbar-color:var(--newt-scrollbar-thumb,var(--newt-bg-active))_transparent]\",\n  \"[&::-webkit-scrollbar]:h-4 [&::-webkit-scrollbar]:w-4\",\n  \"[&::-webkit-scrollbar-track]:bg-transparent\",\n  \"[&::-webkit-scrollbar-thumb]:rounded-[var(--newt-radius-lg)] [&::-webkit-scrollbar-thumb]:border-4 [&::-webkit-scrollbar-thumb]:border-solid [&::-webkit-scrollbar-thumb]:border-transparent [&::-webkit-scrollbar-thumb]:bg-[var(--newt-scrollbar-thumb,var(--newt-bg-active))] [&::-webkit-scrollbar-thumb]:bg-clip-padding\",\n  \"[&::-webkit-scrollbar-thumb:hover]:bg-[var(--newt-scrollbar-thumb-hover,var(--newt-text-muted))] [&::-webkit-scrollbar-thumb:hover]:bg-clip-padding\",\n  \"[&::-webkit-scrollbar-corner]:bg-transparent\",\n].join(\" \")\n\n/**\n * Add next to `scrollbarClassName` to hide the thumb until the pointer is\n * over the element or keyboard focus is inside it. Set `data-scrolling` on\n * the element to show it for any other reason; `<ScrollArea autoHide>` does\n * that while the area scrolls.\n */\nexport const scrollbarAutoHideClassName = [\n  \"[&:not(:hover):not(:focus-visible):not(:has(:focus-visible)):not([data-scrolling])]:[scrollbar-color:transparent_transparent]\",\n  \"[&:not(:hover):not(:focus-visible):not(:has(:focus-visible)):not([data-scrolling])::-webkit-scrollbar-thumb]:bg-transparent\",\n].join(\" \")\n",
      "type": "registry:ui"
    }
  ]
}