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
14 changes: 14 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,20 @@ 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");
const link = await page.find(`#alert >>> calcite-link`);
await link.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: 7 additions & 0 deletions packages/calcite-components/src/components/alert/alert.scss
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ $alertDurations:
:host(:hover[auto-close-duration="#{$name}"]) .dismiss-progress:after {
animation-play-state: paused;
}
: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 {
Expand Down
40 changes: 38 additions & 2 deletions packages/calcite-components/src/components/alert/alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,13 @@ export class Alert implements OpenCloseComponent, LoadableComponent, T9nComponen
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.autoCloseTimeoutId ? this.handleKeyBoardUnfocus : null
}
>
<slot name={SLOTS.title} />
<slot name={SLOTS.message} />
<slot name={SLOTS.link} />
Expand All @@ -246,6 +252,20 @@ export class Alert implements OpenCloseComponent, LoadableComponent, T9nComponen
);
}

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

private handleKeyBoardUnfocus = (): void => {
this.transitionEl.classList.remove("focused");
this.keyBoardFocus = false;
if (!this.mouseFocus) {
this.handleUnfocus();
}
};

private renderCloseButton(): VNode {
return (
<button
Expand Down Expand Up @@ -447,6 +467,10 @@ export class Alert implements OpenCloseComponent, LoadableComponent, T9nComponen

transitionEl: HTMLDivElement;

keyBoardFocus: boolean;
driskull marked this conversation as resolved.
Show resolved Hide resolved

mouseFocus: boolean;

//--------------------------------------------------------------------------
//
// Private Methods
Expand Down Expand Up @@ -510,12 +534,24 @@ 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.handleUnfocus();
}
};

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

private handleMouseLeave = (): void => {
private handleUnfocus = (): void => {
Copy link
Member

Choose a reason for hiding this comment

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

maybe rename to handleBlur? focus/blur and focusin/focusout are the correct opposites I think

const hoverDuration = Date.now() - this.lastMouseOverBegin;
const timeRemaining =
DURATIONS[this.autoCloseDuration] - this.totalOpenTime + this.totalHoverTime;
Expand Down
Loading