Skip to content

Commit 47aa569

Browse files
committed
[dashboard] improve modal
the modal dialog should - close on ESC - close on click outside modal but not within - handle enter
1 parent a406854 commit 47aa569

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

components/dashboard/src/components/Modal.tsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,57 @@
1+
import { Disposable, DisposableCollection } from "@gitpod/gitpod-protocol";
2+
import { useEffect } from "react";
3+
14
export default function Modal(props: {
25
children: React.ReactChild[] | React.ReactChild,
36
visible: boolean,
47
closeable?: boolean,
58
className?: string,
6-
onClose: () => void
9+
onClose: () => void,
10+
onEnter?: () => boolean
711
}) {
12+
const disposable = new DisposableCollection();
13+
const close = () => {
14+
disposable.dispose();
15+
props.onClose();
16+
}
17+
useEffect(() => {
18+
if (!props.visible) {
19+
return;
20+
}
21+
const keyHandler = (k: globalThis.KeyboardEvent) => {
22+
if (k.eventPhase === 1 /* CAPTURING */) {
23+
if (k.key === 'Escape') {
24+
close();
25+
}
26+
if (k.key === 'Enter') {
27+
if (props.onEnter) {
28+
if (props.onEnter() === false) {
29+
return;
30+
}
31+
}
32+
close();
33+
k.stopPropagation();
34+
}
35+
}
36+
}
37+
window.addEventListener('keydown', keyHandler, { capture: true });
38+
disposable.push(Disposable.create(()=> window.removeEventListener('keydown', keyHandler)));
39+
});
840
if (!props.visible) {
941
return null;
1042
}
11-
setTimeout(() => window.addEventListener('click', props.onClose, { once: true }), 0);
1243
return (
13-
<div className="fixed top-0 left-0 bg-black bg-opacity-70 z-50 w-screen h-screen" >
44+
<div className="fixed top-0 left-0 bg-black bg-opacity-70 z-50 w-screen h-screen" onClick={close}>
1445
<div className="w-screen h-screen align-middle" style={{display: 'table-cell'}}>
15-
<div className={"relative bg-white border rounded-xl p-6 max-w-lg mx-auto text-gray-600" + props.className}>
46+
<div className={"relative bg-white border rounded-xl p-6 max-w-lg mx-auto text-gray-600" + props.className} onClick={e => e.stopPropagation()}>
1647
{props.closeable !== false && (
17-
<div className="absolute right-9 top-8 cursor-pointer" onClick={props.onClose}>
48+
<div className="absolute right-7 top-6 cursor-pointer hover:bg-gray-200 rounded-md p-2" onClick={close}>
1849
<svg version="1.1" width="14px" height="14px"
1950
viewBox="0 0 100 100">
2051
<line x1="0" y1="0" x2="100" y2="100" stroke="currentColor" strokeWidth="10px" />
2152
<line x1="0" y1="100" x2="100" y2="0" stroke="currentColor" strokeWidth="10px" />
2253
</svg>
2354
</div>
24-
2555
)}
2656
{props.children}
2757
</div>

components/dashboard/src/workspaces/WorkspaceEntry.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,20 @@ export function WorkspaceEntry({ desc, model }: { desc: WorkspaceInfo, model: Wo
133133
<Modal visible={isChangesModalVisible} onClose={() => setChangesModalVisible(false)}>
134134
{getChangesPopup(pendingChanges)}
135135
</Modal>
136-
<Modal visible={isModalVisible} onClose={() => setModalVisible(false)}>
136+
<Modal visible={isModalVisible} onClose={() => setModalVisible(false)} onEnter={() => {model.deleteWorkspace(ws.id); return true;}}>
137137
<div>
138-
<h3>Delete {ws.id}</h3>
139-
<div className="py-4">
140-
<p>Do you really want to delete this workspace?</p>
138+
<h3>Delete Workspace</h3>
139+
<div className="border-t border-b border-gray-200 mt-2 -mx-6 px-6 py-2">
140+
<p className="mt-1 mb-2 text-base">Are you sure you want to delete this workspace?</p>
141+
<div className="w-full p-4 mb-2 bg-gray-100 rounded-xl group bg-gray-100">
142+
<p className="text-base text-gray-800 font-semibold">{ws.id}</p>
143+
<p>{ws.description}</p>
144+
</div>
141145
</div>
142-
<div className="flex">
143-
<div className="flex-1"></div>
146+
<div className="flex justify-end mt-5">
144147
<button className="cursor-pointer px-3 py-2 text-white text-sm rounded-md border-2 border-red-800 bg-red-600 hover:bg-red-800"
145148
onClick={() => model.deleteWorkspace(ws.id)}>
146-
Delete
149+
Delete Workspace
147150
</button>
148151
</div>
149152
</div>

0 commit comments

Comments
 (0)