Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(drawer): fix drawer push for mode #2614

Merged
merged 2 commits into from
Nov 27, 2023
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
2 changes: 1 addition & 1 deletion src/drawer/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const Drawer = forwardRef<HTMLDivElement, DrawerProps>((originalProps, ref) => {
return dragSizeValue || sizeMap[size] || size;
}, [dragSizeValue, size]);

useLockStyle({ ...props, sizeValue });
useLockStyle({ ...props, sizeValue, drawerWrapper: drawerWrapperRef.current });
useImperativeHandle(ref, () => containerRef.current);

useEffect(() => {
Expand Down
9 changes: 6 additions & 3 deletions src/drawer/__tests__/drawer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ describe('test Drawer', () => {
test('Drawer mode push', () => {
const { getByText } = render(<DrawerDemo attach="body" mode="push" />);
fireEvent.click(getByText('Open'));
expect(document.body).toHaveStyle({
margin: '0 0 0 -300px',
});

setTimeout(() => {
expect(document.body).toHaveStyle({
margin: '0 0 0 -300px',
});
}, 1000);
});
test('Drawer onCancel', () => {
const onCancelFn = vi.fn();
Expand Down
72 changes: 37 additions & 35 deletions src/drawer/_example/attach-parent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,46 @@ export default function () {
borderRadius: '2px',
}}
>
<Space direction="vertical">
<p>渲染在当前元素中。</p>
<div>
<span>抽屉弹出方向:</span>
<Radio.Group value={placement} onChange={(value) => setPlacement(value)}>
<Radio.Button value="left">左侧</Radio.Button>
<Radio.Button value="right">右侧</Radio.Button>
<Radio.Button value="top">上方</Radio.Button>
<Radio.Button value="bottom">下方</Radio.Button>
</Radio.Group>
</div>
<div id="demo-suf-container">
<Space direction="vertical">
<p>渲染在当前元素中。</p>
<div>
<span>抽屉弹出方向:</span>
<Radio.Group value={placement} onChange={(value) => setPlacement(value)}>
<Radio.Button value="left">左侧</Radio.Button>
<Radio.Button value="right">右侧</Radio.Button>
<Radio.Button value="top">上方</Radio.Button>
<Radio.Button value="bottom">下方</Radio.Button>
</Radio.Group>
</div>

<div>
<span>抽屉弹出模式:</span>
<Radio.Group value={mode} onChange={(value) => setMode(value)}>
<Radio.Button value="push">push</Radio.Button>
<Radio.Button value="overlay">overlay</Radio.Button>
</Radio.Group>
</div>
<div>
<span>抽屉弹出模式:</span>
<Radio.Group value={mode} onChange={(value) => setMode(value)}>
<Radio.Button value="push">push</Radio.Button>
<Radio.Button value="overlay">overlay</Radio.Button>
</Radio.Group>
</div>

<div>
<Button theme="primary" onClick={handleClick}>
Open
</Button>
</div>
<div>
<Button theme="primary" onClick={handleClick}>
Open
</Button>
</div>

<Drawer
showInAttachedElement
placement={placement}
header="Drawer"
visible={visible}
onClose={handleClose}
mode={mode}
attach="#demo-container"
>
<p>This is a drawer</p>
</Drawer>
</Space>
<Drawer
showInAttachedElement
placement={placement}
header="Drawer"
visible={visible}
onClose={handleClose}
mode={mode}
attach="#demo-suf-container"
>
<p>This is a drawer</p>
</Drawer>
</Space>
</div>
</div>
);
}
20 changes: 17 additions & 3 deletions src/drawer/hooks/useLockStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getScrollbarWidth } from '../../_common/js/utils/getScrollbarWidth';
let key = 1;

export default function useLockStyle(props) {
const { preventScrollThrough, mode, visible, showInAttachedElement, placement, sizeValue } = props;
const { preventScrollThrough, mode, visible, showInAttachedElement, placement, sizeValue, drawerWrapper } = props;
const lockStyleRef = useRef<HTMLStyleElement>(null);
const timerRef = useRef(null);

Expand All @@ -32,17 +32,31 @@ export default function useLockStyle(props) {
if (!lockStyleRef.current) {
lockStyleRef.current = document.createElement('style');
}

const hasScrollBar = document.documentElement.scrollHeight > document.documentElement.clientHeight;
const scrollbarWidth = hasScrollBar ? getScrollbarWidth() : 0;
lockStyleRef.current.dataset.id = `td_drawer_${+new Date()}_${(key += 1)}`;
lockStyleRef.current.innerHTML = `
html body {
overflow-y: hidden;
transition: margin 300ms cubic-bezier(0.7, 0.3, 0.1, 1) 0s;
${mode === 'push' ? marginString : `width: calc(100% - ${scrollbarWidth}px);`}
${mode === 'push' ? '' : `width: calc(100% - ${scrollbarWidth}px);`}
}
`;
}, [mode, marginString]);
}, [mode]);

useLayoutEffect(() => {
if (drawerWrapper && mode === 'push') {
if (visible) {
drawerWrapper.parentNode.style.cssText += `
transition: margin 300ms cubic-bezier(0.7, 0.3, 0.1, 1) 0s;
${marginString};}
`;
} else {
drawerWrapper.parentNode.style.cssText = drawerWrapper.parentNode.style.cssText.replace(/margin:.+;/, '');
}
}
}, [mode, marginString, drawerWrapper, visible]);

useLayoutEffect(() => {
if (typeof document === 'undefined') return;
Expand Down
Loading
Loading