Skip to content

Commit

Permalink
fix: react 19 compatibility (#1916)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS authored Oct 25, 2024
1 parent b6516f4 commit 29f2558
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@
"typescript": "^5.3.3"
},
"peerDependencies": {
"react": "^16.0.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.0.0 || ^17.0.0 || ^18.0.0"
"react": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0",
"react-dom": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
},
"nano-staged": {
"*.{scss}": [
Expand Down
3 changes: 2 additions & 1 deletion src/components/ActionTooltip/ActionTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {Popup} from '../Popup';
import type {PopupPlacement} from '../Popup';
import type {DOMProps, QAProps} from '../types';
import {block} from '../utils/cn';
import {getElementRef} from '../utils/getElementRef';

import './ActionTooltip.scss';

Expand Down Expand Up @@ -77,7 +78,7 @@ export function ActionTooltip(props: ActionTooltipProps) {
};

const child = React.Children.only(children);
const childRef = (child as any).ref;
const childRef = getElementRef(child);

const ref = useForkRef(setAnchorElement, childRef);

Expand Down
3 changes: 2 additions & 1 deletion src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {PopupPlacement} from '../Popup';
import {Text} from '../Text';
import type {DOMProps, QAProps} from '../types';
import {block} from '../utils/cn';
import {getElementRef} from '../utils/getElementRef';

import './Tooltip.scss';

Expand Down Expand Up @@ -75,7 +76,7 @@ export const Tooltip = (props: TooltipProps) => {
};

const child = React.Children.only(children);
const childRef = (child as any).ref;
const childRef = getElementRef(child);

const ref = useForkRef(setAnchorElement, childRef);

Expand Down
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ export * from './utils/layer-manager';
export {Lang, configure, getConfig} from './utils/configure';
export * from './utils/xpath';
export {getUniqId} from './utils/common';
export {getElementRef} from './utils/getElementRef';
4 changes: 3 additions & 1 deletion src/components/utils/FocusTrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {FocusTrap as FocusTrapInstance} from 'focus-trap';

import {useForkRef, useUniqId} from '../../hooks';

import {getElementRef} from './getElementRef';

interface FocusTrapContext {
addNode: (id: string, node: HTMLElement) => void;
removeNode: (id: string) => void;
Expand Down Expand Up @@ -90,7 +92,7 @@ export function FocusTrap({
if (!React.isValidElement<any>(child)) {
throw new Error('Children must contain only one valid element');
}
const childRef = (child as any).ref;
const childRef = getElementRef(child);

const ref = useForkRef(handleNodeRef, childRef);

Expand Down
11 changes: 11 additions & 0 deletions src/components/utils/getElementRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Access the element's ref using a method that doesn't produce a warning.
export function getElementRef(element: React.ReactElement) {
if (process.env.NODE_ENV !== 'production') {
// Before React 19, there is an access check for ReactElement's props.ref in dev builds.
const getter = Object.getOwnPropertyDescriptor(element.props, 'ref')?.get;
if (getter && 'isReactWarning' in getter && getter.isReactWarning) {
return (element as any).ref;
}
}
return element.props.ref ?? (element as any).ref;
}

0 comments on commit 29f2558

Please sign in to comment.