Skip to content

Commit ed07f83

Browse files
committed
fix(alert): fix alert new
1 parent 13f9454 commit ed07f83

File tree

7 files changed

+46
-237
lines changed

7 files changed

+46
-237
lines changed
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
1-
21
<div class="alerts-page">
3-
2+
43
<pf-alert variant="default">
5-
<pf-icon icon="bell" slot="icon"></pf-icon>
6-
<h3 slot="title">Custom alert title</h3>
7-
</pf-alert>
4+
<h3 slot="title">Custom alert title</h3>
5+
</pf-alert>
86

97
<pf-alert variant="info">
10-
<pf-icon icon="info-circle" slot="icon"></pf-icon>
118
<h3 slot="title">Info alert title</h3>
129
</pf-alert>
1310

1411
<pf-alert variant="success">
15-
<pf-icon icon="check-circle" slot="icon"></pf-icon>
1612
<h3 slot="title">Success alert title</h3>
1713
</pf-alert>
1814

1915
<pf-alert variant="warning">
20-
<pf-icon icon="exclamation-triangle" slot="icon"></pf-icon>
2116
<h3 slot="title">Warning alert title</h3>
2217
</pf-alert>
2318

2419
<pf-alert variant="danger">
25-
<pf-icon icon="exclamation-circle" slot="icon"></pf-icon>
2620
<h3 slot="title">Danger alert title</h3>
2721
</pf-alert>
2822
</div>
2923

3024
<script type="module">
3125
import '@patternfly/elements/pf-alert/pf-alert.js';
26+
import '@patternfly/elements/pf-icon/pf-icon.js';
27+
3228
</script>
3329
<style>
34-
.alerts-page pf-alert::part(container) {
30+
.alerts-page pf-alert::part(container) {
3531
background-color: #fff;
3632
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
3733
}
38-
</style>
39-
34+
</style>

elements/pf-alert/demo/alert-timeout.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
<alert-demo-page></alert-demo-page>
2-
3-
<div id="toast-alerts-container"></div>
1+
<pf-button>Add </pf-button>
2+
<pf-button></pf-button>
43

54
<script type="module">
65
import '@patternfly/elements/pf-alert/pf-alert.js';

elements/pf-alert/demo/custom-icons.html

Whitespace-only changes.

elements/pf-alert/demo/inline-types.html

Lines changed: 0 additions & 74 deletions
This file was deleted.

elements/pf-alert/demo/truncated-alerts.html

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,22 @@
22

33
<pf-alert variant="info">
44
<pf-icon icon="info-circle" slot="icon"></pf-icon>
5-
<h3 slot="title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur pellentesque neque cursus enim
6-
fringilla tincidunt. Proin lobortis aliquam dictum. Nam vel ullamcorper nulla, nec blandit dolor. Vivamus
7-
pellentesque neque justo, nec accumsan nulla rhoncus id. Suspendisse mollis, tortor quis faucibus volutpat, sem
8-
leo fringilla turpis, ac lacinia augue metus in nulla. Cras vestibulum lacinia orci. Pellentesque sodales
9-
consequat interdum. Sed porttitor tincidunt metus nec iaculis. Pellentesque non commodo justo. Morbi feugiat
10-
rhoncus neque, vitae facilisis diam aliquam nec. Sed dapibus vitae quam at tristique. Nunc vel commodo mi. Mauris
11-
et rhoncus leo.
5+
<h3 slot="title">Info alert:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur pellentesque neque
6+
cursus...
127
</h3>
138
</pf-alert>
149

1510
<pf-alert variant="warning">
1611
<pf-icon icon="exclamation-triangle" slot="icon"></pf-icon>
1712
<h3 slot="title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur pellentesque neque cursus enim
18-
fringilla tincidunt. Proin lobortis aliquam dictum. Nam vel ullamcorper nulla, nec blandit</h3>
13+
fringilla tincidunt. Proin lobortis aliquam dictum. Nam vel ullamcorper nulla, nec blandit...</h3>
1914
</pf-alert>
2015

2116
<pf-alert variant="danger">
2217
<pf-icon icon="exclamation-circle" slot="icon"></pf-icon>
2318
<h3 slot="title">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur pellentesque neque cursus enim
2419
fringilla tincidunt. Proin lobortis aliquam dictum. Nam vel ullamcorper nulla, nec blandit dolor. Vivamus
25-
pellentesque neque justo, nec accumsan nulla rhoncus id. Suspendisse mollis,</h3>
20+
pellentesque neque justo, nec accumsan nulla rhoncus id. Suspendisse mollis,...</h3>
2621
</pf-alert>
2722
</div>
2823

elements/pf-alert/pf-alert.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ header {
192192
padding-inline-start: var(--pf-alert--PaddingLeft, 1rem);
193193
}
194194

195+
195196
#container #description {
196197

197198
grid-area: description;

elements/pf-alert/pf-alert.ts

Lines changed: 32 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,59 @@
11
import { LitElement, html, type TemplateResult, type PropertyValues } from 'lit';
22
import { customElement } from 'lit/decorators/custom-element.js';
3-
import { state } from 'lit/decorators.js';
43
import { property } from 'lit/decorators/property.js';
54
import { classMap } from 'lit/directives/class-map.js';
65
import { SlotController } from '@patternfly/pfe-core/controllers/slot-controller.js';
6+
import '@patternfly/elements/pf-icon/pf-icon.js';
77
import styles from './pf-alert.css';
88

9-
export type AlertVariant = (
10-
| 'default'
11-
| 'info'
12-
| 'warning'
13-
| 'danger'
14-
| 'success'
15-
);
16-
17-
interface AlertAction {
18-
action: 'dismiss' | 'confirm' | string;
19-
text: string;
20-
}
21-
interface ToastOptions {
22-
id?: string;
23-
children: string | TemplateResult;
24-
title?: string;
25-
variant?: AlertVariant;
26-
timeout?: number;
27-
actionLinks?: AlertAction[];
28-
}
29-
309

31-
32-
const toasts = new Set<Required<ToastOptions>>();
10+
const variantToIcon = (variant: PfAlert['variant']): string | undefined => {
11+
switch (variant) {
12+
case 'info':
13+
return 'circle-info';
14+
case 'success':
15+
return 'circle-check';
16+
case 'warning':
17+
return 'triangle-exclamation';
18+
case 'danger':
19+
return 'circle-exclamation';
20+
case 'neutral':
21+
return 'bell';
22+
case 'custom':
23+
default:
24+
return undefined;
25+
}
26+
};
3327

3428
@customElement('pf-alert')
3529
export class PfAlert extends LitElement {
3630
static readonly styles: CSSStyleSheet[] = [styles];
3731

38-
@property({ reflect: true }) variant?: AlertVariant;
32+
@property({ reflect: true })
33+
variant:
34+
| 'warning'
35+
| 'custom'
36+
| 'neutral'
37+
| 'info'
38+
| 'success'
39+
| 'danger' = 'neutral';
3940

4041
@property() icon?: string;
4142

4243
#slots = new SlotController(this, null, 'icon', 'actionClose', 'title', 'actionLinks');
4344

4445

45-
46-
private static readonly TIMEOUT_MS = 8000;
47-
private static _toastContainer: HTMLElement | null = null;
48-
private static _instanceCounter = 0;
49-
50-
public static async toast(options: ToastOptions): Promise<PfAlert> {
51-
if (!PfAlert._toastContainer) {
52-
PfAlert._toastContainer = document.getElementById('toast-alerts-container');
53-
if (!PfAlert._toastContainer) {
54-
throw new Error("Toast container '#toast-alerts-container' not found in DOM.");
55-
}
56-
}
57-
58-
const alertElement = document.createElement('pf-alert') as PfAlert;
59-
PfAlert._instanceCounter++;
60-
alertElement.variant = options.variant || 'default';
61-
alertElement.icon = options.variant === 'default' ? 'bell' :
62-
options.variant || 'bell';
63-
if (options.title) {
64-
const title = document.createElement('h3'); title.slot = 'title'; title.textContent = options.title;
65-
alertElement.appendChild(title);
66-
}
67-
if (typeof options.children === 'string') {
68-
const p = document.createElement('p'); p.textContent = options.children;
69-
alertElement.appendChild(p);
70-
}
71-
72-
if (options.actionLinks && options.actionLinks.length > 0) {
73-
const actionsDiv = document.createElement('div'); actionsDiv.slot = 'actionLinks';
74-
options.actionLinks.forEach(actionLinks => {
75-
const button = document.createElement('pf-button'); button.setAttribute('variant', 'link');
76-
button.textContent = actionLinks.text;
77-
switch (actionLinks.text) {
78-
case 'View details':
79-
button.addEventListener('click', () => {
80-
PfAlert._toastContainer!.innerHTML = '';
81-
});
82-
break;
83-
case 'Ignore':
84-
// button.addEventListener('click', () => { });
85-
break;
86-
default:
87-
button.addEventListener('click', () => { alertElement.remove(); });
88-
break;
89-
}
90-
actionsDiv.appendChild(button);
91-
});
92-
alertElement.appendChild(actionsDiv);
93-
}
94-
95-
PfAlert._toastContainer!.prepend(alertElement);
96-
97-
98-
if (!options.timeout) {
99-
let timer: number;
100-
const startTimer = () => { timer = window.setTimeout(() => alertElement.remove(), PfAlert.TIMEOUT_MS); };
101-
alertElement.addEventListener('mouseenter', () => window.clearTimeout(timer));
102-
alertElement.addEventListener('mouseleave', startTimer);
103-
startTimer();
104-
}
105-
return alertElement;
106-
}
107-
108-
109-
110-
111-
override willUpdate(changed: PropertyValues<this>): void {
112-
if (changed.has('icon') && this.icon) {
113-
import('@patternfly/elements/pf-icon/pf-icon.js');
114-
}
115-
}
116-
117-
11846
override render(): TemplateResult<1> {
119-
const { variant, icon } = this;
120-
const hasIcon = !!icon || this.#slots.hasSlotted('icon');
47+
const { variant } = this;
48+
const calculatedIcon = this.icon ?? variantToIcon(variant);
49+
const hasIcon = !!calculatedIcon || this.#slots.hasSlotted('icon');
12150
return html`
12251
<div id="container" part="container"
12352
class=${classMap({ hasIcon, [variant ?? '']: !!variant })}>
12453
12554
<div id="icon-container" part="icon-container">
126-
<slot name="icon" part="icon">${!this.icon ? '' : html`
127-
<pf-icon icon="${this.icon}"></pf-icon>`}
55+
<slot name="icon" part="icon">${!calculatedIcon ? '' : html`
56+
<pf-icon icon="${calculatedIcon}"></pf-icon>`}
12857
</slot>
12958
</div>
13059
@@ -148,43 +77,7 @@ export class PfAlert extends LitElement {
14877
`;
14978
}
15079
}
151-
@customElement('alert-demo-page')
152-
export class AlertDemoPage extends LitElement {
153-
154-
@state() private alertCounter = 0;
155-
private addToastAlert() {
156-
this.alertCounter++;
157-
const id = this.alertCounter;
158-
159-
PfAlert.toast({
160-
variant: 'default',
161-
title: `Default timeout Alert`,
162-
children: `This alert will dismiss after 8 seconds.`,
163-
actionLinks: [
164-
{ action: 'view', text: 'View details' },
165-
{ action: 'ignore', text: 'Ignore' },
166-
],
167-
});
168-
}
169-
private removeAllToastAlerts() {
170-
const container = document.getElementById('toast-alerts-container');
171-
if (container) {
172-
container.innerHTML = '';
173-
}
174-
}
175-
override render(): TemplateResult {
176-
return html`
177-
<div class="demo-controls">
178-
<pf-button variant="control" @click="${this.addToastAlert}">
179-
Add alert
180-
</pf-button>
181-
<pf-button variant="control" @click="${this.removeAllToastAlerts}">
182-
Remove all alerts
183-
</pf-button>
184-
</div>
185-
`;
186-
}
187-
}
80+
18881

18982
declare global {
19083
interface HTMLElementTagNameMap {

0 commit comments

Comments
 (0)