fix: toast component based on review (#14593)
This commit is contained in:
parent
0a8993bbf1
commit
388e5fae22
|
@ -45,7 +45,7 @@
|
||||||
[count-down-circle/circle-timer {:duration duration}]]
|
[count-down-circle/circle-timer {:duration duration}]]
|
||||||
[text/text
|
[text/text
|
||||||
{:size :paragraph-2 :weight :medium :style (merge-theme-style :text {})}
|
{:size :paragraph-2 :weight :medium :style (merge-theme-style :text {})}
|
||||||
[i18n/label :undo]]])
|
[i18n/label :t/undo]]])
|
||||||
|
|
||||||
(defn- toast-container
|
(defn- toast-container
|
||||||
[{:keys [left middle right]}]
|
[{:keys [left middle right]}]
|
||||||
|
|
|
@ -1,22 +1,17 @@
|
||||||
(ns status-im2.common.toasts.events
|
(ns status-im2.common.toasts.events
|
||||||
(:require [taoensso.encore :as enc]
|
(:require [utils.re-frame :as rf]))
|
||||||
[utils.re-frame :as rf]))
|
|
||||||
|
|
||||||
(def ^:private next-toast-id (atom 0))
|
|
||||||
|
|
||||||
(rf/defn upsert
|
(rf/defn upsert
|
||||||
{:events [:toasts/upsert]}
|
{:events [:toasts/upsert]}
|
||||||
[{:keys [db]} id opts]
|
[{:keys [db]} id opts]
|
||||||
(let [{:toasts/keys [index toasts]} db
|
(let [{:keys [ordered toasts]} (:toasts db)
|
||||||
update? (some #{id} index)
|
update? (some #(= % id) ordered)
|
||||||
index (enc/conj-when index (and (not update?) id))
|
ordered (if (not update?) (conj ordered id) ordered)
|
||||||
toasts (assoc toasts id opts)
|
toasts (assoc toasts id opts)
|
||||||
db (-> db
|
db (-> db
|
||||||
(assoc
|
(update :toasts assoc :ordered ordered :toasts toasts)
|
||||||
:toasts/index index
|
(update :toasts dissoc :hide-toasts-timer-set))]
|
||||||
:toasts/toasts toasts)
|
(if (and (not update?) (= (count ordered) 1))
|
||||||
(dissoc :toasts/hide-toasts-timer-set))]
|
|
||||||
(if (and (not update?) (= (count index) 1))
|
|
||||||
{:show-toasts []
|
{:show-toasts []
|
||||||
:db db}
|
:db db}
|
||||||
{:db db})))
|
{:db db})))
|
||||||
|
@ -24,24 +19,29 @@
|
||||||
(rf/defn create
|
(rf/defn create
|
||||||
{:events [:toasts/create]}
|
{:events [:toasts/create]}
|
||||||
[{:keys [db]} opts]
|
[{:keys [db]} opts]
|
||||||
{:dispatch [:toasts/upsert (str "toast-" (swap! next-toast-id inc)) opts]})
|
(let [next-toast-id (or (get-in [:toasts :next-toast-id] db) 1)]
|
||||||
|
{:db (assoc-in db [:toasts :next-toast-id] (inc next-toast-id))
|
||||||
|
:dispatch [:toasts/upsert (str "toast-" next-toast-id) opts]}))
|
||||||
|
|
||||||
(rf/defn hide-toasts-with-check
|
(rf/defn hide-toasts-with-check
|
||||||
{:events [:toasts/hide-with-check]}
|
{:events [:toasts/hide-with-check]}
|
||||||
[{:keys [db]}]
|
[{:keys [db]}]
|
||||||
(when (:toasts/hide-toasts-timer-set db)
|
(when (get-in db [:toasts :hide-toasts-timer-set])
|
||||||
{:db (dissoc db :toasts/hide-toasts-timer-set)
|
{:db (update db :toasts dissoc :hide-toasts-timer-set)
|
||||||
:hide-toasts nil}))
|
:hide-toasts nil}))
|
||||||
|
|
||||||
(rf/defn close
|
(rf/defn close
|
||||||
{:events [:toasts/close]}
|
{:events [:toasts/close]}
|
||||||
[{:keys [db]} id]
|
[{:keys [db]} id]
|
||||||
(when (get-in db [:toasts/toasts id])
|
(when (get-in db [:toasts :toasts id])
|
||||||
(let [{:toasts/keys [toasts index]} db
|
(let [{:keys [toasts ordered]} (:toasts db)
|
||||||
toasts (dissoc toasts id)
|
toasts (dissoc toasts id)
|
||||||
index (remove #{id} index)
|
ordered (remove #(= % id) ordered)
|
||||||
empty-index? (not (seq index))
|
empty-ordered? (not (seq ordered))
|
||||||
db (assoc db :toasts/index index :toasts/toasts toasts)]
|
db (update db :toasts assoc :ordered ordered :toasts toasts)
|
||||||
(cond-> {:db db}
|
effect {:db db}]
|
||||||
empty-index? (update :db assoc :toasts/hide-toasts-timer-set true)
|
(if empty-ordered?
|
||||||
empty-index? (assoc :dispatch-later [{:ms 500 :dispatch [:toasts/hide-with-check]}])))))
|
(-> effect
|
||||||
|
(update-in [:db :toasts] assoc :hide-toasts-timer-set true)
|
||||||
|
(assoc :dispatch-later [{:ms 500 :dispatch [:toasts/hide-with-check]}]))
|
||||||
|
effect))))
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
(ns status-im2.common.toasts.style)
|
||||||
|
|
||||||
|
(def outmost-transparent-container
|
||||||
|
{:elevation 2
|
||||||
|
:pointer-events :box-none
|
||||||
|
:padding-top 52
|
||||||
|
:flex-direction :column
|
||||||
|
:justify-content :center
|
||||||
|
:align-items :center
|
||||||
|
:background-color :transparent})
|
||||||
|
|
||||||
|
(def each-toast-container
|
||||||
|
{:width "100%"
|
||||||
|
:margin-bottom 5})
|
|
@ -1,26 +1,27 @@
|
||||||
(ns status-im2.common.toasts.view
|
(ns status-im2.common.toasts.view
|
||||||
(:require [quo2.core :as quo2]
|
(:require [quo2.core :as quo]
|
||||||
[react-native.core :as rn]
|
[react-native.core :as rn]
|
||||||
[react-native.gesture :as gesture]
|
[react-native.gesture :as gesture]
|
||||||
[react-native.reanimated :as reanimated]
|
[react-native.reanimated :as reanimated]
|
||||||
[reagent.core :as reagent]
|
[reagent.core :as reagent]
|
||||||
[status-im.utils.utils :as utils.utils]
|
[status-im.utils.utils :as utils.utils]
|
||||||
|
[status-im2.common.toasts.style :as style]
|
||||||
[utils.re-frame :as rf]))
|
[utils.re-frame :as rf]))
|
||||||
|
|
||||||
(def ^:private slide-out-up-animation
|
(def ^:private slide-out-up-animation
|
||||||
(-> reanimated/slide-out-up-animation
|
(-> ^js reanimated/slide-out-up-animation
|
||||||
.springify
|
.springify
|
||||||
(.damping 20)
|
(.damping 20)
|
||||||
(.stiffness 300)))
|
(.stiffness 300)))
|
||||||
|
|
||||||
(def ^:private slide-in-up-animation
|
(def ^:private slide-in-up-animation
|
||||||
(-> reanimated/slide-in-up-animation
|
(-> ^js reanimated/slide-in-up-animation
|
||||||
.springify
|
.springify
|
||||||
(.damping 20)
|
(.damping 20)
|
||||||
(.stiffness 300)))
|
(.stiffness 300)))
|
||||||
|
|
||||||
(def ^:private linear-transition
|
(def ^:private linear-transition
|
||||||
(-> reanimated/linear-transition
|
(-> ^js reanimated/linear-transition
|
||||||
.springify
|
.springify
|
||||||
(.damping 20)
|
(.damping 20)
|
||||||
(.stiffness 300)))
|
(.stiffness 300)))
|
||||||
|
@ -47,7 +48,7 @@
|
||||||
;; remove timer on pan start
|
;; remove timer on pan start
|
||||||
(gesture/on-start clear-timer)
|
(gesture/on-start clear-timer)
|
||||||
(gesture/on-update
|
(gesture/on-update
|
||||||
(fn [evt]
|
(fn [^js evt]
|
||||||
(let [evt-translation-y (.-translationY evt)]
|
(let [evt-translation-y (.-translationY evt)]
|
||||||
(cond
|
(cond
|
||||||
;; reset translate y on pan down
|
;; reset translate y on pan down
|
||||||
|
@ -82,22 +83,15 @@
|
||||||
:layout reanimated/linear-transition
|
:layout reanimated/linear-transition
|
||||||
:style (reanimated/apply-animations-to-style
|
:style (reanimated/apply-animations-to-style
|
||||||
{:transform [{:translateY translate-y}]}
|
{:transform [{:translateY translate-y}]}
|
||||||
{:width "100%"
|
style/each-toast-container)}
|
||||||
:margin-bottom 5})}
|
[quo/toast toast-opts]]]))])))
|
||||||
[quo2/toast toast-opts]]]))])))
|
|
||||||
|
|
||||||
(defn toasts
|
(defn toasts
|
||||||
[]
|
[]
|
||||||
(let [toasts-index (rf/sub [:toasts/index])]
|
(let [toasts-ordered (:ordered (rf/sub [:toasts]))]
|
||||||
[into
|
[into
|
||||||
[rn/view
|
[rn/view
|
||||||
{:style {:elevation 2
|
{:style style/outmost-transparent-container}]
|
||||||
:pointer-events :box-none
|
(->> toasts-ordered
|
||||||
:padding-top 52
|
|
||||||
:flex-direction :column
|
|
||||||
:justify-content :center
|
|
||||||
:align-items :center
|
|
||||||
:background-color :transparent}}]
|
|
||||||
(->> toasts-index
|
|
||||||
reverse
|
reverse
|
||||||
(map (fn [id] ^{:key id} [container id])))]))
|
(map (fn [id] ^{:key id} [container id])))]))
|
||||||
|
|
|
@ -198,8 +198,7 @@
|
||||||
;;intro-wizard
|
;;intro-wizard
|
||||||
(reg-root-key-sub :intro-wizard-state :intro-wizard)
|
(reg-root-key-sub :intro-wizard-state :intro-wizard)
|
||||||
|
|
||||||
(reg-root-key-sub :toasts/toasts :toasts/toasts)
|
(reg-root-key-sub :toasts :toasts)
|
||||||
(reg-root-key-sub :toasts/index :toasts/index)
|
|
||||||
(reg-root-key-sub :popover/popover :popover/popover)
|
(reg-root-key-sub :popover/popover :popover/popover)
|
||||||
(reg-root-key-sub :visibility-status-popover/popover :visibility-status-popover/popover)
|
(reg-root-key-sub :visibility-status-popover/popover :visibility-status-popover/popover)
|
||||||
(reg-root-key-sub :add-account :add-account)
|
(reg-root-key-sub :add-account :add-account)
|
||||||
|
|
|
@ -3,6 +3,6 @@
|
||||||
|
|
||||||
(re-frame/reg-sub
|
(re-frame/reg-sub
|
||||||
:toasts/toast
|
:toasts/toast
|
||||||
:<- [:toasts/toasts]
|
:<- [:toasts]
|
||||||
(fn [toasts [_ toast-id]]
|
(fn [toasts [_ toast-id]]
|
||||||
(get toasts toast-id)))
|
(get-in toasts [:toasts toast-id])))
|
||||||
|
|
Loading…
Reference in New Issue