defmodule CanneryWeb.LiveHelpers do @moduledoc """ Contains common helper functions for liveviews """ import Phoenix.Component alias Phoenix.LiveView.JS @doc """ Renders a live component inside a modal. The rendered modal receives a `:return_to` option to properly update the URL when the modal is closed. ## Examples <.modal return_to={Routes.<%= schema.singular %>_index_path(Endpoint, :index)}> <.live_component module={<%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>Live.FormComponent} id={@<%= schema.singular %>.id || :new} title={@page_title} action={@live_action} return_to={Routes.<%= schema.singular %>_index_path(Endpoint, :index)} <%= schema.singular %>: @<%= schema.singular %> /> """ def modal(assigns) do ~H""" <.link patch={@return_to} id="modal-bg" class="fade-in fixed z-10 left-0 top-0 w-full h-full overflow-hidden p-8 flex flex-col justify-center items-center cursor-auto" style="background-color: rgba(0,0,0,0.4);" phx-remove={hide_modal()} > """ end defp hide_modal(js \\ %JS{}) do js |> JS.hide(to: "#modal", transition: "fade-out") |> JS.hide(to: "#modal-bg", transition: "fade-out") |> JS.hide(to: "#modal-content", transition: "fade-out-scale") end @doc """ A toggle button element that can be directed to a liveview or a live_component's `handle_event/3`. ## Examples <.toggle_button action="my_liveview_action" value={@some_value}> Toggle me! <.toggle_button action="my_live_component_action" target={@myself} value={@some_value}> Whatever you want """ def toggle_button(assigns) do assigns = assigns |> assign_new(:id, fn -> assigns.action end) ~H""" """ end end