Skip to content
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

fix(alert): pause auto-close alert when link focused #9503

Merged
merged 11 commits into from
Jun 5, 2024
13 changes: 13 additions & 0 deletions packages/calcite-components/src/components/alert/alert.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,19 @@ describe("calcite-alert", () => {
await page.waitForTimeout(DURATIONS.medium + animationDurationInMs);
await page.waitForSelector("#alert", { visible: false });
});

it("pauses on focus and resumes on unfocus", async () => {
await button.click();
expect(await alert.isVisible()).toBe(true);
expect(await alert.getProperty("autoCloseDuration")).toEqual("medium");
expect(playState).toEqual("running");
alert.focus();
await page.waitForTimeout(DURATIONS.medium);
expect(await alert.isVisible()).toBe(true);
await button.focus();
await page.waitForTimeout(DURATIONS.medium + animationDurationInMs);
await page.waitForSelector("#alert", { visible: false });
});
});

describe("translation support", () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/calcite-components/src/components/alert/alert.scss
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,16 @@ $alertDurations:
:host([auto-close-duration="#{$name}"]) .dismiss-progress:after {
animation: dismissProgress $duration ease-out;
}
:host(:hover[auto-close-duration="#{$name}"]) .dismiss-progress:after {
:host(:hover[auto-close-duration="#{$name}"]) .dismiss-progress:after,
:host(:focus[auto-close-duration="#{$name}"]) .dismiss-progress:after {
Copy link
Member

Choose a reason for hiding this comment

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

I think these can be combined like this:

:host(:hover[auto-close-duration="#{$name}"]) .dismiss-progress:after,
:host(:focus[auto-close-duration="#{$name}"]) .dismiss-progress:after {
    animation-play-state: paused;
  }

animation-play-state: paused;
}
}

.container.focused .dismiss-progress:after {
animation-play-state: paused;
}

@keyframes dismissProgress {
0% {
@apply w-0 opacity-75;
Expand Down
41 changes: 38 additions & 3 deletions packages/calcite-components/src/components/alert/alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export class Alert implements OpenCloseComponent, LoadableComponent, T9nComponen
}),
);
window.clearTimeout(this.autoCloseTimeoutId);
this.autoCloseTimeoutId = null;
window.clearTimeout(this.queueTimeout);
disconnectLocalized(this);
disconnectMessages(this);
Expand All @@ -226,13 +227,18 @@ export class Alert implements OpenCloseComponent, LoadableComponent, T9nComponen
[CSS.containerQueued]: queued,
[`${CSS.container}--${placement}`]: true,
[CSS.containerSlottedInShell]: this.slottedInShell,
["focused"]: this.keyBoardFocus,
Copy link
Member

Choose a reason for hiding this comment

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

@josercarcamo can you add the focused class to the CSS object?

}}
onPointerEnter={this.autoClose && this.autoCloseTimeoutId ? this.handleMouseOver : null}
onPointerLeave={this.autoClose && this.autoCloseTimeoutId ? this.handleMouseLeave : null}
onPointerLeave={this.autoClose ? this.handleMouseLeave : null}
ref={this.setTransitionEl}
>
{effectiveIcon && this.renderIcon(effectiveIcon)}
<div class={CSS.textContainer}>
<div
class={CSS.textContainer}
onFocusin={this.autoClose && this.autoCloseTimeoutId ? this.handleKeyBoardFocus : null}
driskull marked this conversation as resolved.
Show resolved Hide resolved
onFocusout={this.autoClose ? this.handleKeyBoardUnfocus : null}
>
<slot name={SLOTS.title} />
<slot name={SLOTS.message} />
<slot name={SLOTS.link} />
Expand All @@ -246,6 +252,18 @@ export class Alert implements OpenCloseComponent, LoadableComponent, T9nComponen
);
}

private handleKeyBoardFocus = (): void => {
this.keyBoardFocus = true;
driskull marked this conversation as resolved.
Show resolved Hide resolved
this.handleFocus();
};

private handleKeyBoardUnfocus = (): void => {
this.keyBoardFocus = false;
if (!this.mouseFocus) {
this.handleBlur();
}
};

private renderCloseButton(): VNode {
return (
<button
Expand Down Expand Up @@ -429,6 +447,8 @@ export class Alert implements OpenCloseComponent, LoadableComponent, T9nComponen
/** is the alert queued */
@State() queued = false;

@State() keyBoardFocus = false;

private autoCloseTimeoutId: number = null;

private closeButton: HTMLButtonElement;
Expand All @@ -447,6 +467,8 @@ export class Alert implements OpenCloseComponent, LoadableComponent, T9nComponen

transitionEl: HTMLDivElement;

mouseFocus: boolean;

//--------------------------------------------------------------------------
//
// Private Methods
Expand Down Expand Up @@ -510,12 +532,25 @@ export class Alert implements OpenCloseComponent, LoadableComponent, T9nComponen
};

private handleMouseOver = (): void => {
this.mouseFocus = true;
this.handleFocus();
};

private handleMouseLeave = (): void => {
this.mouseFocus = false;
if (!this.keyBoardFocus) {
this.handleBlur();
}
};

private handleFocus = (): void => {
window.clearTimeout(this.autoCloseTimeoutId);
this.autoCloseTimeoutId = null;
this.totalOpenTime = Date.now() - this.initialOpenTime;
this.lastMouseOverBegin = Date.now();
};

private handleMouseLeave = (): void => {
private handleBlur = (): void => {
const hoverDuration = Date.now() - this.lastMouseOverBegin;
const timeRemaining =
DURATIONS[this.autoCloseDuration] - this.totalOpenTime + this.totalHoverTime;
Expand Down
Loading