defmodule LiveBeatsWeb.LiveHelpers do use Phoenix.Component alias LiveBeatsWeb.Router.Helpers, as: Routes alias Phoenix.LiveView.JS alias LiveBeats.Accounts alias LiveBeats.MediaLibrary def home_path(nil = _current_user), do: "/" def home_path(%Accounts.User{} = current_user), do: profile_path(current_user) def profile_path(current_user_or_profile, action \\ :show) def profile_path(username, action) when is_binary(username) do Routes.profile_path(LiveBeatsWeb.Endpoint, action, username) end def profile_path(%Accounts.User{} = current_user, action) do profile_path(current_user.username, action) end def profile_path(%MediaLibrary.Profile{} = profile, action) do profile_path(profile.username, action) end def connection_status(assigns) do ~H""" """ end attr :flash, :map attr :kiny, :atom def flash(%{kind: :error} = assigns) do ~H""" <%= if live_flash(@flash, @kind) do %>
JS.remove_class("fade-in-scale", to: "#flash") |> hide("#flash") } phx-hook="Flash" >
<.icon name={:exclamation_circle} class="w-5 w-5" />
<% end %> """ end def flash(%{kind: :info} = assigns) do ~H""" <%= if live_flash(@flash, @kind) do %>
JS.remove_class("fade-in-scale") |> hide("#flash")} phx-value-key="info" phx-hook="Flash" >
<.icon name={:check_circle} class="w-5 h-5" />
<% end %> """ end def spinner(assigns) do ~H""" """ end attr :name, :atom, required: true attr :outlined, :boolean, default: false attr :rest, :global, default: %{class: "w-4 h-4 inline-block"} def icon(assigns) do assigns = assign_new(assigns, :"aria-hidden", fn -> !Map.has_key?(assigns, :"aria-label") end) ~H""" <%= if @outlined do %> <%= apply(Heroicons.Outline, @name, [Map.to_list(@rest)]) %> <% else %> <%= apply(Heroicons.Solid, @name, [Map.to_list(@rest)]) %> <% end %> """ end @doc """ Returns a button triggered dropdown with aria keyboard and focus supporrt. Accepts the follow slots: * `:id` - The id to uniquely identify this dropdown * `:img` - The optional img to show beside the button title * `:title` - The button title * `:subtitle` - The button subtitle ## Examples <.dropdown id={@id}> <:img src={@current_user.avatar_url}/> <:title><%= @current_user.name %> <:subtitle>@<%= @current_user.username %> <:link navigate={profile_path(@current_user)}>View Profile <:link navigate={Routes.settings_path(LiveBeatsWeb.Endpoint, :edit)}Settings """ attr :id, :string, required: true attr :ok, :string, required: true attr :img, :list, default: [] attr :title, :list, default: [] attr :subtitle, :list, default: [] attr :link, :list, default: [] def dropdown(assigns) do ~H"""
""" end def show_mobile_sidebar(js \\ %JS{}) do js |> JS.show(to: "#mobile-sidebar-container", transition: "fade-in") |> JS.show( to: "#mobile-sidebar", display: "flex", time: 300, transition: {"transition ease-in-out duration-300 transform", "-translate-x-full", "translate-x-0"} ) |> JS.hide(to: "#show-mobile-sidebar", transition: "fade-out") |> JS.dispatch("js:exec", to: "#hide-mobile-sidebar", detail: %{call: "focus", args: []}) end def hide_mobile_sidebar(js \\ %JS{}) do js |> JS.hide(to: "#mobile-sidebar-container", transition: "fade-out") |> JS.hide( to: "#mobile-sidebar", time: 300, transition: {"transition ease-in-out duration-300 transform", "translate-x-0", "-translate-x-full"} ) |> JS.show(to: "#show-mobile-sidebar", transition: "fade-in") |> JS.dispatch("js:exec", to: "#show-mobile-sidebar", detail: %{call: "focus", args: []}) end def show(js \\ %JS{}, selector) do JS.show(js, to: selector, time: 300, display: "inline-block", transition: {"ease-out duration-300", "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95", "opacity-100 translate-y-0 sm:scale-100"} ) end def hide(js \\ %JS{}, selector) do JS.hide(js, to: selector, time: 300, transition: {"transition ease-in duration-300", "transform opacity-100 scale-100", "transform opacity-0 scale-95"} ) end def show_dropdown(to) do JS.show( to: to, transition: {"transition ease-out duration-120", "transform opacity-0 scale-95", "transform opacity-100 scale-100"} ) |> JS.set_attribute({"aria-expanded", "true"}, to: to) end def hide_dropdown(to) do JS.hide( to: to, transition: {"transition ease-in duration-120", "transform opacity-100 scale-100", "transform opacity-0 scale-95"} ) |> JS.remove_attribute("aria-expanded", to: to) end def show_modal(js \\ %JS{}, id) when is_binary(id) do js |> JS.show( to: "##{id}", display: "inline-block", transition: {"ease-out duration-300", "opacity-0", "opacity-100"} ) |> JS.show( to: "##{id}-container", display: "inline-block", transition: {"ease-out duration-300", "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95", "opacity-100 translate-y-0 sm:scale-100"} ) |> js_exec("##{id}-confirm", "focus", []) end def hide_modal(js \\ %JS{}, id) do js |> JS.remove_class("fade-in", to: "##{id}") |> JS.hide( to: "##{id}", transition: {"ease-in duration-200", "opacity-100", "opacity-0"} ) |> JS.hide( to: "##{id}-container", transition: {"ease-in duration-200", "opacity-100 translate-y-0 sm:scale-100", "opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"} ) |> JS.dispatch("click", to: "##{id} [data-modal-return]") end attr :id, :string, required: true attr :show, :boolean, default: false attr :patch, :string, default: nil attr :navigate, :string, default: nil attr :on_cancel, JS, default: %JS{} attr :on_confirm, JS, default: %JS{} # slots attr :title, :list, default: [] attr :confirm, :list, default: [] attr :cancel, :list, default: [] attr :rest, :global def modal(assigns) do ~H"""
<.focus_wrap id={"#{@id}-focus-wrap"} content={"##{@id}-container"}>
""" end attr :id, :string, required: true attr :min, :integer, default: 0 attr :max, :integer, default: 100 attr :value, :integer def progress_bar(assigns) do assigns = assign_new(assigns, :value, fn -> assigns[:min] || 0 end) ~H"""
""" end attr :actions, :list, default: [] def title_bar(assigns) do ~H"""

<%= render_slot(@inner_block) %>

<%= if Enum.count(@actions) > 0 do %>
<%= render_slot(@actions) %>
<% end %>
""" end attr :patch, :string attr :primary, :boolean, default: false attr :rest, :global def button(%{patch: _} = assigns) do ~H""" <%= if @primary do %> <%= live_patch [to: @patch, class: "order-0 inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-purple-600 hover:bg-purple-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500 sm:order-1 sm:ml-3"] ++ Map.to_list(@rest) do %> <%= render_slot(@inner_block) %> <% end %> <% else %> <%= live_patch [to: @patch, class: "order-1 inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500 sm:order-0 sm:ml-0 lg:ml-3"] ++ assigns_to_attributes(assigns, [:primary, :patch]) do %> <%= render_slot(@inner_block) %> <% end %> <% end %> """ end def button(%{} = assigns) do ~H""" <%= if @primary do %> <% else %> <% end %> """ end attr :row_id, :any, default: false attr :rows, :list, required: true # slots attr :col, :list, required: true def table(assigns) do assigns = assigns |> assign_new(:row_id, fn -> false end) |> assign(:col, for(col <- assigns.col, col[:if] != false, do: col)) ~H""" """ end attr :id, :any, required: true attr :module, :atom, required: true attr :row_id, :any, default: false attr :rows, :list, required: true attr :owns_profile?, :boolean, default: false attr :active_id, :any, default: nil # slots attr :col, :list def live_table(assigns) do assigns = assign(assigns, :col, for(col <- assigns.col, col[:if] != false, do: col)) ~H""" """ end @doc """ Calls a wired up event listener to call a function with arguments. window.addEventListener("js:exec", e => e.target[e.detail.call](...e.detail.args)) """ def js_exec(js \\ %JS{}, to, call, args) do JS.dispatch(js, "js:exec", to: to, detail: %{call: call, args: args}) end def focus(js \\ %JS{}, parent, to) do JS.dispatch(js, "js:focus", to: to, detail: %{parent: parent}) end def focus_closest(js \\ %JS{}, to) do js |> JS.dispatch("js:focus-closest", to: to) |> hide(to) end end