Skip to content

Commit 43c789e

Browse files
authored
Merge pull request #1052 from Patternslib/close-panel-dialog
feat(pat close panel): Support for closing dialog panels.
2 parents e8839e1 + 7593048 commit 43c789e

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/pat/close-panel/close-panel.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ export default Base.extend({
77

88
init() {
99
this.el.addEventListener("click", (e) => {
10-
// Find the first element which has a close-panel.
10+
// Find the first element which has a close-panel or is a dialog.
1111
// This should the panel-root itself.
12-
const panel = this.el.closest(".has-close-panel");
12+
const panel = this.el.closest(".has-close-panel, dialog");
1313

1414
if (!panel) {
15-
// No ``.has-close-panel``. Exiting.
15+
// Nothing to do. Exiting.
1616
return;
17-
}
18-
19-
// Get the close panel method.
20-
const close_method = dom.get_data(panel, "close_panel");
17+
} else if (panel.tagName === "DIALOG") {
18+
// Close the dialog.
19+
panel.close();
20+
} else if (panel.classList.contains("has-close-panel")) {
21+
// Get the close panel method.
22+
const close_method = dom.get_data(panel, "close_panel");
2123

22-
// Now execute the method and close the panel.
23-
close_method && close_method(e);
24+
// Now execute the method and close the panel.
25+
close_method && close_method(e);
26+
}
2427
});
2528
},
2629
});

src/pat/close-panel/close-panel.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,22 @@ describe("pat close-panel", function () {
4040
await utils.timeout(1); // wait a tick async to settle.
4141
expect(document.querySelectorAll(".pat-modal").length).toBe(0);
4242
});
43+
44+
it("Closes a dialog's panel.", function () {
45+
document.body.innerHTML = `
46+
<dialog open>
47+
<button class="close-panel">close</button>
48+
</div>
49+
`;
50+
51+
registry.scan(document.body); // Also need to instantiate close-panel
52+
53+
const dialog = document.querySelector("dialog");
54+
55+
expect(dialog.open).toBe(true);
56+
57+
document.querySelector(".close-panel").click();
58+
59+
expect(dialog.open).toBe(false);
60+
});
4361
});

src/setup-tests.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ import dom from "./core/dom";
1414
dom.is_visible = (el) => {
1515
return !el.hidden;
1616
};
17+
18+
HTMLDialogElement.prototype.close = jest.fn().mockImplementation(function () {
19+
this.open = false;
20+
});

0 commit comments

Comments
 (0)