Skip to content

Conversation

@kaspth
Copy link

@kaspth kaspth commented Jan 8, 2023

First, we add a turbo helper in Rails views, so we can do:

Turbo::Elements::TurboStream.new(action: "morph", target: "post_1", view_context: self, partial: "posts/post", locals: { post: @post })
turbo.stream action: "morph", target: "post_1", partial: "posts/post", locals: { post: @post }

Next, it's now possible to register custom stream actions, which are just shorthands:

Turbo::Ruby.stream_actions do
  register :morph # Shorthand to define a method `morph` that calls `stream` behind the scenes.
end

turbo.morph target: "post_1", partial: "posts/post", locals: { post: @post }

There's also specific target contexts to help build stream actions.

turbo.for(@posts).morph partial: "posts/post" # Will generate a `morph` action for each post.
turbo.target(@post) do |target|
  target.frame do
    target.stream action: "yolo"
  end
end

Note: we'll pass along object: when **rendering, so users can spare passing locals::

turbo.morph @post, partial: "posts/post"

There's also specific frame methods:

turbo.frame @post
turbo.target(@post).frame
turbo.(@posts).frame do |post|
  # Generate a frame for each post, yielding it into the block.
end

First, we add a `turbo` helper in Rails views, so we can do:

```ruby
Turbo::Elements::TurboStream.new(action: "morph", target: "post_1", view_context: self, partial: "posts/post", locals: { post: @post })
turbo.stream action: "morph", target: "post_1", partial: "posts/post", locals: { post: @post }
```

Next, it's now possible to register custom stream actions, which are just shorthands:

```ruby
Turbo::Ruby.stream_actions do
  register :morph # Shorthand to define a method `morph` that calls `stream` behind the scenes.
end

turbo.morph target: "post_1", partial: "posts/post", locals: { post: @post }
```

There's also specific target contexts to help build stream actions.

```ruby
turbo.for(@posts).morph partial: "posts/post" # Will generate a `morph` action for each post.
turbo.target(@post) do |target|
  target.frame do
    target.stream action: "yolo"
  end
end
```

Note: we'll pass along `object:` when `**rendering`, so users can spare passing `locals:`:

```ruby
turbo.morph @post, partial: "posts/post"
```

There's also specific `frame` methods:

```ruby
turbo.frame @post
turbo.target(@post).frame
turbo.(@posts).frame do |post|
  # Generate a frame for each post, yielding it into the block.
end
```
@kaspth
Copy link
Author

kaspth commented Jan 8, 2023

@marcoroth @joeldrapper this is just me playing around with stuff, I have no idea if it's useful 😄 But this should show the thinking so far.

@kaspth
Copy link
Author

kaspth commented Jan 8, 2023

Ok, so I've tried to move the whole actions context stuff so it's also available on Turbo including moving the targets/target stuff over. It's late here so I probably screwed something up.

And then the view context version is a lot slimmer and really just forwards on the view_context argument.

@marcoroth
Copy link
Owner

Thank you @kaspth! I really love where this is heading and I can definitely see how some of these ideas are going to improve the DX for TurboPower (which is why I started to build this gem).

I'm going to rebuild the Ruby/Rails helpers for TurboPower based on your ideas and see how that feels/works out. I'll keep you posted!

@kaspth
Copy link
Author

kaspth commented Jan 10, 2023

@marcoroth awesome, I'll look forward to it! For me the main thing is to try to have a consistent structure to how the context gets to interpret positional arguments (whether they mean e.g. target/targets or actions) and converting them to the TurboStream.new keyword arguments.

So that both turbo.target(@post).morph or turbo.morph @post are converted into TurboStream.new(action: "morph", target: @post). I think there's nice ergonomics to having that work.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants