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
7 changes: 6 additions & 1 deletion src/Preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export interface InternalPreviewConfig {
zIndex?: number;
afterOpenChange?: (open: boolean) => void;

// Focus
/** Whether to trap focus within the preview when open. Default is true. */
focusTrap?: boolean;

// Operation
movable?: boolean;
icons?: OperationIcons;
Expand Down Expand Up @@ -193,6 +197,7 @@ const Preview: React.FC<PreviewProps> = props => {
styles = {},
mousePosition,
zIndex,
focusTrap = true,
} = props;

const imgRef = useRef<HTMLImageElement>();
Expand Down Expand Up @@ -397,7 +402,7 @@ const Preview: React.FC<PreviewProps> = props => {
}
}, [open]);

useLockFocus(open && portalRender, () => wrapperRef.current);
useLockFocus(focusTrap && open && portalRender, () => wrapperRef.current);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

While this change correctly disables the focus lock via useLockFocus, the Preview component still maintains other modal-like behaviors that should ideally respect the focusTrap prop for a consistent non-modal experience:

  1. Accessibility: The aria-modal="true" attribute (line 450) should be set to false (or match focusTrap) to correctly inform assistive technologies that the rest of the page remains interactive when the preview is open.
  2. Scroll Locking: The scroll lock logic (line 368) should be bypassed when focusTrap is false, allowing users to scroll the page while the preview is open (especially useful for the "inline" documentation use case mentioned in the PR).

Since these lines are not part of the current diff hunks, they couldn't be included in a code suggestion, but they are important for the completeness of this feature.


// ========================== Render ==========================
const bodyStyle: React.CSSProperties = {
Expand Down
24 changes: 24 additions & 0 deletions tests/preview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1296,4 +1296,28 @@ describe('Preview', () => {

rectSpy.mockRestore();
});

it('Focus should not be trapped when focusTrap is false', () => {
const rectSpy = jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockReturnValue({
x: 0, y: 0, width: 100, height: 100,
top: 0, right: 100, bottom: 100, left: 0,
toJSON: () => undefined,
} as DOMRect);

const { container } = render(<Image src="src" alt="no trap" preview={{ open: true, focusTrap: false }} />);

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

const preview = document.querySelector('.rc-image-preview') as HTMLElement;
expect(preview).toBeTruthy();

// Focus outside the preview should not be redirected back
const wrapper = container.querySelector('.rc-image') as HTMLElement;
wrapper.focus();
expect(document.activeElement).toBe(wrapper);

rectSpy.mockRestore();
});
});
Loading