Abdulkader Safi

LaralCN-UI dialogs: Esc never reached my listener

Pressing Esc in dialog, alert-dialog, sheet or the mobile sidebar closed the panel, so it looked fine. Underneath, the exit animation never played, data-state stayed "open" and the page stayed locked so it could not scroll. The cause was one missing argument to addEventListener, and I only found it while building the command palette.

5 min read

Share
A LaralCN-UI dialog open over the docs page, with devtools listing the capture-phase cancel listener on document.

Pressing Esc in dialog, alert-dialog, sheet or the mobile sidebar closed the panel, so it looked fine. Underneath, the exit animation never played, data-state stayed "open" and the page stayed locked so it could not scroll. The cause was one missing argument to addEventListener, and I only found it while building the command palette.

What a user actually saw

All four components sit on a native <dialog> opened with showModal(). When one opens, the script sets overflow: hidden on <html> so the page behind it does not scroll. Closing is meant to go through a small close() function that flips data-state, gives the page its scroll back and waits for the exit transition before calling the native dialog.close():

function close(dialog) {
    if (dialog.dataset.state === 'closed') return;

    dialog.dataset.state = 'closed';
    document.documentElement.style.overflow = '';
    // Let the exit transition play out before leaving the top layer.
    setTimeout(function() {
        dialog.close();
    }, 200);
}

Clicking the close button or the backdrop went through close() and worked. Esc did not. The browser's own Esc handling removed the dialog at once, with no fade. data-state still said "open". And because close() never ran, <html> kept overflow: hidden, so after pressing Esc you were left on a page that would not scroll until you reloaded it.

cancel does not bubble

Every one of these components had the same listener, meant to catch Esc and route it through close():

// Esc: animate out instead of the instant native dismissal.
document.addEventListener('cancel', function(event) {
    if (!event.target.matches('[data-ui-dialog]')) return;
    event.preventDefault();
    close(event.target);
});

It never ran once. When you press Esc on a modal dialog, the browser fires cancel at the <dialog> element, and cancel is one of the events that does not bubble. A listener on document in the default bubble phase only hears events that travel back up from the target, so this one heard nothing. The preventDefault() never happened either, which is why the native close went ahead.

Capture is different. In the capture phase an event travels from the window down to its target, and it does that whether or not it bubbles. So the fix is the third argument:

// cancel does not bubble, so listen in the capture phase.
document.addEventListener('cancel', function(event) {
    if (!event.target.matches('[data-ui-dialog]')) return;
    event.preventDefault();
    close(event.target);
}, true);

The matches() check at the top stays. With four components all listening for cancel on document, each one has to ignore dialogs that are not its own, and its data attribute (data-ui-dialog, data-ui-sheet and so on) is how it tells them apart.

The same one-word change went into alert-dialog, sheet and sidebar. The sidebar calls its own toggle() on the provider instead of close(), but the listener had the identical problem.

How the command palette found it

The command palette (part 14) is a native <dialog> too, and I built it on the dialog's pattern, including the cancel listener. Testing it in Chrome, Esc closed the palette with no animation and left the docs page unable to scroll. Tracing that back led to the listener, and from there to the plain dialog and the other three components, which had the same line.

The bug had been there since the dialog's first version. It survived because the visible part worked: the dialog disappeared, which is the thing anyone checks when they press Esc. The scroll lock only shows on a page long enough to scroll, and the missing fade is easy to read as "Esc is instant".

The test suite could not catch it. SmokeRenderTest renders each component in a Testbench app and checks the HTML and the merged classes. It never runs JavaScript, never presses a key, and has no idea what overflow on <html> is after a close. Every new component in this round was checked in Chrome with keyboard and mouse, in English and Arabic, light and dark, and that manual pass is what caught it. After the fix, Esc animates out and scrolling comes back in all four components.

Getting the fix into your app

This is the cost of copy-and-own. The fix is in the registry, so anyone who runs ui:add today gets it, but files you installed earlier are your files and nothing updates them behind your back. You have to pull them again:

php artisan ui:diff
php artisan ui:add dialog
php artisan ui:add alert-dialog
php artisan ui:add sheet
php artisan ui:add sidebar

ui:diff lists the installed copies that differ from the registry. When ui:add hits an existing file that differs, it asks whether to skip, overwrite or show the diff first, which matters if you edited the component yourself. In that case the change to make by hand is the , true at the end of the cancel listener.

Where it is

composer require --dev safi/laralcn-ui
php artisan ui:init
php artisan ui:add dialog

The fix shipped in PR #7 and is listed in the v0.6.0 release notes. Docs: https://laralcn-ui.abdulkadersafi.com/components/dialog (Arabic at /ar/components/dialog). GitHub: https://github.com/Abdulkader-Safi/LaralCN-UI. MIT licensed.

Next: LaralCN-UI 0.6.0, the release that bundles the seven new components, this fix and the Arabic docs.

Last updated 1 Dec 2026 · filed under Blade, javascript, laralcn-ui, accessibility

Every entry on this project

26 build notes, in order

Including the ones where nothing worked. You are on part 20.

Read the series →

FAQ

Frequently asked questions

Do I have this bug if I installed dialog before v0.6.0?

Yes, if your copy's cancel listener has no true as its third argument. Run php artisan ui:diff to see which installed components differ from the registry.

Why not listen for keydown on Escape instead?

cancel is the event the browser fires when it is about to close a modal dialog, and calling preventDefault() on it stops the native close.

Which other events do not bubble?

focus, blur, load and scroll on elements are common ones, alongside the dialog's cancel. A capture-phase listener on document still sees all of them.

I edited my dialog component. How do I apply the fix without losing my changes?

Add true as the third argument to the document.addEventListener('cancel', ...) call. That one change is the whole fix.

Written by

Abdulkader Safi

Software Engineer

Lead engineer at DSRPT, from Lebanon and based in Kuwait. I write about the tools and bugs from real client work, with the numbers I measured.

About me → GitHub LinkedIn

Need this kind of work done on your project?

Start a project →

Keep reading

More from LaralCN-UI: shadcn-style Blade components and blocks you own

All 26 entries →