From 741daf8ae03ad28137ea97571c9da24ee6c935f6 Mon Sep 17 00:00:00 2001 From: Justin Wiebe Date: Tue, 17 Feb 2026 11:17:16 -0600 Subject: [PATCH] Add morph controller to allow persisting attributes on a turbo morph event --- Gemfile.lock | 2 +- example_rails7/Gemfile.lock | 2 +- example_rails8/Gemfile.lock | 5 ++-- .../controllers/morph_controller.js | 23 +++++++++++++++++++ lib/rolemodel_rails/version.rb | 2 +- .../ui_components/modal_generator_spec.rb | 1 + 6 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 lib/generators/rolemodel/ui_components/modals/templates/app/javascript/controllers/morph_controller.js diff --git a/Gemfile.lock b/Gemfile.lock index bb6cf419..54ad989b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - rolemodel_rails (0.25.1) + rolemodel_rails (0.26.0) rails (> 7.1) GEM diff --git a/example_rails7/Gemfile.lock b/example_rails7/Gemfile.lock index daf9598f..33368a1d 100644 --- a/example_rails7/Gemfile.lock +++ b/example_rails7/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: .. specs: - rolemodel_rails (0.25.1) + rolemodel_rails (0.26.0) rails (> 7.1) GEM diff --git a/example_rails8/Gemfile.lock b/example_rails8/Gemfile.lock index 546960ce..a3f1a18c 100644 --- a/example_rails8/Gemfile.lock +++ b/example_rails8/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: .. specs: - rolemodel_rails (0.25.1) + rolemodel_rails (0.26.0) rails (> 7.1) GEM @@ -382,6 +382,7 @@ PLATFORMS aarch64-linux-musl arm-linux-gnu arm-linux-musl + arm64-darwin-23 arm64-darwin-25 x86_64-linux x86_64-linux-gnu @@ -521,7 +522,7 @@ CHECKSUMS regexp_parser (2.11.3) sha256=ca13f381a173b7a93450e53459075c9b76a10433caadcb2f1180f2c741fc55a4 reline (0.6.3) sha256=1198b04973565b36ec0f11542ab3f5cfeeec34823f4e54cebde90968092b1835 rexml (3.4.4) sha256=19e0a2c3425dfbf2d4fc1189747bdb2f849b6c5e74180401b15734bc97b5d142 - rolemodel_rails (0.25.1) + rolemodel_rails (0.26.0) rubocop (1.84.1) sha256=14cc626f355141f5a2ef53c10a68d66b13bb30639b26370a76559096cc6bcc1a rubocop-ast (1.49.0) sha256=49c3676d3123a0923d333e20c6c2dbaaae2d2287b475273fddee0c61da9f71fd rubocop-performance (1.26.1) sha256=cd19b936ff196df85829d264b522fd4f98b6c89ad271fa52744a8c11b8f71834 diff --git a/lib/generators/rolemodel/ui_components/modals/templates/app/javascript/controllers/morph_controller.js b/lib/generators/rolemodel/ui_components/modals/templates/app/javascript/controllers/morph_controller.js new file mode 100644 index 00000000..fe61a4bf --- /dev/null +++ b/lib/generators/rolemodel/ui_components/modals/templates/app/javascript/controllers/morph_controller.js @@ -0,0 +1,23 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static values = { + attributeName: String + } + + /** + * Use this method in conjunction with the turbo:before-morph-attribute event to prevent morphing of a specific + * attribute on an element. This is useful for attributes that may change due to user interaction, such as the "open" + * attribute on a details element, which can be toggled by the user and should not be overridden by Turbo's morphing process. + * + * Example: + *
+ * + * @param {import("@hotwired/turbo").TurboBeforeMorphAttributeEvent} event + */ + skipMorphAttribute(event) { + if (event.detail.attributeName === this.attributeNameValue) { + event.preventDefault() + } + } +} diff --git a/lib/rolemodel_rails/version.rb b/lib/rolemodel_rails/version.rb index 62509551..3914e82f 100644 --- a/lib/rolemodel_rails/version.rb +++ b/lib/rolemodel_rails/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RolemodelRails - VERSION = '0.25.1' + VERSION = '0.26.0' end diff --git a/spec/generators/rolemodel/ui_components/modal_generator_spec.rb b/spec/generators/rolemodel/ui_components/modal_generator_spec.rb index 6fb67255..c3d6fe8a 100644 --- a/spec/generators/rolemodel/ui_components/modal_generator_spec.rb +++ b/spec/generators/rolemodel/ui_components/modal_generator_spec.rb @@ -11,6 +11,7 @@ let(:command_line_options) { [] } it 'adds the correct javascript files' do + assert_file 'app/javascript/controllers/morph_controller.js' assert_file 'app/javascript/controllers/toggle_controller.js' assert_file 'app/javascript/initializers/turbo_confirm.js' assert_file 'app/javascript/initializers/frame_missing_handler.js'