defmodule ReflectOS.ConsoleWeb.PageComponents do
@moduledoc """
Provides core page components.
"""
use Phoenix.Component
alias Phoenix.LiveView.JS
import ReflectOS.ConsoleWeb.CoreComponents
@doc """
Renders a header with title.
"""
attr :class, :string, default: nil
slot :inner_block, required: true
slot :actions
slot :subtitle
def header(assigns) do
~H"""
<%= render_slot(@inner_block) %>
<%= render_slot(@subtitle) %>
"""
end
@doc """
Renders a list of items
"""
attr :id, :any
attr :items, :list, required: true
attr :item_id, :any, default: nil, doc: "the function for generating the item id"
attr :item_click, :any, default: nil, doc: "the function for handling phx-click on each item"
attr :item_map, :any,
default: &Function.identity/1,
doc: "the function for mapping each item before"
slot :item, required: true, doc: "Slot for rendering the content of a list item"
def list(assigns) do
assigns =
with %{items: %Phoenix.LiveView.LiveStream{}} <- assigns do
assign(assigns, item_id: assigns.item_id || fn {id, _item} -> id end)
end
~H"""
"""
end
attr :icon, :string, doc: "The raw SVG string to be used as an icon."
attr :title_click, :any,
default: nil,
doc: "the function for handling phx-click on the title of the item"
attr :title_icon, :string, default: nil, doc: "the hericon to append to the title"
attr :id, :any, doc: "The unique id for this item"
attr :title, :string
attr :subtitle, :string
attr :actions, :list, default: [], doc: "List of actions to show in the dropdown menu"
def list_item(assigns) do
~H"""
<.icon raw={@icon} class="w-8 h-8" />
<%= @title %>
<.icon :if={@title_icon != nil} name={@title_icon} class="w-3 h-3 self-center ml-1" />
<%= @subtitle %>
"""
end
def toggle_dropdown(js \\ %JS{}, selector) do
JS.toggle(js,
to: selector,
time: 100,
in:
{"transition-all transform ease-out duration-100",
"opacity-0 -translate-y-4 sm:translate-y-0 sm:scale-95",
"opacity-100 translate-y-0 sm:scale-100"},
out:
{"transition-all transform ease-in duration-100",
"opacity-100 translate-y-0 sm:scale-100",
"opacity-0 -translate-y-4 sm:translate-y-0 sm:scale-95"}
)
end
end