Custom Modal - Prevent esc key dismissal
# package-development
w
Is it possible to have the custom modal of type dialog (not sidebar modal) to not be able to be dismissed with the escape
key
?
Is this possible or wishful thinking I can set some prop or something to prevent it
OK well I have hacked the planet as per usual. I am sure the HQ crew will tell me it's all wrong 🙈
Copy code
ts
import { css, customElement, html } from "@umbraco-cms/backoffice/external/lit";
import { UmbModalBaseElement } from "@umbraco-cms/backoffice/modal";
import { NoEscapeModalData, NoEscapeModalValue } from "./noescape.modal.token";
import { umbFocus } from "@umbraco-cms/backoffice/lit-element";

@customElement('no-escape-modal')
export class NoEscapeModalElement extends UmbModalBaseElement<NoEscapeModalData, NoEscapeModalValue>
{
    connectedCallback() {
        super.connectedCallback();
        document.addEventListener('keydown', this.handleKeyDown);
    }

    disconnectedCallback() {
        document.removeEventListener('keydown', this.handleKeyDown);
        super.disconnectedCallback();
    }

    private handleKeyDown = (event: KeyboardEvent) => {
        if (event.key === 'Escape') {
            event.preventDefault();
        }
    };


    private confirmTheThing() {
        console.log('Submitting modal and confirming the thing');
        this.updateValue({confimed:true});
        this._submitModal();
    }

    render() {
        return html`
            <uui-dialog-layout class="uui-text" headline="There is no escape">
                You can only perform one action that will dismiss this dialog, which is clicking the button below.
                If you try to escape by pressing the escape key, it will not work.

                <uui-button
                    slot="actions"
                    id="confirm"
                    color='danger'
                    look="primary"
                    label="Yes I give in"
                    @click=${this.confirmTheThing}
                    ${umbFocus()}></uui-button>

            </uui-dialog-layout>
        `;
    }
    
    static styles = css`
        uui-box {
            height:100%;
        }
    `;
}

export default NoEscapeModalElement;
But my custom modal element uses
connectedCallback
&
disconnectedCallback
to add an event listener for the escape key. if its pushed when this element (our custom modal) is present on the page. Then
event.preventDefault();
& cancel any other events that are listening for the event of the escape key being pushed. Is it the right way, probably not. Does it work, yep !
@Markus Johansson I see you posted an issue about this https://github.com/umbraco/Umbraco-CMS/issues/16835
Curious what your hack/approach was ?
m
@Warren Buckley at first I made some strange shadow-dom hack that replaced the “close”-event handler in the dialog, that stopped working after updating to 14.1.1 - but found something very similar to your approach in the core and went with that. So I think your hack is the best that can be done at the moment. Would be even better is there was an option when opening the modal 🙂
w
Yeh agree, be nice to have an option when opening the modal rather than this approach.
m
Yeeh, it would make sense to be able to pass down the settings when opening or on the modal token where the mode (sidebar/dialog) and size is configured.
9 Views