Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Dialog/Content/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ const Panel = React.forwardRef<PanelRef, PanelProps>((props, ref) => {
classNames: modalClassNames,
styles: modalStyles,
isFixedPos,
focusTrap,
} = props;

// ================================= Refs =================================
const { panel: panelRef } = React.useContext(RefContext);
const internalRef = useRef<HTMLDivElement>(null);
const mergedRef = useComposeRef(holderRef, panelRef, internalRef);

useLockFocus(visible && isFixedPos, () => internalRef.current);
useLockFocus(visible && isFixedPos && focusTrap !== false, () => internalRef.current);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and to use a more modern JavaScript/TypeScript feature, you could use the nullish coalescing operator (??). The expression focusTrap ?? true is equivalent to focusTrap !== false for boolean and undefined values, and it more clearly conveys the intent of defaulting to true when focusTrap is not provided. Note that the parentheses are important due to operator precedence.

Suggested change
useLockFocus(visible && isFixedPos && focusTrap !== false, () => internalRef.current);
useLockFocus(visible && isFixedPos && (focusTrap ?? true), () => internalRef.current);


React.useImperativeHandle(ref, () => ({
focus: () => {
Expand Down
1 change: 1 addition & 0 deletions src/IDialogPropTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type IDialogPropTypes = {
// https://github.com/ant-design/ant-design/issues/19771
// https://github.com/react-component/dialog/issues/95
focusTriggerAfterClose?: boolean;
focusTrap?: boolean;

// Refs
panelRef?: React.Ref<HTMLDivElement>;
Expand Down
18 changes: 18 additions & 0 deletions tests/focus.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,22 @@ describe('Dialog.Focus', () => {

expect(globalThis.__useLockFocusVisible).toBe(false);
});

it('should not lock focus when focusTrap is false', () => {
render(
<Dialog
visible
focusTrap={false}
styles={{
wrapper: { position: 'fixed' },
}}
/>,
);

act(() => {
jest.runAllTimers();
});

expect(globalThis.__useLockFocusVisible).toBe(false);
});
});
Loading