diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c926bb..a005e0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +* Changed: + * Invoke `HTMLDialogElement.close` instead of removing `[open]` attribute. (@seanpdoyle) + * Removed: * Move the htmx extension out of tree into [htmx-ext-morph](https://github.com/bigskysoftware/htmx-extensions/tree/main/src/morph). (@botandrose) #111 * Remove the undocumented `head.block` option. I hereby commit `Idiomorph.morph()` to being a sync function. Please open an issue if you miss this. (@botandrose) diff --git a/src/idiomorph.js b/src/idiomorph.js index 20fe99a..1a4d5ea 100644 --- a/src/idiomorph.js +++ b/src/idiomorph.js @@ -698,6 +698,14 @@ var Idiomorph = (function () { if (ignoreAttribute(oldAttribute.name, oldElt, "remove", ctx)) { continue; } + if ( + oldAttribute.name === "open" && + oldElt instanceof HTMLDialogElement && + newElt instanceof HTMLDialogElement + ) { + oldElt.close(); + continue; + } oldElt.removeAttribute(oldAttribute.name); } } diff --git a/test/core.js b/test/core.js index d543b5f..e3e2938 100644 --- a/test/core.js +++ b/test/core.js @@ -842,4 +842,39 @@ describe("Core morphing tests", function () { warn.called.should.equal(false); }); }); + + describe("open dialog elements", function () { + beforeEach(function () { + clearWorkArea(); + }); + + it("invokes HTMLDialogElement.close when removing [open] attribute", function () { + let html = "

Foo

"; + let dialog = make(html); + getWorkArea().append(dialog); + dialog.showModal(); + let close = sinon.spy(dialog, "close"); + Idiomorph.morph(dialog, html); + dialog.open.should.equal(false, "removes [open] attribute"); + close.calledOnce.should.equal(true, "invokes HTMLDialogElement.close()"); + }); + + it("does not invoke HTMLDialogElement.close when cancelling the removal of the [open] attribute", function () { + let html = "

Foo

"; + let dialog = make(html); + getWorkArea().append(dialog); + dialog.showModal(); + let close = sinon.spy(dialog, "close"); + Idiomorph.morph(dialog, html, { + callbacks: { + beforeAttributeUpdated: (attr, to, updatetype) => false, + }, + }); + dialog.open.should.equal(true, "retains [open] attribute"); + close.called.should.equal( + false, + "does not invoke HTMLDialogElement.close()", + ); + }); + }); });