From 219fc2f3bc76f16e6ecc1ec7df491971d3dc0f73 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Sun, 8 May 2022 12:33:27 +0200 Subject: [PATCH] fix(turbo): patch turbo to make link_to work without global turbo enabled --- app/views/layouts/application.html.haml | 14 +++++++---- patches/@hotwired+turbo+7.1.0.patch | 32 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 patches/@hotwired+turbo+7.1.0.patch diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 8a7048d6b..81caa3ebb 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -42,8 +42,12 @@ = yield :charts_js - // Container for custom turbo-stream actions - %turbo-events - // Container for turbo form that we can submit from inside other forms - %div{ 'data-turbo': 'true' } - = form_tag('', id: :turbo_form) + // Container for custom turbo-stream actions + %turbo-events + + // We patch `@hotwired/turbo` to attach forms generated from links to this + // container instead of the body to avoid conflicts with `@rails/ujs`. We also + // patch `@hotwired/turbo` to add a timeout before removing the form because in + // order to be accepted as a valid `turbo form`` either global `turbo drive`` should + // be enabled or the form needs to have a parent with `data-turbo="true"` on it. + %div{ 'data-turbo': 'true' } diff --git a/patches/@hotwired+turbo+7.1.0.patch b/patches/@hotwired+turbo+7.1.0.patch new file mode 100644 index 000000000..d6b551b3e --- /dev/null +++ b/patches/@hotwired+turbo+7.1.0.patch @@ -0,0 +1,32 @@ +diff --git a/node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js b/node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js +index 963422f..65364f1 100644 +--- a/node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js ++++ b/node_modules/@hotwired/turbo/dist/turbo.es2017-esm.js +@@ -2621,9 +2621,9 @@ class Session { + form.addEventListener("turbo:submit-start", () => form.remove()); + } + else { +- form.addEventListener("submit", () => form.remove()); ++ form.addEventListener("submit", () => setTimeout(() => form.remove(), 500)); + } +- document.body.appendChild(form); ++ (document.querySelector('body > [data-turbo="true"]') || document.body).appendChild(form); + return dispatch("submit", { cancelable: true, target: form }); + } + else { +diff --git a/node_modules/@hotwired/turbo/dist/turbo.es2017-umd.js b/node_modules/@hotwired/turbo/dist/turbo.es2017-umd.js +index 101db1f..ce43031 100644 +--- a/node_modules/@hotwired/turbo/dist/turbo.es2017-umd.js ++++ b/node_modules/@hotwired/turbo/dist/turbo.es2017-umd.js +@@ -2627,9 +2627,9 @@ Copyright © 2021 Basecamp, LLC + form.addEventListener("turbo:submit-start", () => form.remove()); + } + else { +- form.addEventListener("submit", () => form.remove()); ++ form.addEventListener("submit", () => setTimeout(() => form.remove(), 500)); + } +- document.body.appendChild(form); ++ (document.querySelector('body > [data-turbo="true"]') || document.body).appendChild(form); + return dispatch("submit", { cancelable: true, target: form }); + } + else {