UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

1 lines • 1.81 kB
{"version":3,"file":"utils.cjs","names":[],"sources":["../../src/utils.ts"],"sourcesContent":["import * as Solid from 'solid-js'\n\n/**\n * Observe a Link's element while it preloads on `'viewport'`: `callback`\n * receives each intersection entry. Whenever the element or preload mode\n * changes, and on cleanup, a preloading Link gets `callback()` so it can\n * cancel a pending preload.\n */\nexport function useIntersectionObserver<T extends Element>(\n ref: Solid.Accessor<T | null>,\n callback: (entry?: IntersectionObserverEntry) => void,\n preload: Solid.Accessor<unknown>,\n): void {\n Solid.createEffect(() => {\n const r = ref()\n const mode = preload()\n if (\n mode === 'viewport' &&\n r &&\n typeof IntersectionObserver === 'function'\n ) {\n let active = true\n const observer = new IntersectionObserver(\n (entries) => {\n // Queued notifications can arrive after this effect has cleaned up.\n if (active) {\n callback(entries.pop())\n }\n },\n { rootMargin: '100px' },\n )\n observer.observe(r)\n Solid.onCleanup(() => {\n active = false\n observer.disconnect()\n callback()\n })\n } else if (mode) {\n // Intent preloading still needs timer cleanup without an observer.\n Solid.onCleanup(() => callback())\n }\n })\n}\n"],"mappings":";;;;;;;;;;AAQA,SAAgB,wBACd,KACA,UACA,SACM;CACN,SAAM,mBAAmB;EACvB,MAAM,IAAI,IAAI;EACd,MAAM,OAAO,QAAQ;EACrB,IACE,SAAS,cACT,KACA,OAAO,yBAAyB,YAChC;GACA,IAAI,SAAS;GACb,MAAM,WAAW,IAAI,sBAClB,YAAY;IAEX,IAAI,QACF,SAAS,QAAQ,IAAI,CAAC;GAE1B,GACA,EAAE,YAAY,QAAQ,CACxB;GACA,SAAS,QAAQ,CAAC;GAClB,SAAM,gBAAgB;IACpB,SAAS;IACT,SAAS,WAAW;IACpB,SAAS;GACX,CAAC;EACH,OAAO,IAAI,MAET,SAAM,gBAAgB,SAAS,CAAC;CAEpC,CAAC;AACH"}