/
ConfirmationDialog.tsx
187 lines (177 loc) · 5.95 KB
/
ConfirmationDialog.tsx
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import React, {useCallback} from 'react'
import ReactDOM from 'react-dom'
import styled from 'styled-components'
import Box from '../Box'
import {ThemeProvider, useTheme, ThemeProviderProps} from '../ThemeProvider'
import {FocusKeys} from '@primer/behaviors'
import {get} from '../constants'
import {Dialog, DialogProps, DialogHeaderProps, DialogButtonProps} from '../Dialog/Dialog'
import {useFocusZone} from '../hooks/useFocusZone'
/**
* Props to customize the ConfirmationDialog.
*/
export interface ConfirmationDialogProps {
/**
* Required. This callback is invoked when a gesture to close the dialog
* is performed. The first argument indicates the gesture.
*/
onClose: (gesture: 'confirm' | 'cancel' | 'close-button' | 'cancel' | 'escape') => void
/**
* Required. The title of the ConfirmationDialog. This is usually a brief
* question.
*/
title: React.ReactNode
/**
* The text to use for the cancel button. Default: "Cancel".
*/
cancelButtonContent?: React.ReactNode
/**
* The text to use for the confirm button. Default: "OK".
*/
confirmButtonContent?: React.ReactNode
/**
* The type of button to use for the confirm button. Default: Button.
*/
confirmButtonType?: 'normal' | 'primary' | 'danger'
}
const StyledConfirmationHeader = styled.div`
padding: ${get('space.2')};
display: flex;
flex-direction: row;
`
const StyledTitle = styled(Box).attrs({as: 'h1'})`
font-size: ${get('fontSizes.3')};
font-weight: ${get('fontWeights.bold')};
padding: 6px ${get('space.2')};
flex-grow: 1;
margin: 0; /* override default margin */
`
const ConfirmationHeader: React.FC<React.PropsWithChildren<DialogHeaderProps>> = ({title, onClose, dialogLabelId}) => {
const onCloseClick = useCallback(() => {
onClose('close-button')
}, [onClose])
return (
<StyledConfirmationHeader>
<StyledTitle id={dialogLabelId}>{title}</StyledTitle>
<Dialog.CloseButton onClose={onCloseClick} />
</StyledConfirmationHeader>
)
}
const StyledConfirmationBody = styled(Box)`
font-size: ${get('fontSizes.1')};
padding: 0 ${get('space.3')} ${get('space.3')} ${get('space.3')};
color: ${get('colors.fg.muted')};
flex-grow: 1;
`
const ConfirmationBody: React.FC<React.PropsWithChildren<DialogProps>> = ({children}) => {
return <StyledConfirmationBody>{children}</StyledConfirmationBody>
}
const StyledConfirmationFooter = styled(Box)`
display: grid;
grid-auto-flow: column;
grid-auto-columns: max-content;
grid-gap: ${get('space.2')};
align-items: end;
justify-content: end;
padding: ${get('space.1')} ${get('space.3')} ${get('space.3')};
`
const ConfirmationFooter: React.FC<React.PropsWithChildren<DialogProps>> = ({footerButtons}) => {
const {containerRef: footerRef} = useFocusZone({
bindKeys: FocusKeys.ArrowHorizontal | FocusKeys.Tab,
focusInStrategy: 'closest'
})
// Must have exactly 2 buttons!
return (
<StyledConfirmationFooter ref={footerRef as React.RefObject<HTMLDivElement>}>
<Dialog.Buttons buttons={footerButtons ?? []} />
</StyledConfirmationFooter>
)
}
/**
* A ConfirmationDialog is a special kind of dialog with more rigid behavior. It
* is used to confirm a user action. ConfirmationDialogs always have exactly
* two buttons: one to cancel the action and one to confirm it. No custom
* rendering capabilities are provided for ConfirmationDialog.
*/
export const ConfirmationDialog: React.FC<React.PropsWithChildren<ConfirmationDialogProps>> = props => {
const {
onClose,
title,
cancelButtonContent = 'Cancel',
confirmButtonContent = 'OK',
confirmButtonType = 'normal',
children
} = props
const onCancelButtonClick = useCallback(() => {
onClose('cancel')
}, [onClose])
const onConfirmButtonClick = useCallback(() => {
onClose('confirm')
}, [onClose])
const isConfirmationDangerous = confirmButtonType === 'danger'
const cancelButton: DialogButtonProps = {
content: cancelButtonContent,
onClick: onCancelButtonClick,
autoFocus: isConfirmationDangerous
}
const confirmButton: DialogButtonProps = {
content: confirmButtonContent,
buttonType: confirmButtonType,
onClick: onConfirmButtonClick,
autoFocus: !isConfirmationDangerous
}
const footerButtons = [cancelButton, confirmButton]
return (
<Dialog
onClose={onClose}
title={title}
footerButtons={footerButtons}
role="alertdialog"
width="medium"
renderHeader={ConfirmationHeader}
renderBody={ConfirmationBody}
renderFooter={ConfirmationFooter}
>
{children}
</Dialog>
)
}
export type ConfirmOptions = Omit<ConfirmationDialogProps, 'onClose'> & {content: React.ReactNode}
async function confirm(themeProps: ThemeProviderProps, options: ConfirmOptions): Promise<boolean> {
const {content, ...confirmationDialogProps} = options
return new Promise(resolve => {
const hostElement = document.createElement('div')
const onClose: ConfirmationDialogProps['onClose'] = gesture => {
ReactDOM.unmountComponentAtNode(hostElement)
if (gesture === 'confirm') {
resolve(true)
} else {
resolve(false)
}
}
ReactDOM.render(
<ThemeProvider {...themeProps}>
<ConfirmationDialog {...confirmationDialogProps} onClose={onClose}>
{content}
</ConfirmationDialog>
</ThemeProvider>,
hostElement
)
})
}
/**
* This hook takes no parameters and returns an `async` function, `confirm`. When `confirm`
* is called, it shows the confirmation dialog. When the dialog is dismissed, a promise is
* resolved with `true` or `false` depending on whether or not the confirm button was used.
*/
export function useConfirm() {
const {theme, colorMode, dayScheme, nightScheme} = useTheme()
const result = useCallback(
(options: ConfirmOptions) => {
const themeProps: ThemeProviderProps = {theme, colorMode, dayScheme, nightScheme}
return confirm(themeProps, options)
},
[theme, colorMode, dayScheme, nightScheme]
)
return result
}