Skip to content

Commit 0210b99

Browse files
authored
Merge pull request #245 from hashicorp/alert/finalize-implementation
"Alert" + "Toast" components - Finalize implementation in code (feature branch)
2 parents 23c74d5 + d9c63b8 commit 0210b99

File tree

30 files changed

+2038
-3
lines changed

30 files changed

+2038
-3
lines changed

.changeset/stale-teachers-flash.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@hashicorp/design-system-components": minor
3+
---
4+
5+
- Added `Alert` component
6+
- Added `Toast` component
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<div class={{this.classNames}} role="alert" aria-live="polite" ...attributes>
2+
{{#if this.icon}}
3+
<div class="hds-alert__icon">
4+
<FlightIcon @name={{this.icon}} @size={{this.iconSize}} @stretched={{true}} @isInlineBlock={{false}} />
5+
</div>
6+
{{/if}}
7+
8+
<div class="hds-alert__content">
9+
<div class="hds-alert__text">
10+
{{#if @title}}
11+
<div class="hds-alert__title">{{@title}}</div>
12+
{{/if}}
13+
{{#if @description}}
14+
<div class="hds-alert__description">{{html-safe @description}}</div>
15+
{{/if}}
16+
</div>
17+
18+
<div class="hds-alert__actions">
19+
{{yield
20+
(hash
21+
Button=(component "hds/button" size="small")
22+
Link::Standalone=(component "hds/link/standalone" size="small")
23+
LinkTo::Standalone=(component "hds/link-to/standalone" size="small")
24+
)
25+
}}
26+
</div>
27+
{{yield (hash Generic=(component "hds/yield"))}}
28+
</div>
29+
30+
{{#if this.onDismiss}}
31+
<button class="hds-alert__dismiss" type="button" aria-label="Dismiss" {{on "click" this.onDismiss}}>
32+
<FlightIcon @name="x" @size="16" @isInlineBlock={{false}} />
33+
</button>
34+
{{/if}}
35+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import Component from '@glimmer/component';
2+
import { assert } from '@ember/debug';
3+
4+
export const TYPES = ['page', 'inline', 'compact'];
5+
export const DEFAULT_COLOR = 'neutral';
6+
export const COLORS = [
7+
'neutral',
8+
'highlight',
9+
'success',
10+
'warning',
11+
'critical',
12+
];
13+
export const MAPPING_COLORS_TO_ICONS = {
14+
neutral: 'info',
15+
highlight: 'info',
16+
success: 'check-circle',
17+
warning: 'alert-triangle',
18+
critical: 'alert-diamond',
19+
};
20+
21+
export default class HdsAlertIndexComponent extends Component {
22+
constructor() {
23+
super(...arguments);
24+
25+
assert(
26+
`@type for "Hds::Alert" must be one of the following: ${TYPES.join(
27+
', '
28+
)}; received: ${this.args.type}`,
29+
TYPES.includes(this.args.type)
30+
);
31+
}
32+
33+
/**
34+
* @param color
35+
* @type {enum}
36+
* @default neutral
37+
* @description Determines the color scheme for the alert.
38+
*/
39+
get color() {
40+
let { color = DEFAULT_COLOR } = this.args;
41+
42+
assert(
43+
`@color for "Hds::Alert" must be one of the following: ${COLORS.join(
44+
', '
45+
)}; received: ${color}`,
46+
COLORS.includes(color)
47+
);
48+
49+
return color;
50+
}
51+
52+
/**
53+
* @param icon
54+
* @type {string}
55+
* @default null
56+
* @description The name of the icon to be used.
57+
*/
58+
get icon() {
59+
let { icon } = this.args;
60+
61+
// If `icon` isn't passed, use the pre-defined one from `color`
62+
if (icon === undefined) {
63+
if (this.args.type === 'compact') {
64+
// for the "compact" type by default we use filled icons
65+
return `${MAPPING_COLORS_TO_ICONS[this.color]}-fill`;
66+
} else {
67+
// for all the other types by default we use outlined icons
68+
return MAPPING_COLORS_TO_ICONS[this.color];
69+
}
70+
// If `icon` is set explicitly to false, user doesn't want any icon in the alert
71+
} else if (icon === false) {
72+
assert(
73+
`@icon for "Hds::Alert" with @type "compact" is required`,
74+
this.args.type !== 'compact'
75+
);
76+
77+
return false;
78+
} else {
79+
// If a name for `icon` is passed, set FlightIcon to that name
80+
return icon;
81+
}
82+
}
83+
84+
/**
85+
* @param onDismiss
86+
* @type {function}
87+
* @default () => {}
88+
*/
89+
get onDismiss() {
90+
let { onDismiss } = this.args;
91+
92+
if (typeof onDismiss === 'function') {
93+
return onDismiss;
94+
} else {
95+
return false;
96+
}
97+
}
98+
99+
/**
100+
* @param iconSize
101+
* @type {string}
102+
* @description ensures that the correct icon size is used. Automatically calculated.
103+
*/
104+
get iconSize() {
105+
if (this.args.type === 'compact') {
106+
return '16';
107+
} else {
108+
return '24';
109+
}
110+
}
111+
112+
/**
113+
* Get the class names to apply to the component.
114+
* @method Alert#classNames
115+
* @return {string} The "class" attribute to apply to the component.
116+
*/
117+
get classNames() {
118+
let classes = ['hds-alert'];
119+
120+
// Add a class based on the @type argument
121+
classes.push(`hds-alert--type-${this.args.type}`);
122+
123+
// Add a class based on the @color argument
124+
classes.push(`hds-alert--color-${this.color}`);
125+
126+
return classes.join(' ');
127+
}
128+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Hds::Alert
2+
class="hds-toast"
3+
@type="inline"
4+
@color={{@color}}
5+
@icon={{@icon}}
6+
@title={{@title}}
7+
@description={{@description}}
8+
@onDismiss={{@onDismiss}}
9+
...attributes
10+
as |A|
11+
>
12+
{{yield
13+
(hash Button=A.Button Link::Standalone=A.Link::Standalone LinkTo::Standalone=A.LinkTo::Standalone Generic=A.Generic)
14+
}}
15+
</Hds::Alert>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div ...attributes>
2+
{{yield}}
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from '@hashicorp/design-system-components/components/hds/alert/index';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from '@hashicorp/design-system-components/components/hds/toast/index';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from '@hashicorp/design-system-components/components/hds/yield/index';

packages/components/app/styles/@hashicorp/design-system-components.scss

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@use "helpers/focus-ring";
66
@use "helpers/typography";
77

8+
@use "../components/alert";
89
@use "../components/badge";
910
@use "../components/badge-count";
1011
@use "../components/breadcrumb";
@@ -15,6 +16,7 @@
1516
@use "../components/icon-tile";
1617
@use "../components/link/cta";
1718
@use "../components/link/standalone";
19+
@use "../components/toast";
1820

1921
.sr-only {
2022
border: 0 !important;

0 commit comments

Comments
 (0)