{
if (open) {
if (event.key === "Escape") {
// Escape that cancels an IME composition is not a close.
if (isImeComposition(event)) return;
// Stop stacked (DOM-nested) modals from also closing: this
// handler is on each modal's own root and Escape bubbles from
// the focused (innermost, topmost) modal up through ancestors.
event.stopPropagation();
close("escape-key");
} else if (event.key === "Tab" && !event.defaultPrevented) {
// A nested modal or a child that uses Tab itself (an editor that
// indents) already handled this Tab; trapping again moves focus twice.
trapFocus({ container: /** @type {HTMLDivElement} */ (ref), event });
} else if (
shouldSubmitOnEnter &&
event.key === "Enter" &&
!primaryButtonDisabled &&
!primaryBusy
) {
// Enter that commits an IME composition is not a submit.
if (isImeComposition(event)) return;
const target = /** @type {HTMLElement | null} */ (event.target);
const tag = target?.tagName;
if (
tag === "BUTTON" ||
tag === "A" ||
tag === "TEXTAREA" ||
tag === "SELECT" ||
target?.isContentEditable
) {
return;
}
if (formId && primaryButtonRef) {
primaryButtonRef.click();
} else {
dispatch("submit");
dispatch("click:button--primary");
}
}
}
}}
on:click
on:mousedown={(event) => {
if (event.target === event.currentTarget) {
// A press on the backdrop would otherwise blur the focused control,
// dropping focus to even when the dialog stays open.
event.preventDefault();
outsideDismiss.pressOutside();
}
}}
on:mouseup={outsideDismiss.release}
on:mouseover
on:mouseenter
on:mouseleave
on:transitionend={(event) => {
if (event.propertyName === "transform") {
dispatch("transitionend", { open });
if (!open) focusReturn.restore(ref);
}
}}
>
{
// Keep a newly-focused element (e.g. via Tab) from being hidden
// under the scroll gradient at the bottom of the content area.
if (event.target instanceof HTMLElement) {
scrollIntoViewWithin(event.target, ".bx--modal-content");
}
}}
>
{#if renderContent}
{/if}