Files
gitea-MIRROR/web_src/js/features/comp/ConfirmModal.ts
wxiaoguang 410c7cc55e refactor: use modern HTML template element and make template-based modal dialog work with morph (#40443)
If an element is being morphed, then it can't contain direct modal
dialog, because "show-modal" moves the dialog into body, then a
duplicate dialog will be morphed into the element.

So, we need to use template-based modal dialog when morphing.

---------

Co-authored-by: silverwind <me@silverwind.io>
2026-11-19 19:13:03 +00:00

41 lines
1.3 KiB
TypeScript

import {svg} from '../../svg.ts';
import {html, htmlRaw} from '../../utils/html.ts';
import {createElementFromHTML} from '../../utils/dom.ts';
import {showModal} from '../../modules/modal.ts';
const {i18n} = window.config;
type ConfirmModalOptions = {
header?: string;
content?: string;
confirmButtonColor?: 'primary' | 'red' | 'green' | 'blue';
};
export function createConfirmModal({header = '', content = '', confirmButtonColor = 'primary'}:ConfirmModalOptions = {}): HTMLDialogElement {
const headerHtml = header ? html`<div class="header">${header}</div>` : '';
return createElementFromHTML(html`
<dialog class="modal-dialog">
${htmlRaw(headerHtml)}
<div class="content">${content}</div>
<div class="actions">
<button class="ui cancel button">${htmlRaw(svg('octicon-x'))} ${i18n.modal_cancel}</button>
<button class="ui ${confirmButtonColor} ok button" autofocus>${htmlRaw(svg('octicon-check'))} ${i18n.modal_confirm}</button>
</div>
</dialog>
`.trim());
}
export function confirmModal(modal: HTMLDialogElement | ConfirmModalOptions): Promise<boolean> {
if (!(modal instanceof HTMLDialogElement)) modal = createConfirmModal(modal);
return new Promise((resolve) => {
showModal(modal, {
onApprove() {
resolve(true);
},
onHidden() {
resolve(false);
},
});
});
}