Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 17 additions & 5 deletions core/src/utils/overlays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const activeAnimations = new WeakMap<OverlayInterface, Animation[]>();

type OverlayWithFocusTrapProps = HTMLIonOverlayElement & {
focusTrap?: boolean;
keyboardClose?: boolean;
showBackdrop?: boolean;
backdropBreakpoint?: number;
};
Expand Down Expand Up @@ -527,10 +528,16 @@ export const cleanupRootFocusTrapAccessibility = () => {
* below, which have to stay in lockstep.
*/
const applyRootLock = (el: OverlayWithFocusTrapProps) => {
const viewContainer = getViewContainer();
// Hiding the container the overlay now sits in would hide the overlay too.
if (!getViewContainer()?.contains(el)) {
const containsOverlay = !!viewContainer?.contains(el);
// With keyboardClose false, focus stays in the page, so the page stays visible.
const keepsPageFocus = el.keyboardClose === false && !!viewContainer?.contains(document.activeElement);

if (!containsOverlay && !keepsPageFocus) {
setRootAriaHidden(true);
}

document.body.classList.add(BACKDROP_NO_SCROLL);
};

Expand Down Expand Up @@ -579,7 +586,7 @@ export const present = async <OverlayPresentOptions>(
* from returning focus as a result.
*/
if (overlay.el.tagName !== 'ION-TOAST') {
restoreElementFocus(overlay.el);
restoreElementFocus(overlay.el, overlay.keyboardClose);
}

/**
Expand Down Expand Up @@ -672,14 +679,19 @@ export const present = async <OverlayPresentOptions>(
* to where they were before they
* opened the overlay.
*/
const restoreElementFocus = async (overlayEl: any) => {
const restoreElementFocus = async (overlayEl: any, keyboardClose?: boolean) => {
let previousElement = document.activeElement as HTMLElement | null;
if (!previousElement) {
return;
}

// Ensure active element is blurred to prevent a11y warning issues
previousElement.blur();
/**
* Blur the active element to prevent a11y warnings, unless the overlay
* opted out of closing the keyboard, since blurring would close it.
*/
if (keyboardClose !== false) {
previousElement.blur();
}
Comment on lines +688 to +694

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • Skipping only the blur() leaves the input focused inside the view container, and applyRootLock still sets aria-hidden="true" on that container.
  • With the markup from the new tests on 9.0.15, Chrome then logs "Blocked aria-hidden on an element because its descendant retained focus", the warning fix(overlays,picker): remove invalid aria-hidden attribute #30563 added this blur() to avoid. Safari doesn't block it, so there the focused input stays inside a hidden subtree.
  • With keyboardClose={false} the user keeps working in the page, so the page should stay visible while focus is in it. Skipping the container's aria-hidden in that case keeps focus on the input with no warning.

Both spots are outside the diff, so please update OverlayWithFocusTrapProps and applyRootLock like this:

type OverlayWithFocusTrapProps = HTMLIonOverlayElement & {
  focusTrap?: boolean;
  keyboardClose?: boolean;
  showBackdrop?: boolean;
  backdropBreakpoint?: number;
};

const applyRootLock = (el: OverlayWithFocusTrapProps) => {
  const viewContainer = getViewContainer();
  // Hiding the container the overlay now sits in would hide the overlay too.
  const containsOverlay = !!viewContainer?.contains(el);
  // With keyboardClose false, focus stays in the page, so the page stays visible.
  const keepsPageFocus = el.keyboardClose === false && !!viewContainer?.contains(document.activeElement);

  if (!containsOverlay && !keepsPageFocus) {
    setRootAriaHidden(true);
  }

  document.body.classList.add(BACKDROP_NO_SCROLL);
};


const shadowRoot = previousElement?.shadowRoot;
if (shadowRoot) {
Expand Down
108 changes: 108 additions & 0 deletions core/src/utils/test/overlays/overlays.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,5 +651,113 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>

await expect(page.locator('#last')).toBeFocused();
});

test('should keep focus on the input while presenting a popover with keyboardClose false', async ({ page }) => {
test.info().annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/31199',
});
// Nest the input under the app root so the popover locks it, as in a real app.
await page.setContent(
`
<ion-app>
<div id="ion-view-container-root">
<input id="test-input" />
</div>
</ion-app>
<ion-popover keyboard-close="false">
<ion-content>Popover Content</ion-content>
</ion-popover>
`,
config
);

const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent');
const popover = page.locator('ion-popover');
const input = page.locator('#test-input');

await input.click();
await expect(input).toBeFocused();

await popover.evaluate((el: HTMLIonPopoverElement) => el.present());
await ionPopoverDidPresent.next();

// The input keeps focus while the popover is presented
await expect(input).toBeFocused();
Comment thread
thetaPC marked this conversation as resolved.
Resolved
await expect(page.locator('#ion-view-container-root')).not.toHaveAttribute('aria-hidden', 'true');

// Typing still goes into the input
await page.keyboard.press('a');
await expect(input).toHaveValue('a');
});

test('should blur the input when presenting a popover with the default keyboardClose', async ({ page }) => {
test.info().annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/31199',
});
await page.setContent(
`
<ion-popover>
<ion-content>Popover Content</ion-content>
</ion-popover>
<input id="test-input" />
`,
config
);

const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent');
const popover = page.locator('ion-popover');
const input = page.locator('#test-input');

await input.click();
await expect(input).toBeFocused();

await popover.evaluate((el: HTMLIonPopoverElement) => el.present());
await ionPopoverDidPresent.next();

// The overlay takes focus, so it is removed from the input
await expect(input).not.toBeFocused();
});

test('should return focus to the input when dismissing a popover with keyboardClose false', async ({ page }) => {
test.info().annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/31199',
});
// Nest the input under the app root so the popover locks it, as in a real app.
await page.setContent(
`
<ion-app>
<div id="ion-view-container-root">
<input id="test-input" />
</div>
</ion-app>
<ion-popover keyboard-close="false">
<ion-content>Popover Content</ion-content>
</ion-popover>
`,
config
);

const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent');
const ionPopoverDidDismiss = await page.spyOnEvent('ionPopoverDidDismiss');
const popover = page.locator('ion-popover');
const input = page.locator('#test-input');

await input.click();
await expect(input).toBeFocused();

await popover.evaluate((el: HTMLIonPopoverElement) => el.present());
await ionPopoverDidPresent.next();

await expect(input).toBeFocused();

await popover.evaluate((el: HTMLIonPopoverElement) => el.dismiss());
await ionPopoverDidDismiss.next();

// Focus is restored to the input after the popover is dismissed
await expect(input).toBeFocused();
});
});
});
Loading