Skip to content

Stricter Typescript #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ import * as React from "react";

import { isBrowser, isDev } from "./constants.macro";

type RequestIdleCallbackHandle = number;
type RequestIdleCallbackOptions = {
timeout: number;
};
type RequestIdleCallbackDeadline = {
readonly didTimeout: boolean;
timeRemaining: () => number;
};

declare global {
interface Window {
requestIdleCallback?: (
callback: (deadline: RequestIdleCallbackDeadline) => void,
opts?: RequestIdleCallbackOptions
) => RequestIdleCallbackHandle;
cancelIdleCallback?: (handle: RequestIdleCallbackHandle) => void;
}
}

export type LazyProps = {
ssrOnly?: boolean;
whenIdle?: boolean;
Expand Down Expand Up @@ -40,7 +59,7 @@ const useIsomorphicLayoutEffect = isBrowser
? React.useLayoutEffect
: React.useEffect;

function LazyHydrate(props: Props) {
const LazyHydrate: React.FunctionComponent<Props> = function(props) {
const childRef = React.useRef<HTMLDivElement>(null);

// Always render on server
Expand Down Expand Up @@ -74,7 +93,7 @@ function LazyHydrate(props: Props) {

useIsomorphicLayoutEffect(() => {
// No SSR Content
if (!childRef.current.hasChildNodes()) {
if (!childRef.current?.hasChildNodes()) {
setHydrated(true);
}
}, []);
Expand All @@ -84,7 +103,7 @@ function LazyHydrate(props: Props) {
const cleanupFns: VoidFunction[] = [];
function cleanup() {
while (cleanupFns.length) {
cleanupFns.pop()();
cleanupFns.pop()!();
}
}
function hydrate() {
Expand All @@ -97,13 +116,15 @@ function LazyHydrate(props: Props) {
}

if (whenIdle) {
// @ts-ignore
if (typeof requestIdleCallback !== "undefined") {
// @ts-ignore
const idleCallbackId = requestIdleCallback(hydrate, { timeout: 500 });
if (typeof window !== "undefined" && window.requestIdleCallback) {
const idleCallbackId = window.requestIdleCallback(hydrate, {
timeout: 500
});

cleanupFns.push(() => {
// @ts-ignore
cancelIdleCallback(idleCallbackId);
if (window.cancelIdleCallback) {
window.cancelIdleCallback(idleCallbackId);
}
});
} else {
const id = setTimeout(hydrate, 2000);
Expand All @@ -116,7 +137,7 @@ function LazyHydrate(props: Props) {
let events = Array.isArray(on) ? on.slice() : [on];

if (whenVisible) {
if (io && childRef.current.childElementCount) {
if (io && childRef.current?.childElementCount) {
// As root node does not have any box model, it cannot intersect.
const el = childRef.current.children[0];
io.observe(el);
Expand All @@ -131,13 +152,15 @@ function LazyHydrate(props: Props) {
}

events.forEach(event => {
childRef.current.addEventListener(event, hydrate, {
childRef.current?.addEventListener(event, hydrate, {
once: true,
capture: true,
passive: true
});
cleanupFns.push(() => {
childRef.current.removeEventListener(event, hydrate, { capture: true });
childRef.current?.removeEventListener(event, hydrate, {
capture: true
});
});
});

Expand All @@ -146,7 +169,7 @@ function LazyHydrate(props: Props) {

if (hydrated) {
if (noWrapper) {
return children;
return <>{children}</>;
}
return (
<div ref={childRef} style={{ display: "contents" }} {...rest}>
Expand All @@ -164,6 +187,6 @@ function LazyHydrate(props: Props) {
/>
);
}
}
};

export default LazyHydrate;
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"declaration": true,
"declarationDir": "./types",
"emitDeclarationOnly": true,
"jsx": "preserve"
"jsx": "preserve",
"strict": true
}
}