|
| 1 | +## Description |
| 2 | + |
| 3 | +`sp-alert-dialog` displays important information that users need to acknowledge. When used directly the `sp-alert-dialog` element surfaces a `slot` based API for deep customization of the content to be included in the overlay. |
| 4 | + |
| 5 | +### Usage |
| 6 | + |
| 7 | +[](https://www.npmjs.com/package/@spectrum-web-components/alert-dialog) |
| 8 | +[](https://bundlephobia.com/result?p=@spectrum-web-components/alert-dialog) |
| 9 | + |
| 10 | +``` |
| 11 | +yarn add @spectrum-web-components/alert-dialog |
| 12 | +``` |
| 13 | + |
| 14 | +Import the side effectful registration of `<sp-alert-dialog>` via: |
| 15 | + |
| 16 | +``` |
| 17 | +import '@spectrum-web-components/alert-dialog/sp-alert-dialog.js'; |
| 18 | +``` |
| 19 | + |
| 20 | +When looking to leverage the `AlertDialog` base class as a type and/or for extension purposes, do so via: |
| 21 | + |
| 22 | +``` |
| 23 | +import { AlertDialog } from '@spectrum-web-components/alert-dialog'; |
| 24 | +``` |
| 25 | + |
| 26 | +## Variants |
| 27 | + |
| 28 | +### Confirmation |
| 29 | + |
| 30 | +This is the default variant for alert dialogs. Use a confirmation variant for asking a user to confirm a choice. |
| 31 | + |
| 32 | +```html |
| 33 | +<sp-alert-dialog variant="confirmation"> |
| 34 | + <h2 slot="heading">Disclaimer</h2> |
| 35 | + Smart filters are nondestructive and will preserve your original images. |
| 36 | + <sp-button |
| 37 | + slot="button" |
| 38 | + id="cancelButton" |
| 39 | + variant="secondary" |
| 40 | + treatment="outline" |
| 41 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 42 | + > |
| 43 | + Cancel |
| 44 | + </sp-button> |
| 45 | + <sp-button |
| 46 | + slot="button" |
| 47 | + id="confirmButton" |
| 48 | + variant="accent" |
| 49 | + treatment="fill" |
| 50 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 51 | + > |
| 52 | + Enable |
| 53 | + </sp-button> |
| 54 | +</sp-alert-dialog> |
| 55 | +``` |
| 56 | + |
| 57 | +### Information |
| 58 | + |
| 59 | +Information alert dialogs communicate important information that a user needs to acknowledge. Before using this kind of alert dialog, make sure it’s the appropriate communication channel for the message instead of a toast or a more lightweight messaging option. |
| 60 | + |
| 61 | +```html |
| 62 | +<sp-alert-dialog variant="information"> |
| 63 | + <h2 slot="heading">Connect to wifi</h2> |
| 64 | + Please connect to wifi to sync your projects or go to Settings to change |
| 65 | + your preferences. |
| 66 | + <sp-button |
| 67 | + slot="button" |
| 68 | + id="cancelButton" |
| 69 | + variant="secondary" |
| 70 | + treatment="outline" |
| 71 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 72 | + > |
| 73 | + Cancel |
| 74 | + </sp-button> |
| 75 | + <sp-button |
| 76 | + slot="button" |
| 77 | + id="confirmButton" |
| 78 | + variant="primary" |
| 79 | + treatment="outline" |
| 80 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 81 | + > |
| 82 | + Continue |
| 83 | + </sp-button> |
| 84 | +</sp-alert-dialog> |
| 85 | +``` |
| 86 | + |
| 87 | +### Warning |
| 88 | + |
| 89 | +Warning alert dialogs communicate important information to users in relation to an issue that needs to be acknowledged, but does not block the user from moving forward. |
| 90 | + |
| 91 | +```html |
| 92 | +<sp-alert-dialog variant="warning"> |
| 93 | + <h2 slot="heading">Unverified format</h2> |
| 94 | + This format has not been verified and may not be viewable for some users. Do |
| 95 | + you want to continue publishing? |
| 96 | + <sp-button |
| 97 | + slot="button" |
| 98 | + id="cancelButton" |
| 99 | + variant="secondary" |
| 100 | + treatment="outline" |
| 101 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 102 | + > |
| 103 | + Cancel |
| 104 | + </sp-button> |
| 105 | + <sp-button |
| 106 | + slot="button" |
| 107 | + id="confirmButton" |
| 108 | + variant="primary" |
| 109 | + treatment="outline" |
| 110 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 111 | + > |
| 112 | + Continue |
| 113 | + </sp-button> |
| 114 | +</sp-alert-dialog> |
| 115 | +``` |
| 116 | + |
| 117 | +### Error |
| 118 | + |
| 119 | +Error alert dialogs communicate critical information about an issue that a user needs to acknowledge. |
| 120 | + |
| 121 | +```html |
| 122 | +<sp-alert-dialog variant="error"> |
| 123 | + <h2 slot="heading">Unable to share</h2> |
| 124 | + An error occured while sharing your project. Please verify the email address |
| 125 | + and try again. |
| 126 | + <sp-button |
| 127 | + slot="button" |
| 128 | + id="confirmButton" |
| 129 | + variant="primary" |
| 130 | + treatment="outline" |
| 131 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 132 | + > |
| 133 | + Continue |
| 134 | + </sp-button> |
| 135 | +</sp-alert-dialog> |
| 136 | +``` |
| 137 | + |
| 138 | +### Destructive |
| 139 | + |
| 140 | +Destructive alert dialogs are for when a user needs to confirm an action that will impact their data or experience in a potentially negative way, such as deleting files or contacts. |
| 141 | + |
| 142 | +```html |
| 143 | +<sp-alert-dialog variant="destructive"> |
| 144 | + <h2 slot="heading">Delete 3 documents?</h2> |
| 145 | + Are you sure you want to delete the 3 selected documents? |
| 146 | + <sp-button |
| 147 | + slot="button" |
| 148 | + id="cancelButton" |
| 149 | + variant="secondary" |
| 150 | + treatment="outline" |
| 151 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 152 | + > |
| 153 | + Cancel |
| 154 | + </sp-button> |
| 155 | + <sp-button |
| 156 | + slot="button" |
| 157 | + id="confirmButton" |
| 158 | + variant="negative" |
| 159 | + treatment="fill" |
| 160 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 161 | + > |
| 162 | + Delete |
| 163 | + </sp-button> |
| 164 | +</sp-alert-dialog> |
| 165 | +``` |
| 166 | + |
| 167 | +### Secondary Button |
| 168 | + |
| 169 | +An alert dialog can have a total of 3 buttons if the secondary outline button label is defined. |
| 170 | + |
| 171 | +```html |
| 172 | +<sp-alert-dialog variant="secondary"> |
| 173 | + <h2 slot="heading">Rate this app</h2> |
| 174 | + If you enjoy our app, would you mind taking a moment to rate it? |
| 175 | + <sp-button |
| 176 | + slot="button" |
| 177 | + id="secondaryButton" |
| 178 | + variant="secondary" |
| 179 | + treatment="outline" |
| 180 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 181 | + > |
| 182 | + No, thanks |
| 183 | + </sp-button> |
| 184 | + <sp-button |
| 185 | + slot="button" |
| 186 | + id="cancelButton" |
| 187 | + variant="secondary" |
| 188 | + treatment="outline" |
| 189 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 190 | + > |
| 191 | + Remind me later |
| 192 | + </sp-button> |
| 193 | + <sp-button |
| 194 | + slot="button" |
| 195 | + id="confirmButton" |
| 196 | + variant="primary" |
| 197 | + treatment="outline" |
| 198 | + onclick="this.dispatchEvent(new Event('close', { bubbles: true, composed: true }));" |
| 199 | + > |
| 200 | + Rate now |
| 201 | + </sp-button> |
| 202 | +</sp-alert-dialog> |
| 203 | +``` |
0 commit comments