Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/idiomorph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
35 changes: 35 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<dialog><h1>Foo</h1></dialog>";
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 = "<dialog><h1>Foo</h1></dialog>";
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()",
);
});
});
});
Loading