Tauri dialog instead of window.confirm

nk_maker

ke na

Posted on August 19, 2024

Tauri dialog instead of window.confirm

[Background]
I used window.confirm to confirm actions before calling backend processes. But, Tauri’s dialog are also available,so I tried to use it.

[Conclusion]
window.confirm has minimal customization.
But Tauri’s dialog has various customizations.
This could be useful when you don't need many customization just like MUI.

Before

typescript

import { invoke } from "@tauri-apps/api/tauri";

const executeCommands = () => {
    invoke<string>("command");
};

...

const handleCommand = async () => {
    const Confirm = await window.confirm("Do you want to execute the hello command?");
    if (Confirm) {
        executeCommands();
    }
};

...

return (
  <div>
      <button onClick={handleCommand}>Execute</button>
  </div>
);
Enter fullscreen mode Exit fullscreen mode

After

typescript

import { invoke } from "@tauri-apps/api/tauri";
import { dialog } from "@tauri-apps/api";

const executeCommands = () => {
    invoke<string>("command");
};

...

const handleCommand = async () => {
    const Confirm = await dialog.ask("Do you want to execute the hello command?", "Ask Dialog");
    if (Confirm) {
        // yes
        executeCommands();
    }
};

...

return (
  <div>
      <button onClick={handleCommand}>Execute</button>
  </div>
);
Enter fullscreen mode Exit fullscreen mode

In tauri.conf.json

json

{
  "tauri": {
    "allowlist": {
      "all": false,
      "dialog": {
        "confirm": true,
        "open": true,
        "ask": true
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
nk_maker
ke na

Posted on August 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Viaje al centro de Godot
spanish Viaje al centro de Godot

June 5, 2023