-
Notifications
You must be signed in to change notification settings - Fork 24
Web components : Add usa-alert component #271
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 8 commits
ac03c1c
c779e35
051da3b
a2f2fa5
5f50de2
98cdc2d
b30cbf9
be7808f
9613a36
637abff
044899a
e1abe06
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { UsaAlert } from "./usa-alert"; | ||
| import { UsaLink } from "./usa-link"; | ||
| import { UsaBanner } from "./usa-banner"; | ||
|
|
||
| export { UsaLink, UsaBanner }; | ||
| export { UsaAlert, UsaLink, UsaBanner }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { LitElement, html, css, unsafeCSS, nothing } from "lit"; | ||
| import { classMap } from "lit/directives/class-map.js"; | ||
|
|
||
| import styles from "./usa-alert.css"; | ||
| import iconClose from "../../shared/icons/close.svg"; | ||
| import { defineCustomElement } from "../../utils"; | ||
|
|
||
| const VALID_STATUSES = [ | ||
| "info", | ||
| "warning", | ||
| "error", | ||
| "success", | ||
| "emergency", | ||
| ] as const; | ||
|
|
||
| type AlertStatus = (typeof VALID_STATUSES)[number]; | ||
|
|
||
| /** | ||
| * @summary Displays important messages to the user with contextual status styling. | ||
| * | ||
| * @attribute {"info" | "warning" | "error" | "success" | "emergency"} status - Determines the icon and border/background color. | ||
| * @attribute {boolean} slim - Displays the slim variation (smaller icon, no heading). | ||
| * @attribute {boolean} no-icon - Hides the status icon. | ||
| * @attribute {boolean} closeable - Shows a close button. | ||
| * @attribute {string} close-label - Aria-label for the close button. Defaults to "Close alert". | ||
| * | ||
| * @cssprop --usa-alert-background-color - Override background color. | ||
| * @cssprop --usa-alert-border-color - Override left border color. | ||
| * @cssprop --usa-alert-icon-color - Override icon color. | ||
| * @cssprop --usa-alert-icon-size - Override icon size. | ||
| * @cssprop --usa-alert-padding-x - Override horizontal padding. | ||
| * @cssprop --usa-alert-padding-y - Override vertical padding. | ||
| * @cssprop --usa-alert-font-family - Override font family. | ||
| * @cssprop --usa-alert-text-color - Override text color. | ||
| * | ||
| * @slot headline - The alert heading (use an h-element: h2, h3, etc.). | ||
| * @slot - Default slot for alert body content. | ||
| * | ||
| * @fires close - Dispatched when the close button is clicked. | ||
| * | ||
| * @element usa-alert | ||
| */ | ||
| export class UsaAlert extends LitElement { | ||
| static properties = { | ||
| status: { type: String, reflect: true }, | ||
| slim: { type: Boolean, reflect: true }, | ||
| noIcon: { type: Boolean, attribute: "no-icon", reflect: true }, | ||
| closeable: { type: Boolean, reflect: true }, | ||
| closeLabel: { type: String, attribute: "close-label" }, | ||
| _visible: { state: true }, | ||
| }; | ||
|
|
||
| status!: AlertStatus; | ||
| slim!: boolean; | ||
| noIcon!: boolean; | ||
| closeable!: boolean; | ||
| closeLabel!: string; | ||
| _visible!: boolean; | ||
|
|
||
| constructor() { | ||
| super(); | ||
| this.status = "info"; | ||
| this.slim = false; | ||
| this.noIcon = false; | ||
| this.closeable = false; | ||
| this.closeLabel = "Close alert"; | ||
|
ethangardner marked this conversation as resolved.
|
||
| this._visible = true; | ||
| } | ||
|
|
||
| private get #role(): string { | ||
|
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. It appears that we don't have a build check running in CI, but when I built this locally with
Contributor
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. Fixed, I'll be sure to check for this in the future. |
||
| if ( | ||
| this.status === "error" || | ||
| this.status === "emergency" || | ||
| this.status === "warning" | ||
| ) { | ||
| return "alert"; | ||
| } | ||
| return "status"; | ||
| } | ||
|
|
||
| private _handleClose() { | ||
| this.dispatchEvent( | ||
| new CustomEvent("close", { bubbles: true, composed: 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. This is the first instance of a custom event in this project, and we don't have an established convention for naming them yet. I'll write an ADR for this in a follow up PR, but researching other design systems, the safest option in terms of avoiding a collision with other libraries is to prefix the event with the tag name i.e. That way, if someone uses
Contributor
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. Updated! |
||
| ); | ||
| this._visible = false; | ||
| } | ||
|
|
||
| static styles = [ | ||
| css` | ||
| :host { | ||
| --usa-icon-close: url("${unsafeCSS(iconClose)}"); | ||
| } | ||
| `, | ||
| styles, | ||
| ]; | ||
|
|
||
| render() { | ||
| if (!this._visible) { | ||
| return nothing; | ||
| } | ||
|
|
||
| const status = VALID_STATUSES.includes(this.status) ? this.status : "info"; | ||
|
|
||
| const classes = { | ||
| "usa-alert": true, | ||
| [`usa-alert--${status}`]: true, | ||
| "usa-alert--slim": this.slim, | ||
| "usa-alert--no-icon": this.noIcon, | ||
| }; | ||
|
|
||
| return html` | ||
| <div class="${classMap(classes)}" role="${this.#role}"> | ||
| <div class="usa-alert__body"> | ||
| ${!this.slim ? html`<slot name="headline"></slot>` : null} | ||
| <div class="usa-alert__text"> | ||
| <slot></slot> | ||
| </div> | ||
| </div> | ||
| ${this.closeable | ||
| ? html` | ||
| <button | ||
| class="usa-alert__close" | ||
| type="button" | ||
| aria-label="${this.closeLabel}" | ||
| @click="${this._handleClose}" | ||
| > | ||
| <span class="usa-alert__close-icon"></span> | ||
| </button> | ||
| ` | ||
| : null} | ||
| </div> | ||
| `; | ||
| } | ||
| } | ||
|
|
||
| defineCustomElement("usa-alert", UsaAlert); | ||
Uh oh!
There was an error while loading. Please reload this page.