-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ac03c1c
Move sass to devDeps, fix banner toggle, spacing
ericsorenson c779e35
Merge branch 'develop' into develop
ethangardner 051da3b
Merge remote-tracking branch 'upstream/develop' into develop
ericsorenson a2f2fa5
Add usa-alert component
ericsorenson 5f50de2
Use updateComplete helper in usa-alert tests
ericsorenson 98cdc2d
Simplify .usa-alert--slim left padding calc
ericsorenson b30cbf9
Use private #role getter and remove aria-hidden
ericsorenson be7808f
Delete add-usa-alert-component.md
ericsorenson 9613a36
Remove 'private' modifier from private getter
ericsorenson 637abff
Use CSS var for emergency text and rename field
ericsorenson 044899a
Consolidate info status tests
ericsorenson e1abe06
Use usa-alert-close event for alerts
ericsorenson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 usa-alert-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; | ||
| } | ||
|
|
||
| get #role(): string { | ||
| if ( | ||
| this.status === "error" || | ||
| this.status === "emergency" || | ||
| this.status === "warning" | ||
| ) { | ||
| return "alert"; | ||
| } | ||
| return "status"; | ||
| } | ||
|
|
||
| private _handleClose() { | ||
| this.dispatchEvent( | ||
| new CustomEvent("usa-alert-close", { bubbles: true, composed: true }), | ||
| ); | ||
| 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); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.