2020-04-08 11:57:29 +03:00
|
|
|
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
;;
|
2022-09-20 23:23:22 +02:00
|
|
|
;; Copyright (c) KALEIDOS INC
|
2021-04-10 09:43:04 +02:00
|
|
|
;; Copyright (c) Mathieu BRUNOT <mathieu.brunot@monogramm.io>
|
2020-04-08 11:57:29 +03:00
|
|
|
|
2020-08-18 19:26:37 +02:00
|
|
|
(ns app.util.theme
|
2020-04-08 11:57:29 +03:00
|
|
|
"A theme manager."
|
|
|
|
(:require
|
2020-08-18 19:26:37 +02:00
|
|
|
[app.config :as cfg]
|
|
|
|
[app.util.dom :as dom]
|
2021-06-17 13:26:38 +02:00
|
|
|
[app.util.storage :refer [storage]]
|
|
|
|
[beicon.core :as rx]
|
|
|
|
[rumext.alpha :as mf]))
|
2020-04-08 11:57:29 +03:00
|
|
|
|
2021-05-04 15:18:59 +02:00
|
|
|
(defonce theme (get @storage ::theme cfg/default-theme))
|
2020-04-08 11:57:29 +03:00
|
|
|
(defonce theme-sub (rx/subject))
|
|
|
|
(defonce themes #js {})
|
|
|
|
|
|
|
|
(defn init!
|
|
|
|
[data]
|
|
|
|
(set! themes data))
|
|
|
|
|
|
|
|
(defn set-current-theme!
|
|
|
|
[v]
|
|
|
|
(when (not= theme v)
|
|
|
|
(when-some [el (dom/get-element "theme")]
|
|
|
|
(set! (.-href el) (str "css/main-" v ".css")))
|
|
|
|
(swap! storage assoc ::theme v)
|
|
|
|
(set! theme v)
|
|
|
|
(rx/push! theme-sub v)))
|
|
|
|
|
|
|
|
(defn set-default-theme!
|
|
|
|
[]
|
|
|
|
(set-current-theme! cfg/default-theme))
|
|
|
|
|
|
|
|
(defn use-theme
|
|
|
|
[]
|
|
|
|
(let [[theme set-theme] (mf/useState theme)]
|
|
|
|
(mf/useEffect (fn []
|
|
|
|
(let [sub (rx/sub! theme-sub #(set-theme %))]
|
|
|
|
#(rx/dispose! sub)))
|
|
|
|
#js [])
|
|
|
|
theme))
|
|
|
|
|