Prompt Dialog
Used to interrupt the user with a mandatory validation or action.
Import
import { PromptProvider, usePrompt } from '@pluralsight/react'
Anatomy
- Dialog Container
- Header (optional)
- Heading (optional)
- Body
- Label (hidden or visible)
- Input
- Footer
- Cancel Button
- Confirm Button
Usage
Non-destructive
Use a non-destructive Prompt Dialog when you need a validation that does not result in a destructive behavior.
import { Button, PromptProvider, usePrompt } from '@pluralsight/react'
import Container from '../Container/Container.js'
function PromptFeature() {
const { prompt } = usePrompt()
const CONFIRM_KEY = 'CONFIRM'
async function handleOnClick() {
try {
const validation = await prompt({
heading: 'Validation required.',
text: 'This action requires additional validation.',
promptValidationKey: CONFIRM_KEY,
})
if (validation === CONFIRM_KEY) {
// do something
}
} catch (error) {
// handle error
}
}
return <Button onClick={handleOnClick}>Update role</Button>
}
export default function PromptDialog() {
return (
<Container>
<PromptProvider>
<PromptFeature />
</PromptProvider>
</Container>
)
}
Destructive
When you need a confirmation from a user that will result in a destructive action, just add the kind
option with a value of destructive
to the prompt
function.
import { Button, PromptProvider, usePrompt } from '@pluralsight/react'
import Container from '../Container/Container.js'
function PromptFeature() {
const { prompt } = usePrompt()
const CONFIRM_KEY = 'DELETE'
async function handleOnClick() {
try {
const validation = await prompt({
heading: 'Delete Channel',
kind: 'destructive',
text: 'Are you sure you want to delete this channel? This action cannot be undone.',
promptValidationKey: CONFIRM_KEY,
})
if (validation === CONFIRM_KEY) {
// do something
}
} catch (error) {
// handle error
}
}
return (
<Button sentiment="danger" onClick={handleOnClick}>
Delete channel
</Button>
)
}
export default function PromptDialog() {
return (
<Container>
<PromptProvider>
<PromptFeature />
</PromptProvider>
</Container>
)
}
Customizing
You can customize the Prompt Dialog in the following ways:
1. Unused Classes
Each component layer of the Prompt has a unused class name that can be utilized in your local CSS to customize the Prompt at any level.
pando-alert
: The element of the Prompt 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-label
: The label element of the Alert.pando-alert-input
: The input 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 Prompt Dialog
For a low-level "ejected" approach, you can use the Headless-styles API to customize the Prompt 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
Prompt 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.prompt()
function.
Patterns
Do use the Prompt Dialog when you need the user to validate a decision.
Don't use the Modal when you need the user to validate a decision.
Do keep Prompt Dialog layout unaltered.
Don't manipulate or add any additional elements/styles to the Prompt 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
PromptProvider
The PromptProvider
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 Prompt Dialog for better performance.
Paramters
usePrompt
The hook returns a string value of either "cancel" or the promptValidationKey
value.
interface ContextProps {
prompt: (options: PromptDialogProps) => Promise<string>
}
function usePrompt(): ContextProps
Parameters
interface PromptDialogProps {
bodyId?: string
headingId?: string
heading?: string
kind?: 'non-destructive' | 'destructive'
promptValidationKey?: string
text: string
}
bodyId
: Theid
of the content element of the Prompt Dialog. A defaultid
will be generated if not provided.headingId
: Theid
of the heading element of the Prompt Dialog. A defaultid
will be generated if not provided.heading
: The text of the heading element of the Prompt Dialog.kind
: The kind of Prompt Dialog. The value can be eithernon-destructive
ordestructive
.promptValidationKey
: The value that will determine the forminvalid
state and returned on successful submission.text
: The text of the content element of the Prompt Dialog.
Accessibility
The Pando Prompt Dialog is fully accessible and screen-readable through the following features:
- The Prompt 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 content 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.