|
| 1 | +import * as React from "react"; |
| 2 | + |
| 3 | +import { isBrowser, isDev } from "./constants.macro"; |
| 4 | + |
| 5 | +// React currently throws a warning when using useLayoutEffect on the server. |
| 6 | +const useIsomorphicLayoutEffect = isBrowser |
| 7 | + ? React.useLayoutEffect |
| 8 | + : React.useEffect; |
| 9 | + |
| 10 | +function useHydrationState(): [ |
| 11 | + React.MutableRefObject<HTMLDivElement>, |
| 12 | + boolean, |
| 13 | + VoidFunction |
| 14 | +] { |
| 15 | + const childRef = React.useRef<HTMLDivElement>(null); |
| 16 | + |
| 17 | + const [hydrated, setHydrated] = React.useState(!isBrowser); |
| 18 | + |
| 19 | + useIsomorphicLayoutEffect(() => { |
| 20 | + // No SSR Content |
| 21 | + if (!childRef.current.hasChildNodes()) { |
| 22 | + setHydrated(true); |
| 23 | + } |
| 24 | + }, []); |
| 25 | + |
| 26 | + const hydrate = React.useCallback(() => { |
| 27 | + setHydrated(true); |
| 28 | + }, []); |
| 29 | + |
| 30 | + return React.useMemo(() => [childRef, hydrated, hydrate], [ |
| 31 | + hydrated, |
| 32 | + hydrate |
| 33 | + ]); |
| 34 | +} |
| 35 | + |
| 36 | +const defaultStyle: React.CSSProperties = { display: "contents" }; |
| 37 | + |
| 38 | +function warnAboutDeprecation({ on, whenIdle, whenVisible, ssrOnly }) { |
| 39 | + if (isDev) { |
| 40 | + console.warn( |
| 41 | + "[%creact-lazy-hydration%c]: Default export is deprecated", |
| 42 | + "font-weight:bold", |
| 43 | + "" |
| 44 | + ); |
| 45 | + if (on != null) { |
| 46 | + console.warn( |
| 47 | + `To hydrate on events, use the new HydrateOn component |
| 48 | + %cimport { HydrateOn } from "react-lazy-hydration"; |
| 49 | +
|
| 50 | + <HydrateOn on={${JSON.stringify(on)}}> |
| 51 | + {children} |
| 52 | + </HydrateOn> |
| 53 | + `, |
| 54 | + "color:red" |
| 55 | + ); |
| 56 | + } |
| 57 | + if (whenIdle != null) { |
| 58 | + console.warn( |
| 59 | + `To hydrate on idle, use the new HydrateOnIdle component |
| 60 | + %cimport { HydrateOnIdle } from "react-lazy-hydration"; |
| 61 | +
|
| 62 | + <HydrateOnIdle> |
| 63 | + {children} |
| 64 | + </HydrateOnIdle> |
| 65 | + `, |
| 66 | + "color:red" |
| 67 | + ); |
| 68 | + } |
| 69 | + if (whenVisible != null) { |
| 70 | + console.warn( |
| 71 | + `To hydrate when component becomes visible, use the new HydrateWhenVisible component |
| 72 | + %cimport { HydrateWhenVisible } from "react-lazy-hydration"; |
| 73 | +
|
| 74 | + <HydrateWhenVisible> |
| 75 | + {children} |
| 76 | + </HydrateWhenVisible> |
| 77 | + `, |
| 78 | + "color:red" |
| 79 | + ); |
| 80 | + } |
| 81 | + if (ssrOnly != null) { |
| 82 | + console.warn( |
| 83 | + `To skip client side hydration, use the new SsrOnly component |
| 84 | + %cimport { SsrOnly } from "react-lazy-hydration"; |
| 85 | +
|
| 86 | + <SsrOnly> |
| 87 | + {children} |
| 88 | + </SsrOnly> |
| 89 | + `, |
| 90 | + "color:red" |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export { useHydrationState, defaultStyle, warnAboutDeprecation }; |
0 commit comments