0 directories, 46 files

util

Home / tinai / util
/**
 * Multi-choice modal dialog built by the openDialog utility, using the confirm
 * dialog styling. One button per option; resolves with the chosen option's
 * value, or null when dismissed.
 */

import { openDialog } from './dialog.js';

/**
 * Displays an asynchronous dialog with one button per option.
 * @param {string} title - Dialog title.
 * @param {string} text - Dialog description / message.
 * @param {Array<{label: string, value: string}>} options - Buttons in display order.
 * @returns {Promise<string|null>} Resolves to the chosen value, or null when dismissed.
 */
export function customChoices(title, text, options) {
	return new Promise((resolve) => {
		if (!options || options.length === 0) {
			return resolve(window.prompt(text || title || '') || null);
		}

		const content = document.createElement('div');
		const text_el = document.createElement('p');
		text_el.style.whiteSpace = 'pre-line';
		text_el.textContent = text || '';
		content.appendChild(text_el);

		let value = null;
		openDialog({
			title,
			content,
			buttons: options.map(option => ({
				caption: option.label,
				onclick: (close) => {
					value = option.value;
					close();
				}
			})),
			// Escape resolves as dismissed, like the original handler
			on_escape: () => {
				value = null;
				return true;
			},
			on_close: () => resolve(value)
		});
	});
}
🌐
multi-choice-dialog.js ×
Type: Web, text/x-java
1.33 Kilobytes
Last Modified 2026-11-04 09:48:28
⬇ Download File