ui.cljs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. (ns frontend.ui
  2. (:require [rum.core :as rum]
  3. [frontend.rum :as r]
  4. ["react-transition-group" :refer [TransitionGroup CSSTransition]]
  5. ["react-textarea-autosize" :as TextareaAutosize]
  6. ["react-resize-context" :as Resize]
  7. [frontend.util :as util]
  8. [frontend.mixins :as mixins]
  9. [frontend.handler.notification :as notification-handler]
  10. [frontend.state :as state]
  11. [frontend.components.svg :as svg]
  12. [clojure.string :as string]
  13. [goog.object :as gobj]
  14. [goog.dom :as gdom]
  15. [medley.core :as medley]
  16. [frontend.ui.date-picker]
  17. [frontend.context.i18n :as i18n]))
  18. (defonce transition-group (r/adapt-class TransitionGroup))
  19. (defonce css-transition (r/adapt-class CSSTransition))
  20. (defonce textarea (r/adapt-class (gobj/get TextareaAutosize "default")))
  21. (def resize-provider (r/adapt-class (gobj/get Resize "ResizeProvider")))
  22. (def resize-consumer (r/adapt-class (gobj/get Resize "ResizeConsumer")))
  23. (rum/defc ls-textarea < rum/reactive
  24. [{:keys [on-change] :as props}]
  25. (let [skip-composition? (or
  26. (state/sub :editor/show-page-search?)
  27. (state/sub :editor/show-block-search?)
  28. (state/sub :editor/show-template-search?))
  29. on-composition (fn [e]
  30. (if skip-composition?
  31. (on-change e)
  32. (case e.type
  33. "compositionend" (do
  34. (state/set-editor-in-composition! false)
  35. (on-change e))
  36. (state/set-editor-in-composition! true))))
  37. props (assoc props
  38. :on-change (fn [e] (when-not (state/editor-in-composition?)
  39. (on-change e)))
  40. :on-composition-start on-composition
  41. :on-composition-update on-composition
  42. :on-composition-end on-composition)]
  43. (textarea props)))
  44. (rum/defc dropdown-content-wrapper [state content class]
  45. (let [class (or class
  46. (util/hiccup->class "origin-top-right.absolute.right-0.mt-2.rounded-md.shadow-lg"))]
  47. [:div.dropdown-wrapper
  48. {:class (str class " "
  49. (case state
  50. "entering" "transition ease-out duration-100 transform opacity-0 scale-95"
  51. "entered" "transition ease-out duration-100 transform opacity-100 scale-100"
  52. "exiting" "transition ease-in duration-75 transform opacity-100 scale-100"
  53. "exited" "transition ease-in duration-75 transform opacity-0 scale-95"))}
  54. content]))
  55. ;; public exports
  56. (rum/defcs dropdown < (mixins/modal :open?)
  57. [state content-fn modal-content-fn
  58. & [{:keys [modal-class z-index]
  59. :or {z-index 999}
  60. :as opts}]]
  61. (let [{:keys [open? toggle-fn]} state
  62. modal-content (modal-content-fn state)]
  63. [:div.ml-1.relative {:style {:z-index z-index}}
  64. (content-fn state)
  65. (css-transition
  66. {:in @open? :timeout 0}
  67. (fn [dropdown-state]
  68. (when @open?
  69. (dropdown-content-wrapper dropdown-state modal-content modal-class))))]))
  70. (rum/defc menu-link
  71. [options child]
  72. [:a.block.px-4.py-2.text-sm.text-gray-700.transition.ease-in-out.duration-150.cursor.menu-link
  73. options
  74. child])
  75. (rum/defc dropdown-with-links
  76. [content-fn links {:keys [modal-class links-header links-footer z-index] :as opts}]
  77. (dropdown
  78. content-fn
  79. (fn [{:keys [close-fn] :as state}]
  80. [:div.py-1.rounded-md.shadow-xs
  81. (when links-header links-header)
  82. (for [{:keys [options title icon]} links]
  83. (let [new-options
  84. (assoc options
  85. :on-click (fn [e]
  86. (when-let [on-click-fn (:on-click options)]
  87. (on-click-fn e))
  88. (close-fn)))
  89. child [:div
  90. {:style {:display "flex" :flex-direction "row"}}
  91. [:div {:style {:margin-right "8px"}} title]
  92. ;; [:div {:style {:position "absolute" :right "8px"}}
  93. ;; icon]
  94. ]]
  95. (rum/with-key
  96. (menu-link new-options child)
  97. title)))
  98. (when links-footer links-footer)])
  99. opts))
  100. (defn button
  101. [text & {:keys [background on-click href]
  102. :as option}]
  103. (let [class "inline-flex.items-center.px-3.py-2.border.border-transparent.text-sm.leading-4.font-medium.rounded-md.text-white.bg-indigo-600.hover:bg-indigo-700.focus:outline-none.focus:border-indigo-700.focus:shadow-outline-indigo.active:bg-indigo-700.transition.ease-in-out.duration-150.mt-1"
  104. class (if background (string/replace class "indigo" background) class)]
  105. (if href
  106. [:a.button (merge
  107. {:type "button"
  108. :class (util/hiccup->class class)}
  109. (dissoc option :background))
  110. text]
  111. [:button
  112. (merge
  113. {:type "button"
  114. :class (util/hiccup->class class)}
  115. (dissoc option :background))
  116. text])))
  117. (rum/defc notification-content
  118. [state content status uid]
  119. (when (and content status)
  120. (let [[color-class svg]
  121. (case status
  122. :success
  123. ["text-gray-900 dark:text-gray-300 "
  124. [:svg.h-6.w-6.text-green-400
  125. {:stroke "currentColor", :viewBox "0 0 24 24", :fill "none"}
  126. [:path
  127. {:d "M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
  128. :stroke-width "2"
  129. :stroke-linejoin "round"
  130. :stroke-linecap "round"}]]]
  131. :warning
  132. ["text-gray-900"
  133. [:svg.h-6.w-6.text-yellow-500
  134. {:stroke "currentColor", :viewBox "0 0 24 24", :fill "none"}
  135. [:path
  136. {:d "M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
  137. :stroke-width "2"
  138. :stroke-linejoin "round"
  139. :stroke-linecap "round"}]]]
  140. ["text-red-500"
  141. [:svg.h-6.w-6.text-red-500
  142. {:view-box "0 0 20 20", :fill "currentColor"}
  143. [:path
  144. {:clip-rule "evenodd"
  145. :d
  146. "M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z"
  147. :fill-rule "evenodd"}]]])]
  148. [:div.ui__notifications-content
  149. {:style {:z-index (if (or (= state "exiting")
  150. (= state "exited"))
  151. -1
  152. 99)
  153. :top "3.2em"}}
  154. [:div.max-w-sm.w-full.shadow-lg.rounded-lg.pointer-events-auto.notification-area
  155. {:class (case state
  156. "entering" "transition ease-out duration-300 transform opacity-0 translate-y-2 sm:translate-x-0"
  157. "entered" "transition ease-out duration-300 transform translate-y-0 opacity-100 sm:translate-x-0"
  158. "exiting" "transition ease-in duration-100 opacity-100"
  159. "exited" "transition ease-in duration-100 opacity-0")}
  160. [:div.rounded-lg.shadow-xs.overflow-hidden
  161. [:div.p-4
  162. [:div.flex.items-start
  163. [:div.flex-shrink-0
  164. svg]
  165. [:div.ml-3.w-0.flex-1
  166. [:div.text-sm.leading-5.font-medium {:style {:margin 0}
  167. :class color-class}
  168. content]]
  169. [:div.ml-4.flex-shrink-0.flex
  170. [:button.inline-flex.text-gray-400.focus:outline-none.focus:text-gray-500.transition.ease-in-out.duration-150
  171. {:on-click (fn []
  172. (notification-handler/clear! uid))}
  173. [:svg.h-5.w-5
  174. {:fill "currentColor", :view-Box "0 0 20 20"}
  175. [:path
  176. {:clip-rule "evenodd"
  177. :d
  178. "M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
  179. :fill-rule "evenodd"}]]]]]]]]])))
  180. (rum/defc notification < rum/reactive
  181. []
  182. (let [contents (state/sub :notification/contents)]
  183. (transition-group
  184. {:class-name "notifications ui__notifications"}
  185. (doall (map (fn [el]
  186. (let [k (first el)
  187. v (second el)]
  188. (css-transition
  189. {:timeout 100
  190. :key (name k)}
  191. (fn [state]
  192. (notification-content state (:content v) (:status v) k)))))
  193. contents)))))
  194. (defn checkbox
  195. [option]
  196. [:input.form-checkbox.h-4.w-4.transition.duration-150.ease-in-out
  197. (merge {:type "checkbox"} option)])
  198. (defn badge
  199. [text option]
  200. [:span.inline-flex.items-center.px-2.5.py-0.5.rounded-full.text-xs.font-medium.leading-4.bg-purple-100.text-purple-800
  201. option
  202. text])
  203. ;; scroll
  204. (defn get-doc-scroll-top []
  205. (.-scrollTop js/document.documentElement))
  206. (defn main-node
  207. []
  208. (gdom/getElement "main-content"))
  209. (defn get-scroll-top []
  210. (.-scrollTop (main-node)))
  211. (defn get-dynamic-style-node
  212. []
  213. (js/document.getElementById "dynamic-style-scope"))
  214. (defn inject-document-devices-envs!
  215. []
  216. (let [cl (.-classList js/document.documentElement)]
  217. (if util/mac? (.add cl "is-mac"))
  218. (if util/win32? (.add cl "is-win32"))
  219. (if (util/electron?) (.add cl "is-electron"))
  220. (if (util/ios?) (.add cl "is-ios"))
  221. (if (util/mobile?) (.add cl "is-mobile"))
  222. (if (util/safari?) (.add cl "is-safari"))
  223. (when (util/electron?)
  224. (js/window.apis.on "full-screen" #(js-invoke cl (if (= % "enter") "add" "remove") "is-fullscreen")))))
  225. (defn inject-dynamic-style-node!
  226. []
  227. (let [style (get-dynamic-style-node)]
  228. (if (nil? style)
  229. (let [node (js/document.createElement "style")]
  230. (set! (.-id node) "dynamic-style-scope")
  231. (.appendChild js/document.head node))
  232. style)))
  233. (defn setup-patch-ios-fixed-bottom-position!
  234. "fix a common issue about ios webpage viewport
  235. when soft keyboard setup"
  236. []
  237. (if (and
  238. (util/ios?)
  239. (not (nil? js/window.visualViewport)))
  240. (let [viewport js/visualViewport
  241. style (get-dynamic-style-node)
  242. sheet (.-sheet style)
  243. raf-pending? (atom false)
  244. set-raf-pending! #(reset! raf-pending? %)
  245. handler
  246. (fn []
  247. (if-not @raf-pending?
  248. (let [f (fn []
  249. (set-raf-pending! false)
  250. (let [vh (+ (.-offsetTop viewport) (.-height viewport))
  251. rule (.. sheet -rules (item 0))
  252. set-top #(set! (.. rule -style -top) (str % "px"))]
  253. (set-top vh)))]
  254. (set-raf-pending! true)
  255. (js/window.requestAnimationFrame f))))]
  256. (.insertRule sheet ".fix-ios-fixed-bottom {bottom:unset !important; transform: translateY(-100%); top: 100vh;}")
  257. (.addEventListener viewport "resize" handler)
  258. (.addEventListener viewport "scroll" handler)
  259. (fn []
  260. (.removeEventListener viewport "resize" handler)
  261. (.removeEventListener viewport "scroll" handler)))))
  262. ;; FIXME: compute the right scroll position when scrolling back to the top
  263. (defn on-scroll
  264. [on-load on-top-reached]
  265. (let [node js/document.documentElement
  266. full-height (gobj/get node "scrollHeight")
  267. scroll-top (gobj/get node "scrollTop")
  268. client-height (gobj/get node "clientHeight")
  269. bottom-reached? (<= (- full-height scroll-top client-height) 100)
  270. top-reached? (= scroll-top 0)]
  271. (when (and bottom-reached? on-load)
  272. (on-load))
  273. (when (and top-reached? on-top-reached)
  274. (on-top-reached))))
  275. (defn attach-listeners
  276. "Attach scroll and resize listeners."
  277. [state]
  278. (let [opts (-> state :rum/args second)
  279. debounced-on-scroll (util/debounce 500 #(on-scroll
  280. (:on-load opts) ; bottom reached
  281. (:on-top-reached opts)))]
  282. (mixins/listen state js/document :scroll debounced-on-scroll)))
  283. (rum/defcs infinite-list <
  284. (mixins/event-mixin attach-listeners)
  285. "Render an infinite list."
  286. [state body {:keys [on-load on-top-reached]
  287. :as opts}]
  288. body)
  289. (rum/defcs auto-complete <
  290. (rum/local 0 ::current-idx)
  291. (mixins/event-mixin
  292. (fn [state]
  293. (mixins/on-key-down
  294. state
  295. {;; up
  296. 38 (fn [_ e]
  297. (let [current-idx (get state ::current-idx)
  298. matched (first (:rum/args state))]
  299. (util/stop e)
  300. (cond
  301. (>= @current-idx 1)
  302. (swap! current-idx dec)
  303. (= @current-idx 0)
  304. (reset! current-idx (dec (count matched)))
  305. :else
  306. nil)
  307. (when-let [element (gdom/getElement (str "ac-" @current-idx))]
  308. (let [ac-inner (gdom/getElement "ui__ac-inner")
  309. element-top (gobj/get element "offsetTop")
  310. scroll-top (- (gobj/get element "offsetTop") 360)]
  311. (set! (.-scrollTop ac-inner) scroll-top)))))
  312. ;; down
  313. 40 (fn [state e]
  314. (let [current-idx (get state ::current-idx)
  315. matched (first (:rum/args state))]
  316. (util/stop e)
  317. (let [total (count matched)]
  318. (if (>= @current-idx (dec total))
  319. (reset! current-idx 0)
  320. (swap! current-idx inc)))
  321. (when-let [element (gdom/getElement (str "ac-" @current-idx))]
  322. (let [ac-inner (gdom/getElement "ui__ac-inner")
  323. element-top (gobj/get element "offsetTop")
  324. scroll-top (- (gobj/get element "offsetTop") 360)]
  325. (set! (.-scrollTop ac-inner) scroll-top)))))
  326. ;; enter
  327. 13 (fn [state e]
  328. (util/stop e)
  329. (let [[matched {:keys [on-chosen on-enter]}] (:rum/args state)]
  330. (let [current-idx (get state ::current-idx)]
  331. (if (and (seq matched)
  332. (> (count matched)
  333. @current-idx))
  334. (on-chosen (nth matched @current-idx) false)
  335. (and on-enter (on-enter state))))))})))
  336. [state matched {:keys [on-chosen
  337. on-shift-chosen
  338. on-enter
  339. empty-div
  340. item-render
  341. class]}]
  342. (let [current-idx (get state ::current-idx)]
  343. [:div#ui__ac {:class class}
  344. (if (seq matched)
  345. [:div#ui__ac-inner.hide-scrollbar
  346. (for [[idx item] (medley/indexed matched)]
  347. (rum/with-key
  348. (menu-link
  349. {:id (str "ac-" idx)
  350. :class (when (= @current-idx idx)
  351. "chosen")
  352. ;; :tab-index -1
  353. :on-click (fn [e]
  354. (.preventDefault e)
  355. (if (and (gobj/get e "shiftKey") on-shift-chosen)
  356. (on-shift-chosen item)
  357. (on-chosen item)))}
  358. (if item-render (item-render item) item))
  359. idx))]
  360. (when empty-div
  361. empty-div))]))
  362. (def datepicker frontend.ui.date-picker/date-picker)
  363. (defn toggle
  364. [on? on-click]
  365. [:a {:on-click on-click}
  366. [:span.relative.inline-block.flex-shrink-0.h-6.w-11.border-2.border-transparent.rounded-full.cursor-pointer.transition-colors.ease-in-out.duration-200.focus:outline-none.focus:shadow-outline
  367. {:aria-checked "false", :tab-index "0", :role "checkbox"
  368. :class (if on? "bg-indigo-600" "bg-gray-200")}
  369. [:span.inline-block.h-5.w-5.rounded-full.bg-white.shadow.transform.transition.ease-in-out.duration-200
  370. {:class (if on? "translate-x-5" "translate-x-0")
  371. :aria-hidden "true"}]]])
  372. (defn tooltip
  373. ([label children]
  374. (tooltip label children {}))
  375. ([label children {:keys [label-style]}]
  376. [:div.Tooltip {:style {:display "inline"}}
  377. [:div (cond->
  378. {:class "Tooltip__label"}
  379. label-style
  380. (assoc :style label-style))
  381. label]
  382. children]))
  383. (defonce modal-show? (atom false))
  384. (rum/defc modal-overlay
  385. [state]
  386. [:div.fixed.inset-0.transition-opacity
  387. {:class (case state
  388. "entering" "ease-out duration-300 opacity-0"
  389. "entered" "ease-out duration-300 opacity-100"
  390. "exiting" "ease-in duration-200 opacity-100"
  391. "exited" "ease-in duration-200 opacity-0")}
  392. [:div.absolute.inset-0.bg-gray-500.opacity-75]])
  393. (rum/defc modal-panel
  394. [panel-content state close-fn]
  395. [:div.relative.bg-white.rounded-lg.px-4.pt-5.pb-4.overflow-hidden.shadow-xl.transform.transition-all.sm:max-w-lg.sm:w-full.sm:p-6
  396. {:class (case state
  397. "entering" "ease-out duration-300 opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
  398. "entered" "ease-out duration-300 opacity-100 translate-y-0 sm:scale-100"
  399. "exiting" "ease-in duration-200 opacity-100 translate-y-0 sm:scale-100"
  400. "exited" "ease-in duration-200 opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95")}
  401. [:div.absolute.top-0.right-0.pt-4.pr-4
  402. [:button.text-gray-400.hover:text-gray-500.focus:outline-none.focus:text-gray-500.transition.ease-in-out.duration-150
  403. {:aria-label "Close"
  404. :type "button"
  405. :on-click close-fn}
  406. [:svg.h-6.w-6
  407. {:stroke "currentColor", :view-box "0 0 24 24", :fill "none"}
  408. [:path
  409. {:d "M6 18L18 6M6 6l12 12"
  410. :stroke-width "2"
  411. :stroke-linejoin "round"
  412. :stroke-linecap "round"}]]]]
  413. (panel-content close-fn)])
  414. (rum/defc modal < rum/reactive
  415. []
  416. (let [modal-panel-content (state/sub :modal/panel-content)
  417. show? (boolean modal-panel-content)
  418. close-fn #(state/close-modal!)
  419. modal-panel-content (or modal-panel-content (fn [close] [:div]))]
  420. [:div.fixed.bottom-0.inset-x-0.px-4.pb-4.sm:inset-0.sm:flex.sm:items-center.sm:justify-center
  421. {:style {:z-index (if show? 10 -1)}}
  422. (css-transition
  423. {:in show? :timeout 0}
  424. (fn [state]
  425. (modal-overlay state)))
  426. (css-transition
  427. {:in show? :timeout 0}
  428. (fn [state]
  429. (modal-panel modal-panel-content state close-fn)))]))
  430. (defn make-confirm-modal
  431. [{:keys [tag title sub-title sub-checkbox? on-cancel on-confirm] :as opts}]
  432. (fn [close-fn]
  433. (rum/with-context [[t] i18n/*tongue-context*]
  434. (let [*sub-checkbox-selected (and sub-checkbox? (atom []))]
  435. [:div.ui__confirm-modal
  436. {:class (str "is-" tag)}
  437. [:div.sm:flex.sm:items-start
  438. [:div.mx-auto.flex-shrink-0.flex.items-center.justify-center.h-12.w-12.rounded-full.bg-red-100.sm:mx-0.sm:h-10.sm:w-10
  439. [:svg.h-6.w-6.text-red-600
  440. {:stroke "currentColor", :view-box "0 0 24 24", :fill "none"}
  441. [:path
  442. {:d
  443. "M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
  444. :stroke-width "2"
  445. :stroke-linejoin "round"
  446. :stroke-linecap "round"}]]]
  447. [:div.mt-3.text-center.sm:mt-0.sm:ml-4.sm:text-left
  448. [:h2.headline.text-lg.leading-6.font-medium.text-gray-900
  449. (if (keyword? title) (t title) title)]
  450. [:label.sublabel
  451. (when sub-checkbox?
  452. (checkbox
  453. {:default-value false
  454. :on-change (fn [e]
  455. (let [checked (.. e -target -checked)]
  456. (reset! *sub-checkbox-selected [checked])))}))
  457. [:h3.subline.text-gray-400
  458. (if (keyword? sub-title)
  459. (t sub-title)
  460. sub-title)]]]]
  461. [:div.mt-5.sm:mt-4.sm:flex.sm:flex-row-reverse
  462. [:span.flex.w-full.rounded-md.shadow-sm.sm:ml-3.sm:w-auto
  463. [:button.inline-flex.justify-center.w-full.rounded-md.border.border-transparent.px-4.py-2.bg-indigo-600.text-base.leading-6.font-medium.text-white.shadow-sm.hover:bg-indigo-500.focus:outline-none.focus:border-indigo-700.focus:shadow-outline-indigo.transition.ease-in-out.duration-150.sm:text-sm.sm:leading-5
  464. {:type "button"
  465. :on-click #(and (fn? on-confirm)
  466. (on-confirm % {:close-fn close-fn
  467. :sub-selected (and *sub-checkbox-selected @*sub-checkbox-selected)}))}
  468. (t :yes)]]
  469. [:span.mt-3.flex.w-full.rounded-md.shadow-sm.sm:mt-0.sm:w-auto
  470. [:button.inline-flex.justify-center.w-full.rounded-md.border.border-gray-300.px-4.py-2.bg-white.text-base.leading-6.font-medium.text-gray-700.shadow-sm.hover:text-gray-500.focus:outline-none.focus:border-blue-300.focus:shadow-outline-blue.transition.ease-in-out.duration-150.sm:text-sm.sm:leading-5
  471. {:type "button"
  472. :on-click (comp on-cancel close-fn)}
  473. (t :cancel)]]]]))))
  474. (defn loading
  475. [content]
  476. [:div.flex.flex-row.items-center
  477. [:span.icon.flex.items-center svg/loading]
  478. [:span.text.pl-2 content]])
  479. (rum/defcs foldable <
  480. (rum/local false ::control?)
  481. (rum/local false ::collapsed?)
  482. {:will-mount (fn [state]
  483. (let [args (:rum/args state)]
  484. (when (true? (last args))
  485. (reset! (get state ::collapsed?) true)))
  486. state)}
  487. [state header content default-collapsed?]
  488. (let [control? (get state ::control?)
  489. collapsed? (get state ::collapsed?)]
  490. [:div.flex.flex-col
  491. [:div.content
  492. [:div.flex-1.flex-row.foldable-title {:on-mouse-over #(reset! control? true)
  493. :on-mouse-out #(reset! control? false)}
  494. [:div.flex.flex-row.items-center
  495. [:a.block-control.opacity-50.hover:opacity-100.mr-2
  496. {:style {:width 14
  497. :height 16
  498. :margin-left -24}
  499. :on-click (fn [e]
  500. (util/stop e)
  501. (swap! collapsed? not))}
  502. (cond
  503. @collapsed?
  504. (svg/caret-right)
  505. @control?
  506. (svg/caret-down)
  507. :else
  508. [:span ""])]
  509. (if (fn? header)
  510. (header @collapsed?)
  511. header)]]]
  512. [:div {:class (if @collapsed?
  513. "hidden"
  514. "initial")}
  515. (cond
  516. (and (fn? content) (not @collapsed?))
  517. (content)
  518. (fn? content)
  519. nil
  520. :else
  521. content)]]))
  522. (defn admonition
  523. [type content]
  524. (let [type (name type)]
  525. (when-let [icon (case (string/lower-case type)
  526. "note" svg/note
  527. "tip" svg/tip
  528. "important" svg/important
  529. "caution" svg/caution
  530. "warning" svg/warning
  531. nil)]
  532. [:div.flex.flex-row.admonitionblock.align-items {:class type}
  533. [:div.pr-4.admonition-icon.flex.flex-col.justify-center
  534. {:title (string/upper-case type)} (icon)]
  535. [:div.ml-4.text-lg
  536. content]])))
  537. (rum/defcs catch-error
  538. < {:did-catch
  539. (fn [state error info]
  540. (js/console.dir error)
  541. (assoc state ::error error))}
  542. [{error ::error, c :rum/react-component} error-view view]
  543. (if (some? error)
  544. error-view
  545. view))
  546. (rum/defc select
  547. [options on-change]
  548. [:select.mt-1.form-select.block.w-full.px-3.text-base.leading-6.border-gray-300.focus:outline-none.focus:shadow-outline-blue.focus:border-blue-300.sm:text-sm.sm:leading-5.ml-4
  549. {:style {:padding "0 0 0 12px"}
  550. :on-change (fn [e]
  551. (let [value (util/evalue e)]
  552. (on-change value)))}
  553. (for [{:keys [label value selected]} options]
  554. [:option (cond->
  555. {:key label
  556. :value (or value label)}
  557. selected
  558. (assoc :selected selected))
  559. label])])