Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

turbo_stream helper requires capture #672

Open
boardfish opened this issue Aug 26, 2024 · 0 comments
Open

turbo_stream helper requires capture #672

boardfish opened this issue Aug 26, 2024 · 0 comments

Comments

@boardfish
Copy link
Contributor

boardfish commented Aug 26, 2024

Reflects ViewComponent/view_component#1137.

ViewComponent provides a capture compatibility patch, which patches Action View for compatibility with some helpers. Enabling this patch is necessary for the turbo_stream helper to work, but we don't want to recommend that ViewComponent users enable it unless they really must. Earlier, I was able to write a patch that roughly reflected the helper for my needs:

  module TurboStreamTagPatch
    def turbo_stream
      TurboStreamTagHelpers.new(self)
    end

    # The `turbo_stream` helper provided by `turbo-rails` uses ActionView's `capture`
    # in a way that's incompatible with ViewComponent and leads to strange duplicated output.
    # These patches restore the behaviour without us needing to change how we call the original helpers,
    # but their behaviour doesn't exactly match them.
    # https://github.com/ViewComponent/view_component/issues/1137
    # https://viewcomponent.org/known_issues.html#turbo_frame_tag-double-rendering-or-scrambled-html-structure
    class TurboStreamTagHelpers
      def initialize(component)
        @component = component
      end

      def replace(target, _content = nil, **_rendering, &block)
        content = @component.capture(&block)
        tag.turbo_stream(action: :replace, target:) { content }
      end

      def update(target, _content = nil, **_rendering, &block)
        content = @component.capture(&block)
        tag.turbo_stream(action: :update, target:) { content }
      end

      # ...more for other tag helper methods if necessary.

      delegate :tag, to: :@component
    end
  end

Including this patch on a component means you can call turbo_stream and have it render as we intend without the need for the capture compatibility patch.

It'd be helpful to investigate whether we can do something that reflects that within turbo-rails to remove the dependency on the capture compatibility patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant