From 1e2603f1f59c6bec15d7f7cbba51d93b8f51d515 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 29 Aug 2023 17:16:40 +0200 Subject: [PATCH] :sparkles: Add minor improvements to use-visible hook --- frontend/src/app/main/ui/hooks.cljs | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/frontend/src/app/main/ui/hooks.cljs b/frontend/src/app/main/ui/hooks.cljs index d311d71e1f..c0748fe450 100644 --- a/frontend/src/app/main/ui/hooks.cljs +++ b/frontend/src/app/main/ui/hooks.cljs @@ -302,11 +302,14 @@ (fn [entries _] (run! (partial rx/push! intersection-subject) (seq entries))) #js {:rootMargin "0px" - :threshold 1.0}))) + :threshold #js [0 1.0]}))) (defn use-visible [ref & {:keys [once?]}] - (let [[state update-state!] (mf/useState false)] + (let [state (mf/useState false) + update-state! (aget state 1) + state (aget state 0)] + (mf/with-effect [once?] (let [node (mf/ref-val ref) stream (->> intersection-subject @@ -314,15 +317,16 @@ (let [target (unchecked-get entry "target")] (identical? target node)))) (rx/map (fn [entry] - (let [ratio (unchecked-get entry "intersectionRatio") - intersecting? (unchecked-get entry "isIntersecting")] - (or intersecting? (> ratio 0.5))))) + (let [ratio (unchecked-get entry "intersectionRatio") + intersecting? (unchecked-get entry "isIntersecting") + intersecting? (or ^boolean intersecting? + ^boolean (> ratio 0.5))] + (when (and (true? intersecting?) (true? once?)) + (.unobserve ^js @intersection-observer node)) + + intersecting?))) + (rx/dedupe)) - stream (if once? - (->> stream - (rx/filter identity) - (rx/take 1)) - stream) subs (rx/subscribe stream update-state!)] (.observe ^js @intersection-observer node) (fn []