{
  "name": "scroll-spy",
  "type": "registry:ui",
  "dependencies": [
    "cn",
    "radix-ui",
    "@base-ui/utils@0.5.0"
  ],
  "files": [
    {
      "path": "ui/scroll-spy.tsx",
      "type": "registry:ui",
      "content": "\"use client\";\n\nimport { NOOP } from \"@base-ui/utils/empty\";\nimport { ReactStore, useStore } from \"@base-ui/utils/store\";\nimport { useAnimationFrame } from \"@base-ui/utils/useAnimationFrame\";\nimport { useIsoLayoutEffect } from \"@base-ui/utils/useIsoLayoutEffect\";\nimport { useMergedRefs } from \"@base-ui/utils/useMergedRefs\";\nimport { useTimeout } from \"@base-ui/utils/useTimeout\";\nimport { cn } from \"cn\";\nimport {\n  Direction as DirectionPrimitive,\n  Slot as SlotPrimitive,\n} from \"radix-ui\";\nimport * as React from \"react\";\n\nconst ROOT_NAME = \"ScrollSpy\";\nconst NAV_NAME = \"ScrollSpyNav\";\nconst LINK_NAME = \"ScrollSpyLink\";\nconst VIEWPORT_NAME = \"ScrollSpyViewport\";\nconst SECTION_NAME = \"ScrollSpySection\";\n\ntype Direction = \"ltr\" | \"rtl\";\ntype Orientation = \"horizontal\" | \"vertical\";\n\ntype LinkElement = HTMLAnchorElement;\ntype SectionElement = HTMLDivElement;\n\nfunction getDefaultScrollBehavior(): ScrollBehavior {\n  if (typeof window === \"undefined\") return \"smooth\";\n  return window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches\n    ? \"auto\"\n    : \"smooth\";\n}\n\ninterface StoreState {\n  value: string;\n}\n\ninterface StoreContext {\n  isValueControlled: boolean;\n  onValueChange: (value: string) => void;\n}\n\nconst selectors = {\n  value: (state: StoreState) => state.value,\n};\n\ntype Store = ReturnType<typeof createStore>;\n\nfunction createStore(state: StoreState) {\n  const store = new ReactStore<StoreState, StoreContext, typeof selectors>(\n    state,\n    { isValueControlled: false, onValueChange: NOOP },\n    selectors,\n  );\n  let requestedValue = state.value;\n\n  function setContext(context: Partial<StoreContext>) {\n    Object.assign(store.context, context);\n  }\n\n  function setValue(value: string) {\n    if (Object.is(store.state.value, value)) return;\n    requestedValue = value;\n    if (value) store.context.onValueChange(value);\n    if (!store.context.isValueControlled) store.set(\"value\", value);\n  }\n\n  function syncValue(value: string) {\n    const isRequested = Object.is(requestedValue, value);\n    requestedValue = value;\n    store.set(\"value\", value);\n    return isRequested;\n  }\n\n  return Object.assign(store, { setContext, setValue, syncValue });\n}\n\nconst StoreContext = React.createContext<Store | null>(null);\n\nfunction useStoreContext(consumerName: string) {\n  const context = React.useContext(StoreContext);\n  if (!context) {\n    throw new Error(`\\`${consumerName}\\` must be used within \\`${ROOT_NAME}\\``);\n  }\n  return context;\n}\n\ninterface ScrollSpyContextValue {\n  offset: number;\n  scrollBehavior: ScrollBehavior;\n  dir: Direction;\n  orientation: Orientation;\n  scrollContainer: HTMLElement | null;\n  isScrollingRef: React.RefObject<boolean>;\n  onSectionRegister: (id: string, element: SectionElement) => void;\n  onSectionUnregister: (id: string) => void;\n  onScrollToSection: (sectionId: string) => void;\n}\n\nconst ScrollSpyContext = React.createContext<ScrollSpyContextValue | null>(\n  null,\n);\n\nfunction useScrollSpyContext(consumerName: string) {\n  const context = React.useContext(ScrollSpyContext);\n  if (!context) {\n    throw new Error(`\\`${consumerName}\\` must be used within \\`${ROOT_NAME}\\``);\n  }\n  return context;\n}\n\ninterface ScrollSpyProps extends React.ComponentProps<\"div\"> {\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (value: string) => void;\n  rootMargin?: string;\n  threshold?: number | number[];\n  offset?: number;\n  scrollBehavior?: ScrollBehavior;\n  scrollContainer?: HTMLElement | null;\n  dir?: Direction;\n  orientation?: Orientation;\n  asChild?: boolean;\n}\n\nfunction ScrollSpy(props: ScrollSpyProps) {\n  const {\n    value,\n    defaultValue,\n    onValueChange,\n    rootMargin,\n    threshold = 0.1,\n    offset = 0,\n    scrollBehavior = getDefaultScrollBehavior(),\n    scrollContainer = null,\n    dir: dirProp,\n    orientation = \"horizontal\",\n    asChild,\n    className,\n    ...rootProps\n  } = props;\n\n  const dir = DirectionPrimitive.useDirection(dirProp);\n\n  const [store] = React.useState(() =>\n    createStore({ value: value ?? defaultValue ?? \"\" }),\n  );\n\n  const isValueControlled = value !== undefined;\n\n  useIsoLayoutEffect(() => {\n    store.setContext({\n      isValueControlled,\n      onValueChange: onValueChange ?? NOOP,\n    });\n  });\n\n  const sectionMapRef = React.useRef(new Map<string, Element>());\n  const isScrollingRef = React.useRef(false);\n  const scrollTimeout = useTimeout();\n  const frame = useAnimationFrame();\n\n  const onSectionRegister = React.useCallback(\n    (id: string, element: SectionElement) => {\n      sectionMapRef.current.set(id, element);\n    },\n    [],\n  );\n\n  const onSectionUnregister = React.useCallback((id: string) => {\n    sectionMapRef.current.delete(id);\n  }, []);\n\n  const scrollToSection = React.useCallback(\n    (sectionId: string) => {\n      const section = scrollContainer\n        ? scrollContainer.querySelector(`#${sectionId}`)\n        : document.getElementById(sectionId);\n\n      if (!section) return;\n\n      // Keeps the observer from firing during the programmatic scroll.\n      isScrollingRef.current = true;\n\n      if (scrollContainer) {\n        const containerRect = scrollContainer.getBoundingClientRect();\n        const sectionRect = section.getBoundingClientRect();\n        const scrollTop = scrollContainer.scrollTop;\n        const offsetPosition =\n          sectionRect.top - containerRect.top + scrollTop - offset;\n\n        scrollContainer.scrollTo({\n          top: offsetPosition,\n          behavior: scrollBehavior,\n        });\n      } else {\n        const sectionPosition = section.getBoundingClientRect().top;\n        const offsetPosition = sectionPosition + window.scrollY - offset;\n\n        window.scrollTo({\n          top: offsetPosition,\n          behavior: scrollBehavior,\n        });\n      }\n\n      scrollTimeout.start(500, () => {\n        isScrollingRef.current = false;\n      });\n    },\n    [scrollContainer, offset, scrollBehavior, scrollTimeout],\n  );\n\n  const onScrollToSection = React.useCallback(\n    (sectionId: string) => {\n      store.setValue(sectionId);\n      scrollToSection(sectionId);\n    },\n    [store, scrollToSection],\n  );\n\n  useIsoLayoutEffect(() => {\n    if (value === undefined) return;\n    const isRequested = store.syncValue(value);\n    if (!isRequested) scrollToSection(value);\n  }, [value, scrollToSection, store]);\n\n  useIsoLayoutEffect(() => {\n    const sectionMap = sectionMapRef.current;\n    if (sectionMap.size === 0) return;\n\n    const observerRootMargin = rootMargin ?? `${-offset}px 0px -70% 0px`;\n\n    const observer = new IntersectionObserver(\n      (entries) => {\n        if (isScrollingRef.current) return;\n\n        frame.request(() => {\n          const intersecting = entries.filter((entry) => entry.isIntersecting);\n\n          if (intersecting.length === 0) return;\n\n          const topmost = intersecting.reduce((prev, curr) => {\n            return curr.boundingClientRect.top < prev.boundingClientRect.top\n              ? curr\n              : prev;\n          });\n\n          const id = topmost.target.id;\n          if (id && sectionMap.has(id)) {\n            store.setValue(id);\n          }\n        });\n      },\n      {\n        root: scrollContainer,\n        rootMargin: observerRootMargin,\n        threshold,\n      },\n    );\n\n    for (const element of sectionMap.values()) {\n      observer.observe(element);\n    }\n\n    return () => {\n      observer.disconnect();\n      frame.cancel();\n    };\n  }, [offset, rootMargin, threshold, scrollContainer, store, frame]);\n\n  const contextValue = React.useMemo<ScrollSpyContextValue>(\n    () => ({\n      dir,\n      orientation,\n      offset,\n      scrollBehavior,\n      scrollContainer,\n      isScrollingRef,\n      onSectionRegister,\n      onSectionUnregister,\n      onScrollToSection,\n    }),\n    [\n      dir,\n      orientation,\n      offset,\n      scrollBehavior,\n      scrollContainer,\n      onSectionRegister,\n      onSectionUnregister,\n      onScrollToSection,\n    ],\n  );\n\n  const RootPrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <StoreContext.Provider value={store}>\n      <ScrollSpyContext.Provider value={contextValue}>\n        <RootPrimitive\n          data-orientation={orientation}\n          data-slot=\"scroll-spy\"\n          dir={dir}\n          {...rootProps}\n          className={cn(\n            \"flex\",\n            orientation === \"horizontal\" ? \"flex-row\" : \"flex-col\",\n            className,\n          )}\n        />\n      </ScrollSpyContext.Provider>\n    </StoreContext.Provider>\n  );\n}\n\ninterface ScrollSpyNavProps extends React.ComponentProps<\"nav\"> {\n  asChild?: boolean;\n}\n\nfunction ScrollSpyNav(props: ScrollSpyNavProps) {\n  const { asChild, className, ...navProps } = props;\n\n  const { dir, orientation } = useScrollSpyContext(NAV_NAME);\n\n  const NavPrimitive = asChild ? SlotPrimitive.Slot : \"nav\";\n\n  return (\n    <NavPrimitive\n      data-orientation={orientation}\n      data-slot=\"scroll-spy-nav\"\n      dir={dir}\n      {...navProps}\n      className={cn(\n        \"flex gap-2\",\n        orientation === \"horizontal\" ? \"flex-col\" : \"flex-row\",\n        className,\n      )}\n    />\n  );\n}\n\ninterface ScrollSpyLinkProps extends React.ComponentProps<\"a\"> {\n  value: string;\n  asChild?: boolean;\n}\n\nfunction ScrollSpyLink(props: ScrollSpyLinkProps) {\n  const { value: linkValue, asChild, onClick, className, ...linkProps } = props;\n\n  const { orientation, onScrollToSection } = useScrollSpyContext(LINK_NAME);\n  const store = useStoreContext(LINK_NAME);\n  const value = useStore(store, selectors.value);\n  const isActive = value === linkValue;\n\n  const onLinkClick = React.useCallback(\n    (event: React.MouseEvent<LinkElement>) => {\n      event.preventDefault();\n      onClick?.(event);\n      onScrollToSection(linkValue);\n    },\n    [linkValue, onClick, onScrollToSection],\n  );\n\n  const LinkPrimitive = asChild ? SlotPrimitive.Slot : \"a\";\n\n  return (\n    <LinkPrimitive\n      data-orientation={orientation}\n      data-slot=\"scroll-spy-link\"\n      data-state={isActive ? \"active\" : \"inactive\"}\n      {...linkProps}\n      href={asChild ? undefined : `#${linkValue}`}\n      className={cn(\"rounded-md px-3 py-1.5 text-sm font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-accent-foreground data-[state=active]:bg-accent data-[state=active]:text-foreground\", className)}\n      onClick={onLinkClick}\n    />\n  );\n}\n\ninterface ScrollSpyViewportProps extends React.ComponentProps<\"div\"> {\n  asChild?: boolean;\n}\n\nfunction ScrollSpyViewport(props: ScrollSpyViewportProps) {\n  const { asChild, className, ...viewportProps } = props;\n\n  const { dir, orientation } = useScrollSpyContext(VIEWPORT_NAME);\n\n  const ViewportPrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <ViewportPrimitive\n      data-orientation={orientation}\n      data-slot=\"scroll-spy-viewport\"\n      dir={dir}\n      {...viewportProps}\n      className={cn(\"flex flex-1 flex-col gap-8\", className)}\n    />\n  );\n}\n\ninterface ScrollSpySectionProps extends React.ComponentProps<\"div\"> {\n  value: string;\n  asChild?: boolean;\n}\n\nfunction ScrollSpySection(props: ScrollSpySectionProps) {\n  const { asChild, ref, value, ...sectionProps } = props;\n\n  const { orientation, onSectionRegister, onSectionUnregister } =\n    useScrollSpyContext(SECTION_NAME);\n  const sectionRef = React.useRef<SectionElement>(null);\n  const composedRef = useMergedRefs(ref, sectionRef);\n\n  useIsoLayoutEffect(() => {\n    const element = sectionRef.current;\n    if (!element || !value) return;\n\n    onSectionRegister(value, element);\n\n    return () => {\n      onSectionUnregister(value);\n    };\n  }, [value, onSectionRegister, onSectionUnregister]);\n\n  const SectionPrimitive = asChild ? SlotPrimitive.Slot : \"div\";\n\n  return (\n    <SectionPrimitive\n      data-orientation={orientation}\n      data-slot=\"scroll-spy-section\"\n      {...sectionProps}\n      id={value}\n      ref={composedRef}\n    />\n  );\n}\n\nexport {\n  ScrollSpy,\n  ScrollSpyLink,\n  ScrollSpyNav,\n  type ScrollSpyProps,\n  ScrollSpySection,\n  ScrollSpyViewport,\n};\n",
      "target": ""
    }
  ]
}