jojo/web_src/js/modules/modal.ts
Gusted 555d88070d feat: migrate show-modal to native dialogs (#10287)
Test coverage:

|Modal|Test|
|-|-|
|admin: adopt unadopted|missing, not needed|
|admin: delete unadopted|missing, not needed|
|admin: delete user|e2e added: `Admin: delete a user`|
|delete package|missing|
|new project|?|
|edit project col|?|
|default project col|?|
|delete project col|?|
|commit cherry-pick|?|
|commit delete note|?|
|fork redirect|?|
|lock/unlock issue|?|
|dismiss PR review|?|
|migration delete|?|
|migration cancel|?|
|lfs delete|?|
|convert mirror|?|
|convert fork|?|
|transfer repo|?|
|delete repo|?|
|archive repo|integration present, selectors adjusted|
|delete wiki|?|
|rename wiki branch|?|
|push mirror edit|?|
|mde: new table|e2e present, selectors adjusted|
|mde: new link|e2e present, selectors adjusted|
|actions: add secret|?|
|actions: edit variable|?|

Co-authored-by: 0ko <0ko@noreply.codeberg.org>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10287
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
2026-05-03 06:42:14 +02:00

42 lines
1.5 KiB
TypeScript

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
// showModal will show the given modal and run `onApprove` if the approve/ok/yes
// button is pressed.
export function showModal(modalID: string | HTMLDialogElement, onApprove: () => void) {
let modal: HTMLDialogElement;
if (typeof modalID === 'string') {
modal = document.getElementById(modalID) as HTMLDialogElement;
} else {
modal = modalID;
}
// Move the modal to `<body>`, to avoid inheriting any bad CSS or if the
// parent becomes `display: hidden`.
document.body.append(modal);
// Close the modal if the cancel button is pressed.
modal.querySelector('.cancel')?.addEventListener('click', () => {
modal.close();
}, {once: true, passive: true});
modal.querySelector('.ok')?.addEventListener('click', onApprove, {passive: true});
// Call a `onShow` callback if one is registered for this element.
modal?.$modal?.onShow();
// The modal is ready to be shown.
modal.showModal();
}
// NOTE: Can be replaced in late 2026 with `closedBy` attribute on `<dialog>` element.
export function initModalClose() {
document.addEventListener('click', (event) => {
const dialog = document.querySelector<HTMLDialogElement>('dialog[open]');
// No open dialogs on page, nothing to do.
if (dialog === null) return;
const target = event.target as HTMLElement;
// User clicked dialog itself (not it's content), likely ::backdrop, so close it.
if (dialog === target) dialog.close();
});
}