improve(ui): support ESC close global modal dialog

This commit is contained in:
charlie
2021-02-28 12:55:55 +08:00
parent ee4b126c33
commit a6ed392d7a
3 changed files with 42 additions and 12 deletions

View File

@@ -52,15 +52,16 @@
;; (dissoc state name))})
(defn hide-when-esc-or-outside
[state & {:keys [on-hide node visibilitychange?]}]
[state & {:keys [on-hide node visibilitychange? outside?]}]
(try
(let [dom-node (rum/dom-node state)]
(when-let [dom-node (or node dom-node)]
(listen state js/window "mousedown"
(fn [e]
;; If the click target is outside of current node
(when-not (dom/contains dom-node (.. e -target))
(on-hide state e :click))))
(or (false? outside?)
(listen state js/window "mousedown"
(fn [e]
;; If the click target is outside of current node
(when-not (dom/contains dom-node (.. e -target))
(on-hide state e :click)))))
(listen state js/window "keydown"
(fn [e]
(case (.-keyCode e)