|
| 1 | +import { AlertDialog as _AlertDialog } from '@base-ui/react/alert-dialog'; |
| 2 | +import clsx from 'clsx'; |
1 | 3 | import { forwardRef, useContext } from '@wordpress/element'; |
2 | 4 | import { __ } from '@wordpress/i18n'; |
| 5 | +import { |
| 6 | + type ThemeProvider as ThemeProviderType, |
| 7 | + privateApis as themePrivateApis, |
| 8 | +} from '@wordpress/theme'; |
| 9 | + |
3 | 10 | import { Button } from '../button'; |
4 | | -import * as Dialog from '../dialog'; |
| 11 | +import dialogStyles from '../dialog/style.module.css'; |
| 12 | +import { unlock } from '../lock-unlock'; |
| 13 | +import { Stack } from '../stack'; |
| 14 | +import { Text } from '../text'; |
5 | 15 | import { AlertDialogContext } from './context'; |
6 | | -import styles from './style.module.css'; |
| 16 | +import alertDialogStyles from './style.module.css'; |
7 | 17 | import type { PopupProps } from './types'; |
8 | 18 |
|
| 19 | +const ThemeProvider: typeof ThemeProviderType = |
| 20 | + unlock( themePrivateApis ).ThemeProvider; |
| 21 | + |
9 | 22 | const Popup = forwardRef< HTMLDivElement, PopupProps >( |
10 | 23 | function AlertDialogPopup( |
11 | 24 | { |
| 25 | + className, |
| 26 | + intent = 'default', |
12 | 27 | title, |
| 28 | + description, |
13 | 29 | children, |
14 | | - onConfirm, |
15 | 30 | confirmButtonText = __( 'OK' ), |
16 | 31 | cancelButtonText = __( 'Cancel' ), |
17 | | - loading, |
| 32 | + ...props |
18 | 33 | }, |
19 | 34 | ref |
20 | 35 | ) { |
21 | | - const { intent } = useContext( AlertDialogContext ); |
| 36 | + const { phase, showSpinner, errorMessage, confirm } = |
| 37 | + useContext( AlertDialogContext ); |
22 | 38 |
|
23 | | - // When `loading` is provided, the consumer controls when the dialog |
24 | | - // closes (async flow). Use a plain Button so clicking confirm doesn't |
25 | | - // auto-close — the consumer sets `open={false}` after their operation. |
26 | | - const ConfirmButton = loading !== undefined ? Button : Dialog.Action; |
| 39 | + const confirmClassName = |
| 40 | + intent === 'irreversible' |
| 41 | + ? alertDialogStyles[ 'irreversible-action' ] |
| 42 | + : undefined; |
| 43 | + |
| 44 | + const buttonsDisabled = phase !== 'idle' || undefined; |
27 | 45 |
|
28 | 46 | return ( |
29 | | - <Dialog.Popup ref={ ref }> |
30 | | - <Dialog.Header> |
31 | | - <Dialog.Title>{ title }</Dialog.Title> |
32 | | - </Dialog.Header> |
33 | | - { children } |
34 | | - <Dialog.Footer> |
35 | | - <Dialog.Action |
36 | | - variant="minimal" |
37 | | - disabled={ loading || undefined } |
38 | | - > |
39 | | - { cancelButtonText } |
40 | | - </Dialog.Action> |
41 | | - <ConfirmButton |
42 | | - className={ |
43 | | - intent === 'irreversible' |
44 | | - ? styles[ 'irreversible-action' ] |
45 | | - : undefined |
46 | | - } |
47 | | - onClick={ onConfirm } |
48 | | - loading={ loading } |
| 47 | + <_AlertDialog.Portal> |
| 48 | + <_AlertDialog.Backdrop className={ dialogStyles.backdrop } /> |
| 49 | + <ThemeProvider> |
| 50 | + <_AlertDialog.Popup |
| 51 | + ref={ ref } |
| 52 | + className={ clsx( |
| 53 | + dialogStyles.popup, |
| 54 | + className, |
| 55 | + dialogStyles[ 'is-medium' ] |
| 56 | + ) } |
| 57 | + { ...props } |
49 | 58 | > |
50 | | - { confirmButtonText } |
51 | | - </ConfirmButton> |
52 | | - </Dialog.Footer> |
53 | | - </Dialog.Popup> |
| 59 | + <Stack |
| 60 | + direction="column" |
| 61 | + gap="sm" |
| 62 | + className={ alertDialogStyles.header } |
| 63 | + > |
| 64 | + <Text |
| 65 | + variant="heading-xl" |
| 66 | + render={ <_AlertDialog.Title /> } |
| 67 | + className={ dialogStyles.title } |
| 68 | + > |
| 69 | + { title } |
| 70 | + </Text> |
| 71 | + { description && ( |
| 72 | + <Text |
| 73 | + variant="body-md" |
| 74 | + render={ <_AlertDialog.Description /> } |
| 75 | + > |
| 76 | + { description } |
| 77 | + </Text> |
| 78 | + ) } |
| 79 | + </Stack> |
| 80 | + { children } |
| 81 | + <Stack direction="column" gap="md"> |
| 82 | + <div className={ dialogStyles.footer }> |
| 83 | + <_AlertDialog.Close |
| 84 | + render={ <Button variant="minimal" /> } |
| 85 | + disabled={ buttonsDisabled } |
| 86 | + > |
| 87 | + { cancelButtonText } |
| 88 | + </_AlertDialog.Close> |
| 89 | + <Button |
| 90 | + className={ confirmClassName } |
| 91 | + onClick={ confirm } |
| 92 | + loading={ showSpinner || undefined } |
| 93 | + disabled={ buttonsDisabled } |
| 94 | + > |
| 95 | + { confirmButtonText } |
| 96 | + </Button> |
| 97 | + </div> |
| 98 | + { errorMessage && ( |
| 99 | + <Text |
| 100 | + variant="body-sm" |
| 101 | + className={ |
| 102 | + alertDialogStyles[ 'error-message' ] |
| 103 | + } |
| 104 | + > |
| 105 | + { errorMessage } |
| 106 | + </Text> |
| 107 | + ) } |
| 108 | + </Stack> |
| 109 | + </_AlertDialog.Popup> |
| 110 | + </ThemeProvider> |
| 111 | + </_AlertDialog.Portal> |
54 | 112 | ); |
55 | 113 | } |
56 | 114 | ); |
|
0 commit comments