Skip to content

Commit

Permalink
use react version
Browse files Browse the repository at this point in the history
  • Loading branch information
kyletsang committed Apr 26, 2024
1 parent ef89844 commit 96c3b3e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/useRTGTransitionProps.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from 'react';
import { cloneElement, useCallback, useRef } from 'react';
import useMergedRefs from '@restart/hooks/useMergedRefs';
import {
TransitionProps as RTGTransitionProps,
TransitionStatus,
} from 'react-transition-group/Transition';
import { getReactVersion } from './utils';

export type TransitionProps = RTGTransitionProps & {
children:
Expand Down Expand Up @@ -32,12 +34,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).props.ref || (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],
};
}
3 changes: 0 additions & 3 deletions test/ModalSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import simulant from 'simulant';

import { render } from '@testing-library/react';
import { mount } from 'enzyme';
import { shouldWarn } from './helpers';

import Modal from '../src/Modal';
import { OPEN_DATA_ATTRIBUTE } from '../src/ModalManager';
Expand Down Expand Up @@ -240,8 +239,6 @@ describe('<Modal>', () => {
});

it('should pass transition callbacks to Transition', (done) => {
// Expected since ref is only a prop in react 19+.
shouldWarn('Warning: div: `ref` is not a prop');
let count = 0;
let increment = () => count++;

Expand Down

0 comments on commit 96c3b3e

Please sign in to comment.