Skip to content

Commit 3e4df95

Browse files
Dispatch DialogClosed event when clicking X button (#1828)
Related #1814. Stacked on #1827. This PR changes the control flow when clicking the `X` button in the `<overlay-panel>`: instead of hiding the panel directly, it now dispatches a `dialog-closed` event into the slotted element, to give the slotted element a chance to hook into this event and react to it. We need this in [a subsequent PR (the network status dialog)](#1829), which needs to stop its internal update loop once the dialog terminates. This also must work if the user clicks the `X` button, not just when using the `Close` one. <a data-ca-tag href="https://codeapprove.com/pr/tiny-pilot/tinypilot/1828"><img src="https://codeapprove.com/external/github-tag-allbg.png" alt="Review on CodeApprove" /></a> --------- Co-authored-by: Jan Heuermann <[email protected]>
1 parent 69519d8 commit 3e4df95

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

app/templates/custom-elements/overlay-panel.html

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@
6161
/>
6262
</svg>
6363
</button>
64-
<slot></slot>
64+
<slot id="dialog-slot"></slot>
6565
</dialog>
6666
</template>
6767

6868
<script type="module">
69+
import { DialogClosedEvent } from "/js/events.js";
70+
6971
(function () {
7072
const template = document.querySelector("#overlay-panel-template");
7173

@@ -99,10 +101,21 @@
99101
}
100102
}
101103
);
104+
// Dispatch dialog-closed event into slotted element(s), to allow the
105+
// dialog to react to it if need be (e.g., for performing clean-ups).
106+
// In theory, there could be multiple elements in the slot, so they
107+
// all need to receive the close event – hence the `forEach`. In
108+
// reality, we usually only slot a single dialog element, though. It
109+
// shouldn’t matter anyway, since the dialog-closed event handler is
110+
// idempotent.
102111
this.shadowRoot
103-
.getElementById("close-button")
104-
.addEventListener("click", () => this.show(false));
105-
112+
.querySelector("#close-button")
113+
.addEventListener("click", () =>
114+
this.shadowRoot
115+
.querySelector("#dialog-slot")
116+
.assignedElements()
117+
.forEach((el) => el.dispatchEvent(new DialogClosedEvent()))
118+
);
106119
// Prevent auto-close behavior of native <dialog> if the user presses
107120
// the ESC key.
108121
this._elements.dialog.addEventListener("cancel", (evt) => {

0 commit comments

Comments
 (0)