Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't touch the DOM unless a style has actually changed #9

Merged
merged 1 commit into from
Mar 20, 2021
Merged
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
14 changes: 11 additions & 3 deletions src/spade/runtime.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,30 @@
(defn compile-css [elements]
(garden/css *css-compile-flags* elements))

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

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

(defn inject! [id css]
(let [head (.-head js/document)
element (doto (js/document.createElement "style")
(.setAttribute "spade-id" (str id)))
obj {:element element
:source css
:id id}]
(assert (some? head)
"An head element is required in the dom to inject the style.")

(.appendChild head element)

(swap! *injected* assoc id obj)
(update! obj css)))
(perform-update! obj css)))

(defn- compose-names [{style-name :name composed :composes}]
(if-not composed
Expand Down Expand Up @@ -58,7 +66,7 @@

(if existing
; update existing style element
(update! existing css)
(update! style-name css)

; create a new element
(inject! style-name css))
Expand Down