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

Make dialog focus async when removing open attribute #49929

Merged
merged 1 commit into from
Jan 6, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,34 @@
<script src="/resources/testdriver-vendor.js"></script>

<button>button</button>
<dialog>hello world</dialog>
<dialog>
<button>button in dialog</button>
</dialog>

<script>
const dialog = document.querySelector('dialog');
const button = document.querySelector('button');
const dialogbutton = document.querySelector('dialog > button');

promise_test(async t => {
button.focus();
dialog.showModal();
assert_equals(document.activeElement, dialogbutton,
'<button> in <dialog> should be focused after opening.');

let closeFired = false;
let cancelFired = false;
dialog.addEventListener('close', () => closeFired = true);
dialog.addEventListener('cancel', () => cancelFired = true);

dialog.removeAttribute('open');
assert_equals(document.activeElement, dialogbutton,
'<button> in <dialog> should still be focused immediately after removing open.');
await new Promise(resolve => t.step_timeout(resolve, 0));
await new Promise(requestAnimationFrame);

assert_equals(document.activeElement, button,
'Previously focused element should be focused after waiting for a task.');
assert_false(dialog.matches(':modal'),
'The dialog should not match :modal after closing.');
assert_false(cancelFired,
Expand All @@ -40,17 +50,24 @@
}, 'Removing the open attribute from an open modal dialog should run the closing algorithm.');

promise_test(async t => {
button.focus();
dialog.show();
assert_equals(document.activeElement, dialogbutton,
'<button> in <dialog> should be focused after opening.');

let closeFired = false;
let cancelFired = false;
dialog.addEventListener('close', () => closeFired = true);
dialog.addEventListener('cancel', () => cancelFired = true);

dialog.removeAttribute('open');
assert_equals(document.activeElement, dialogbutton,
'<button> in <dialog> should still be focused immediately after removing open.');
await new Promise(resolve => t.step_timeout(resolve, 0));
await new Promise(requestAnimationFrame);

assert_equals(document.activeElement, button,
'Previously focused element should be focused after waiting for a task.');
assert_false(cancelFired,
'The cancel event should not fire when removing the open attribute.');
assert_true(closeFired,
Expand Down