Skip to content

Commit

Permalink
Upgrade to spade 5197e54
Browse files Browse the repository at this point in the history
  • Loading branch information
kimo-k committed Jan 18, 2024
1 parent 788bbd8 commit 3913548
Show file tree
Hide file tree
Showing 34 changed files with 262 additions and 172 deletions.
2 changes: 1 addition & 1 deletion .clj-kondo/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
{day8.re-frame-10x.inlined-deps.garden.v1v3v10.garden.selectors/defpseudoclass clojure.core/def
day8.re-frame-10x.inlined-deps.garden.v1v3v10.garden.selectors/defpseudoelement clojure.core/def
day8.re-frame-10x.inlined-deps.garden.v1v3v10.garden.units/defunit clojure.core/def
day8.re-frame-10x.inlined-deps.spade.git-sha-93ef290.core/defclass clojure.core/defn}}
day8.re-frame-10x.inlined-deps.spade.git-sha-5197e54.core/defclass clojure.core/defn}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns day8.re-frame-10x.inlined-deps.spade.git-sha-5197e54.container)

(defprotocol IStyleContainer
"The IStyleContainer represents anything that can be used by Spade to
'mount' styles for access by Spade style components."
(mounted-info
[this style-name]
"Given a style-name, return the info object that was passed when style-name
was mounted, or nil if that style is not currently mounted.")
(mount-style!
[this style-name css info]
"Ensure the style with the given name and CSS is available. [info]
should be stored somewhere in-memory to be quickly retrieved
by a call to [mounted-info]."))
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns day8.re-frame-10x.inlined-deps.spade.git-sha-5197e54.container.alternate
"The AlternateStyleContainer may be used when a preferred container
is not always available."
(:require [day8.re-frame-10x.inlined-deps.spade.git-sha-5197e54.container :as sc :refer [IStyleContainer]]))

(deftype AlternateStyleContainer [get-preferred fallback]
IStyleContainer
(mounted-info
[_ style-name]
(or (when-let [preferred (get-preferred)]
(sc/mounted-info preferred style-name))
(sc/mounted-info fallback style-name)))
(mount-style!
[_ style-name css info]
(or (when-let [preferred (get-preferred)]
(sc/mount-style! preferred style-name css info))
(sc/mount-style! fallback style-name css info))))
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(ns day8.re-frame-10x.inlined-deps.spade.git-sha-5197e54.container.atom
"The AtomStyleContainer renders styles into an atom it is provided with."
(:require [day8.re-frame-10x.inlined-deps.spade.git-sha-5197e54.container :refer [IStyleContainer]]))

(deftype AtomStyleContainer [styles-atom info-atom]
IStyleContainer
(mounted-info [_ style-name]
(get @info-atom style-name))
(mount-style! [_ style-name css info]
(swap! styles-atom assoc style-name css)
(swap! info-atom assoc style-name info)))

(defn create-container
([] (create-container (atom nil)))
([styles-atom] (create-container styles-atom (atom nil)))
([styles-atom info-atom] (->AtomStyleContainer styles-atom info-atom)))
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
(ns day8.re-frame-10x.inlined-deps.spade.git-sha-93ef290.container.dom
(ns day8.re-frame-10x.inlined-deps.spade.git-sha-5197e54.container.dom
"The DomStyleContainer renders styles into DOM elements. References to those
elements are stored in a `styles` atom, or `*injected-styles*` if that is
not provided. Similarly, if no `target-dom` is provided, the `document.head`
element is used."
(:require [day8.re-frame-10x.inlined-deps.spade.git-sha-93ef290.container :refer [IStyleContainer]]))
(:require [day8.re-frame-10x.inlined-deps.spade.git-sha-5197e54.container :refer [IStyleContainer]]))

(defonce ^:dynamic *injected-styles* (atom nil))

(defn- perform-update! [obj css]
(set! (.-innerHTML (:element obj)) css))

(defn update! [styles-container id css]
(defn update! [styles-container id css info]
(swap! styles-container update id
(fn update-injected-style [obj]
(when-not (= (:source obj) css)
(perform-update! obj css))
(assoc obj :source css))))
(assoc obj :source css :info info))))

(defn inject! [target-dom styles-container id css]
(defn inject! [target-dom styles-container id css info]
(let [element (doto (js/document.createElement "style")
(.setAttribute "spade-id" (str id)))
obj {:element element
:source css
:info info
:id id}]
(assert (some? target-dom)
"An <head> element or target DOM is required to inject the style.")
Expand All @@ -33,17 +34,22 @@

(deftype DomStyleContainer [target-dom styles]
IStyleContainer
(mount-style! [_ style-name css]
(mounted-info [_ style-name]
(let [resolved-container (or styles
*injected-styles*)]
(:info (get @resolved-container style-name))))

(mount-style! [_ style-name css info]
(let [resolved-container (or styles
*injected-styles*)]
(if (contains? @resolved-container style-name)
(update! resolved-container style-name css)
(update! resolved-container style-name css info)

(let [resolved-dom (or (when (ifn? target-dom)
(target-dom))
target-dom
(.-head js/document))]
(inject! resolved-dom resolved-container style-name css))))))
(inject! resolved-dom resolved-container style-name css info))))))

(defn create-container
"Create a DomStyleContainer. With no args, the default is created, which
Expand Down
Loading

0 comments on commit 3913548

Please sign in to comment.