scroll-spy
diceui-radix/scroll-spyFreeComponent
diceui/radix publishes no description for this item.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/sadmann7/diceui/HEAD/docs/public/r/styles/radix-nova/scroll-spy.jsonSource
1"use client";23import {4 Direction as DirectionPrimitive,5 Slot as SlotPrimitive,6} from "radix-ui";7import * as React from "react";8import { useComposedRefs } from "@/lib/compose-refs";9import { cn } from "@/lib/utils";10import { useAsRef } from "@/registry/bases/radix/hooks/use-as-ref";11import { useIsomorphicLayoutEffect } from "@/registry/bases/radix/hooks/use-isomorphic-layout-effect";12import { useLazyRef } from "@/registry/bases/radix/hooks/use-lazy-ref";1314const ROOT_NAME = "ScrollSpy";15const NAV_NAME = "ScrollSpyNav";16const LINK_NAME = "ScrollSpyLink";17const VIEWPORT_NAME = "ScrollSpyViewport";18const SECTION_NAME = "ScrollSpySection";1920type Direction = "ltr" | "rtl";21type Orientation = "horizontal" | "vertical";2223type LinkElement = React.ComponentRef<typeof ScrollSpyLink>;24type SectionElement = React.ComponentRef<typeof ScrollSpySection>;2526function getDefaultScrollBehavior(): ScrollBehavior {27 if (typeof window === "undefined") return "smooth";28 return window.matchMedia("(prefers-reduced-motion: reduce)").matches29 ? "auto"30 : "smooth";31}3233interface StoreState {34 value: string;35}3637interface Store {38 subscribe: (callback: () => void) => () => void;39 getState: () => StoreState;40 setState: <K extends keyof StoreState>(key: K, value: StoreState[K]) => void;41 notify: () => void;42}4344const StoreContext = React.createContext<Store | null>(null);4546function useStore<T>(47 selector: (state: StoreState) => T,48 ogStore?: Store | null,49): T {50 const contextStore = React.useContext(StoreContext);5152 const store = ogStore ?? contextStore;5354 if (!store) {55 throw new Error(`\`useStore\` must be used within \`${ROOT_NAME}\``);56 }5758 const getSnapshot = React.useCallback(59 () => selector(store.getState()),60 [store, selector],61 );6263 return React.useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);64}6566interface ScrollSpyContextValue {67 offset: number;68 scrollBehavior: ScrollBehavior;69 dir: Direction;70 orientation: Orientation;71 scrollContainer: HTMLElement | null;72 isScrollingRef: React.RefObject<boolean>;73 onSectionRegister: (id: string, element: SectionElement) => void;74 onSectionUnregister: (id: string) => void;75 onScrollToSection: (sectionId: string) => void;76}7778const ScrollSpyContext = React.createContext<ScrollSpyContextValue | null>(79 null,80);8182function useScrollSpyContext(consumerName: string) {83 const context = React.useContext(ScrollSpyContext);84 if (!context) {85// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from diceui/radix
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| action-baraction-bar | diceui/radix | Components | Free | 1 dep · 2 files | |
| angle-sliderangle-slider | diceui/radix | Components | Free | 1 dep · 3 files | |
| avatar-groupavatar-group | diceui/radix | Components | Free | 1 dep | |
| badge-overflowbadge-overflow | diceui/radix | Components | Free | 1 dep · 2 files | |
| bannerbanner | diceui/radix | Components | Free | 1 dep | |
| checkbox-groupcheckbox-group | diceui/radix | Components | Free | 1 dep | |
| circular-progresscircular-progress | diceui/radix | Components | Free | 1 dep | |
| client-onlyclient-only | diceui/radix | Components | Free | no deps |
