Skip to content

Commit 3ebf65c

Browse files
EmilyyyLiu刘欢claude
authored
feat: add scrollLock prop to control body scroll lock (#564)
* feat: add scrollLock prop to control body scroll lock - Add scrollLock prop (default: true) to control whether to lock body scroll when dialog opens - Add test cases for scrollLock functionality - Update README.md with new prop documentation Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix: remove unnecessary props from Dialog component in tests --------- Co-authored-by: 刘欢 <lh01217311@antgroup.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 9105b08 commit 3ebf65c

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ ReactDOM.render(
8383
| closeIcon | ReactNode | | specific the close icon. | |
8484
| forceRender | Boolean | false | Create dialog dom node before dialog first show | |
8585
| focusTriggerAfterClose | Boolean | true | focus trigger element when dialog closed | |
86+
| scrollLock | Boolean | true | Control whether to lock body scroll when dialog opens | |
8687
| modalRender | (node: ReactNode) => ReactNode | | Custom modal content render | 8.3.0 |
8788

8889
## Development

src/DialogWrap.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props) => {
2323
closable,
2424
panelRef,
2525
keyboard = true,
26+
scrollLock = true,
2627
onClose,
2728
} = props;
29+
const { scrollLock: _, ...restProps } = props;
2830
const [animatedVisible, setAnimatedVisible] = React.useState<boolean>(visible);
2931

3032
const refContext = React.useMemo(() => ({ panel: panelRef }), [panelRef]);
@@ -55,10 +57,10 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props) => {
5557
onEsc={onEsc}
5658
autoDestroy={false}
5759
getContainer={getContainer}
58-
autoLock={visible || animatedVisible}
60+
autoLock={scrollLock && (visible || animatedVisible)}
5961
>
6062
<Dialog
61-
{...props}
63+
{...restProps}
6264
destroyOnHidden={destroyOnHidden}
6365
afterClose={() => {
6466
const closableObj = closable && typeof closable === 'object' ? closable : {};

src/IDialogPropTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export type IDialogPropTypes = {
5959
// https://github.com/react-component/dialog/issues/95
6060
focusTriggerAfterClose?: boolean;
6161
focusTrap?: boolean;
62+
/** Control whether to lock body scroll when modal opens. Default is true. */
63+
scrollLock?: boolean;
6264

6365
// Refs
6466
panelRef?: React.Ref<HTMLDivElement>;

tests/scroll.spec.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,23 @@ describe('Dialog.Scroll', () => {
7878

7979
unmount();
8080
});
81+
82+
it('should not lock body scroll when scrollLock is false', () => {
83+
const { unmount } = render(<Dialog visible scrollLock={false} />);
84+
85+
// body should not have overflow:hidden when scrollLock is false
86+
expect(document.body).not.toHaveStyle({
87+
overflowY: 'hidden',
88+
});
89+
90+
unmount();
91+
});
92+
93+
it('should lock body scroll when scrollLock is true (default)', () => {
94+
const { unmount } = render(<Dialog visible scrollLock={true} />);
95+
expect(document.body).toHaveStyle({
96+
overflowY: 'hidden',
97+
});
98+
unmount();
99+
});
81100
});

0 commit comments

Comments
 (0)