Hi everyone!
I’m reading the book “Programming Phoenix LiveView” and I can’t do an exercise with JS commands.
At one point, the book asks to create a live component that “[…] toggles a button showing either + expand or - contract, and then marks a corresponding div as hidden or visible.”
I had no particular problem in doing that. After that, the book asks to refactor the component to implement the component without round-tripping to the server, using JS command.
This is my partial solution.
Template:
<.button phx-click={toggle_visibility(@id)} >+ expand</.button>
<div id={@id}>
<%= render_slot(@inner_block) %>
</div>
and this is the event handler:
defp toggle_visibility(js \\ %JS{}, id) do
js
|> JS.toggle(to: "##{id}")
end
My question is: how can I toggle the text of the button as required by the exercise?
Thanks!