Skip to content

Commit

Permalink
fix: memoize focusOptions to prevent react-focus-lock Trap resetting …
Browse files Browse the repository at this point in the history
…focus
  • Loading branch information
tkajtoch committed Jul 5, 2023
1 parent bba4e07 commit ae360d7
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/UI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,33 @@ export const FocusOn = React.forwardRef<HTMLElement, ReactFocusOnSideProps>(
...rest
} = props;

// Don't let focusOptions object change reference on each render.
// It causes react-clientside-effect to call handleStateChangeOnClient
// and change focus to first focusable element inside the trap.
const focusOptions = React.useMemo(
() => (preventScrollOnFocus ? { preventScroll: true } : undefined),
[preventScrollOnFocus]
);

const SideCar: SideCarComponent<EffectProps> = sideCar;

const { onActivation, onDeactivation, ...restProps } = lockProps;

const appliedLockProps = {
...restProps,
...restProps,

as,
style,
as,
style,

sideCar,
sideCar,

shards,
shards,

allowPinchZoom,
gapMode,
inert,
allowPinchZoom,
gapMode,
inert,

enabled: enabled && scrollLock,
enabled: enabled && scrollLock
} as const;

return (
Expand All @@ -68,7 +76,7 @@ export const FocusOn = React.forwardRef<HTMLElement, ReactFocusOnSideProps>(
className={className}
whiteList={shouldIgnore}
lockProps={appliedLockProps}
focusOptions={preventScrollOnFocus ? { preventScroll: true } : undefined}
focusOptions={focusOptions}
as={RemoveScroll}
>
{children}
Expand Down

0 comments on commit ae360d7

Please sign in to comment.