Skip to content

Commit

Permalink
fix: cache visible component heights
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed May 13, 2022
1 parent 22d4166 commit 8a8e3ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ dummy.getNodesObjects = function() {};
dummy.getEdgesObjects = function() {};
dummy.alphaTarget = function() {};
dummy.restart = function() {};
dummy.observe = function() {};
dummy.contentRect = function() {};
dummy.height = function() {};

/**
* @typedef {{
Expand Down
20 changes: 17 additions & 3 deletions src/main/frontend/ui.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,23 @@
label-right]]
(progress-bar width)])

(rum/defc lazy-visible-inner
[visible? content-fn loading-label]
[:div.lazy-visibility
(rum/defcs lazy-visible-inner <
{:init (fn [state]
(assoc state
::ref (atom nil)
::height (atom 1)))
:did-mount (fn [state]
(let [observer (js/ResizeObserver. (fn [entries]
(let [entry (first entries)
*height (::height state)
height' (.-height (.-contentRect entry))]
(when (> height' @*height)
(reset! *height height')))))]
(.observe observer @(::ref state)))
state)}
[state visible? content-fn loading-label]
[:div.lazy-visibility {:ref #(reset! (::ref state) %)
:style {:min-height @(::height state)}}
(if visible?
(when (fn? content-fn) (content-fn))
(when loading-label [:span.text-sm.font-medium
Expand Down

0 comments on commit 8a8e3ee

Please sign in to comment.