sidjainn

sidjainn

Overriding the default confirm behaviour on phoenix.html.link

While trying to override the default confirm behaviour of phoenix.link.click in live view, the operation happens already without waiting for confirmation from the user. I’m using vex for creating custom confirm dialog like in the docs below.

https://hexdocs.pm/phoenix_html/Phoenix.HTML.Link.html#link/2-overriding-the-default-confirm-behaviour

Most Liked

kerryb

kerryb

I came across this thread while seeing the same issues (in 2024). I think it might have been caused by not loading the vex css. In case it helps anyone, this is the working setup I ended up with (this is an old project using webpack – I assume it’ll be a bit different with esbuild):

(cd assets && npm install --save vex-js vex-dialog)

New file assets/js/confirm_dialog.js (for tidyness):

import vex from "vex-js"
import vex_dialog from "vex-dialog"
vex.registerPlugin(vex_dialog)
vex.defaultOptions.className = 'vex-theme-os'

function init() {
  document.body.addEventListener('phoenix.link.click', function (e) {
    e.stopPropagation();
    var message = e.target.getAttribute("data-confirm");
    if(!message){ return true; }
    vex.dialog.confirm({
      message: message,
      callback: function (value) {
        if (value == false) { e.preventDefault(); }
      }
    })
  }, false);
}

export default { init: init }

In assets/js/app.js:

import ConfirmDialog from "./confirm_dialog"
…
 $(document).ready(function() {
   …
  ConfirmDialog.init()
  …
})

In assets/css/app.scss:

@import "../node_modules/vex-js/dist/css/vex";
@import "../node_modules/vex-js/dist/css/vex-theme-os";
Neophen

Neophen

yeah i forgot i wanted to post this here somewhere :smiley:

@kerryb the problem with the vex solution lies in the fact that e.preventDefault needs to be instant, the event handlers are instant you can’t really await on them. Hence in my implementation i prevent it if it has a data-confirm and then re-trigger on the success case, you could adjust it based on your modal/dialog, i just used the native dialog method and created promise with closures to handle the results.

Also, my implementation just puts the markup dialog into the html markup as a live component. You could put it into a template tag and create it a dialog from there. ofcourse make sure to remove the dialog after resolving it.

Neophen

Neophen

It’s been merged, should be part of the docs on the new version of liveview

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement