/** * The system's alert dialogs. * * **Details** * * `alert` shows a dialog and succeeds with the `id` of the button pressed, * or `undefined` when it's dismissed without one. `prompt` shows an iOS * dialog with text fields, and succeeds with the button and the text * entered. `Alert` and `Prompt` are their Commands. * * With no `buttons`, or an empty list, the dialog has one, * `{ id: "ok", text: "OK" }`. So the result's type includes `"ok"` unless * the options' type requires `buttons` with at least one button. * * **Gotchas** * * - Android shows at most three buttons: the last is the positive one, the * one before it the negative one, and the one before that the neutral one. * Android ignores `style` and `preferred`. * - iOS ignores `cancelable`: an alert closes only with a button. * - Interrupting `alert` doesn't close the dialog. Its button then does * nothing. * * **Example** (Confirming a delete) * * ```ts * import { Native } from "@effect/ui-native" * import { Alert } from "@effect/ui-native/kit" * import { Command, UiSchema } from "effect/ui" * import { Result, Schema } from "effect" * * const Message = UiSchema.TaggedEnum({ * PressedDelete: {}, * Answered: { deleted: Schema.Boolean } * }) * * const Note = Native.component({ * name: "Note", * model: Schema.Struct({ deleted: Schema.Boolean }), * message: Message * }).make({ * init: () => ({ deleted: false }), * update: { * PressedDelete: ($) => * $.run( * Alert.Alert.make({ * title: "Delete this note?", * buttons: [ * { id: "cancel", text: "Cancel", style: "cancel" }, * { id: "delete", text: "Delete", style: "destructive" } * ] * }).pipe(Command.map((pressed) => * Message.Answered({ deleted: Result.getOrUndefined(pressed) === "delete" }) * )) * ), * Answered: ($, { deleted }) => $.set({ deleted }) * }, * view: (h) => h.view.on("press", Message.PressedDelete()).children(h.text.children("Delete")) * }) * ``` * * @stability experimental * @since 4.2.0 */ import * as Effect from "effect/Effect" import type * as Result from "effect/Result" import * as Schema from "effect/Schema" import * as Command from "effect/ui/Command" import * as Native from "../Native.ts" import { androidButtons, buttonsOf } from "./internal/alert.ts" import * as device from "./internal/device.ts" /** * A dialog's button. `id` is what `alert` succeeds with when it's pressed. * * **Details** * * On iOS, `style` `"cancel"` places the button where iOS puts the one that * cancels, `"destructive"` shows it in red, and `preferred` makes it the * one Return presses. * * @stability experimental * @category models * @since 4.2.0 */ export interface Button { readonly id: Id readonly text: string readonly style?: "default" | "cancel" | "destructive" | undefined readonly preferred?: boolean | undefined } /** * The options of `alert`. * * **Details** * * `cancelable` lets an Android dialog close with Back or a tap outside it, * and `userInterfaceStyle` sets an iOS dialog's colour scheme. * * @stability experimental * @category models * @since 4.2.0 */ export interface Options< Buttons extends ReadonlyArray