Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions installer/templates/phx_assets/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
Make sure to look at the daisyUI changelog: https://daisyui.com/docs/changelog/ */
@plugin "../vendor/daisyui" {
themes: false;
/* Uncomment to include specific built-in themes or all themes. Then remove the custom
themes below and update "layouts/root.html.heex" to select which themes to use. */
/* themes: corporate --default, business --prefersdark; */
/* themes: all; */
}

/* daisyUI theme plugin. You can update this file by fetching the latest version with:
curl -sLO https://github.com/saadeghi/daisyui/releases/latest/download/daisyui-theme.js
We ship with two themes, a light one inspired on Phoenix colors and a dark one inspired
on Elixir colors. Build your own at: https://daisyui.com/theme-generator/ */
@plugin "../vendor/daisyui-theme" {
name: "dark";
name: "elixir-dark";
default: false;
prefersdark: true;
color-scheme: "dark";
Expand All @@ -32,7 +36,7 @@
--color-base-content: oklch(97.807% 0.029 256.847);
--color-primary: oklch(58% 0.233 277.117);
--color-primary-content: oklch(96% 0.018 272.314);
--color-secondary: oklch(58% 0.233 277.117);
--color-secondary: oklch(50% 0.02 277.117);
--color-secondary-content: oklch(96% 0.018 272.314);
--color-accent: oklch(60% 0.25 292.717);
--color-accent-content: oklch(96% 0.016 293.756);
Expand All @@ -57,7 +61,7 @@
}

@plugin "../vendor/daisyui-theme" {
name: "light";
name: "phoenix-light";
default: true;
prefersdark: false;
color-scheme: "light";
Expand Down Expand Up @@ -97,7 +101,7 @@
@custom-variant phx-change-loading (.phx-change-loading&, .phx-change-loading &);

/* Use the data attribute for dark mode */
@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
@custom-variant dark (&:where([data-phx-theme*=dark], [data-phx-theme*=dark] *));

/* Make LiveView wrapper divs transparent for layout */
[data-phx-session], [data-phx-teleported-src] { display: contents }
Expand Down
2 changes: 1 addition & 1 deletion installer/templates/phx_web/components/layouts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ defmodule <%= @web_namespace %>.Layouts do
def theme_toggle(assigns) do
~H"""
<div class="card relative flex flex-row items-center border-2 border-base-300 bg-base-300 rounded-full">
<div class="absolute w-1/3 h-full rounded-full border-1 border-base-200 bg-base-100 brightness-200 left-0 [[data-theme=light]_&]:left-1/3 [[data-theme=dark]_&]:left-2/3 transition-[left]" />
<div class="absolute w-1/3 h-full rounded-full border-1 border-base-200 bg-base-100 brightness-200 left-0 [[data-phx-theme=light]_&]:left-1/3 [[data-phx-theme=dark]_&]:left-2/3 transition-[left]" />

<button
class="flex p-2 cursor-pointer w-1/3"<%= if @live do %>
Expand Down
23 changes: 20 additions & 3 deletions installer/templates/phx_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,33 @@
</script><%= if @css do %>
<script>
(() => {
// Configure the desired themes. Must be loaded in "assets/css/app.css".
const lightTheme = "phoenix-light";
const darkTheme = "elixir-dark";

const prefersDarkMatch = window.matchMedia("(prefers-color-scheme: dark)");
const setSystemTheme = () => {
if (prefersDarkMatch.matches) {
document.documentElement.setAttribute("data-phx-theme", "system-dark");
document.documentElement.setAttribute("data-theme", darkTheme);
} else {
document.documentElement.setAttribute("data-phx-theme", "system-light");
document.documentElement.setAttribute("data-theme", lightTheme);
}
}
const setTheme = (theme) => {
if (theme === "system") {
localStorage.removeItem("phx:theme");
document.documentElement.removeAttribute("data-theme");
prefersDarkMatch.addEventListener("change", setSystemTheme);
setSystemTheme();
} else {
localStorage.setItem("phx:theme", theme);
document.documentElement.setAttribute("data-theme", theme);
prefersDarkMatch.removeEventListener("change", setSystemTheme);
document.documentElement.setAttribute("data-phx-theme", theme);
document.documentElement.setAttribute("data-theme", theme === "light" ? lightTheme : darkTheme);
}
};
if (!document.documentElement.hasAttribute("data-theme")) {
if (!document.documentElement.hasAttribute("data-phx-theme")) {
setTheme(localStorage.getItem("phx:theme") || "system");
}
window.addEventListener("storage", (e) => e.key === "phx:theme" && setTheme(e.newValue || "system"));
Expand Down
Loading