defmodule Observer.Web.ThemeComponent do
@moduledoc """
This module provides Theme component
## References:
* https://github.com/oban-bg/oban_web/blob/main/lib/oban/web/live/theme_component.ex
"""
use Observer.Web, :live_component
alias Observer.Web.Components.Icons
@impl Phoenix.LiveComponent
def render(assigns) do
~H"""
<.option :for={theme <- ~w(light dark system)} myself={@myself} theme={@theme} value={theme} />
"""
end
attr :myself, :any, required: true
attr :theme, :string, required: true
attr :value, :string, required: true
defp option(assigns) do
class =
if assigns.theme == assigns.value do
"text-blue-500 dark:text-blue-400"
else
"text-gray-500 dark:text-gray-400 "
end
assigns = assign(assigns, :class, class)
~H"""
<.theme_icon theme={@value} />
{@value}
"""
end
attr :theme, :string, required: true
defp theme_icon(assigns) do
~H"""
<%= case @theme do %>
<% "light" -> %>
<% "dark" -> %>
<% "system" -> %>
<% end %>
"""
end
@impl Phoenix.LiveComponent
def handle_event("update-theme", %{"theme" => theme}, socket) do
send(self(), {:update_theme, theme})
{:noreply, socket}
end
def handle_event("cycle-theme", _params, socket) do
theme =
case socket.assigns.theme do
"light" -> "dark"
"dark" -> "system"
"system" -> "light"
end
send(self(), {:update_theme, theme})
{:noreply, socket}
end
end