Skip to content

Turbo-Streaming ViewComponents #1106

@cpjmcquillan

Description

@cpjmcquillan

Feature request

Provide an interface for rendering view components outside the context of a request-response cycle (e.g without depending on a controller or view context).

Motivation

Rails 7 ships with Hotwire by default. Turbo Streams are a component of Hotwire that (among other things) allow applications to broadcast HTML "over the wire" based on model changes.

Like Rails, Turbo Streams lean heavily on partials - although you can now broadcast HTML from the model. Since ViewComponents replace partials in lots of scenarios, I think it would be beneficial for the library to integrate seamlessly with Turbo Streams in the same way partials do.

Currently there are few ways to integrate ViewComponents with Turbo Streams, but I think it would be nice to offer an "approved" approach.

  • We can use #render_in(view_context) to broadcast HTML from a controller.
  • If a component implements a #call method we can invoke it to return HTML and broadcast from a model (or any other class).
  • We can render the component inside a view partial and broadcast from a model (or any other class).

Context

Turbo broadcasting discussion
Hotwire by default in Rails 7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions