-
Notifications
You must be signed in to change notification settings - Fork 869
/
AlertDialog.tsx
276 lines (227 loc) · 10.8 KB
/
AlertDialog.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import * as React from 'react';
import { createContext } from '@radix-ui/react-context';
import { useComposedRefs } from '@radix-ui/react-compose-refs';
import { composeEventHandlers } from '@radix-ui/primitive';
import { Primitive, extendPrimitive } from '@radix-ui/react-primitive';
import * as DialogPrimitive from '@radix-ui/react-dialog';
import { useId } from '@radix-ui/react-id';
import { Slottable } from '@radix-ui/react-slot';
import type * as Polymorphic from '@radix-ui/react-polymorphic';
/* -------------------------------------------------------------------------------------------------
* AlertDialog
* -----------------------------------------------------------------------------------------------*/
const ROOT_NAME = 'AlertDialog';
type AlertDialogContextValue = {
descriptionId: string;
titleId: string;
};
const [AlertDialogProvider, useAlertDialogContext] = createContext<AlertDialogContextValue>(
ROOT_NAME
);
const AlertDialog: React.FC<React.ComponentProps<typeof DialogPrimitive.Root>> = (props) => (
<AlertDialogProvider descriptionId={useId()} titleId={useId()}>
<DialogPrimitive.Root {...props} />
</AlertDialogProvider>
);
AlertDialog.displayName = ROOT_NAME;
/* -------------------------------------------------------------------------------------------------
* AlertDialogContent
* -----------------------------------------------------------------------------------------------*/
const CONTENT_NAME = 'AlertDialogContent';
type AlertDialogContentContextValue = {
cancelRef: React.MutableRefObject<React.ElementRef<typeof AlertDialogCancel> | null>;
};
const [
AlertDialogContentProvider,
useAlertDialogContentContext,
] = createContext<AlertDialogContentContextValue>(CONTENT_NAME);
type AlertDialogContentOwnProps = Omit<
Polymorphic.OwnProps<typeof DialogPrimitive.Content>,
'refToFocusOnOpen' | 'id'
>;
type AlertDialogContentPrimitive = Polymorphic.ForwardRefComponent<
Polymorphic.IntrinsicElement<typeof DialogPrimitive.Content>,
AlertDialogContentOwnProps
>;
const AlertDialogContent = React.forwardRef((props, forwardedRef) => {
const {
'aria-label': ariaLabel,
'aria-labelledby': ariaLabelledBy,
'aria-describedby': ariaDescribedBy,
children,
...dialogContentProps
} = props;
const { descriptionId, titleId } = useAlertDialogContext(CONTENT_NAME);
const cancelRef = React.useRef<React.ElementRef<typeof AlertDialogCancel> | null>(null);
return (
<DialogPrimitive.Content
role="alertdialog"
aria-describedby={ariaDescribedBy || descriptionId}
// If `aria-label` is set, ensure `aria-labelledby` is undefined as to avoid confusion.
// Otherwise fallback to an explicit `aria-labelledby` or the ID used in the
// `AlertDialogTitle`
aria-labelledby={ariaLabel ? undefined : ariaLabelledBy || titleId}
aria-label={ariaLabel || undefined}
{...dialogContentProps}
ref={forwardedRef}
onOpenAutoFocus={composeEventHandlers(dialogContentProps.onOpenAutoFocus, (event) => {
event.preventDefault();
cancelRef.current?.focus({ preventScroll: true });
})}
>
<AlertDialogContentProvider cancelRef={cancelRef}>
{process.env.NODE_ENV === 'development' && (
<AccessibilityDevWarnings
ariaLabel={ariaLabel}
ariaLabelledBy={ariaLabelledBy}
ariaDescribedBy={ariaDescribedBy}
/>
)}
{/**
* We have to use `Slottable` here as we cannot wrap the `AlertDialogContentProvider`
* around everything, otherwise the `AccessibilityDevWarnings` would be rendered straight away.
* This is because we want the accessibility checks to run only once the content is actually
* open and that behaviour is already encapsulated in `DialogContent`.
*/}
<Slottable>{children}</Slottable>
</AlertDialogContentProvider>
</DialogPrimitive.Content>
);
}) as AlertDialogContentPrimitive;
AlertDialogContent.displayName = CONTENT_NAME;
/* -------------------------------------------------------------------------------------------------
* AlertDialogTitle
* -----------------------------------------------------------------------------------------------*/
// Because `AlertDialog` depends more heavily on both a title and description for proper screen
// reader announcements, we provide explicit components for each to reduce the friction and make it
// simpler to get these right. These are optional if the consumer prefers to pass appropriate aria
// labelling props directly.
const TITLE_NAME = 'AlertDialogTitle';
const TITLE_DEFAULT_TAG = 'h2';
type AlertDialogTitleOwnProps = Polymorphic.OwnProps<typeof Primitive>;
type AlertDialogTitlePrimitive = Polymorphic.ForwardRefComponent<
typeof TITLE_DEFAULT_TAG,
AlertDialogTitleOwnProps
>;
const AlertDialogTitle = React.forwardRef((props, forwardedRef) => {
const { as = TITLE_DEFAULT_TAG, ...titleProps } = props;
const { titleId } = useAlertDialogContext(TITLE_NAME);
return <Primitive id={titleId} {...titleProps} as={as} ref={forwardedRef} />;
}) as AlertDialogTitlePrimitive;
AlertDialogTitle.displayName = TITLE_NAME;
/* -------------------------------------------------------------------------------------------------
* AlertDialogDescription
* -----------------------------------------------------------------------------------------------*/
const DESCRIPTION_NAME = 'AlertDialogDescription';
const DESCRIPTION_DEFAULT_TAG = 'p';
type AlertDialogDescriptionOwnProps = Polymorphic.OwnProps<typeof Primitive>;
type AlertDialogDescriptionPrimitive = Polymorphic.ForwardRefComponent<
typeof DESCRIPTION_DEFAULT_TAG,
AlertDialogDescriptionOwnProps
>;
const AlertDialogDescription = React.forwardRef((props, forwardedRef) => {
const { as = DESCRIPTION_DEFAULT_TAG, ...descriptionProps } = props;
const { descriptionId } = useAlertDialogContext(DESCRIPTION_NAME);
return <Primitive id={descriptionId} {...descriptionProps} as={as} ref={forwardedRef} />;
}) as AlertDialogDescriptionPrimitive;
AlertDialogDescription.displayName = DESCRIPTION_NAME;
/* -------------------------------------------------------------------------------------------------
* AlertDialogCancel
* -----------------------------------------------------------------------------------------------*/
const CANCEL_NAME = 'AlertDialogCancel';
type AlertDialogCancelOwnProps = Polymorphic.OwnProps<typeof DialogPrimitive.Close>;
type AlertDialogCancelPrimitive = Polymorphic.ForwardRefComponent<
Polymorphic.IntrinsicElement<typeof DialogPrimitive.Close>,
AlertDialogCancelOwnProps
>;
const AlertDialogCancel = React.forwardRef((props, forwardedRef) => {
const { cancelRef } = useAlertDialogContentContext(CANCEL_NAME);
const ref = useComposedRefs(forwardedRef, cancelRef);
return <DialogPrimitive.Close {...props} ref={ref} />;
}) as AlertDialogCancelPrimitive;
AlertDialogCancel.displayName = CANCEL_NAME;
/* ---------------------------------------------------------------------------------------------- */
const AlertDialogTrigger = extendPrimitive(DialogPrimitive.Trigger, {
displayName: 'AlertDialogTrigger',
});
const AlertDialogOverlay = extendPrimitive(DialogPrimitive.Overlay, {
displayName: 'AlertDialogOverlay',
});
const AlertDialogAction = extendPrimitive(DialogPrimitive.Close, {
displayName: 'AlertDialogAction',
});
/* ---------------------------------------------------------------------------------------------- */
// TODO: Add link to docs when available
const LABEL_WARNING = `${CONTENT_NAME} requires a label for the component to be accessible for screen reader users.
You can label the ${CONTENT_NAME} by passing a ${TITLE_NAME} component as a child, which also benefits sighted users by adding visible context to the dialog.
Alternatively, you can use your own component as a title by assigning it an \`id\` and passing the same value to the \`aria-labelledby\` prop in ${CONTENT_NAME}. If the label is confusing or duplicative for sighted users, you can also pass a label directly by using the \`aria-label\` prop.
For more information, see https://LINK-TO-DOCS.com`;
const DESC_WARNING = `${CONTENT_NAME} requires a description for the component to be accessible for screen reader users.
You can add a description to the ${CONTENT_NAME} by passing a ${DESCRIPTION_NAME} component as a child, which also benefits sighted users by adding visible context to the dialog.
Alternatively, you can use your own component as a description by assigning it an \`id\` and passing the same value to the \`aria-describedby\` prop in ${CONTENT_NAME}. If the description is confusing or duplicative for sighted users, you can use the \`@radix-ui/react-visually-hidden\` primitive as a wrapper around your description component.
For more information, see https://LINK-TO-DOCS.com`;
// We need some effects to fire when DialogContent is mounted that will give us some useful dev
// warnings. DialogContent returns `null` when the Dialog is closed, meaning that if we put these
// effects up in AlertDialog.Content these effects only fire on its initial mount, NOT when the
// underlying DialogContent mounts. We stick this inner component inside DialogContent to make
// sure the effects fire as expected. This component is only useful in a dev environment, so we
// won't bother rendering it in production.
const AccessibilityDevWarnings: React.FC<{
ariaLabel?: string;
ariaLabelledBy?: string;
ariaDescribedBy?: string;
}> = (props) => {
const { ariaLabel, ariaLabelledBy, ariaDescribedBy } = props;
const context = useAlertDialogContext('AlertDialogContent');
React.useEffect(() => {
const hasLabel = Boolean(
ariaLabel ||
(ariaLabelledBy && document.getElementById(ariaLabelledBy)) ||
(context.titleId && document.getElementById(context.titleId))
);
if (!hasLabel) {
console.warn(LABEL_WARNING);
}
const hasDescription = Boolean(
(ariaDescribedBy && document.getElementById(ariaDescribedBy)) ||
(context.descriptionId && document.getElementById(context.descriptionId))
);
if (!hasDescription) {
console.warn(DESC_WARNING);
}
}, [context.titleId, ariaLabel, ariaDescribedBy, context.descriptionId, ariaLabelledBy]);
return null;
};
const Root = AlertDialog;
const Title = AlertDialogTitle;
const Cancel = AlertDialogCancel;
const Action = AlertDialogAction;
const Content = AlertDialogContent;
const Description = AlertDialogDescription;
const Overlay = AlertDialogOverlay;
const Trigger = AlertDialogTrigger;
export {
AlertDialog,
AlertDialogTitle,
AlertDialogCancel,
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogOverlay,
AlertDialogTrigger,
//
Root,
Title,
Cancel,
Action,
Content,
Description,
Overlay,
Trigger,
};
export type {
AlertDialogTitlePrimitive,
AlertDialogCancelPrimitive,
AlertDialogContentPrimitive,
AlertDialogDescriptionPrimitive,
};