baoagency / polaris_view_components

ViewComponents for Polaris Design System
https://polarisviewcomponents.org
MIT License
380 stars 53 forks source link

Button with `a` tag is clicked automatically on hover in Turbo 8 #409

Closed k0va1 closed 5 months ago

k0va1 commented 6 months ago

I've bumped into a problem while using polaris_button. The button is automatically clicked because of the prefetch feature in Turbo 8. I'm not quite sure that this behavior is expected. For example this code <%= polaris_button(url: add_condition_size_charts_path(id: 5)) { "+ Add condition" } %> transforms into

<a href="/size_charts/add_condition?id=5" data-controller="polaris-button" class="Polaris-Button">
  <span class="Polaris-Button__Content">
      <div class="Polaris-Button__Text">
        + Add condition
      </div>
  </span>
</a>

And all links are prefetched by default in Turbo 8. So to make it looks like a button you need to disable prefetch feature or wrap polaris_button with <div data-turbo-prefetch="false"> tag.

kirillplatonov commented 6 months ago

I guess this is expected behavior. All regular links are being prefetched. And if the link responses with turbo stream instead of Turbo visit then it will cause immediate response on hover.

Possible solutions are:

  1. Using data-turbo-prefetch attribute as you mentioned.
  2. Using buttons instead of links when turbo_stream response is expected. For example:
    polaris_button_to("+ Add condition", add_condition_size_charts_path(id: 5))

    https://polarisviewcomponents.org/lookbook/inspect/url_helpers/button