/
popbar.cljc
213 lines (189 loc) · 6.99 KB
/
popbar.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
(ns nsfw.popbar
#? (:cljs
(:require [reagent.core :as r]
[nsfw.util :as nu]
[nsfw.css :as nc]
[nsfw.page :as page]
[rx.css :as css]
[dommy.core :as dommy]
[cljs.core.async :as async
:refer [<! >! chan close! put! take! timeout]]))
#? (:cljs (:require-macros [cljs.core.async.macros :refer [go go-loop]])))
#?
(:cljs
(do
(def !popbar (r/atom nil))
(defn gen-show [!state]
(fn [view-key & args]
(if (:visible? @!state)
(go
(swap! !state assoc :visible? false)
(<! (timeout 250))
(swap! !state assoc
:visible? true
:current-view-key view-key
:current-args args))
(swap! !state assoc
:visible? true
:current-view-key view-key
:current-args args))))
(def show (gen-show !popbar))
(defn gen-hide [!state]
(fn []
(swap! !state assoc
:visible? false)
(go
(<! (timeout 300))
(swap! !state assoc
:current-view-key nil))))
(def hide (gen-hide !popbar))
(def gen-current-view-key
(fn [!state]
(:current-view-key @!state)))
(defn current-view-key []
(gen-current-view-key !popbar))
(defn gen-visible? [!state]
(fn []
(:visible? @!state)))
(def visible? (gen-visible? !popbar))
(defn cdu-diff [f]
(fn [this [_ & old-args]]
(let [new-args (rest (r/argv this))]
(f old-args new-args))))
(defn inject-args [view args]
(if args
(if (vector? view)
(let [[el & tail] view]
(if (map? (first tail))
(vec
(concat
[el (merge (first tail) args)]
(rest tail)))
view))
view)
view))
(defn $container [& args]
(let [[{:keys [visible?
initial-view
initial-args
on-visible-change
!state]}]
(page/ensure-opts args)
!state (or !state !popbar)]
(swap! !state assoc
:visible? visible?
:current-view-key initial-view
:current-args initial-args)
(let [!ui (r/atom {:anim-state (if visible?
:post-in
:post-out)})
on-keydown (fn [e]
(when (= 27 (.. e -keyCode))
(swap! !state assoc :visible? false)))]
(r/create-class
{:component-did-mount
(fn [_]
(dommy/listen!
js/window
:keydown
on-keydown)
(add-watch !state :anims
(fn [_ _ {ov? :visible?} {nv? :visible?}]
(when (not= ov? nv?)
(when on-visible-change
(on-visible-change nv?))
(if nv?
(go
(swap! !ui assoc :anim-state :pre-in)
(<! (timeout 17))
(swap! !ui assoc :anim-state :post-in))
(go
(swap! !ui assoc :anim-state :pre-out)
(<! (timeout 300))
(swap! !ui assoc :anim-state :post-out)))))))
:component-will-unmount
(fn [_]
(dommy/unlisten!
js/window
:keydown
on-keydown)
(remove-watch !state :anims))
:reagent-render
(fn [& args]
(let [[{:keys [style
stick-to]
:or {stick-to :bot}}
views]
(page/ensure-opts args)
{:keys [anim-state]} @!ui
view-lookup (nu/lookup-map
:key
views)
current-view-key (or (:current-view-key @!state)
initial-view)
current-view (if (keyword? current-view-key)
(get view-lookup current-view-key)
{:comp current-view-key})
comp (:comp current-view)
comp (if (fn? comp)
(apply comp (:current-args @!state))
comp)
visible? (get @!state :visible?)
stick-to (or
(:stick-to current-view)
stick-to)
wrap-style
(condp = stick-to
:bot (merge
{:left 0
:right 0
:bottom 0}
(if visible?
(nc/transform "translate3d(0,0,0)")
(nc/transform "translate3d(0,15px,0)")))
:right (merge
{:top 0
:right 0
:bottom 0}
(if visible?
(nc/transform "translate3d(0,0,0)")
(nc/transform "translate3d(15px,0,0)")))
:left (merge
{:top 0
:left 0
:bottom 0}
(if visible?
(nc/transform "translate3d(0,0,0)")
(nc/transform "translate3d(-15px,0,0)")))
:top (merge
{:left 0
:right 0
:top 0}
(if visible?
(nc/transform "translate3d(0,0,0)")
(nc/transform "translate3d(0,-15px,0)"))))]
(when (and visible?
(not comp))
(throw (js/Error.
(str "No view for key: "
(pr-str current-view)))))
[:div.bottombar-wrapper
{:style (merge
{:position 'fixed
:opacity (if visible? 1 0)
:pointer-events (if visible? "auto" "none")}
wrap-style
(nc/transition "opacity 0.15s ease, transform 0.15s ease")
style)
:on-scroll (fn [e]
(.stopPropagation e))}
comp]))}))))
(defn $standalone [!state & args]
(let [[opts & children] (page/ensure-opts args)]
(vec
(concat
[$container
(merge
{:!state !state}
opts)]
children))))))