From 6845527d4beefde56ae406c48ab6b75bdf253ff7 Mon Sep 17 00:00:00 2001 From: Eric Dvorsak Date: Mon, 30 Oct 2017 20:26:33 +0100 Subject: [PATCH] [bug] close dropdown when clicking outside of it --- src/cljs/commiteth/core.cljs | 4 ++-- src/cljs/commiteth/handlers.cljs | 15 +++++++++++++++ src/cljs/commiteth/subscriptions.cljs | 5 +++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/cljs/commiteth/core.cljs b/src/cljs/commiteth/core.cljs index 2ed7128..9c19757 100644 --- a/src/cljs/commiteth/core.cljs +++ b/src/cljs/commiteth/core.cljs @@ -36,12 +36,12 @@ (def user-dropdown-open? (r/atom false)) (defn user-dropdown [user items] - (let [menu (if @user-dropdown-open? + (let [menu (if @(rf/subscribe [:user-dropdown-open?]) [:div.ui.menu.transition.visible] [:div.ui.menu.transition.hidden]) avatar-url (:avatar_url user)] [:div.ui.left.item.dropdown - {:on-click #(swap! user-dropdown-open? not)} + {:on-click #(rf/dispatch [:user-dropdown-open])} [:div.item [:img.ui.mini.circular.image {:src avatar-url}]] [:div.item diff --git a/src/cljs/commiteth/handlers.cljs b/src/cljs/commiteth/handlers.cljs index 4eb1347..8ac43d0 100644 --- a/src/cljs/commiteth/handlers.cljs +++ b/src/cljs/commiteth/handlers.cljs @@ -430,3 +430,18 @@ :metrics-loaded (fn [db [_]] (dissoc db :metrics-loading?))) + +(defn close-dropdown [] + (dispatch [:user-dropdown-close])) + +(reg-event-db + :user-dropdown-open + (fn [db [_]] + (.addEventListener js/window "click" close-dropdown) + (assoc db :user-dropdown-open? true))) + +(reg-event-db + :user-dropdown-close + (fn [db [_]] + (.removeEventListener js/window "click" close-dropdown) + (assoc db :user-dropdown-open? false))) diff --git a/src/cljs/commiteth/subscriptions.cljs b/src/cljs/commiteth/subscriptions.cljs index 1ce5110..6e18eb1 100644 --- a/src/cljs/commiteth/subscriptions.cljs +++ b/src/cljs/commiteth/subscriptions.cljs @@ -75,3 +75,8 @@ :metrics-loading? (fn [db _] (:metrics-loading? db))) + +(reg-sub + :user-dropdown-open? + (fn [db _] + (:user-dropdown-open? db)))