UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

1 lines 2.14 kB
{"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 callback - The callback to call when the intersection changes\n * @param disabled - Whether observation is disabled\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 * 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) => void,\n disabled: Solid.Accessor<boolean>,\n): Solid.Accessor<IntersectionObserver | null> {\n const isIntersectionObserverAvailable =\n typeof IntersectionObserver === 'function'\n let observerRef: IntersectionObserver | null = null\n\n Solid.createEffect(() => {\n const r = ref()\n if (disabled() || !r || !isIntersectionObserverAvailable) {\n Solid.onCleanup(() => callback())\n return\n }\n\n observerRef = new IntersectionObserver(\n (entries) => {\n callback(entries.pop())\n },\n { rootMargin: '100px' },\n )\n\n observerRef.observe(r)\n\n Solid.onCleanup(() => {\n observerRef?.disconnect()\n callback()\n })\n })\n\n return () => observerRef\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,wBACd,KACA,UACA,UAC6C;CAC7C,MAAM,kCACJ,OAAO,yBAAyB;CAClC,IAAI,cAA2C;CAE/C,SAAM,mBAAmB;EACvB,MAAM,IAAI,IAAI;EACd,IAAI,SAAS,KAAK,CAAC,KAAK,CAAC,iCAAiC;GACxD,SAAM,gBAAgB,SAAS,CAAC;GAChC;EACF;EAEA,cAAc,IAAI,sBACf,YAAY;GACX,SAAS,QAAQ,IAAI,CAAC;EACxB,GACA,EAAE,YAAY,QAAQ,CACxB;EAEA,YAAY,QAAQ,CAAC;EAErB,SAAM,gBAAgB;GACpB,aAAa,WAAW;GACxB,SAAS;EACX,CAAC;CACH,CAAC;CAED,aAAa;AACf"}