Confirm Dialog
Used to interrupt the user with a mandatory confirmation or action.
Import
import { ConfirmProvider, useConfirm } from '@pluralsight/react'
Anatomy
- Dialog Container
- Header (optional)
- Heading (optional)
- Body
- Footer
- Cancel Button
- Confirm Button
Usage
Non-destructive
Use a non-destructive Confirm Dialog when you need a confirmation that does not result in a permanent destructive behavior of the database.
If you need to interrupt the user with non-critical information, use a Toast instead. Confirm Dialogs should only be used for critical information which requires a user's confirmation and should be a rare use case.
import { Button, ConfirmProvider, useConfirm } from '@pluralsight/react'
function PaymentFeature() {
const { confirm } = useConfirm()
async function handleShowAlert() {
try {
const consent = await confirm({
heading: 'Confirm payment',
text: 'Are you sure you want to make this payment?',
})
if (consent) {
// fetch request to make payment
}
} catch (error) {
// handle error
}
}
return <Button onClick={handleShowAlert}>Confirm payment</Button>
}
export default function PaymentsPage() {
return (
<div>
<ConfirmProvider>
<PaymentFeature />
</ConfirmProvider>
<div>Some other content like a historical list of payments.</div>
</div>
)
}
Destructive
When you need a confirmation from a user that will result in a destructive action, just add the kind
prop to the options with the value of destructive
.
import { Button, ConfirmProvider, useConfirm } from '@pluralsight/react'
function PaymentFeature() {
const { confirm } = useConfirm()
async function handleShowAlert() {
try {
const consent = await confirm({
heading: 'Delete payment method',
kind: 'destructive',
text: 'Are you sure you want to remove this payment method? This will be a permanent action.',
})
if (consent) {
// fetch request to make payment
}
} catch (error) {
// handle error
}
}
return (
<Button sentiment="danger" onClick={handleShowAlert}>
Confirm payment
</Button>
)
}
export default function PaymentsPage() {
return (
<div>
<ConfirmProvider>
<PaymentFeature />
</ConfirmProvider>
<div>Some other content like a historical list of payments.</div>
</div>
)
}
Customizing
You can customize the Confirm Dialog in the following ways:
1. Unused Classes
Each component layer of the Confirm has a unused class name that can be utilized in your local CSS to customize the Confirm at any level.
pando-alert
: The element of the Confirm Alert.pando-alert-header
: The header element of the Alert.pando-alert-icon
: The heading icon element of the Alert.pando-alert-heading
: The heading element of the Alert.pando-alert-body
: The content element of the Alert.pando-alert-footer
: The footer element of the Alert.pando-alert-cancel
: The cancel button element of the Alert.pando-alert-confirm
: The confirm button element of the Alert.
3. Ejected Confirm Dialog
For a low-level "ejected" approach, you can use the Headless-styles API to customize the Confirm Alert however you prefer while keeping the accessibility behavior.
import {
getAlertDialogProps,
getAlertDialogHeaderProps,
getAlertDialogHeadingProps,
getAlertDialogBodyProps,
getAlertDialogFooterProps,
} from '@pluralsight/headless-styles'
To learn more about the Headless-styles API, check out the Headless-styles documentation.
Behavior
Confirm dialogs are to be used to interrupt the user's task with important, critical, or warning information. This is the Pluralsight themed version of a browser's native window.confirm()
function.
Patterns
Do use the Confirm Dialog when you need the user to confirm a decision.
Don't use the Modal when you need the user to confirm a decision.
Do keep Confirm Dialog layout unaltered.
Don't manipulate or add any additional elements/styles to the Confirm Dialog.
Usage
Do use sparingly in a user experience.
Don't abuse the use of this Dialog by using multiple times within a page.
API
ConfirmProvider
The ConfirmProvider
should NOT be used at the top level of your application. It should be used at the top level of the feature that needs a Confirm Dialog for better performance.
Paramters
useConfirm
interface ContextProps {
confirm: (options: ConfirmDialogProps) => Promise<boolean>
}
function useConfirm(): ContextProps
Parameters
interface ConfirmDialogProps {
bodyId?: string
headingId?: string
heading?: string
kind?: 'non-destructive' | 'destructive'
text: string
}
bodyId
: Theid
of the body element of the Confirm Dialog. A defaultid
will be generated if not provided.headingId
: Theid
of the heading element of the Confirm Dialog. A defaultid
will be generated if not provided.heading
: The text of the heading element of the Confirm Dialog.kind
: The kind of Confirm Dialog. The value can be eithernon-destructive
ordestructive
.text
: The text of the body element of the Confirm Dialog.
Accessibility
The Pando Confirm Dialog is fully accessible and screen-readable through the following features:
- The Confirm Dialog uses the
dialog
element for native browser a11y support. - The
dialog
element uses the role ofalertdialog
to indicate to screen readers that the dialog contains important information. - The
dialog
element uses thearia-modal
attribute to indicate to screen readers that the dialog is modal. - The
dialog
element uses thearia-labelledby
attribute to indicate to screen readers that the dialog has a heading. - The
dialog
element uses thearia-describedby
attribute to indicate to screen readers that the dialog has a body. - The Dialog API automatically focuses back on the trigger when the Dialog is closed.
- The Dialog traps focus within the Dialog when it is open.