Packages
phoenix_duskmoon
9.2.0
9.9.2
9.9.1
9.9.0
9.8.0
9.7.1
9.7.0
9.6.4
9.6.3
9.6.2
9.6.1
9.6.0
9.5.4
9.5.3
9.5.2
9.5.1
9.5.0
9.4.3
9.4.2
9.4.1
9.4.0
9.3.0
9.2.0
9.1.4
9.1.3
9.1.1
9.1.1-rc.1
retired
9.0.1
9.0.0-rc.3
9.0.0-rc.2
9.0.0-rc.1
8.0.0
7.2.1
7.2.0
7.1.3
7.1.2
7.1.1
6.3.2
6.3.1
6.3.0
6.2.0
6.1.3
6.1.2
6.1.1
6.1.0
6.0.10
6.0.9
6.0.8
6.0.7
6.0.6
6.0.5
6.0.4
6.0.3
6.0.2
6.0.1
6.0.0
5.2.0-beta.5
5.2.0-beta.4
5.2.0-beta.3
5.2.0-beta.2
5.2.0-beta.1
5.1.0
5.0.6
5.0.5
5.0.4
5.0.3
5.0.2
5.0.1
5.0.0
4.6.6
4.6.5
4.6.4
4.6.3
4.6.2
4.6.1
4.6.0
4.5.2
4.5.1
4.5.0
4.4.4
4.4.3
4.4.2
4.4.1
4.4.0
4.3.2
4.3.1
4.3.0
4.2.0
4.1.1
4.1.0
4.0.0
Duskmoon UI component library for Phoenix LiveView
Current section
Files
Jump to
Current section
Files
lib/phoenix_duskmoon/component/layout/theme_switcher.ex
defmodule PhoenixDuskmoon.Component.Layout.ThemeSwitcher do
@moduledoc """
Duskmoon UI ThemeSwitcher Component.
Uses the `theme-controller-dropdown` CSS component from `@duskmoon-dev/core`.
Renders as a `<details>/<summary>` dropdown with radio inputs for theme selection.
Requires the `ThemeSwitcher` LiveView hook for localStorage persistence.
"""
use Phoenix.Component
@doc """
A theme switcher dropdown component.
Renders a `<details>` dropdown with three radio options: auto (default),
sunshine (light), and moonlight (dark). The built-in chevron and hover/checked
states come from `@duskmoon-dev/core`'s theme-controller CSS.
## Examples
<.dm_theme_switcher />
<.dm_theme_switcher theme="moonlight" />
<.dm_theme_switcher button_text="Thème" auto_label="Auto" light_label="Clair" dark_label="Sombre" />
"""
@doc type: :component
attr(:id, :any, default: nil, doc: "HTML id attribute")
attr(:class, :any, default: nil, doc: "Additional CSS classes")
attr(:theme, :string, default: "", doc: "The active theme value")
attr(:select_theme_label, :string,
default: "Select theme",
doc: "Accessible label for the theme selector button"
)
attr(:button_text, :string, default: "Theme", doc: "Visible text on the trigger button")
attr(:auto_label, :string, default: "Auto", doc: "Label for the auto/default theme option")
attr(:light_label, :string, default: "Sunshine", doc: "Label for the light theme option")
attr(:dark_label, :string, default: "Moonlight", doc: "Label for the dark theme option")
attr(:rest, :global, doc: "additional HTML attributes")
def dm_theme_switcher(assigns) do
assigns =
assigns
|> assign_new(:rid, fn -> System.unique_integer([:positive]) end)
base_id = assigns[:id] || "theme-switcher-#{assigns.rid}"
assigns = assign(assigns, :base_id, base_id)
~H"""
<details
id={@base_id}
class={["theme-controller-dropdown theme-controller-dropdown-end", @class]}
phx-hook="ThemeSwitcher"
data-theme={@theme}
{@rest}
>
<summary class="theme-controller-trigger" aria-label={@select_theme_label} aria-haspopup="menu">
{@button_text}
</summary>
<div class="theme-controller-menu" role="radiogroup" aria-label={@select_theme_label}>
<input
type="radio"
id={"#{@base_id}-default"}
name={"theme-#{@base_id}"}
class="theme-controller-item"
value="default"
checked={@theme == "default"}
/>
<label for={"#{@base_id}-default"} class="theme-controller-label">
{@auto_label}
</label>
<input
type="radio"
id={"#{@base_id}-sunshine"}
name={"theme-#{@base_id}"}
class="theme-controller-item"
value="sunshine"
checked={@theme == "sunshine"}
/>
<label for={"#{@base_id}-sunshine"} class="theme-controller-label">
{@light_label}
</label>
<input
type="radio"
id={"#{@base_id}-moonlight"}
name={"theme-#{@base_id}"}
class="theme-controller-item"
value="moonlight"
checked={@theme == "moonlight"}
/>
<label for={"#{@base_id}-moonlight"} class="theme-controller-label">
{@dark_label}
</label>
</div>
</details>
"""
end
end