Skip to content

Commit

Permalink
fix: drag event lose when drag over iframe
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoooooo committed May 28, 2024
1 parent d2e3f0b commit 69d9aeb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
43 changes: 43 additions & 0 deletions apps/storybook/src/ui/drag-panel.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import { DragPanel } from '@music163/tango-ui';
import { Box, Text } from 'coral-system';
import { Button } from 'antd';

export default {
title: 'UI/DragPanel',
};

export function Basic() {
return (
<DragPanel
title="弹层标题"
extra="右上角内容"
width={350}
body={<Box p="m">内容</Box>}
footer={<Text>底部信息</Text>}
>
<Button>点击唤起浮层</Button>
</DragPanel>
);
}

export function FooterCustom() {
return (
<DragPanel
title="弹层标题"
extra="右上角内容"
width={350}
body={<Box p="m">内容</Box>}
footer={(close) => (
<Box display="flex" justifyContent="space-between">
<Text>底部信息</Text>
<Button size="small" onClick={() => close()}>
点此关闭
</Button>
</Box>
)}
>
<Button>点击唤起浮层</Button>
</DragPanel>
);
}
27 changes: 25 additions & 2 deletions packages/ui/src/drag-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ import Draggable from 'react-draggable';
import { CloseOutlined } from '@ant-design/icons';
import { noop } from '@music163/tango-helpers';

// Dragging over an iframe stops dragging when moving the mouse too fast #613
// https://github.com/react-grid-layout/react-draggable/issues/613
const injectStyleToBody = () => {
const id = 'react-draggable-transparent-selection';
if (document.getElementById(id)) {
return;
}
const style = document.createElement('style');
style.id = id;
style.innerHTML = `
/* Prevent iframes from stealing drag events */
.react-draggable-transparent-selection iframe {
pointer-events: none;
}
`;
document.head.appendChild(style);
};

const CloseIcon = styled(CloseOutlined)`
cursor: pointer;
margin-left: 10px;
Expand All @@ -17,7 +35,7 @@ const CloseIcon = styled(CloseOutlined)`
}
`;

interface DragPanelProps extends Omit<PopoverProps, 'overlay'> {
interface DragPanelProps extends Omit<PopoverProps, 'overlay' | 'open'> {
// 标题
title?: React.ReactNode | string;
// 内容
Expand Down Expand Up @@ -59,7 +77,12 @@ export function DragPanel({
onOpenChange(innerOpen);
}}
overlay={
<Draggable handle=".selection-drag-bar">
<Draggable
handle=".selection-drag-bar"
onStart={() => {
injectStyleToBody();
}}
>
<Box
bg="#FFF"
borderRadius="m"
Expand Down

0 comments on commit 69d9aeb

Please sign in to comment.