Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy Markdown
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}
Comment thread
driskull marked this conversation as 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;
Comment thread
driskull marked this conversation as 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;
Comment thread
driskull marked this conversation as resolved.
Outdated

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
Copy Markdown
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