Skip to content

Commit b5d46d3

Browse files
committed
chore: lock only when fixed
1 parent 332b2c3 commit b5d46d3

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

src/Dialog/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
5959
const contentRef = useRef<ContentRef>(null);
6060

6161
const [animatedVisible, setAnimatedVisible] = React.useState(visible);
62-
const [isFixedPos, setIsFixedPos] = React.useState(true);
62+
const [isFixedPos, setIsFixedPos] = React.useState(false);
6363

6464
// ========================== Init ==========================
6565
const ariaId = useId();
@@ -117,7 +117,7 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
117117
const contentClickRef = useRef(false);
118118
const contentTimeoutRef = useRef<ReturnType<typeof setTimeout>>(null);
119119

120-
// We need record content click incase content popup out of dialog
120+
// We need record content click in case content popup out of dialog
121121
const onContentMouseDown: React.MouseEventHandler = () => {
122122
clearTimeout(contentTimeoutRef.current);
123123
contentClickRef.current = true;
@@ -159,7 +159,7 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
159159
// Calc the position style
160160
if (wrapperRef.current) {
161161
const computedWrapStyle = getComputedStyle(wrapperRef.current);
162-
setIsFixedPos(computedWrapStyle.position === 'absolute');
162+
setIsFixedPos(computedWrapStyle.position === 'fixed');
163163
}
164164
} else if (
165165
animatedVisible &&

tests/focus.spec.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* eslint-disable react/no-render-return-value, max-classes-per-file, func-names, no-console */
2+
import React from 'react';
3+
import { act, render } from '@testing-library/react';
4+
import Dialog from '../src';
5+
6+
// Mock: import { useLockFocus } from '@rc-component/util/lib/Dom/focus';
7+
jest.mock('@rc-component/util/lib/Dom/focus', () => {
8+
const actual = jest.requireActual('@rc-component/util/lib/Dom/focus');
9+
10+
const useLockFocus = (visible: boolean, ...rest: any[]) => {
11+
globalThis.__useLockFocusVisible = visible;
12+
return actual.useLockFocus(visible, ...rest);
13+
};
14+
15+
return {
16+
...actual,
17+
useLockFocus,
18+
};
19+
});
20+
21+
/**
22+
* Since overflow scroll test need a clear env which may affect by other test.
23+
* Use a clean env instead.
24+
*/
25+
describe('Dialog.Focus', () => {
26+
beforeEach(() => {
27+
jest.useFakeTimers();
28+
});
29+
30+
afterEach(() => {
31+
jest.useRealTimers();
32+
});
33+
34+
it('Should lock when fixed', () => {
35+
render(
36+
<Dialog
37+
visible
38+
styles={{
39+
wrapper: { position: 'fixed' },
40+
}}
41+
/>,
42+
);
43+
44+
act(() => {
45+
jest.runAllTimers();
46+
});
47+
48+
expect(globalThis.__useLockFocusVisible).toBe(true);
49+
});
50+
51+
it('Should not lock when not fixed', () => {
52+
render(
53+
<Dialog
54+
visible
55+
styles={{
56+
wrapper: { position: 'absolute' },
57+
}}
58+
/>,
59+
);
60+
61+
act(() => {
62+
jest.runAllTimers();
63+
});
64+
65+
expect(globalThis.__useLockFocusVisible).toBe(false);
66+
});
67+
});

0 commit comments

Comments
 (0)