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(overlays): focus is not moved if active element is in overlay #25481

Merged
merged 8 commits into from
Jun 16, 2022
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
8 changes: 7 additions & 1 deletion core/src/utils/overlays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,13 @@ export const present = async (
focusPreviousElementOnDismiss(overlay.el);
}

if (overlay.keyboardClose) {
/**
* If the focused element is already
* inside the overlay component then
* focus should not be moved from that
* to the overlay container.
*/
if (overlay.keyboardClose && (document.activeElement === null || !overlay.el.contains(document.activeElement))) {
overlay.el.focus();
}
};
Expand Down
29 changes: 29 additions & 0 deletions core/src/utils/test/overlays/overlays.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from '@playwright/test';
import { test } from '@utils/test/playwright';

test.describe('overlays: focus', () => {
test('should not focus the overlay container if element inside of overlay is focused', async ({ page }, testInfo) => {
test.skip(testInfo.project.metadata.rtl === true, 'RTL tests are not needed as layout is not checked');

await page.setContent(`
<ion-button id="open-modal">Show Modal</ion-button>
<ion-modal trigger="open-modal">
<ion-content>
<ion-input autofocus="true"></ion-input>
</ion-content>
</ion-modal>
`);

const ionModalDidPresent = await page.spyOnEvent('ionModalDidPresent');
const button = page.locator('ion-button');
const input = page.locator('ion-input');

await button.click();
await input.evaluate((el: HTMLIonInputElement) => el.setFocus());

await ionModalDidPresent.next();
await page.waitForChanges();

expect(page.locator('ion-input input')).toBeFocused();
});
});