From c133b8b3a8705ade915564e2eccca2624c7ddc12 Mon Sep 17 00:00:00 2001 From: Inhji Date: Fri, 17 Mar 2023 20:46:25 +0100 Subject: [PATCH] add dark mode with toggle for admin \o/ --- assets/js/app.js | 19 +++++++ assets/tailwind.config.js | 1 + lib/chiya_web/components/core_components.ex | 55 +++++++++++-------- .../components/layouts/app.html.heex | 14 ++--- .../components/layouts/public.html.heex | 5 +- .../components/layouts/root_app.html.heex | 11 +++- .../controllers/setting_html/show.html.heex | 2 +- lib/chiya_web/live/note_show_live.ex | 2 +- lib/chiya_web/router.ex | 2 +- 9 files changed, 73 insertions(+), 38 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index b1f03d2..6ec18af 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -42,3 +42,22 @@ liveSocket.connect() // >> liveSocket.disableLatencySim() window.liveSocket = liveSocket +document + .querySelector("#dark-mode-toggle") + .addEventListener("click", (e) => { + e.preventDefault() + const data = document.documentElement.dataset + if (data["mode"] && data["mode"] == "dark") { + + delete data["mode"] + window.localStorage.removeItem("theme") + + } else { + + data["mode"] = "dark" + window.localStorage.setItem("theme", "dark") + + } + }) + +console.log("boot complete!") \ No newline at end of file diff --git a/assets/tailwind.config.js b/assets/tailwind.config.js index 3c64e78..c359830 100644 --- a/assets/tailwind.config.js +++ b/assets/tailwind.config.js @@ -12,6 +12,7 @@ module.exports = { "../lib/*_web.ex", "../lib/*_web/**/*.*ex" ], + darkMode: ['class', '[data-mode="dark"]'], theme: { extend: { colors: { diff --git a/lib/chiya_web/components/core_components.ex b/lib/chiya_web/components/core_components.ex index 3278c7c..17de460 100644 --- a/lib/chiya_web/components/core_components.ex +++ b/lib/chiya_web/components/core_components.ex @@ -23,7 +23,7 @@ defmodule ChiyaWeb.CoreComponents do """ def line(assigns) do ~H""" -
+
""" end @@ -40,6 +40,15 @@ defmodule ChiyaWeb.CoreComponents do
  • <%= @current_user.email %>
  • +
  • + <.link + href="" + id="dark-mode-toggle" + class="text-xs leading-6 text-gray-100 font-semibold hover:text-gray-300" + > + <.icon name="hero-bolt-mini" class="h-4 w-4" /> + +
  • <.link href={~p"/user"} @@ -97,7 +106,7 @@ defmodule ChiyaWeb.CoreComponents do def live_upload(assigns) do ~H"""
    - <.live_file_input upload={@upload} /> + <.live_file_input upload={@upload} class="dark:text-gray-300" />
    <%= for entry <- @upload.entries do %> @@ -273,8 +282,8 @@ defmodule ChiyaWeb.CoreComponents do role="alert" class={[ "fixed hidden top-2 right-2 w-80 sm:w-96 z-50 rounded-lg p-3 shadow-md shadow-gray-900/5 ring-1", - @kind == :info && "bg-emerald-50 text-emerald-800 ring-emerald-500 fill-cyan-900", - @kind == :error && "bg-rose-50 p-3 text-rose-900 shadow-md ring-rose-500 fill-rose-900" + @kind == :info && "bg-emerald-50 text-emerald-800 ring-emerald-500 fill-cyan-900 dark:bg-emerald-900 dark:text-emerald-200 hover:opacity-40", + @kind == :error && "bg-rose-50 p-3 text-rose-900 shadow-md ring-rose-500 fill-rose-900 dark:bg-rose-900 dark:text-rose-200 hover:opacity-40" ]} {@rest} > @@ -349,7 +358,7 @@ defmodule ChiyaWeb.CoreComponents do def simple_form(assigns) do ~H""" <.form :let={f} for={@for} as={@as} {@rest}> -
    +
    <%= render_slot(@inner_block, f) %>
    <%= render_slot(action, f) %> @@ -379,7 +388,7 @@ defmodule ChiyaWeb.CoreComponents do type={@type} class={[ "phx-submit-loading:opacity-75 rounded-lg bg-gray-900 hover:bg-gray-700 py-2 px-3", - "text-sm font-semibold leading-6 text-white active:text-white/80", + "text-sm font-semibold leading-6 text-white active:text-white/80 dark:bg-gray-800 dark:hover:bg-gray-700", @class ]} {@rest} @@ -438,7 +447,7 @@ defmodule ChiyaWeb.CoreComponents do ~H"""
    -