diff --git a/assets/js/app.js b/assets/js/app.js index 44a8122..e14eed6 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -22,8 +22,12 @@ import {Socket} from "phoenix" import {LiveSocket} from "phoenix_live_view" import topbar from "../vendor/topbar" +import {DarkModeHook} from './dark-mode' let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") -let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}}) +let liveSocket = new LiveSocket("/live", Socket, { + params: {_csrf_token: csrfToken}, + hooks: {dark_mode_widget: DarkModeHook}, +}) // Show progress bar on live navigation and form submits topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"}) diff --git a/assets/js/dark-mode.js b/assets/js/dark-mode.js new file mode 100644 index 0000000..09771a5 --- /dev/null +++ b/assets/js/dark-mode.js @@ -0,0 +1,36 @@ +let DarkModeHook = { + + mounted() { + this.button = this.el.querySelector("button") + this.button.addEventListener("click", this.show_selector.bind(this)) + this.selector = this.el.querySelector("ul") + var lis = this.el.querySelectorAll("li") + lis[0].addEventListener("click", this.switch_to_day_mode.bind(this)) + lis[1].addEventListener("click", this.switch_to_night_mode.bind(this)) + lis[2].addEventListener("click", this.switch_to_system_mode.bind(this)) + }, + show_selector(){ + this.selector.classList.remove("hidden") + }, + switch_to_day_mode() { + document.documentElement.classList.remove('dark') + localStorage.theme = 'light' + this.selector.classList.add("hidden") + }, + switch_to_night_mode() { + document.documentElement.classList.add('dark') + localStorage.theme = 'dark' + this.selector.classList.add("hidden") + }, + switch_to_system_mode() { + if (window.matchMedia('(prefers-color-scheme: dark)').matches) { + document.documentElement.classList.add('dark') + } else { + document.documentElement.classList.remove('dark') + } + localStorage.removeItem('theme') + this.selector.classList.add("hidden") + }, +} + +export {DarkModeHook} diff --git a/assets/tailwind.config.js b/assets/tailwind.config.js index b611701..c7bd73f 100644 --- a/assets/tailwind.config.js +++ b/assets/tailwind.config.js @@ -4,6 +4,7 @@ const plugin = require("tailwindcss/plugin") module.exports = { + darkMode: 'class', content: [ "./js/**/*.js", "../lib/*_web.ex", @@ -23,4 +24,4 @@ module.exports = { plugin(({addVariant}) => addVariant("phx-submit-loading", [".phx-submit-loading&", ".phx-submit-loading &"])), plugin(({addVariant}) => addVariant("phx-change-loading", [".phx-change-loading&", ".phx-change-loading &"])) ] -} \ No newline at end of file +} diff --git a/lib/outlook_web.ex b/lib/outlook_web.ex index a545a18..5f77b3b 100644 --- a/lib/outlook_web.ex +++ b/lib/outlook_web.ex @@ -90,6 +90,7 @@ defmodule OutlookWeb do import OutlookWeb.HtmlDocComponent import OutlookWeb.TunitEditorComponent import OutlookWeb.PublicComponents + import OutlookWeb.DarkModeComponent import OutlookWeb.Gettext # Shortcut for generating JS commands diff --git a/lib/outlook_web/components/dark_mode_component.ex b/lib/outlook_web/components/dark_mode_component.ex new file mode 100644 index 0000000..d04dd62 --- /dev/null +++ b/lib/outlook_web/components/dark_mode_component.ex @@ -0,0 +1,38 @@ +defmodule OutlookWeb.DarkModeComponent do + @moduledoc """ + Provides components for showing and listing artikel and autoren. + """ + use Phoenix.Component + import Phoenix.HTML + + # alias Phoenix.LiveView.JS + + def dark_mode_widget(assigns) do + ~H""" +
+ + +
+ """ + end +end diff --git a/lib/outlook_web/components/layouts/app.html.heex b/lib/outlook_web/components/layouts/app.html.heex index 679efbc..045b1d1 100644 --- a/lib/outlook_web/components/layouts/app.html.heex +++ b/lib/outlook_web/components/layouts/app.html.heex @@ -34,6 +34,7 @@ + <.dark_mode_widget />
diff --git a/lib/outlook_web/components/layouts/proot.html.heex b/lib/outlook_web/components/layouts/proot.html.heex index da67fea..5478b71 100644 --- a/lib/outlook_web/components/layouts/proot.html.heex +++ b/lib/outlook_web/components/layouts/proot.html.heex @@ -10,8 +10,16 @@ + - + <%= @inner_content %> diff --git a/lib/outlook_web/components/layouts/public.html.heex b/lib/outlook_web/components/layouts/public.html.heex index bbd1d6c..6b05510 100644 --- a/lib/outlook_web/components/layouts/public.html.heex +++ b/lib/outlook_web/components/layouts/public.html.heex @@ -3,6 +3,7 @@ + <.dark_mode_widget />
diff --git a/lib/outlook_web/components/layouts/root.html.heex b/lib/outlook_web/components/layouts/root.html.heex index a20365f..107ba80 100644 --- a/lib/outlook_web/components/layouts/root.html.heex +++ b/lib/outlook_web/components/layouts/root.html.heex @@ -10,6 +10,14 @@ +
    diff --git a/lib/outlook_web/controllers/artikel_html/show.html.heex b/lib/outlook_web/controllers/artikel_html/show.html.heex index ddcd02d..7c428e1 100644 --- a/lib/outlook_web/controllers/artikel_html/show.html.heex +++ b/lib/outlook_web/controllers/artikel_html/show.html.heex @@ -1,5 +1,5 @@
    -

    <%= @artikel.title %>

    +

    <%= @artikel.title %>

    <.link href={"/autoren/#{@artikel.article.author.id}"}><%= @artikel.article.author.name %>     —    <%= Calendar.strftime(@artikel.article.date, "%d.%m.%Y") %>

    Original