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

Allow additional attributes on the turbo_stream_action_tag helper #373

Merged
merged 2 commits into from
Sep 13, 2022

Conversation

marcoroth
Copy link
Member

@marcoroth marcoroth commented Aug 20, 2022

Turbo now supports Custom Stream Actions via hotwired/turbo#479. In order to be able to properly use Custom Stream Actions in your application you also need to have a new server-side helper to generate the corresponding <turbo-stream> element.

Imagine a Custom Stream Action console_log which prints out a message via JavaScript's console.log. The action takes two arguments: the first one is the message itself and the second optional argument is the log-level.

import { StreamActions } from '@hotwired/turbo'

StreamActions.console_log = function () {
  const level = this.getAttribute('level') || 'log'
  const message = this.getAttribute('message')

  console[level](message)
}

This Pull Requests allows to write a custom helper, like so:

# app/helpers/custom_stream_helper.rb

module CustomStreamHelper
  def console_log(message, level = :log)
    turbo_stream_action_tag :console_log, target: "body", message: message, level: level
  end
end

Turbo::Streams::TagBuilder.prepend(CustomStreamHelper)

This now allows you to call console_log on turbo_stream throughout the application with:

turbo_stream.console_log "Hello World"

Which produces the following payload:

<turbo-stream message="Hello World" level="log" action="console_log" target="body"><template></template></turbo-stream>

Previously this wasn't possible because the turbo_stream_action_tag method wouldn't take any additional arguments.

marcoroth added a commit to marcoroth/turbo-rails that referenced this pull request Aug 22, 2022
…all`

This allows the `action` and `action_all` methods to be used to build 
custom action helpers when the `target` is not required, as seen in 
hotwired#373.
marcoroth added a commit to marcoroth/turbo-rails that referenced this pull request Aug 26, 2022
…all`

This allows the `action` and `action_all` methods to be used to build 
custom action helpers when the `target` is not required, as seen in 
hotwired#373.
@dhh dhh merged commit 50fb92f into hotwired:main Sep 13, 2022
dhh added a commit that referenced this pull request Sep 13, 2022
* main:
  Allow additional attributes on the `turbo_stream_action_tag` helper (#373)
  Encode HTTP method into Request body as `_method` (#370)
@marcoroth marcoroth deleted the turbo-stream-additional-attributes branch March 27, 2023 01:34
marcoroth added a commit to marcoroth/turbo-rails that referenced this pull request Mar 29, 2023
…all`

This allows the `action` and `action_all` methods to be used to build 
custom action helpers when the `target` is not required, as seen in 
hotwired#373.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants