hotwired / turbo-rails

Use Turbo in your Ruby on Rails app
https://turbo.hotwired.dev
MIT License
2.11k stars 327 forks source link

data-turbo-confirm ignored when data-turbo="false"? #663

Closed erict11 closed 2 months ago

erict11 commented 2 months ago

I'm seeing an issue that prevents the turbo confirmation from firing when turbo is disabled but not sure if this is by design. Seems logical that one may want to disable the default turbo_streams request to the backend but keep the confirmation on the element.

Example:

<div>
  <%= button_to 'Click Me', { action: nil },
    class: 'btn btn-warning',
    data: {
      turbo: false,
      turbo_confirm: 'Are you sure?'
    } 
  %> 
</div>

With turbo: false set, no confirmation is triggered but commenting that out or changing to turbo: true starts firing the confirmation again. Is this a known issue or by design?

For now I have a workaround in place using stimulus as such:

<div>
  <%= button_to 'Click Me', { action: nil },
    class: 'btn btn-warning',
    data: {
      turbo: false,
      turbo_confirm: 'Are you sure?',
      controller: 'disabled-turbo-confirm'
    } 
  %> 
</div>

app/javascript/controllers/disabled_turbo_confirm_controller.js

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
    connect() {
        this.element.addEventListener('click', (event) => {
            this._confirmation(event)
        })
    }

    disconnect() {
        this.element.removeEventListener('click', (event) => {
            this._confirmation(event)
        })
    }

    _confirmation(event) {
        if (!("turboConfirm" in event.target.dataset)) {
            console.error('Element missing data-turbo-confirm attribute')
            event.preventDefault()
        } else {
            const confirmed = confirm(event.target.dataset.turboConfirm)
            if (!confirmed) { event.preventDefault() }
        }
    }
}

Details

ruby: 3.3.2 rails: 7.1.3.4 turbo-rails: 1.5.0

brunoprietog commented 2 months ago

Hi, this is intentional. If you disable Turbo on that link, don't expect Turbo to intervene on it when you press it (neither to show the confirmation dialog). Turbo will simply ignore anything where you explicitly tell it that Turbo is disabled there.

erict11 commented 2 months ago

Appreciate the clarification @brunoprietog but i must say this seems like a questionable decision if there is no fallback to the old confirmation syntax. The need for a regular HTML format request should not render en element incapable of facilitating a confirmation. I don't suppose there is a way to disable a turbo_stream format request without disabling all of turbo so that a confirmation may still be supported?

brunoprietog commented 2 months ago

It's not a direct replacement as such for data-confirm. In fact, they can coexist together. However, don't worry if all requests come in Turbo stream format. If you don't use Turbo streams at all, it will still behave as if you were using the HTML format.