Skip to content

fix(alert): properly form a queue of alerts #10032

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 21 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions packages/calcite-components/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ActionBarMessages } from "./components/action-bar/assets/action-bar/t9n
import { Columns } from "./components/action-group/interfaces";
import { ActionGroupMessages } from "./components/action-group/assets/action-group/t9n";
import { ActionPadMessages } from "./components/action-pad/assets/action-pad/t9n";
import { AlertDuration, AlertQueue, Sync } from "./components/alert/interfaces";
import { AlertDuration, AlertQueue } from "./components/alert/interfaces";
import { NumberingSystem } from "./utils/locale";
import { AlertMessages } from "./components/alert/assets/alert/t9n";
import { HeadingLevel } from "./components/functional/Heading";
Expand Down Expand Up @@ -111,7 +111,7 @@ export { ActionBarMessages } from "./components/action-bar/assets/action-bar/t9n
export { Columns } from "./components/action-group/interfaces";
export { ActionGroupMessages } from "./components/action-group/assets/action-group/t9n";
export { ActionPadMessages } from "./components/action-pad/assets/action-pad/t9n";
export { AlertDuration, AlertQueue, Sync } from "./components/alert/interfaces";
export { AlertDuration, AlertQueue } from "./components/alert/interfaces";
export { NumberingSystem } from "./utils/locale";
export { AlertMessages } from "./components/alert/assets/alert/t9n";
export { HeadingLevel } from "./components/functional/Heading";
Expand Down Expand Up @@ -510,6 +510,10 @@ export namespace Components {
"setFocus": () => Promise<void>;
}
interface CalciteAlert {
/**
* This internal property, managed by the AlertManager, is used to inform the component if it is the active open Alert.
*/
"active": boolean;
/**
* When `true`, the component closes automatically. Recommended for passive, non-blocking alerts.
*/
Expand Down Expand Up @@ -557,6 +561,10 @@ export namespace Components {
* When `true`, displays and positions the component.
*/
"open": boolean;
/**
* This internal property, managed by the AlertManager, is used to inform the component of how many alerts are currently open.
*/
"openAlertCount": number;
/**
* Specifies the placement of the component.
*/
Expand All @@ -571,6 +579,7 @@ export namespace Components {
"scale": Scale;
/**
* Sets focus on the component's "close" button, the first focusable item.
* @returns
*/
"setFocus": () => Promise<void>;
}
Expand Down Expand Up @@ -6348,8 +6357,6 @@ declare global {
"calciteAlertClose": void;
"calciteAlertBeforeOpen": void;
"calciteAlertOpen": void;
"calciteInternalAlertSync": Sync;
"calciteInternalAlertRegister": void;
}
interface HTMLCalciteAlertElement extends Components.CalciteAlert, HTMLStencilElement {
addEventListener<K extends keyof HTMLCalciteAlertElementEventMap>(type: K, listener: (this: HTMLCalciteAlertElement, ev: CalciteAlertCustomEvent<HTMLCalciteAlertElementEventMap[K]>) => any, options?: boolean | AddEventListenerOptions): void;
Expand Down Expand Up @@ -8411,6 +8418,10 @@ declare namespace LocalJSX {
"scale"?: Scale;
}
interface CalciteAlert {
/**
* This internal property, managed by the AlertManager, is used to inform the component if it is the active open Alert.
*/
"active"?: boolean;
/**
* When `true`, the component closes automatically. Recommended for passive, non-blocking alerts.
*/
Expand Down Expand Up @@ -8470,18 +8481,14 @@ declare namespace LocalJSX {
* Fires when the component is open and animation is complete.
*/
"onCalciteAlertOpen"?: (event: CalciteAlertCustomEvent<void>) => void;
/**
* Fires when the component is added to DOM - used to receive initial queue.
*/
"onCalciteInternalAlertRegister"?: (event: CalciteAlertCustomEvent<void>) => void;
/**
* Fires to sync queue when opened or closed.
*/
"onCalciteInternalAlertSync"?: (event: CalciteAlertCustomEvent<Sync>) => void;
/**
* When `true`, displays and positions the component.
*/
"open"?: boolean;
/**
* This internal property, managed by the AlertManager, is used to inform the component of how many alerts are currently open.
*/
"openAlertCount"?: number;
/**
* Specifies the placement of the component.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import AlertManager, { alertQueueTimeoutMs } from "./AlertManager";

describe("AlertManager", () => {
let alertManager: AlertManager;
let mockAlert1: HTMLCalciteAlertElement;
let mockAlert2: HTMLCalciteAlertElement;

beforeEach(() => {
alertManager = new AlertManager();

mockAlert1 = {
active: false,
queue: "last",
openAlertCount: 0,
} as HTMLCalciteAlertElement;

mockAlert2 = {
active: false,
queue: "last",
openAlertCount: 0,
} as HTMLCalciteAlertElement;
});

it("should activate the first alert after a timeout", async () => {
alertManager.registerElement(mockAlert1);

expect(mockAlert1.active).toBe(false);

await new Promise((resolve) => setTimeout(resolve, alertQueueTimeoutMs));

expect(mockAlert1.active).toBe(true);
});

it("should deactivate subsequent alerts", async () => {
alertManager.registerElement(mockAlert1);
alertManager.registerElement(mockAlert2);

expect(mockAlert1.active).toBe(false);
expect(mockAlert2.active).toBe(false);

await new Promise((resolve) => setTimeout(resolve, alertQueueTimeoutMs));

expect(mockAlert1.active).toBe(true);
expect(mockAlert2.active).toBe(false);
});

it("should set the openAlertCount correctly", () => {
alertManager.registerElement(mockAlert1);
alertManager.registerElement(mockAlert2);

expect(mockAlert1.openAlertCount).toBe(2);
expect(mockAlert2.openAlertCount).toBe(2);
});
});
73 changes: 73 additions & 0 deletions packages/calcite-components/src/components/alert/AlertManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
export const alertQueueTimeoutMs = 300;

export default class AlertManager {
// --------------------------------------------------------------------------
//
// Private Properties
//
// --------------------------------------------------------------------------

private registeredElements: HTMLCalciteAlertElement[] = [];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, this could be a map

 private registeredElementsMap: Map<string, HTMLCalciteAlertElement[]> = new Map([
    ["default", []],
  ]);

This would allow us to have alert groups by adding a group property to Alert. Then only certain alerts would be grouped with other ones.


private queueTimeoutId: number = null;

// --------------------------------------------------------------------------
//
// Public Methods
//
// --------------------------------------------------------------------------

registerElement(alert: HTMLCalciteAlertElement): void {
const { registeredElements } = this;

if (!registeredElements.includes(alert)) {
switch (alert.queue) {
case "immediate":
registeredElements.unshift(alert);
break;
case "next":
registeredElements.splice(1, 0, alert);
break;
case "last":
registeredElements.push(alert);
break;
}

this.updateAlerts();
}
}

unregisterElement(alert: HTMLCalciteAlertElement): void {
const { registeredElements } = this;

const index = registeredElements.indexOf(alert);

if (index !== -1) {
registeredElements.splice(index, 1);
}

alert.active = false;
this.updateAlerts();
}

// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------

private updateAlerts(): void {
window.clearTimeout(this.queueTimeoutId);
this.queueTimeoutId = null;

this.registeredElements.forEach((alert, index) => {
alert.openAlertCount = this.registeredElements.length;

if (index === 0) {
this.queueTimeoutId = window.setTimeout(() => (alert.active = true), alertQueueTimeoutMs);
} else {
alert.active = false;
}
});
}
}
5 changes: 3 additions & 2 deletions packages/calcite-components/src/components/alert/alert.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getElementXY, skipAnimations } from "../../tests/utils";
import { openClose } from "../../tests/commonTests";
import { CSS, DURATIONS } from "./resources";

const animationDurationInMs = 400;

describe("defaults", () => {
defaults("calcite-alert", [
{
Expand Down Expand Up @@ -129,11 +131,10 @@ describe("calcite-alert", () => {
expect(await alert2.isVisible()).toBe(true);

await page.waitForTimeout(alertSpeedFastMs);
await page.waitForTimeout(animationDurationInMs);
expect(await alert2.isVisible()).not.toBe(true);
});

const animationDurationInMs = 400;

it("opens and then closes a single alert", async () => {
const page = await newE2EPage();
await page.setContent(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ $border-style: 1px solid var(--calcite-color-border-3);
}

:host([open]) {
.container:not(.container--queued) {
.container--active {
@apply border-t-2 opacity-100;
pointer-events: initial;
&[class*="bottom"] {
Expand Down
Loading
Loading