MCP Apps Kit - v0.5.0
    Preparing search index...

    Function useModal

    • Hook for showing host-owned modal dialogs (ChatGPT only)

      Spawns native ChatGPT modals for confirmations, inputs, etc. On unsupported platforms, isSupported will be false.

      Returns {
          isOpen: boolean;
          isSupported: boolean;
          showModal: (options: ModalOptions) => Promise<ModalResult>;
      }

      Modal function and state

      • isOpen: boolean

        Whether a modal is currently open

      • isSupported: boolean

        Whether modal API is supported

      • showModal: (options: ModalOptions) => Promise<ModalResult>

        Show a modal dialog

      function DeleteButton() {
      const { showModal, isSupported, isOpen } = useModal();

      const handleDelete = async () => {
      const result = await showModal({
      title: "Confirm Delete",
      body: "Are you sure you want to delete this item?",
      buttons: [
      { label: "Cancel", variant: "secondary", value: "cancel" },
      { label: "Delete", variant: "destructive", value: "delete" },
      ],
      });

      if (result?.action === "delete") {
      // Perform delete
      }
      };

      if (!isSupported) {
      return <button onClick={() => window.confirm("Delete?")}>Delete</button>;
      }

      return (
      <button onClick={handleDelete} disabled={isOpen}>
      Delete
      </button>
      );
      }

      // Input modal example
      function RenameButton() {
      const { showModal } = useModal();

      const handleRename = async () => {
      const result = await showModal({
      title: "Rename Item",
      input: {
      type: "text",
      placeholder: "Enter new name",
      defaultValue: "Current Name",
      },
      buttons: [
      { label: "Cancel", variant: "secondary", value: "cancel" },
      { label: "Rename", variant: "primary", value: "rename" },
      ],
      });

      if (result?.action === "rename" && result.inputValue) {
      console.log("New name:", result.inputValue);
      }
      };

      return <button onClick={handleRename}>Rename</button>;
      }