Skip to content

Commit f6466e4

Browse files
authored
create an overlay when dragging (codesandbox#3846)
1 parent 2021fb3 commit f6466e4

File tree

1 file changed

+99
-64
lines changed
  • packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Comments/Dialog

1 file changed

+99
-64
lines changed

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Comments/Dialog/index.tsx

Lines changed: 99 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -82,89 +82,104 @@ export const Dialog: React.FC = () => {
8282
setRepliesRendered(false);
8383
}, [comment.id]);
8484

85+
const [dragging, setDragging] = React.useState(false);
86+
8587
const onDragHandlerPan = (deltaX: number, deltaY: number) => {
8688
controller.set((_, target) => ({
8789
x: Number(target.x) + deltaX,
8890
y: Number(target.y) + deltaY,
8991
}));
92+
setDragging(true);
93+
};
94+
95+
const onDragHandlerPanEnd = () => {
96+
setDragging(false);
9097
};
9198

9299
if (!currentCommentPositions) {
93100
return null;
94101
}
95102
return (
96-
<motion.div
97-
key={isCodeComment ? 'code' : 'global'}
98-
initial={{ ...initialPosition, scale: 0.5, opacity: 0 }}
99-
animate={controller}
100-
transition={{ duration: DIALOG_TRANSITION_DURATION }}
101-
style={{ position: 'absolute', zIndex: 2 }}
102-
>
103-
<Stack
104-
ref={dialogRef}
105-
direction="vertical"
106-
css={css({
107-
position: 'absolute',
108-
zIndex: 2,
109-
backgroundColor: 'dialog.background',
110-
color: 'dialog.foreground',
111-
border: '1px solid',
112-
borderColor: 'dialog.border',
113-
borderRadius: 4,
114-
width: DIALOG_WIDTH,
115-
height: 'auto',
116-
maxHeight: '80vh',
117-
fontFamily: 'Inter, sans-serif',
118-
overflow: 'hidden',
119-
boxShadow: 2,
120-
})}
103+
<>
104+
{dragging && <Overlay />}
105+
<motion.div
106+
key={isCodeComment ? 'code' : 'global'}
107+
initial={{ ...initialPosition, scale: 0.5, opacity: 0 }}
108+
animate={controller}
109+
transition={{ duration: DIALOG_TRANSITION_DURATION }}
110+
style={{ position: 'absolute', zIndex: 2 }}
121111
>
122-
{isNewComment && editing ? (
123-
<DialogAddComment
124-
comment={comment}
125-
onSave={() => setEditing(false)}
126-
onDragHandlerPan={onDragHandlerPan}
127-
/>
128-
) : (
129-
<>
130-
<DragHandle onPan={onDragHandlerPan}>
131-
<DialogHeader comment={comment} hasShadow={scrollTop > 0} />
132-
</DragHandle>
133-
<Element as="div" css={{ overflow: 'auto' }} ref={listRef}>
134-
<CommentBody
135-
comment={comment}
136-
editing={editing}
137-
setEditing={setEditing}
138-
hasReplies={replies.length}
139-
/>
140-
141-
<Replies
142-
replies={replies}
143-
repliesRenderedCallback={() => setRepliesRendered(true)}
144-
/>
145-
</Element>
146-
<AddReply
112+
<Stack
113+
ref={dialogRef}
114+
direction="vertical"
115+
css={css({
116+
position: 'absolute',
117+
zIndex: 2,
118+
backgroundColor: 'dialog.background',
119+
color: 'dialog.foreground',
120+
border: '1px solid',
121+
borderColor: 'dialog.border',
122+
borderRadius: 4,
123+
width: DIALOG_WIDTH,
124+
height: 'auto',
125+
maxHeight: '80vh',
126+
fontFamily: 'Inter, sans-serif',
127+
overflow: 'hidden',
128+
boxShadow: 2,
129+
})}
130+
>
131+
{isNewComment && editing ? (
132+
<DialogAddComment
147133
comment={comment}
148-
onSubmit={() => {
149-
// scroll to bottom of the list,
150-
// have to wait for it to load though :)
151-
window.requestAnimationFrame(() => {
152-
listRef.current.scrollTop = listRef.current.scrollHeight;
153-
});
154-
}}
134+
onSave={() => setEditing(false)}
135+
onDragHandlerPan={onDragHandlerPan}
136+
onDragHandlerPanEnd={onDragHandlerPanEnd}
155137
/>
156-
</>
157-
)}
158-
</Stack>
159-
</motion.div>
138+
) : (
139+
<>
140+
<DragHandle
141+
onPan={onDragHandlerPan}
142+
onPanEnd={onDragHandlerPanEnd}
143+
>
144+
<DialogHeader comment={comment} hasShadow={scrollTop > 0} />
145+
</DragHandle>
146+
<Element as="div" css={{ overflow: 'auto' }} ref={listRef}>
147+
<CommentBody
148+
comment={comment}
149+
editing={editing}
150+
setEditing={setEditing}
151+
hasReplies={replies.length}
152+
/>
153+
154+
<Replies
155+
replies={replies}
156+
repliesRenderedCallback={() => setRepliesRendered(true)}
157+
/>
158+
</Element>
159+
<AddReply
160+
comment={comment}
161+
onSubmit={() => {
162+
// scroll to bottom of the list,
163+
// have to wait for it to load though :)
164+
window.requestAnimationFrame(() => {
165+
listRef.current.scrollTop = listRef.current.scrollHeight;
166+
});
167+
}}
168+
/>
169+
</>
170+
)}
171+
</Stack>
172+
</motion.div>
173+
</>
160174
);
161175
};
162176

163177
const DialogAddComment: React.FC<{
164178
comment: CommentFragment;
165179
onSave: () => void;
166180
onDragHandlerPan: (deltaX: number, deltaY: number) => void;
167-
}> = ({ comment, onSave, onDragHandlerPan }) => {
181+
onDragHandlerPanEnd: () => void;
182+
}> = ({ comment, onSave, onDragHandlerPan, onDragHandlerPanEnd }) => {
168183
const { actions } = useOvermind();
169184
const [value, setValue] = useState('');
170185

@@ -181,7 +196,7 @@ const DialogAddComment: React.FC<{
181196

182197
return (
183198
<Stack direction="vertical" css={css({ paddingBottom: 4 })}>
184-
<DragHandle onPan={onDragHandlerPan}>
199+
<DragHandle onPan={onDragHandlerPan} onPanEnd={onDragHandlerPanEnd}>
185200
<Stack
186201
justify="space-between"
187202
marginY={4}
@@ -242,11 +257,13 @@ const GlobalStyles = createGlobalStyle`
242257

243258
const DragHandle: React.FC<{
244259
onPan: (deltaX: number, deltaY: number) => void;
245-
}> = ({ onPan, children }) => (
260+
onPanEnd: () => void;
261+
}> = ({ onPan, onPanEnd, children }) => (
246262
<motion.div
247263
onPan={(_, info) => {
248264
onPan(info.delta.x, info.delta.y);
249265
}}
266+
onPanEnd={onPanEnd}
250267
style={{ cursor: 'move', zIndex: 2 }}
251268
>
252269
<GlobalStyles />
@@ -581,6 +598,24 @@ const AddReply = ({ comment, ...props }) => {
581598
);
582599
};
583600

601+
/** We use an transparent overlay when dragging
602+
* so that we can stop selection of the text
603+
* in the background when your drag the dialog.
604+
*/
605+
const Overlay = () => (
606+
<Element
607+
as="div"
608+
css={{
609+
position: 'absolute',
610+
top: 0,
611+
bottom: 0,
612+
left: 0,
613+
right: 0,
614+
zIndex: 1,
615+
}}
616+
/>
617+
);
618+
584619
// trying to match the position for code comments
585620
const FALLBACK_POSITION = { x: 800, y: 120 };
586621

0 commit comments

Comments
 (0)