Repository navigation
Components: Add experimental ConfirmDialog
#34153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
72cd96b
335053a
f4a3396
1b2f104
d5e8d73
f7250b6
6062648
a01225f
a4d2453
18e0231
2391e35
a878802
b09651b
c221e9d
cac2732
0942707
7d629b8
12f0b18
f99c357
f746119
5fd979f
13bf86a
d68508f
56ea610
6245385
814fc6f
2b47a46
bf9c062
37befa2
31b88e2
2cd86e9
21600b0
b21c175
922c103
9c931c6
5494585
2c43770
fdab69f
835c2e6
61f7c1a
c1cf65a
20e2d23
33e97e1
566fe18
d1be655
81d9e76
8665ad6
29c8c3a
f59553d
55ac46a
c96f575
3abfc81
605c2ae
6a60f92
995f45f
40cb594
ceabb07
aa7025d
d130ed1
054caae
85e9510
c20f85d
e9ee813
316afc5
5b2b026
4e5abed
9bf6f88
64c2816
414ab15
49eaa0e
9f62ad3
af340b2
710932d
5e24ff0
7d5860c
febda4a
8bed797
16ce455
2ddbabc
bd6eb33
94f8b47
4d35f84
74c5293
f69a973
541490c
8eeb0c7
3c3f49a
173f2d4
4a81ba6
e2ce3ef
c2a9adb
a320d85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # `ConfirmDialog` | ||
|
|
||
| <div class="callout callout-alert"> | ||
| This feature is still experimental. "Experimental" means this is an early implementation subject to drastic and breaking changes. | ||
| </div> | ||
|
|
||
| `ConfirmDialog` is built of top of [`Modal`](/packages/components/src/modal/README.md] and displays a confirmation dialog, with _confirm_ and _cancel_ buttons. | ||
|
|
||
| The dialog is confirmed by clicking the _confirm_ button or by pressing the `Enter` key. It is cancelled (closed) by clicking the _cancel_ button, by pressing the `ESC` key, or by clicking outside the dialog focus (i.e, the overlay). | ||
|
|
||
| ## Usage | ||
|
|
||
| `ConfirmDialog` has two main implicit modes: controlled and uncontrolled. | ||
|
|
||
| ### Uncontrolled mode | ||
|
|
||
| Allows the component to be used standalone, just by declaring it as part of another React's component render method: | ||
| * It will be automatically open (displayed) upon mounting; | ||
| * It will be automatically closed when when clicking the _cancel_ button, by pressing the `ESC` key, or by clicking outside the dialog focus (i.e, the overlay); | ||
| * `onCancel` is not mandatory but can be passed. Even if passed, the dialog will still be able to close itself. | ||
|
|
||
| Activating this mode is as simple as omitting the `isOpen` prop. The only mandatory prop, in this case, is the `onConfirm` callback. The message is passed as the `children`. You can pass any JSX you'd like, which allows to further format the message or include sub-component if you'd like: | ||
|
|
||
| ```jsx | ||
| import { | ||
| __experimentalConfirmDialog as ConfirmDialog | ||
| } from '@wordpress/components'; | ||
|
|
||
| function Example() { | ||
| return ( | ||
| <ConfirmDialog onConfirm={ () => console.debug(' Confirmed! ') }> | ||
| Are you sure? <strong>This action cannot be undone!</strong> | ||
| </ConfirmDialog> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ### Controlled mode | ||
|
|
||
| Let the parent component control when the dialog is open/closed. It's activated when a boolean value is passed to `isOpen`: | ||
| * It will not be automatically closed. You need to let it know when to open/close by updating the value of the `isOpen` prop; | ||
| * Both `onConfirm` and the `onCancel` callbacks are mandatory props in this mode; | ||
| * You'll want to update the state that controls `isOpen` by updating it from the `onCancel` and `onConfirm` callbacks. | ||
|
|
||
|
|
||
| ```jsx | ||
| import { | ||
| __experimentalConfirmDialog as ConfirmDialog | ||
| } from '@wordpress/components'; | ||
|
|
||
| function Example() { | ||
| const [ isOpen, setIsOpen ] = useState( true ); | ||
|
|
||
| const handleConfirm = () => { | ||
| console.debug( 'Confirmed!' ); | ||
| setIsOpen( false ); | ||
| } | ||
|
|
||
| const handleCancel = () => { | ||
| console.debug( 'Cancelled!' ); | ||
| setIsOpen( false ); | ||
| } | ||
|
|
||
| return ( | ||
| <ConfirmDialog | ||
| isOpen={ isOpen } | ||
| onConfirm={ handleConfirm } | ||
| onCancel={ handleCancel } | ||
| > | ||
| Are you sure? <strong>This action cannot be undone!</strong> | ||
| </ConfirmDialog> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ### Unsupported: Multiple instances | ||
|
|
||
| Multiple `ConfirmDialog's is an edge case that's currently not officially supported by this component. At the moment, new instances will end up closing the last instance due to the way the `Modal` is implemented. | ||
|
|
||
| ## Custom Types | ||
|
|
||
| ```ts | ||
| type DialogInputEvent = | ||
| | KeyboardEvent< HTMLDivElement > | ||
| | MouseEvent< HTMLButtonElement > | ||
| ``` | ||
|
|
||
| ## Props | ||
|
|
||
| ### `title`: `string` | ||
|
|
||
| - Required: No | ||
|
|
||
| An optional `title` for the dialog. Setting a title will render it in a title bar at the top of the dialog, making it a bit taller. The bar will also include an `x` close button at the top-right corner. | ||
|
|
||
| ### `children`: `React.ReactNode` | ||
|
|
||
| - Required: Yes | ||
|
|
||
| The actual message for the dialog. It's passed as children and any valid `ReactNode` is accepted: | ||
|
|
||
| ```jsx | ||
| <ConfirmDialog> | ||
| Are you sure? <strong>This action cannot be undone!</strong> | ||
| </ConfirmDialog> | ||
| ``` | ||
|
|
||
| ### `isOpen`: `boolean` | ||
|
|
||
| - Required: No | ||
|
|
||
| Defines if the dialog is open (displayed) or closed (not rendered/displayed). It also implicitly toggles the controlled mode if set or the uncontrolled mode if it's not set. | ||
|
|
||
| ### `onConfirm`: `( event: DialogInputEvent ) => void` | ||
|
|
||
| - Required: Yes | ||
|
|
||
| The callback that's called when the user confirms. A confirmation can happen when the `OK` button is clicked or when `Enter` is pressed. | ||
|
|
||
| ### `onCancel`: `( event: DialogInputEvent ) => void` | ||
|
|
||
| - Required: Only if `isOpen` is not set | ||
|
|
||
| The callback that's called when the user cancels. A cancellation can happen when the `Cancel` button is clicked, when the `ESC` key is pressed, or when a click outside of the dialog focus is detected (i.e. in the overlay). | ||
|
|
||
| It's not required if `isOpen` is not set (uncontrolled mode), as the component will take care of closing itself, but you can still pass a callback if something must be done upon cancelling (the component will still close itself in this case). | ||
|
|
||
| If `isOpen` is set (controlled mode), then it's required, and you need to set the state that defines `isOpen` to `false` as part of this callback if you want the dialog to close when the user cancels. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| // eslint-disable-next-line no-restricted-imports | ||
| import React, { useEffect, useState } from 'react'; | ||
| // eslint-disable-next-line no-restricted-imports | ||
| import type { Ref, KeyboardEvent } from 'react'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useCallback } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import Modal from '../modal'; | ||
| import type { OwnProps, DialogInputEvent } from './types'; | ||
| import { | ||
| useContextSystem, | ||
| contextConnect, | ||
| WordPressComponentProps, | ||
| } from '../ui/context'; | ||
| import { Flex } from '../flex'; | ||
| import Button from '../button'; | ||
| import { Text } from '../text'; | ||
| import { VStack } from '../v-stack'; | ||
|
|
||
| function ConfirmDialog( | ||
| props: WordPressComponentProps< OwnProps, 'div', false >, | ||
| forwardedRef: Ref< any > | ||
| ) { | ||
| const { | ||
| isOpen: isOpenProp, | ||
| onConfirm, | ||
| onCancel, | ||
| children, | ||
| ...otherProps | ||
| } = useContextSystem( props, 'ConfirmDialog' ); | ||
|
|
||
| const [ isOpen, setIsOpen ] = useState< boolean >(); | ||
| const [ shouldSelfClose, setShouldSelfClose ] = useState< boolean >(); | ||
|
|
||
| useEffect( () => { | ||
| // We only allow the dialog to close itself if `isOpenProp` is *not* set. | ||
| // If `isOpenProp` is set, then it (probably) means it's controlled by a | ||
| // parent component. In that case, `shouldSelfClose` might do more harm than | ||
| // good, so we disable it. | ||
| const isIsOpenSet = typeof isOpenProp !== 'undefined'; | ||
| setIsOpen( isIsOpenSet ? isOpenProp : true ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder what happens if:
Definitely somewhat of an edge case, can be tackled in a follow-up PR
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, not sure. I can add a test case for that in a follow-up PR, I'll take note! |
||
| setShouldSelfClose( ! isIsOpenSet ); | ||
| }, [ isOpenProp ] ); | ||
|
|
||
| const handleEvent = useCallback( | ||
| ( callback?: ( event: DialogInputEvent ) => void ) => ( | ||
| event: DialogInputEvent | ||
| ) => { | ||
| callback?.( event ); | ||
| if ( shouldSelfClose ) { | ||
| setIsOpen( false ); | ||
| } | ||
| }, | ||
| [ shouldSelfClose, setIsOpen ] | ||
| ); | ||
|
|
||
| const handleEnter = useCallback( | ||
| ( event: KeyboardEvent< HTMLDivElement > ) => { | ||
| if ( event.key === 'Enter' ) { | ||
| handleEvent( onConfirm )( event ); | ||
| } | ||
| }, | ||
| [ handleEvent, onConfirm ] | ||
| ); | ||
|
|
||
| const cancelLabel = __( 'Cancel' ); | ||
| const confirmLabel = __( 'OK' ); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏻 |
||
|
|
||
| return ( | ||
| <> | ||
| { isOpen && ( | ||
| <Modal | ||
| onRequestClose={ handleEvent( onCancel ) } | ||
| onKeyDown={ handleEnter } | ||
| closeButtonLabel={ cancelLabel } | ||
| isDismissible={ true } | ||
| ref={ forwardedRef } | ||
| __experimentalHideHeader | ||
| { ...otherProps } | ||
| > | ||
| <VStack spacing={ 8 }> | ||
| <Text>{ children }</Text> | ||
| <Flex direction="row" justify="flex-end"> | ||
| <Button | ||
| variant="tertiary" | ||
| onClick={ handleEvent( onCancel ) } | ||
| > | ||
| { cancelLabel } | ||
| </Button> | ||
| <Button | ||
| variant="primary" | ||
| onClick={ handleEvent( onConfirm ) } | ||
| > | ||
| { confirmLabel } | ||
| </Button> | ||
| </Flex> | ||
| </VStack> | ||
| </Modal> | ||
| ) } | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default contextConnect( ConfirmDialog, 'ConfirmDialog' ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import ConfirmDialog from './component'; | ||
|
|
||
| export { ConfirmDialog }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| // eslint-disable-next-line no-restricted-imports | ||
| import React, { useState } from 'react'; | ||
| import { text } from '@storybook/addon-knobs'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import Button from '../../button'; | ||
| import { Heading } from '../../heading'; | ||
| import { ConfirmDialog } from '..'; | ||
|
|
||
| export default { | ||
| component: ConfirmDialog, | ||
| title: 'Components (Experimental)/ConfirmDialog', | ||
| parameters: { | ||
| knobs: { disabled: false }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should move away from
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good! Note taken! |
||
| }, | ||
| }; | ||
|
|
||
| const daText = () => | ||
| text( 'message', 'Would you like to privately publish the post now?' ); | ||
|
|
||
| // Simplest usage: just declare the component with the required `onConfirm` prop. | ||
| export const _default = () => { | ||
| const [ confirmVal, setConfirmVal ] = useState( "Hasn't confirmed yet" ); | ||
|
|
||
| return ( | ||
| <> | ||
| <ConfirmDialog onConfirm={ () => setConfirmVal( 'Confirmed!' ) }> | ||
| { daText() } | ||
| </ConfirmDialog> | ||
| <Heading level={ 1 }>{ confirmVal }</Heading> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export const WithJSXMessage = () => { | ||
| const [ confirmVal, setConfirmVal ] = useState( "Hasn't confirmed yet" ); | ||
|
|
||
| return ( | ||
| <> | ||
| <ConfirmDialog onConfirm={ () => setConfirmVal( 'Confirmed!' ) }> | ||
| <Heading level={ 2 }>{ daText() }</Heading> | ||
| </ConfirmDialog> | ||
| <Heading level={ 1 }>{ confirmVal }</Heading> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export const VeeeryLongMessage = () => { | ||
| const [ confirmVal, setConfirmVal ] = useState( "Hasn't confirmed yet" ); | ||
|
|
||
| return ( | ||
| <> | ||
| <ConfirmDialog onConfirm={ () => setConfirmVal( 'Confirmed!' ) }> | ||
| { daText().repeat( 20 ) } | ||
| </ConfirmDialog> | ||
| <Heading level={ 1 }>{ confirmVal }</Heading> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export const UncontrolledAndWithExplicitOnCancel = () => { | ||
| const [ confirmVal, setConfirmVal ] = useState( | ||
| "Hasn't confirmed or cancelled yet" | ||
| ); | ||
|
|
||
| return ( | ||
| <> | ||
| <ConfirmDialog | ||
| onConfirm={ () => setConfirmVal( 'Confirmed!' ) } | ||
| onCancel={ () => setConfirmVal( 'Cancelled' ) } | ||
| > | ||
| { daText() } | ||
| </ConfirmDialog> | ||
| <Heading level={ 1 }>{ confirmVal }</Heading> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| // Controlled `ConfirmDialog`s require both `onConfirm` *and* `onCancel to be passed | ||
| // It's expected that the user will then use it to hide the dialog, too (see the | ||
| // `setIsOpen` calls below). | ||
| export const Controlled = () => { | ||
| const [ isOpen, setIsOpen ] = useState( false ); | ||
| const [ confirmVal, setConfirmVal ] = useState( | ||
| "Hasn't confirmed or cancelled yet" | ||
| ); | ||
|
|
||
| const handleConfirm = () => { | ||
| setConfirmVal( 'Confirmed!' ); | ||
| setIsOpen( false ); | ||
| }; | ||
|
|
||
| const handleCancel = () => { | ||
| setConfirmVal( 'Cancelled' ); | ||
| setIsOpen( false ); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <ConfirmDialog | ||
| isOpen={ isOpen } | ||
| onConfirm={ handleConfirm } | ||
| onCancel={ handleCancel } | ||
| > | ||
| { daText() } | ||
| </ConfirmDialog> | ||
|
|
||
| <Heading level={ 1 }>{ confirmVal }</Heading> | ||
|
|
||
| <Button variant="primary" onClick={ () => setIsOpen( true ) }> | ||
| Open ConfirmDialog | ||
| </Button> | ||
| </> | ||
| ); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.