Skip to content

Commit

Permalink
Fix #7403: FocusTrap in React19 must not access element.ref
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Jan 1, 2025
1 parent c4ea455 commit b1525a8
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions components/lib/focustrap/FocusTrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ export const FocusTrap = React.memo(
const createHiddenFocusableElements = () => {
const { tabIndex = 0 } = props || {};

const createFocusableElement = (onFocus, section) => {
const createFocusableElement = (inRef, onFocus, section) => {
return (
<span
ref={section === 'firstfocusableelement' ? firstFocusableElementRef : lastFocusableElementRef}
ref={inRef}
className={'p-hidden-accessible p-hidden-focusable'}
tabIndex={tabIndex}
role={'presentation'}
Expand All @@ -104,12 +104,12 @@ export const FocusTrap = React.memo(
);
};

const firstFocusableElement = createFocusableElement(onFirstHiddenElementFocus, 'firstfocusableelement');
const lastFocusableElement = createFocusableElement(onLastHiddenElementFocus, 'lastfocusableelement');
const firstFocusableElement = createFocusableElement(firstFocusableElementRef, onFirstHiddenElementFocus, 'firstfocusableelement');
const lastFocusableElement = createFocusableElement(lastFocusableElementRef, onLastHiddenElementFocus, 'lastfocusableelement');

if (firstFocusableElement.ref.current && lastFocusableElement.ref.current) {
firstFocusableElement.ref.current.$_pfocustrap_lasthiddenfocusableelement = lastFocusableElement.ref.current;
lastFocusableElement.ref.current.$_pfocustrap_firsthiddenfocusableelement = firstFocusableElement.ref.current;
if (firstFocusableElementRef.current && lastFocusableElementRef.current) {
firstFocusableElementRef.current.$_pfocustrap_lasthiddenfocusableelement = lastFocusableElementRef.current;
lastFocusableElementRef.current.$_pfocustrap_firsthiddenfocusableelement = firstFocusableElementRef.current;
}

return (
Expand Down

0 comments on commit b1525a8

Please sign in to comment.