Skip to content

Commit 0e42cb4

Browse files
mkphilippamarkovics
authored andcommitted
Fix regression regarding useless toc animation on initial load
1 parent a8358cf commit 0e42cb4

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

src/nextjournal/clerk/render.cljs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns nextjournal.clerk.render
2-
(:require ["framer-motion" :refer [m LazyMotion domAnimation]]
2+
(:require ["framer-motion" :refer [motion]]
33
["react" :as react]
44
["react-dom/client" :as react-client]
55
["vh-sticky-table-header" :as sticky-table-header]
@@ -40,25 +40,25 @@
4040
[:button.text-slate-400.hover:text-slate-600.dark:hover:text-white.cursor-pointer
4141
{:on-click #(swap! !dark-mode? not)}
4242
(if @!dark-mode?
43-
[:> (.-svg m)
43+
[:> (.-svg motion)
4444
{:xmlns "http://www.w3.org/2000/svg"
4545
:class "w-5 h-5 md:w-4 md:h-4"
4646
:viewBox "0 0 50 50"
4747
:key "moon"}
48-
[:> (.-path m)
48+
[:> (.-path motion)
4949
{:d "M 43.81 29.354 C 43.688 28.958 43.413 28.626 43.046 28.432 C 42.679 28.238 42.251 28.198 41.854 28.321 C 36.161 29.886 30.067 28.272 25.894 24.096 C 21.722 19.92 20.113 13.824 21.683 8.133 C 21.848 7.582 21.697 6.985 21.29 6.578 C 20.884 6.172 20.287 6.022 19.736 6.187 C 10.659 8.728 4.691 17.389 5.55 26.776 C 6.408 36.163 13.847 43.598 23.235 44.451 C 32.622 45.304 41.28 39.332 43.816 30.253 C 43.902 29.96 43.9 29.647 43.81 29.354 Z"
5050
:fill "currentColor"
5151
:initial "initial"
5252
:animate "animate"
5353
:variants {:initial {:scale 0.6 :rotate 90}
5454
:animate {:scale 1 :rotate 0 :transition spring}}}]]
55-
[:> (.-svg m)
55+
[:> (.-svg motion)
5656
{:key "sun"
5757
:class "w-5 h-5 md:w-4 md:h-4"
5858
:viewBox "0 0 24 24"
5959
:fill "none"
6060
:xmlns "http://www.w3.org/2000/svg"}
61-
[:> (.-circle m)
61+
[:> (.-circle motion)
6262
{:cx "11.9998"
6363
:cy "11.9998"
6464
:r "5.75375"
@@ -67,7 +67,7 @@
6767
:animate "animate"
6868
:variants {:initial {:scale 1.5}
6969
:animate {:scale 1 :transition spring}}}]
70-
[:> (.-g m)
70+
[:> (.-g motion)
7171
{:initial "initial"
7272
:animate "animate"
7373
:variants {:initial {:rotate 45}
@@ -154,29 +154,29 @@
154154
(catch js/Error _
155155
(js/console.warn (str "Clerk render-notebook, invalid hash: "
156156
(.-hash js/location))))))]
157-
(js/requestAnimationFrame #(.scrollIntoViewIfNeeded heading)))))]
158-
[:> LazyMotion {:features domAnimation}
159-
[:div.flex
160-
{:ref root-ref-fn}
161-
[:div.fixed.top-2.left-2.md:left-auto.md:right-2.z-10
162-
[dark-mode-toggle !dark-mode?]]
163-
(when (and toc toc-visibility)
164-
[navbar/view toc (assoc render-opts :set-hash? (not bundle?) :toc-visibility toc-visibility)])
165-
[:div.flex-auto.w-screen.scroll-container
166-
(into
167-
[:> (.-div m)
168-
(merge
169-
{:key "notebook-viewer"
170-
:class (cond-> (or doc-css-class [:flex :flex-col :items-center :notebook-viewer :flex-auto])
171-
sidenotes? (conj :sidenotes-layout))}
172-
(when (and toc (not (navbar/mobile?)))
173-
(let [inset {:margin-left (if (and toc-visibility (:toc-open? @!expanded-at)) navbar/width 0)}]
174-
{:initial inset
175-
:animate inset
176-
:transition navbar/spring})))]
177-
;; TODO: restore react keys via block-id
178-
;; ^{:key (str processed-block-id "@" @!eval-counter)}
179-
(inspect-children render-opts) (concat (when header [header]) xs (when footer [footer])))]]]))
157+
(js/requestAnimationFrame #(.scrollIntoViewIfNeeded heading)))))
158+
_ (swap! !expanded-at merge (navbar/->toc-expanded-at toc toc-visibility))]
159+
[:div.flex
160+
{:ref root-ref-fn}
161+
[:div.fixed.top-2.left-2.md:left-auto.md:right-2.z-10
162+
[dark-mode-toggle !dark-mode?]]
163+
(when (and toc toc-visibility)
164+
[navbar/view toc (assoc render-opts :set-hash? (not bundle?) :toc-visibility toc-visibility)])
165+
[:div.flex-auto.w-screen.scroll-container
166+
(into
167+
[:> (.-div motion)
168+
(merge
169+
{:key "notebook-viewer"
170+
:class (cond-> (or doc-css-class [:flex :flex-col :items-center :notebook-viewer :flex-auto])
171+
sidenotes? (conj :sidenotes-layout))}
172+
(when (and toc (not (navbar/mobile?)))
173+
(let [inset {:margin-left (if (and toc-visibility (:toc-open? @!expanded-at)) navbar/width 0)}]
174+
{:initial inset
175+
:animate inset
176+
:transition navbar/spring})))]
177+
;; TODO: restore react keys via block-id
178+
;; ^{:key (str processed-block-id "@" @!eval-counter)}
179+
(inspect-children render-opts) (concat (when header [header]) xs (when footer [footer])))]]))
180180

181181
(defn opts->query [opts]
182182
(->> opts

src/nextjournal/clerk/render/navbar.cljs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(ns nextjournal.clerk.render.navbar
2-
(:require ["framer-motion" :as framer-motion :refer [m AnimatePresence]]
2+
(:require ["framer-motion" :as framer-motion :refer [motion AnimatePresence]]
33
[applied-science.js-interop :as j]
44
[clojure.string :as str]
55
[nextjournal.clerk.render.hooks :as hooks]
@@ -109,7 +109,7 @@
109109
(def spring {:type :spring :duration 0.35 :bounce 0.1})
110110

111111
(defn mobile-backdrop [{:keys [!expanded-at]}]
112-
[:> (.-div m)
112+
[:> (.-div motion)
113113
{:key "mobile-toc-backdrop"
114114
:class "fixed z-10 bg-gray-500 bg-opacity-75 left-0 top-0 bottom-0 right-0"
115115
:initial {:opacity 0}
@@ -147,7 +147,7 @@
147147
(def mobile-width 300)
148148

149149
(defn toc-panel [toc {:as render-opts :keys [!expanded-at mobile-toc?]}]
150-
[:> (.-div m)
150+
[:> (.-div motion)
151151
(let [inset-or-x (if mobile-toc? :x :margin-left)
152152
w (if mobile-toc? mobile-width width)]
153153
{:key "toc-panel"
@@ -165,16 +165,17 @@
165165
"TOC"]
166166
[render-items toc render-opts]]])
167167

168+
(defn ->toc-expanded-at [toc toc-visibility]
169+
{:toc-open? (if-some [stored-open? (localstorage/get-item local-storage-key)]
170+
stored-open?
171+
(not= :collapsed toc-visibility))
172+
:toc (into {}
173+
(map (juxt identity some?))
174+
(keep #(when (and (map? %) (:expanded? %)) (:path %)) (tree-seq coll? not-empty toc)))})
175+
168176
(defn view [toc {:as render-opts :keys [!expanded-at toc-visibility]}]
169-
(hooks/use-effect
170-
(fn []
171-
(swap! !expanded-at assoc :toc-open? (if-some [stored-open? (localstorage/get-item local-storage-key)]
172-
stored-open?
173-
(not= :collapsed toc-visibility)))
174-
(swap! !expanded-at assoc :toc (into {}
175-
(map (juxt identity some?))
176-
(keep #(when (and (map? %) (:expanded? %)) (:path %)) (tree-seq coll? not-empty toc)))))
177-
[toc])
177+
(hooks/use-effect (fn [] (swap! !expanded-at merge (->toc-expanded-at toc toc-visibility)))
178+
[toc toc-visibility])
178179
(r/with-let [!mobile-toc? (r/atom (mobile?))
179180
handle-resize #(reset! !mobile-toc? (mobile?))
180181
ref-fn #(if %

0 commit comments

Comments
 (0)