Skip to content

Commit

Permalink
chore: Add support for React 19 (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
roderickhsiao authored Dec 26, 2024
1 parent 178fb6d commit 14f15ef
Show file tree
Hide file tree
Showing 6 changed files with 1,510 additions and 2,010 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ import React, { useRef } from 'react';
import { useInViewport } from 'react-in-viewport';

const MySectionBlock = () => {
const myRef = useRef();
const myRef = useRef(null);
const {
inViewport,
enterCount,
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-in-viewport",
"version": "1.0.0-beta.7",
"version": "1.0.0-beta.8",
"description": "Track React component in viewport using Intersection Observer API",
"author": "Roderick Hsiao <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -45,7 +45,7 @@
"@testing-library/react": "^14.0.0",
"@types/hoist-non-react-statics": "^3.3.1",
"@types/jest": "^29.0.0",
"@types/react-test-renderer": "^18.0.0",
"@types/react-test-renderer": "^19.0.0",
"@typescript-eslint/eslint-plugin": "^5.17.0",
"@typescript-eslint/parser": "^5.17.0",
"add": "^2.0.6",
Expand All @@ -67,10 +67,10 @@
"jest-cli": "^29.0.0",
"jest-environment-jsdom": "^29.3.1",
"jest-junit": "^15.0.0",
"react": "^18.0.0",
"react": "^19.0.0",
"react-aspect-ratio": "^1.1.8",
"react-dom": "^18.0.0",
"react-test-renderer": "^18.0.0",
"react-dom": "^19.0.0",
"react-test-renderer": "^19.0.0",
"storybook": "^8.3.2",
"typescript": "^5.0.0",
"webpack": "^5.75.0"
Expand Down
14 changes: 8 additions & 6 deletions src/lib/handleViewport.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useRef, forwardRef } from 'react';
import type { ComponentType, PropsWithoutRef } from 'react';
import hoistNonReactStatic from 'hoist-non-react-statics';

import type {
Expand All @@ -15,15 +16,15 @@ function handleViewport<
TElement extends HTMLElement,
TProps extends InjectedViewportProps<TElement>,
>(
TargetComponent: React.ComponentType<TProps>,
TargetComponent: ComponentType<PropsWithoutRef<TProps>>,
options: Options = defaultOptions,
config: Config = defaultConfig,
config: Config = defaultConfig

Check failure on line 21 in src/lib/handleViewport.tsx

View workflow job for this annotation

GitHub Actions / build_test (18.x)

Missing trailing comma

Check failure on line 21 in src/lib/handleViewport.tsx

View workflow job for this annotation

GitHub Actions / build_test (20.x)

Missing trailing comma
) {
const ForwardedRefComponent = forwardRef<TElement, TProps>((props, ref) => {
const refProps = {
forwardedRef: ref,
// pass both ref/forwardedRef for class component for backward compatibility
};
} as const;
return <TargetComponent {...props} {...refProps} />;
});

Expand Down Expand Up @@ -53,9 +54,10 @@ function handleViewport<
return <ForwardedRefComponent {...props} ref={node} />;
};

const name = (TargetComponent as React.FC).displayName
|| (TargetComponent as React.FC).name
|| 'Component';
const name =

Check failure on line 57 in src/lib/handleViewport.tsx

View workflow job for this annotation

GitHub Actions / build_test (18.x)

There should be no line break before or after '='

Check failure on line 57 in src/lib/handleViewport.tsx

View workflow job for this annotation

GitHub Actions / build_test (20.x)

There should be no line break before or after '='
(TargetComponent as React.FC).displayName ||

Check failure on line 58 in src/lib/handleViewport.tsx

View workflow job for this annotation

GitHub Actions / build_test (18.x)

'||' should be placed at the beginning of the line

Check failure on line 58 in src/lib/handleViewport.tsx

View workflow job for this annotation

GitHub Actions / build_test (20.x)

'||' should be placed at the beginning of the line
(TargetComponent as React.FC).name ||

Check failure on line 59 in src/lib/handleViewport.tsx

View workflow job for this annotation

GitHub Actions / build_test (18.x)

'||' should be placed at the beginning of the line

Check failure on line 59 in src/lib/handleViewport.tsx

View workflow job for this annotation

GitHub Actions / build_test (20.x)

'||' should be placed at the beginning of the line
'Component';
InViewport.displayName = `handleViewport(${name})`;

return hoistNonReactStatic(InViewport, ForwardedRefComponent);
Expand Down
8 changes: 5 additions & 3 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { RefObject } from 'react';

export type Config = {
disconnectOnLeave?: boolean;
};
Expand All @@ -6,12 +8,12 @@ export type InjectedViewportProps<TElement extends HTMLElement = HTMLElement> =
inViewport: boolean;
enterCount: number;
leaveCount: number;
forwardedRef: React.RefObject<TElement>;
readonly forwardedRef: RefObject<TElement>;
};

export type CallbackProps = {
onEnterViewport?: () => void;
onLeaveViewport?: () => void;
onEnterViewport?: VoidFunction
onLeaveViewport?: VoidFunction;
};

export type Options = IntersectionObserverInit;
7 changes: 4 additions & 3 deletions src/lib/useInViewport.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, {
import {
type RefObject,
useEffect,
useRef,
useState,
Expand All @@ -15,15 +16,15 @@ const defaultMutationObserverOption = {
};

const useInViewport = (
target: React.RefObject<HTMLElement>,
target: RefObject<HTMLElement | null>,
options: Options = defaultOptions,
config: Config = defaultConfig,
props: CallbackProps = defaultProps,
) => {
const { onEnterViewport, onLeaveViewport } = props;
const [, forceUpdate] = useState<boolean>();

const observer = useRef<IntersectionObserver>();
const observer = useRef<IntersectionObserver>(undefined);

const inViewportRef = useRef<boolean>(false);
const intersected = useRef<boolean>(false);
Expand Down
Loading

0 comments on commit 14f15ef

Please sign in to comment.