This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathConfirmationModal.tsx
More file actions
155 lines (142 loc) · 4.62 KB
/
ConfirmationModal.tsx
File metadata and controls
155 lines (142 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { useCallback, useEffect } from 'react';
import { FocusScope } from 'react-aria';
import DeleteItemIcon from '../../icons/ui/DeleteItemIcon';
import { useStoreActions, useStoreState } from '../../state/Hooks';
import { Button, ButtonVariant } from '../atoms/Button';
export type ConfirmationType = 'save' | 'delete';
export type ConfirmationDialogue = {
header: string;
body: string;
button: string;
buttonVariant: ButtonVariant;
};
export type ConfirmationModalModel = {
modalDialogue: ConfirmationType | ConfirmationDialogue;
labels: string[];
onConfirm: () => void;
};
export type ConfirmationDialogueTemplates = Record<
ConfirmationType,
ConfirmationDialogue
>;
const placeholder = '%s';
const confirmDialogue: ConfirmationDialogueTemplates = {
save: {
header: `Do you want to save changes to ${placeholder}?`,
body: 'If you choose not to save, your changes will be lost.',
button: 'Save',
buttonVariant: 'primary',
},
delete: {
header: `Delete ${placeholder} ${placeholder}?`,
body: `When you delete the ${placeholder} named ${placeholder}, it will be removed from each component that uses it.
This definition has %s dependent components.`,
button: 'Delete',
buttonVariant: 'dangerous',
},
};
const ConfirmationModal = () => {
const confirm = useStoreState((state) => state.confirm);
const updateConfirmation = useStoreActions(
(actions) => actions.triggerConfirmation,
);
const dialogue =
typeof confirm?.modalDialogue === 'string'
? confirmDialogue[confirm.modalDialogue]
: confirm?.modalDialogue;
const dialogueBox = { x: 478, y: 250 }; // TODO: make this dynamic
const closeHandler = useCallback(() => {
updateConfirmation(undefined);
}, [updateConfirmation]);
const handleUserKeyPress = useCallback(
(event: any) => {
const { key } = event;
if (key === 'Escape') {
closeHandler();
}
},
[closeHandler],
);
useEffect(() => {
window.addEventListener('keydown', handleUserKeyPress);
return () => {
window.removeEventListener('keydown', handleUserKeyPress);
};
}, [handleUserKeyPress]);
/**
* Replace placeholders in the dialogue body with the provided confirmation values.
*/
const populatePlaceholders = (input: string) => {
const parts = input.split(placeholder);
return parts.map((part, index) => (
<>
{part}
{index !== parts.length - 1 && (
<strong>{confirm?.labels[index]}</strong>
)}
</>
));
};
// implement dialog dictionary, make it pretty, functionality first bro, get components to delete
return (
<>
{confirm && dialogue && (
<div
aria-label="Confirmation Modal"
className="absolute left-0 top-0 w-full h-full z-50 flex"
style={{ background: 'rgba(20,20,20,.8)' }}
>
<FocusScope contain autoFocus>
<div
className="bg-white w-min rounded absolute"
style={{
left: `calc(50% - ${dialogueBox.x / 2}px`,
top: `calc(50% - ${dialogueBox.y / 2}px`,
}}
>
<div className="px-8 py-4">
<h3 className="font-extrabold py-4 text-2xl">
{populatePlaceholders(dialogue.header)}
</h3>
<div className="w-96 h-20 pt-2">
{populatePlaceholders(dialogue.body)}
</div>
</div>
<div className="border-t border-circle-gray-400 p-4 py-6 flex">
<div className="ml-auto">
<Button
variant="secondary"
type="button"
onClick={closeHandler}
>
Cancel
</Button>
<Button
variant={dialogue.buttonVariant}
type="button"
onClick={() => {
confirm.onConfirm();
updateConfirmation(undefined);
}}
>
{dialogue.button}
</Button>
</div>
<button
className="absolute w-14 h-10 top-0 right-0 hover:bg-circle-gray-300 rounded"
onClick={closeHandler}
>
<DeleteItemIcon
className="m-auto w-3 h-3"
color="#555555"
></DeleteItemIcon>
</button>
</div>
</div>
</FocusScope>
</div>
)}
</>
);
};
export default ConfirmationModal;