Skip to content

Commit

Permalink
fix(drawer): fix drawer push for mode (#2614)
Browse files Browse the repository at this point in the history
* fix(drawer): fix drawer push for mode

* perf(drawer): optimization code performance
  • Loading branch information
HaixingOoO authored Nov 27, 2023
1 parent 2c033c4 commit 6b96937
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 302 deletions.
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

0 comments on commit 6b96937

Please sign in to comment.