@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
1 lines • 2.16 kB
Source Map (JSON)
{"version":3,"file":"utils.cjs","names":[],"sources":["../../src/utils.ts"],"sourcesContent":["import * as Solid from 'solid-js'\n\n/**\n * React hook to wrap `IntersectionObserver`.\n *\n * This hook will create an `IntersectionObserver` and observe the ref passed to it.\n *\n * When the intersection changes, the callback will be called with the `IntersectionObserverEntry`.\n *\n * @param ref - The ref to observe\n * @param intersectionObserverOptions - The options to pass to the IntersectionObserver\n * @param options - The options to pass to the hook\n * @param callback - The callback to call when the intersection changes\n * @returns The IntersectionObserver instance\n * @example\n * ```tsx\n * const MyComponent = () => {\n * const ref = React.useRef<HTMLDivElement>(null)\n * useIntersectionObserver(\n * ref,\n * (entry) => { doSomething(entry) },\n * { rootMargin: '10px' },\n * { disabled: false }\n * )\n * return <div ref={ref} />\n * ```\n */\nexport function useIntersectionObserver<T extends Element>(\n ref: Solid.Accessor<T | null>,\n callback: (entry: IntersectionObserverEntry | undefined) => void,\n intersectionObserverOptions: IntersectionObserverInit = {},\n options: { disabled?: boolean } = {},\n): Solid.Accessor<IntersectionObserver | null> {\n const isIntersectionObserverAvailable =\n typeof IntersectionObserver === 'function'\n let observerRef: IntersectionObserver | null = null\n\n Solid.createEffect(ref, (r) => {\n if (!r || !isIntersectionObserverAvailable || options.disabled) {\n return\n }\n\n observerRef = new IntersectionObserver(([entry]) => {\n callback(entry)\n }, intersectionObserverOptions)\n\n observerRef.observe(r)\n\n return () => {\n observerRef?.disconnect()\n }\n })\n\n return () => observerRef\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,wBACd,KACA,UACA,8BAAwD,EAAE,EAC1D,UAAkC,EAAE,EACS;CAC7C,MAAM,kCACJ,OAAO,yBAAyB;CAClC,IAAI,cAA2C;AAE/C,UAAM,aAAa,MAAM,MAAM;AAC7B,MAAI,CAAC,KAAK,CAAC,mCAAmC,QAAQ,SACpD;AAGF,gBAAc,IAAI,sBAAsB,CAAC,WAAW;AAClD,YAAS,MAAM;KACd,4BAA4B;AAE/B,cAAY,QAAQ,EAAE;AAEtB,eAAa;AACX,gBAAa,YAAY;;GAE3B;AAEF,cAAa"}