Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

visible.tsx

139 lines3.0 KB

1'use client'
2​
3import * as React from 'react'
4​
5import { reactUse } from '@tanstack/react-router'
6import { isServer } from '@tanstack/router-core/isServer'
7import type {
8 HydrationPrefetchStrategy,
9 VisibleHydrationOptions,
10} from '@tanstack/start-client-core/hydration'
11import type {
12 HydrateProps,
13 InternalHydrateProps,
14 ReactHydrationStrategy,
15} from '../Hydrate'
16​
17type VisibleGate = {
18 p: Promise<void>
19 r: boolean
20 s: () => void
22​
23/* @__NO_SIDE_EFFECTS__ */
24function HydrationBoundary(props: {
25 g: VisibleGate
26 o?: () => void
27 children?: React.ReactNode
28}) {
29 const { g, o } = props
30​
31 if (!g.r) {
32 if (!reactUse) {
33 throw g.p
34 }
35​
36 reactUse(g.p)
37 }
38​
39 React.useEffect(() => {
40 o?.()
41 }, [o])
42​
43 return props.children as React.JSX.Element
45​
46/* @__NO_SIDE_EFFECTS__ */
47export function VisibleHydrate(
48 this: ReactHydrationStrategy,
49 props: HydrateProps,
50): React.JSX.Element {
51 const strategy = this as ReactHydrationStrategy<'visible', true>
52 const prefetchStrategy = props.prefetch
53 const preload = (props as InternalHydrateProps).p
54 const markerRef = React.useRef<HTMLDivElement | null>(null)
55 const [gate] = React.useState<VisibleGate>(() => {
56 let resolvePromise!: () => void
57 const nextGate: VisibleGate = {
58 p: new Promise<void>((resolve) => {
59 resolvePromise = resolve
60 }),
61 r: false,
62 s: () => {
63 nextGate.r = true
64 resolvePromise()
65 },
66 }
67 if (
68 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
69 isServer ??
70 typeof window === 'undefined'
71 ) {
72 nextGate.s()
73 }
74​
75 return nextGate
76 })
77​
78 React.useEffect(() => {
79 if (!preload || typeof prefetchStrategy === 'function') {
80 return
81 }
82​
83 return prefetchStrategy?._s?.({
84 element: markerRef.current,
85 prefetch: preload,
86 })
87 }, [prefetchStrategy, preload])
88​
89 React.useEffect(() => {
90 if (gate.r) return
91​
92 return strategy._s?.({
93 element: markerRef.current,
94 gate: gate as never,
95 })
96 }, [gate, strategy])
97​
98 return (
99 <div ref={markerRef}>
100 <React.Suspense fallback={props.fallback}>
101 <HydrationBoundary g={gate} o={props.onHydrated}>
102 {props.children}
103 </HydrationBoundary>
104 </React.Suspense>
105 </div>
106 )
108​
109/* @__NO_SIDE_EFFECTS__ */
110export function visible(
111 options?: VisibleHydrationOptions,
112): ReactHydrationStrategy<'visible', true> &
113 HydrationPrefetchStrategy<'visible'> {
114 const rootMargin = options?.rootMargin ?? '600px'
115 const threshold = options?.threshold ?? 0
116​
117 return {
118 _s: ({ element, gate, prefetch }) => {
119 const callback = prefetch || (gate as never as VisibleGate).s
120​
121 if (!element) {
122 callback()
123 return
124 }
125​
126 const observer = new IntersectionObserver(
127 (entries) => {
128 if (!entries[0]!.isIntersecting) return
129 observer.disconnect()
130 callback()
131 },
132 { rootMargin, threshold },
133 )
134 observer.observe(element)
135 return () => observer.disconnect()
136 },
137 _h: VisibleHydrate,
138 }