mirror of
https://github.com/lexmount/moli.git
synced 2026-11-24 16:02:53 +00:00
fix(dialog): apply modal focus fixup
This commit is contained in:
@@ -13,7 +13,7 @@ use moli_selector::stylo_flat_tree_heading_descendants;
|
||||
use super::super::{
|
||||
PageScriptHost, node::node_runtime_and_handle_from_object_or_detached, throw_dom_exception,
|
||||
};
|
||||
use super::focus::run_dialog_focusing_steps;
|
||||
use super::focus::{apply_modal_dialog_focus_fixup, run_dialog_focusing_steps};
|
||||
use super::popover::popover_is_open;
|
||||
use super::toggle_event::queue_element_toggle_event;
|
||||
use super::{
|
||||
@@ -528,6 +528,7 @@ pub(super) fn dialog_show_modal_callback<'s>(
|
||||
return;
|
||||
}
|
||||
dialog_set_open_state_for_handle(scope, runtime_ptr, handle, true, true);
|
||||
apply_modal_dialog_focus_fixup(scope, runtime_ptr, handle);
|
||||
run_dialog_focusing_steps(scope, runtime_ptr, handle);
|
||||
}
|
||||
|
||||
|
||||
@@ -511,6 +511,22 @@ pub(super) fn run_dialog_focusing_steps(
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn apply_modal_dialog_focus_fixup(
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
runtime_ptr: *mut PageScriptHost,
|
||||
dialog: DomHandle,
|
||||
) {
|
||||
let should_clear_focus = {
|
||||
let runtime = unsafe { &*runtime_ptr };
|
||||
runtime
|
||||
.active_element_handle()
|
||||
.is_some_and(|active| !shadow_including_contains(runtime, dialog, active))
|
||||
};
|
||||
if should_clear_focus {
|
||||
update_focus(scope, runtime_ptr, None);
|
||||
}
|
||||
}
|
||||
|
||||
fn blur_if_focused_chain_is_display_none_callback(
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
_args: v8::FunctionCallbackArguments<'_>,
|
||||
|
||||
@@ -136,6 +136,35 @@ fn dialog_show_skips_hidden_and_nonsequential_descendants() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inert_modal_dialog_clears_focus_outside_its_subtree() {
|
||||
let mut vm = new_storage_test_vm("https://inert-modal-focus-fixup.test/");
|
||||
|
||||
let result = vm
|
||||
.eval(
|
||||
r#"
|
||||
(() => {
|
||||
const root = document.documentElement || document.appendChild(document.createElement('html'));
|
||||
const host = document.body || root.appendChild(document.createElement('body'));
|
||||
const outer = document.createElement('input');
|
||||
const dialog = document.createElement('dialog');
|
||||
dialog.inert = true;
|
||||
dialog.innerHTML = '<input autofocus>';
|
||||
host.append(outer, dialog);
|
||||
outer.focus();
|
||||
dialog.showModal();
|
||||
return JSON.stringify({
|
||||
focusedBody: document.activeElement === document.body,
|
||||
dialogOpen: dialog.open
|
||||
});
|
||||
})()
|
||||
"#,
|
||||
)
|
||||
.expect("inert modal focus-fixup probe should evaluate");
|
||||
|
||||
assert_eq!(result, r#"{"focusedBody":true,"dialogOpen":true}"#);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dialog_toggle_events_cancel_opening_and_coalesce_state_changes() {
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
|
||||
|
||||
Reference in New Issue
Block a user