From c649e6ccc6c6414e774129ae95c1f7b1f10ba5fd Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 7 May 2024 00:08:55 +1200 Subject: [PATCH] Be a little more opinionated about `replace`, `prepend` and `append` fragment generation. --- lib/live/view.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/live/view.rb b/lib/live/view.rb index 43052bd..8feaa2d 100644 --- a/lib/live/view.rb +++ b/lib/live/view.rb @@ -17,22 +17,28 @@ def update!(**options) # Replace the content of the client-side element by rendering this view. # @parameter selector [String] The CSS selector to replace. # @parameter node [String] The HTML to replace. - def replace(selector, node, **options) - rpc(:replace, selector, node.to_s, options) + def replace(selector, fragment = nil, **options, &block) + fragment ||= XRB::Builder.fragment(&block) + + rpc(:replace, selector, fragment.to_s, options) end # Prepend to the content of the client-side element by appending the specified element. # @parameter selector [String] The CSS selector to prepend to. # @parameter node [String] The HTML to prepend. - def prepend(selector, node, **options) - rpc(:prepend, selector, node.to_s, options) + def prepend(selector, fragment = nil, **options, &block) + fragment ||= XRB::Builder.fragment(&block) + + rpc(:prepend, selector, fragment.to_s, options) end # Append to the content of the client-side element by appending the specified element. # @parameter selector [String] The CSS selector to append to. # @parameter node [String] The HTML to prepend. - def append(selector, node, **options) - rpc(:append, selector, node.to_s, options) + def append(selector, fragment = nil, **options, &block) + fragment ||= XRB::Builder.fragment(&block) + + rpc(:append, selector, fragment.to_s, options) end # Remove the specified element from the client-side element.