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

Document how to extend turbo_stream for custom actions #555

Merged
merged 1 commit into from
Jan 12, 2024

Conversation

seanpdoyle
Copy link
Contributor

Closes #554

The current Turbo::Streams::TagBuilder class supports the actions that Turbo provides out of the box.

For applications that define Custom Stream Actions, this new support aims to provide them with a mechanism to define Rails-side helpers.

For example, consider an [action="highlight"] custom stream. To build those with first-party tooling, applications could add a hook block to their initializer:

# config/initializers/turbo.rb
ActiveSupport.on_load :turbo_streams_tag_builder do
  def highlight(target)
    action :highlight, target
  end

  def highlight_all(targets)
    action_all :highlight, targets
  end
end

turbo_stream.highlight "my-element"
# => <turbo-stream action="highlight" target="my-element"><template></template></turbo-stream>

turbo_stream.highlight_all ".my-selector"
# => <turbo-stream action="highlight" targets=".my-selector"><template></template></turbo-stream>

Closes [hotwired#554][]

The current `Turbo::Streams::TagBuilder` class supports the actions that
Turbo provides out of the box.

For applications that define [Custom Stream Actions][], this new support
aims to provide them with a mechanism to define Rails-side helpers.

For example, consider an `[action="highlight"]` custom stream. To build
those with first-party tooling, applications could add a [hook block][]
to their initializer:

  ```ruby
  # config/initializers/turbo.rb
  ActiveSupport.on_load :turbo_streams_tag_builder do
    def highlight(target)
      action :highlight, target
    end

    def highlight_all(targets)
      action_all :highlight, targets
    end
  end

  turbo_stream.highlight "my-element"
  # => <turbo-stream action="highlight" target="my-element"><template></template></turbo-stream>

  turbo_stream.highlight_all ".my-selector"
  # => <turbo-stream action="highlight" targets=".my-selector"><template></template></turbo-stream>
  ```

[hotwired#554]: hotwired#554
[Custom Stream Actions]: https://turbo.hotwired.dev/handbook/streams#custom-actions
[hook block]: https://edgeapi.rubyonrails.org/classes/ActiveSupport/LazyLoadHooks.html#method-i-on_load
@afcapel afcapel merged commit 00cae6a into hotwired:main Jan 12, 2024
15 checks passed
@afcapel
Copy link
Contributor

afcapel commented Jan 12, 2024

Thanks @seanpdoyle!

@seanpdoyle seanpdoyle deleted the tag-builder-extensions branch January 12, 2024 14:08
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.

Suggestion: turbo_stream.clear
2 participants