Add lazy loading to dashboard templates

This commit is contained in:
AzazelN28 2024-04-02 15:47:54 +02:00 committed by Alonso Torres
parent 04fe8f8960
commit 7c1e8a753f
3 changed files with 36 additions and 40 deletions

View file

@ -123,7 +123,9 @@
[:div {:class (stl/css :template-card)} [:div {:class (stl/css :template-card)}
[:div {:class (stl/css :img-container)} [:div {:class (stl/css :img-container)}
[:img {:src (dm/str thb) [:img {:src (dm/str thb)
:alt (:name item)}]] :alt (:name item)
:loading "lazy"
:decoding "async"}]]
[:div {:class (stl/css :card-name)} [:div {:class (stl/css :card-name)}
[:span {:class (stl/css :card-text)} (:name item)] [:span {:class (stl/css :card-text)} (:name item)]
download-icon]]])) download-icon]]]))
@ -194,48 +196,25 @@
last-card (+ (- card-count 1) left-moves) last-card (+ (- card-count 1) left-moves)
content-ref (mf/use-ref) content-ref (mf/use-ref)
move-left (fn [] (dom/scroll-by! (mf/ref-val content-ref) -300 0))
move-right (fn [] (dom/scroll-by! (mf/ref-val content-ref) 300 0))
;; TODO: Hacer que esta función detecte si etamos al final o al
;; principio para hacer aparecer o desaparecer los botones del
;; dashboard de templates
on-scroll (mf/use-fn #(js/console.log "scroll" %))
on-move-left on-move-left
(mf/use-fn (mf/use-fn #(move-left))
(mf/deps card-offset card-width)
(fn [_event]
(when-not (zero? card-offset)
(dom/animate! (mf/ref-val content-ref)
[#js {:left (dm/str card-offset "px")}
#js {:left (dm/str (+ card-offset card-width) "px")}]
#js {:duration 200 :easing "linear"})
(reset! card-offset* (+ card-offset card-width)))))
on-move-left-key-down on-move-left-key-down
(mf/use-fn (mf/use-fn #(move-left))
(mf/deps on-move-left first-card)
(fn [event]
(when (kbd/enter? event)
(dom/stop-propagation event)
(on-move-left event)
(when-let [node (dom/get-element (dm/str "card-container-" first-card))]
(dom/focus! node)))))
on-move-right on-move-right
(mf/use-fn (mf/use-fn #(move-right))
(mf/deps more-cards card-offset card-width)
(fn [_event]
(when more-cards
(swap! card-offset* inc)
(dom/animate! (mf/ref-val content-ref)
[#js {:left (dm/str card-offset "px")}
#js {:left (dm/str (- card-offset card-width) "px")}]
#js {:duration 200 :easing "linear"})
(reset! card-offset* (- card-offset card-width)))))
on-move-right-key-down on-move-right-key-down
(mf/use-fn (mf/use-fn #(move-right))
(mf/deps on-move-right last-card)
(fn [event]
(when (kbd/enter? event)
(dom/stop-propagation event)
(on-move-right event)
(when-let [node (dom/get-element (dm/str "card-container-" last-card))]
(dom/focus! node)))))
on-import-template on-import-template
(mf/use-fn (mf/use-fn
@ -252,9 +231,8 @@
[:& title {:collapsed collapsed}] [:& title {:collapsed collapsed}]
[:div {:class (stl/css :content) [:div {:class (stl/css :content)
:ref content-ref :on-scroll on-scroll
:style {:left card-offset :ref content-ref}
:width (dm/str container-size "px")}}
(for [index (range (count templates))] (for [index (range (count templates))]
[:& card-item [:& card-item

View file

@ -109,24 +109,36 @@
} }
.content { .content {
display: grid;
grid-template-columns: repeat(auto-fill, minmax($s-276, $s-276));
grid-auto-flow: column;
pointer-events: all; pointer-events: all;
/*
width: 200%; width: 200%;
height: $s-228; height: $s-228;
margin-left: $s-6; margin-left: $s-6;
position: absolute; position: absolute;
border-top-left-radius: $s-8; border-top-left-radius: $s-8;
background-color: $db-quaternary; background-color: $db-quaternary;
*/
overflow: scroll hidden;
scroll-behavior: smooth;
scroll-snap-type: x mandatory;
scroll-snap-stop: always;
} }
.card-container { .card-container {
width: $s-276; width: $s-276;
margin-top: $s-20; margin-top: $s-20;
display: inline-block;
text-align: center; text-align: center;
vertical-align: top; vertical-align: top;
background-color: transparent; background-color: transparent;
border: none; border: none;
padding: 0; padding: 0;
scroll-snap-align: center;
} }
.template-card { .template-card {

View file

@ -756,6 +756,12 @@
[] []
(.reload (.-location js/window))) (.reload (.-location js/window)))
(defn scroll-by!
([element x y]
(.scrollBy ^js element x y))
([x y]
(scroll-by! js/window x y)))
(defn animate! (defn animate!
([item keyframes duration] (animate! item keyframes duration nil)) ([item keyframes duration] (animate! item keyframes duration nil))
([item keyframes duration onfinish] ([item keyframes duration onfinish]