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

refactor(modal, panel, popover)!: remove focusId param from setFocus method. #6147

Merged
merged 8 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 0 additions & 8 deletions src/components/modal/modal.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ describe("calcite-modal accessibility checks", () => {
const createModalHTML = (contentHTML?: string, attrs?: string) =>
`<calcite-modal open ${attrs}>${contentHTML}</calcite-modal>`;

const closeButtonFocusId = "close-button";
const closeButtonTargetSelector = ".close";
const focusableContentTargetClass = "test";

Expand All @@ -326,20 +325,13 @@ describe("calcite-modal accessibility checks", () => {

it("focuses close button by default", async () =>
focusable(createModalHTML(focusableContentHTML), {
focusId: closeButtonFocusId,
shadowFocusTargetSelector: closeButtonTargetSelector
}));

it("focuses content if there is no close button", async () =>
focusable(createModalHTML(focusableContentHTML, "close-button-disabled"), {
focusTargetSelector: `.${focusableContentTargetClass}`
}));

it.skip("can focus close button directly", async () =>
focusable(createModalHTML(focusableContentHTML), {
focusId: closeButtonFocusId,
shadowFocusTargetSelector: closeButtonTargetSelector
}));
});

it("has correct aria role/attribute", async () => {
Expand Down
16 changes: 3 additions & 13 deletions src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
VNode,
Watch
} from "@stencil/core";
import { ensureId, focusElement, getSlotted } from "../../utils/dom";
import { ensureId, focusFirstTabbable, getSlotted } from "../../utils/dom";
import { Kind, Scale } from "../interfaces";
import { CSS, ICONS, SLOTS } from "./resources";
import { createObserver } from "../../utils/observers";
Expand All @@ -28,7 +28,6 @@ import {
connectFocusTrap,
activateFocusTrap,
deactivateFocusTrap,
focusFirstTabbable,
updateFocusTrapElements
} from "../../utils/focusTrapComponent";
import {
Expand Down Expand Up @@ -381,20 +380,11 @@ export class Modal
*
* By default, tries to focus on focusable content. If there is none, it will focus on the close button.
* To focus on the close button, use the `close-button` focus ID.
*
* @param focusId
*/
@Method()
async setFocus(focusId?: "close-button"): Promise<void> {
async setFocus(): Promise<void> {
await componentLoaded(this);

const { closeButtonEl } = this;

if (closeButtonEl && focusId === "close-button") {
return focusElement(closeButtonEl);
}

focusFirstTabbable(this);
focusFirstTabbable(this.focusTrapEl);
}

/**
Expand Down
31 changes: 3 additions & 28 deletions src/components/panel/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Watch
} from "@stencil/core";
import { CSS, ICONS, SLOTS } from "./resources";
import { toAriaBoolean } from "../../utils/dom";
import { focusFirstTabbable, toAriaBoolean } from "../../utils/dom";
import { Scale } from "../interfaces";
import { HeadingLevel, Heading } from "../functional/Heading";
import { SLOTS as ACTION_MENU_SLOTS } from "../action-menu/resources";
Expand Down Expand Up @@ -320,36 +320,11 @@ export class Panel

/**
* Sets focus on the component.
*
* @param focusId
*/
@Method()
async setFocus(focusId?: "back-button" | "dismiss-button"): Promise<void> {
async setFocus(): Promise<void> {
await componentLoaded(this);

const { backButtonEl, closeButtonEl, containerEl } = this;

if (focusId === "back-button") {
backButtonEl?.setFocus();
return;
}

if (focusId === "dismiss-button") {
closeButtonEl?.setFocus();
return;
}

if (backButtonEl) {
backButtonEl.setFocus();
return;
}

if (closeButtonEl) {
closeButtonEl.setFocus();
return;
}

containerEl?.focus();
focusFirstTabbable(this.containerEl);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/components/popover/popover.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,19 +720,17 @@ describe("calcite-popover", () => {
const createPopoverHTML = (contentHTML?: string, attrs?: string) =>
`<calcite-popover open ${attrs} reference-element="ref">${contentHTML}</calcite-popover><button id="ref">Button</button>`;

const closeButtonFocusId = "close-button";

const contentButtonClass = "my-button";
const contentHTML = `<button class="${contentButtonClass}">My Button</button>`;
const contentHTML = "Hello World!";
const buttonContentHTML = `<button class="${contentButtonClass}">My Button</button>`;

it("should focus content by default", async () =>
focusable(createPopoverHTML(contentHTML), {
focusable(createPopoverHTML(buttonContentHTML), {
focusTargetSelector: `.${contentButtonClass}`
}));

it("should focus close button", async () =>
focusable(createPopoverHTML(contentHTML, "closable"), {
focusId: closeButtonFocusId,
shadowFocusTargetSelector: `.${CSS.closeButton}`
}));
});
Expand Down
24 changes: 6 additions & 18 deletions src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ import {
connectFocusTrap,
activateFocusTrap,
deactivateFocusTrap,
focusFirstTabbable,
updateFocusTrapElements
} from "../../utils/focusTrapComponent";

import { guid } from "../../utils/guid";
import { queryElementRoots, toAriaBoolean } from "../../utils/dom";
import { focusFirstTabbable, queryElementRoots, toAriaBoolean } from "../../utils/dom";
import {
OpenCloseComponent,
connectOpenCloseComponent,
Expand Down Expand Up @@ -385,23 +384,12 @@ export class Popover

/**
* Sets focus on the component.
*
* @param focusId
*/
@Method()
async setFocus(focusId?: "close-button"): Promise<void> {
async setFocus(): Promise<void> {
await componentLoaded(this);

const { closeButtonEl } = this;

if (focusId === "close-button" && closeButtonEl) {
forceUpdate(closeButtonEl);
closeButtonEl.setFocus();

return;
}

focusFirstTabbable(this);
forceUpdate(this);
Copy link
Member

Choose a reason for hiding this comment

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

this should be forceUpdate(this.el) right?

focusFirstTabbable(this.focusTrapEl);
}

/**
Expand Down Expand Up @@ -552,7 +540,7 @@ export class Popover
renderCloseButton(): VNode {
const { messages, closable } = this;
return closable ? (
<div class={CSS.closeButtonContainer}>
<div class={CSS.closeButtonContainer} key="close-button-container">
Copy link
Member

Choose a reason for hiding this comment

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

Can you use CSS.closeButtonContainer for the key attribute instead of hardcoding it

<calcite-action
class={CSS.closeButton}
onClick={this.hide}
Expand All @@ -575,7 +563,7 @@ export class Popover
) : null;

return headingNode ? (
<div class={CSS.header}>
<div class={CSS.header} key="header">
Copy link
Member

@benelan benelan Dec 21, 2022

Choose a reason for hiding this comment

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

Reuse the string from resources here as well por favor

{headingNode}
{this.renderCloseButton()}
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/tests/commonTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ export async function hidden(componentTagOrHTML: TagOrHTML): Promise<void> {
}

interface FocusableOptions {
/** use this to pass an ID to setFocus() */
/**
* use this to pass an ID to setFocus()
*
* @deprecated components should no longer use a focusId parameter for setFocus()
*/
focusId?: string;

/**
Expand Down
13 changes: 13 additions & 0 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { CSS_UTILITY } from "./resources";
import { guid } from "./guid";
import { tabbable } from "tabbable";

export const tabbableOptions = {
getShadowRoot: true
};
/**
* This helper will guarantee an ID on the provided element.
*
Expand Down Expand Up @@ -165,6 +169,15 @@ export async function focusElement(el: FocusableElement): Promise<void> {
return isCalciteFocusable(el) ? el.setFocus() : el.focus();
}

/**
* Helper to focus the first tabbable element.
*
* @param {HTMLElement} element The html element containing tabbable elements.
*/
export function focusFirstTabbable(element: HTMLElement): void {
(tabbable(element, tabbableOptions)[0] || element).focus();
}

interface GetSlottedOptions {
all?: boolean;
direct?: boolean;
Expand Down
22 changes: 2 additions & 20 deletions src/utils/focusTrapComponent.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import {
FocusTrap as _FocusTrap,
Options as FocusTrapOptions,
createFocusTrap,
FocusTrapTabbableOptions
} from "focus-trap";
import { FocusTrap as _FocusTrap, Options as FocusTrapOptions, createFocusTrap } from "focus-trap";
import { FocusableElement, focusElement } from "./dom";
import { tabbable } from "tabbable";

const tabbableOptions: FocusTrapTabbableOptions = {
getShadowRoot: true
};
import { tabbableOptions } from "./dom";

const trapStack: _FocusTrap[] = [];

Expand Down Expand Up @@ -92,15 +83,6 @@ export function deactivateFocusTrap(component: FocusTrapComponent): void {
component.focusTrap?.deactivate();
}

/**
* Helper to focus the first tabbable element within the FocusTrap component.
*
* @param {FocusTrapComponent} component The FocusTrap component.
*/
export function focusFirstTabbable(component: FocusTrapComponent): void {
tabbable(component.focusTrapEl, tabbableOptions)[0]?.focus();
}

/**
* Helper to update the element(s) that are used within the FocusTrap component.
*
Expand Down