Skip to content

Commit

Permalink
fix(useRTGTransitionProps): fix ref warning for react 18.3+ (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyletsang authored Apr 30, 2024
1 parent f17bc10 commit ba3d534
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/useRTGTransitionProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
TransitionProps as RTGTransitionProps,
TransitionStatus,
} from 'react-transition-group/Transition';
import { getReactVersion } from './utils';

export type TransitionProps = RTGTransitionProps & {
children:
Expand Down Expand Up @@ -32,10 +33,14 @@ export default function useRTGTransitionProps({
children,
...props
}: TransitionProps) {
const { major } = getReactVersion();
const childRef =
major >= 19 ? (children as any).props.ref : (children as any).ref;

const nodeRef = useRef<HTMLElement>(null);
const mergedRef = useMergedRefs(
nodeRef,
typeof children === 'function' ? null : (children as any).ref,
typeof children === 'function' ? null : childRef,
);

const normalize =
Expand Down
12 changes: 11 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
/* eslint-disable import/prefer-default-export */
import * as React from 'react';

export function isEscKey(e: KeyboardEvent) {
return e.code === 'Escape' || e.keyCode === 27;
}

export function getReactVersion() {
const parts = React.version.split('.');
return {
major: +parts[0],
minor: +parts[1],
patch: +parts[2],
};
}

0 comments on commit ba3d534

Please sign in to comment.