scroll-spy
diceui-base/scroll-spyFreeComponent
diceui/base publishes no description for this item.
Install it
npx shadcn@latest add https://raw.githubusercontent.com/sadmann7/diceui/HEAD/docs/public/r/styles/base-vega/scroll-spy.jsonSource
1"use client";23import { mergeProps } from "@base-ui/react/merge-props";4import { useRender } from "@base-ui/react/use-render";5import * as React from "react";6import { cn } from "@/lib/utils";7import { useAsRef } from "@/registry/bases/base/hooks/use-as-ref";8import { useIsomorphicLayoutEffect } from "@/registry/bases/base/hooks/use-isomorphic-layout-effect";9import { useLazyRef } from "@/registry/bases/base/hooks/use-lazy-ref";10import { useComposedRefs } from "@/registry/bases/base/lib/compose-refs";11import { useDirection } from "@/registry/bases/base/ui/direction";1213const ROOT_NAME = "ScrollSpy";14const NAV_NAME = "ScrollSpyNav";15const LINK_NAME = "ScrollSpyLink";16const VIEWPORT_NAME = "ScrollSpyViewport";17const SECTION_NAME = "ScrollSpySection";1819type Direction = "ltr" | "rtl";20type Orientation = "horizontal" | "vertical";2122type LinkElement = HTMLAnchorElement;23type SectionElement = HTMLDivElement;2425function getDefaultScrollBehavior(): ScrollBehavior {26 if (typeof window === "undefined") return "smooth";27 return window.matchMedia("(prefers-reduced-motion: reduce)").matches28 ? "auto"29 : "smooth";30}3132interface StoreState {33 value: string;34}3536interface Store {37 subscribe: (callback: () => void) => () => void;38 getState: () => StoreState;39 setState: <K extends keyof StoreState>(key: K, value: StoreState[K]) => void;40 notify: () => void;41}4243const StoreContext = React.createContext<Store | null>(null);4445function useStore<T>(46 selector: (state: StoreState) => T,47 ogStore?: Store | null,48): T {49 const contextStore = React.useContext(StoreContext);5051 const store = ogStore ?? contextStore;5253 if (!store) {54 throw new Error(`\`useStore\` must be used within \`${ROOT_NAME}\``);55 }5657 const getSnapshot = React.useCallback(58 () => selector(store.getState()),59 [store, selector],60 );6162 return React.useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);63}6465interface ScrollSpyContextValue {66 offset: number;67 scrollBehavior: ScrollBehavior;68 dir: Direction;69 orientation: Orientation;70 scrollContainer: HTMLElement | null;71 isScrollingRef: React.RefObject<boolean>;72 onSectionRegister: (id: string, element: SectionElement) => void;73 onSectionUnregister: (id: string) => void;74 onScrollToSection: (sectionId: string) => void;75}7677const ScrollSpyContext = React.createContext<ScrollSpyContextValue | null>(78 null,79);8081function useScrollSpyContext(consumerName: string) {82 const context = React.useContext(ScrollSpyContext);83// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from diceui/base
All 192 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| action-baraction-bar | diceui/base | Components | Free | 1 dep | |
| angle-sliderangle-slider | diceui/base | Components | Free | 1 dep · 3 files | |
| avatar-groupavatar-group | diceui/base | Components | Free | 1 dep | |
| badge-overflowbadge-overflow | diceui/base | Components | Free | 1 dep · 2 files | |
| bannerbanner | diceui/base | Components | Free | 1 dep | |
| circular-progresscircular-progress | diceui/base | Components | Free | 1 dep | |
| client-onlyclient-only | diceui/base | Components | Free | no deps | |
| color-pickercolor-picker | diceui/base | Components | Free | 1 dep · 2 files |
