ui.cljs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. ["react-tippy" :as react-tippy]
  8. [frontend.util :as util]
  9. [frontend.mixins :as mixins]
  10. [frontend.handler.notification :as notification-handler]
  11. [frontend.handler.ui :as ui-handler]
  12. [frontend.state :as state]
  13. [frontend.components.svg :as svg]
  14. [clojure.string :as string]
  15. [goog.object :as gobj]
  16. [goog.dom :as gdom]
  17. [medley.core :as medley]
  18. [frontend.ui.date-picker]
  19. [frontend.context.i18n :as i18n]
  20. [frontend.modules.shortcut.core :as shortcut]
  21. [lambdaisland.glogi :as log]
  22. [frontend.config :as config]))
  23. (defonce transition-group (r/adapt-class TransitionGroup))
  24. (defonce css-transition (r/adapt-class CSSTransition))
  25. (defonce textarea (r/adapt-class (gobj/get TextareaAutosize "default")))
  26. (def resize-provider (r/adapt-class (gobj/get Resize "ResizeProvider")))
  27. (def resize-consumer (r/adapt-class (gobj/get Resize "ResizeConsumer")))
  28. (def Tippy (r/adapt-class (gobj/get react-tippy "Tooltip")))
  29. (rum/defc ls-textarea < rum/reactive
  30. [{:keys [on-change] :as props}]
  31. (let [skip-composition? (or
  32. (state/sub :editor/show-page-search?)
  33. (state/sub :editor/show-block-search?)
  34. (state/sub :editor/show-template-search?))
  35. on-composition (fn [e]
  36. (if skip-composition?
  37. (on-change e)
  38. (case e.type
  39. "compositionend" (do
  40. (state/set-editor-in-composition! false)
  41. (on-change e))
  42. (state/set-editor-in-composition! true))))
  43. props (assoc props
  44. :on-change (fn [e] (when-not (state/editor-in-composition?)
  45. (on-change e)))
  46. :on-composition-start on-composition
  47. :on-composition-update on-composition
  48. :on-composition-end on-composition)]
  49. (textarea props)))
  50. (rum/defc dropdown-content-wrapper [state content class]
  51. (let [class (or class
  52. (util/hiccup->class "origin-top-right.absolute.right-0.mt-2.rounded-md.shadow-lg"))]
  53. [:div.dropdown-wrapper
  54. {:class (str class " "
  55. (case state
  56. "entering" "transition ease-out duration-100 transform opacity-0 scale-95"
  57. "entered" "transition ease-out duration-100 transform opacity-100 scale-100"
  58. "exiting" "transition ease-in duration-75 transform opacity-100 scale-100"
  59. "exited" "transition ease-in duration-75 transform opacity-0 scale-95"))}
  60. content]))
  61. ;; public exports
  62. (rum/defcs dropdown < (mixins/modal :open?)
  63. [state content-fn modal-content-fn
  64. & [{:keys [modal-class z-index]
  65. :or {z-index 999}
  66. :as opts}]]
  67. (let [{:keys [open? toggle-fn]} state
  68. modal-content (modal-content-fn state)]
  69. [:div.ml-1.relative {:style {:z-index z-index}}
  70. (content-fn state)
  71. (css-transition
  72. {:in @open? :timeout 0}
  73. (fn [dropdown-state]
  74. (when @open?
  75. (dropdown-content-wrapper dropdown-state modal-content modal-class))))]))
  76. (rum/defc menu-link
  77. [options child]
  78. [:a.block.px-4.py-2.text-sm.transition.ease-in-out.duration-150.cursor.menu-link
  79. options
  80. child])
  81. (rum/defc dropdown-with-links
  82. [content-fn links {:keys [modal-class links-header links-footer z-index] :as opts}]
  83. (dropdown
  84. content-fn
  85. (fn [{:keys [close-fn] :as state}]
  86. [:div.py-1.rounded-md.shadow-xs
  87. (when links-header links-header)
  88. (for [{:keys [options title icon]} (if (fn? links) (links) links)]
  89. (let [new-options
  90. (assoc options
  91. :on-click (fn [e]
  92. (when-let [on-click-fn (:on-click options)]
  93. (on-click-fn e))
  94. (close-fn)))
  95. child [:div
  96. {:style {:display "flex" :flex-direction "row"}}
  97. [:div {:style {:margin-right "8px"}} title]
  98. ;; [:div {:style {:position "absolute" :right "8px"}}
  99. ;; icon]
  100. ]]
  101. (rum/with-key
  102. (menu-link new-options child)
  103. title)))
  104. (when links-footer links-footer)])
  105. opts))
  106. (defn button
  107. [text & {:keys [background href class intent on-click small?]
  108. :or {small? false}
  109. :as option}]
  110. (let [klass (if-not intent ".bg-indigo-600.hover:bg-indigo-700.focus:border-indigo-700.active:bg-indigo-700")
  111. klass (if background (string/replace klass "indigo" background) klass)
  112. klass (if small? (str klass ".px-2.py-1") klass)]
  113. (if href
  114. [:a.ui__button.is-link
  115. (merge
  116. {:type "button"
  117. :class (str (util/hiccup->class klass) " " class)}
  118. (dissoc option :background :class))
  119. text]
  120. [:button.ui__button
  121. (merge
  122. {:type "button"
  123. :class (str (util/hiccup->class klass) " " class)}
  124. (dissoc option :background :class))
  125. text])))
  126. (rum/defc notification-content
  127. [state content status uid]
  128. (when (and content status)
  129. (let [[color-class svg]
  130. (case status
  131. :success
  132. ["text-gray-900 dark:text-gray-300 "
  133. [:svg.h-6.w-6.text-green-400
  134. {:stroke "currentColor", :viewBox "0 0 24 24", :fill "none"}
  135. [:path
  136. {:d "M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
  137. :stroke-width "2"
  138. :stroke-linejoin "round"
  139. :stroke-linecap "round"}]]]
  140. :warning
  141. ["text-gray-900 dark:text-gray-300 "
  142. [:svg.h-6.w-6.text-yellow-500
  143. {:stroke "currentColor", :viewBox "0 0 24 24", :fill "none"}
  144. [:path
  145. {:d "M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"
  146. :stroke-width "2"
  147. :stroke-linejoin "round"
  148. :stroke-linecap "round"}]]]
  149. ["text-red-500"
  150. [:svg.h-6.w-6.text-red-500
  151. {:view-box "0 0 20 20", :fill "currentColor"}
  152. [:path
  153. {:clip-rule "evenodd"
  154. :d
  155. "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"
  156. :fill-rule "evenodd"}]]])]
  157. [:div.ui__notifications-content
  158. {:style {:z-index (if (or (= state "exiting")
  159. (= state "exited"))
  160. -1
  161. 99)
  162. :top "3.2em"}}
  163. [:div.max-w-sm.w-full.shadow-lg.rounded-lg.pointer-events-auto.notification-area
  164. {:class (case state
  165. "entering" "transition ease-out duration-300 transform opacity-0 translate-y-2 sm:translate-x-0"
  166. "entered" "transition ease-out duration-300 transform translate-y-0 opacity-100 sm:translate-x-0"
  167. "exiting" "transition ease-in duration-100 opacity-100"
  168. "exited" "transition ease-in duration-100 opacity-0")}
  169. [:div.rounded-lg.shadow-xs.overflow-hidden
  170. [:div.p-4
  171. [:div.flex.items-start
  172. [:div.flex-shrink-0
  173. svg]
  174. [:div.ml-3.w-0.flex-1
  175. [:div.text-sm.leading-5.font-medium {:style {:margin 0}
  176. :class color-class}
  177. content]]
  178. [:div.ml-4.flex-shrink-0.flex
  179. [:button.inline-flex.text-gray-400.focus:outline-none.focus:text-gray-500.transition.ease-in-out.duration-150
  180. {:on-click (fn []
  181. (notification-handler/clear! uid))}
  182. [:svg.h-5.w-5
  183. {:fill "currentColor", :view-Box "0 0 20 20"}
  184. [:path
  185. {:clip-rule "evenodd"
  186. :d
  187. "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"
  188. :fill-rule "evenodd"}]]]]]]]]])))
  189. (rum/defc notification < rum/reactive
  190. []
  191. (let [contents (state/sub :notification/contents)]
  192. (transition-group
  193. {:class-name "notifications ui__notifications"}
  194. (doall (map (fn [el]
  195. (let [k (first el)
  196. v (second el)]
  197. (css-transition
  198. {:timeout 100
  199. :key (name k)}
  200. (fn [state]
  201. (notification-content state (:content v) (:status v) k)))))
  202. contents)))))
  203. (defn checkbox
  204. [option]
  205. [:input.form-checkbox.h-4.w-4.transition.duration-150.ease-in-out
  206. (merge {:type "checkbox"} option)])
  207. (defn badge
  208. [text option]
  209. [: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
  210. option
  211. text])
  212. ;; scroll
  213. (defn get-doc-scroll-top []
  214. (.-scrollTop js/document.documentElement))
  215. (defn main-node
  216. []
  217. (gdom/getElement "main-content"))
  218. (defn get-scroll-top []
  219. (.-scrollTop (main-node)))
  220. (defn get-dynamic-style-node
  221. []
  222. (js/document.getElementById "dynamic-style-scope"))
  223. (defn inject-document-devices-envs!
  224. []
  225. (let [cl (.-classList js/document.documentElement)]
  226. (if util/mac? (.add cl "is-mac"))
  227. (if util/win32? (.add cl "is-win32"))
  228. (if (util/electron?) (.add cl "is-electron"))
  229. (if (util/ios?) (.add cl "is-ios"))
  230. (if (util/mobile?) (.add cl "is-mobile"))
  231. (if (util/safari?) (.add cl "is-safari"))
  232. (when (util/electron?)
  233. (js/window.apis.on "full-screen" #(js-invoke cl (if (= % "enter") "add" "remove") "is-fullscreen")))))
  234. (defn inject-dynamic-style-node!
  235. []
  236. (let [style (get-dynamic-style-node)]
  237. (if (nil? style)
  238. (let [node (js/document.createElement "style")]
  239. (set! (.-id node) "dynamic-style-scope")
  240. (.appendChild js/document.head node))
  241. style)))
  242. (defn setup-patch-ios-fixed-bottom-position!
  243. "fix a common issue about ios webpage viewport
  244. when soft keyboard setup"
  245. []
  246. (if (and
  247. (util/ios?)
  248. (not (nil? js/window.visualViewport)))
  249. (let [viewport js/visualViewport
  250. style (get-dynamic-style-node)
  251. sheet (.-sheet style)
  252. raf-pending? (atom false)
  253. set-raf-pending! #(reset! raf-pending? %)
  254. handler
  255. (fn []
  256. (if-not @raf-pending?
  257. (let [f (fn []
  258. (set-raf-pending! false)
  259. (let [vh (+ (.-offsetTop viewport) (.-height viewport))
  260. rule (.. sheet -rules (item 0))
  261. set-top #(set! (.. rule -style -top) (str % "px"))]
  262. (set-top vh)))]
  263. (set-raf-pending! true)
  264. (js/window.requestAnimationFrame f))))]
  265. (.insertRule sheet ".fix-ios-fixed-bottom {bottom:unset !important; transform: translateY(-100%); top: 100vh;}")
  266. (.addEventListener viewport "resize" handler)
  267. (.addEventListener viewport "scroll" handler)
  268. (fn []
  269. (.removeEventListener viewport "resize" handler)
  270. (.removeEventListener viewport "scroll" handler)))))
  271. (defn setup-system-theme-effect!
  272. []
  273. (let [^js schemaMedia (js/window.matchMedia "(prefers-color-scheme: dark)")]
  274. (.addEventListener schemaMedia "change" state/sync-system-theme!)
  275. (state/sync-system-theme!)
  276. #(.removeEventListener schemaMedia "change" state/sync-system-theme!)))
  277. (defn set-global-active-keystroke [val]
  278. (.setAttribute js/document.body "data-active-keystroke" val))
  279. (defn setup-active-keystroke! []
  280. (let [active-keystroke (atom #{})
  281. handle-global-keystroke (fn [down? e]
  282. (let [handler (if down? conj disj)
  283. keystroke e.key]
  284. (swap! active-keystroke handler keystroke))
  285. (set-global-active-keystroke (apply str (interpose "+" (vec @active-keystroke)))))
  286. keydown-handler (partial handle-global-keystroke true)
  287. keyup-handler (partial handle-global-keystroke false)
  288. clear-all #(do (set-global-active-keystroke "")
  289. (reset! active-keystroke #{}))]
  290. (.addEventListener js/window "keydown" keydown-handler)
  291. (.addEventListener js/window "keyup" keyup-handler)
  292. (.addEventListener js/window "blur" clear-all)
  293. (.addEventListener js/window "visibilitychange" clear-all)
  294. (fn []
  295. (.removeEventListener js/window "keydown" keydown-handler)
  296. (.removeEventListener js/window "keyup" keyup-handler)
  297. (.removeEventListener js/window "blur" clear-all)
  298. (.removeEventListener js/window "visibilitychange" clear-all))))
  299. (defn on-scroll
  300. [node on-load on-top-reached]
  301. (let [full-height (gobj/get node "scrollHeight")
  302. scroll-top (gobj/get node "scrollTop")
  303. client-height (gobj/get node "clientHeight")
  304. bottom-reached? (<= (- full-height scroll-top client-height) 100)
  305. top-reached? (= scroll-top 0)]
  306. (when (and bottom-reached? on-load)
  307. (on-load))
  308. (when (and top-reached? on-top-reached)
  309. (on-top-reached))))
  310. (defn attach-listeners
  311. "Attach scroll and resize listeners."
  312. [state]
  313. (let [list-element-id (first (:rum/args state))
  314. opts (-> state :rum/args (nth 2))
  315. node (js/document.getElementById list-element-id)
  316. debounced-on-scroll (util/debounce 500 #(on-scroll
  317. node
  318. (:on-load opts) ; bottom reached
  319. (:on-top-reached opts)))]
  320. (mixins/listen state node :scroll debounced-on-scroll)))
  321. (rum/defcs infinite-list <
  322. (mixins/event-mixin attach-listeners)
  323. "Render an infinite list."
  324. [state list-element-id body {:keys [on-load has-more on-top-reached]}]
  325. (rum/with-context [[t] i18n/*tongue-context*]
  326. (rum/fragment
  327. body
  328. (when has-more
  329. [:a.fade-link.text-link.font-bold.text-4xl
  330. {:on-click on-load}
  331. (t :page/earlier)]))))
  332. (rum/defcs auto-complete <
  333. (rum/local 0 ::current-idx)
  334. (shortcut/mixin :shortcut.handler/auto-complete)
  335. [state
  336. matched
  337. {:keys [on-chosen
  338. on-shift-chosen
  339. get-group-name
  340. empty-div
  341. item-render
  342. class]}]
  343. (let [current-idx (get state ::current-idx)]
  344. [:div#ui__ac {:class class}
  345. (if (seq matched)
  346. [:div#ui__ac-inner.hide-scrollbar
  347. (for [[idx item] (medley/indexed matched)]
  348. [:<>
  349. {:key idx}
  350. (let [item-cp
  351. [:div {:key idx}
  352. (let [chosen? (= @current-idx idx)]
  353. (menu-link
  354. {:id (str "ac-" idx)
  355. :class (when chosen? "chosen")
  356. :on-mouse-enter #(reset! current-idx idx)
  357. :on-mouse-down (fn [e]
  358. (util/stop e)
  359. (if (and (gobj/get e "shiftKey") on-shift-chosen)
  360. (on-shift-chosen item)
  361. (on-chosen item)))}
  362. (if item-render (item-render item chosen?) item)))]]
  363. (if get-group-name
  364. (if-let [group-name (get-group-name item)]
  365. [:div
  366. [:div.ui__ac-group-name group-name]
  367. item-cp]
  368. item-cp)
  369. item-cp))])]
  370. (when empty-div
  371. empty-div))]))
  372. (def datepicker frontend.ui.date-picker/date-picker)
  373. (defn toggle
  374. ([on? on-click] (toggle on? on-click false))
  375. ([on? on-click small?]
  376. [:a.ui__toggle {:on-click on-click
  377. :class (if small? "is-small" "")}
  378. [:span.wrapper.transition-colors.ease-in-out.duration-200
  379. {:aria-checked "false", :tab-index "0", :role "checkbox"
  380. :class (if on? "bg-indigo-600" "bg-gray-200")}
  381. [:span.switcher.transform.transition.ease-in-out.duration-200
  382. {:class (if on? (if small? "translate-x-4" "translate-x-5") "translate-x-0")
  383. :aria-hidden "true"}]]]))
  384. ;; `sequence` can be a list of symbols or strings
  385. (defn keyboard-shortcut [sequence]
  386. [:div.keyboard-shortcut
  387. (map-indexed (fn [i key]
  388. [:code {:key i}
  389. ;; Display "cmd" rather than "meta" to the user to describe the Mac
  390. ;; mod key, because that's what the Mac keyboards actually say.
  391. (if (or (= :meta key) (= "meta" key))
  392. (util/meta-key-name)
  393. (name key))])
  394. sequence)])
  395. (defonce modal-show? (atom false))
  396. (rum/defc modal-overlay
  397. [state close-fn]
  398. [:div.ui__modal-overlay
  399. {:class (case state
  400. "entering" "ease-out duration-300 opacity-0"
  401. "entered" "ease-out duration-300 opacity-100"
  402. "exiting" "ease-in duration-200 opacity-100"
  403. "exited" "ease-in duration-200 opacity-0")
  404. :on-click close-fn}
  405. [:div.absolute.inset-0.opacity-75]])
  406. (rum/defc modal-panel
  407. [panel-content transition-state close-fn fullscreen?]
  408. [:div.ui__modal-panel.transform.transition-all.sm:min-w-lg.sm
  409. {:class (case transition-state
  410. "entering" "ease-out duration-300 opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
  411. "entered" "ease-out duration-300 opacity-100 translate-y-0 sm:scale-100"
  412. "exiting" "ease-in duration-200 opacity-100 translate-y-0 sm:scale-100"
  413. "exited" "ease-in duration-200 opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95")}
  414. [:div.absolute.top-0.right-0.pt-2.pr-2
  415. [:a.ui__modal-close.opacity-60.hover:opacity-100
  416. {:aria-label "Close"
  417. :type "button"
  418. :on-click close-fn}
  419. [:svg.h-6.w-6
  420. {:stroke "currentColor", :view-box "0 0 24 24", :fill "none"}
  421. [:path
  422. {:d "M6 18L18 6M6 6l12 12"
  423. :stroke-width "2"
  424. :stroke-linejoin "round"
  425. :stroke-linecap "round"}]]]]
  426. [:div {:class (if fullscreen? "" "panel-content")}
  427. (panel-content close-fn)]])
  428. (rum/defc modal < rum/reactive
  429. (mixins/event-mixin
  430. (fn [state]
  431. (mixins/hide-when-esc-or-outside
  432. state
  433. :on-hide (fn []
  434. (some->
  435. (.querySelector (rum/dom-node state) "button.ui__modal-close")
  436. (.click)))
  437. :outside? false)
  438. (mixins/on-key-down
  439. state
  440. {;; enter
  441. 13 (fn [state e]
  442. (some->
  443. (.querySelector (rum/dom-node state) "button.ui__modal-enter")
  444. (.click)))})))
  445. []
  446. (let [modal-panel-content (state/sub :modal/panel-content)
  447. fullscreen? (state/sub :modal/fullscreen?)
  448. show? (boolean modal-panel-content)
  449. close-fn (fn []
  450. (state/close-modal!)
  451. (state/close-settings!))
  452. modal-panel-content (or modal-panel-content (fn [close] [:div]))]
  453. [:div.ui__modal
  454. {:style {:z-index (if show? 10 -1)}}
  455. (css-transition
  456. {:in show? :timeout 0}
  457. (fn [state]
  458. (modal-overlay state close-fn)))
  459. (css-transition
  460. {:in show? :timeout 0}
  461. (fn [state]
  462. (modal-panel modal-panel-content state close-fn fullscreen?)))]))
  463. (defn make-confirm-modal
  464. [{:keys [tag title sub-title sub-checkbox? on-cancel on-confirm]
  465. :or {on-cancel #()}
  466. :as opts}]
  467. (fn [close-fn]
  468. (rum/with-context [[t] i18n/*tongue-context*]
  469. (let [*sub-checkbox-selected (and sub-checkbox? (atom []))]
  470. [:div.ui__confirm-modal
  471. {:class (str "is-" tag)}
  472. [:div.sm:flex.sm:items-start
  473. [: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
  474. [:svg.h-6.w-6.text-red-600
  475. {:stroke "currentColor", :view-box "0 0 24 24", :fill "none"}
  476. [:path
  477. {:d
  478. "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"
  479. :stroke-width "2"
  480. :stroke-linejoin "round"
  481. :stroke-linecap "round"}]]]
  482. [:div.mt-3.text-center.sm:mt-0.sm:ml-4.sm:text-left
  483. [:h2.headline.text-lg.leading-6.font-medium
  484. (if (keyword? title) (t title) title)]
  485. [:label.sublabel
  486. (when sub-checkbox?
  487. (checkbox
  488. {:default-value false
  489. :on-change (fn [e]
  490. (let [checked (.. e -target -checked)]
  491. (reset! *sub-checkbox-selected [checked])))}))
  492. [:h3.subline.text-gray-400
  493. (if (keyword? sub-title)
  494. (t sub-title)
  495. sub-title)]]]]
  496. [:div.mt-5.sm:mt-4.sm:flex.sm:flex-row-reverse
  497. [:span.flex.w-full.rounded-md.shadow-sm.sm:ml-3.sm:w-auto
  498. [: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
  499. {:type "button"
  500. :on-click #(and (fn? on-confirm)
  501. (on-confirm % {:close-fn close-fn
  502. :sub-selected (and *sub-checkbox-selected @*sub-checkbox-selected)}))}
  503. (t :yes)]]
  504. [:span.mt-3.flex.w-full.rounded-md.shadow-sm.sm:mt-0.sm:w-auto
  505. [: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
  506. {:type "button"
  507. :on-click (comp on-cancel close-fn)}
  508. (t :cancel)]]]]))))
  509. (defn loading
  510. [content]
  511. [:div.flex.flex-row.items-center
  512. [:span.icon.flex.items-center svg/loading]
  513. [:span.text.pl-2 content]])
  514. (rum/defc rotating-arrow
  515. [collapsed?]
  516. [:span
  517. {:class (if collapsed? "rotating-arrow collapsed" "rotating-arrow not-collapsed")}
  518. (svg/caret-right)])
  519. (rum/defcs foldable <
  520. (rum/local false ::control?)
  521. (rum/local false ::collapsed?)
  522. {:will-mount (fn [state]
  523. (let [args (:rum/args state)]
  524. (when (true? (last args))
  525. (reset! (get state ::collapsed?) true)))
  526. state)}
  527. [state header content default-collapsed?]
  528. (let [control? (get state ::control?)
  529. collapsed? (get state ::collapsed?)]
  530. [:div.flex.flex-col
  531. [:div.content
  532. [:div.flex-1.flex-row.foldable-title {:on-mouse-over #(reset! control? true)
  533. :on-mouse-out #(reset! control? false)}
  534. [:div.flex.flex-row.items-center
  535. [:a.block-control.opacity-50.hover:opacity-100.mr-2
  536. {:style {:width 14
  537. :height 16
  538. :margin-left -24}
  539. :on-mouse-down (fn [e]
  540. (util/stop e)
  541. (swap! collapsed? not))}
  542. [:span {:class (if @control? "control-show" "control-hide")}
  543. (rotating-arrow @collapsed?)]]
  544. (if (fn? header)
  545. (header @collapsed?)
  546. header)]]]
  547. [:div {:class (if @collapsed? "hidden" "initial")
  548. :on-mouse-down (fn [e] (.stopPropagation e))}
  549. (if (fn? content)
  550. (if (not @collapsed?) (content) nil)
  551. content)]]))
  552. (defn admonition
  553. [type content]
  554. (let [type (name type)]
  555. (when-let [icon (case (string/lower-case type)
  556. "note" svg/note
  557. "tip" svg/tip
  558. "important" svg/important
  559. "caution" svg/caution
  560. "warning" svg/warning
  561. "pinned" svg/pinned
  562. nil)]
  563. [:div.flex.flex-row.admonitionblock.align-items {:class type}
  564. [:div.pr-4.admonition-icon.flex.flex-col.justify-center
  565. {:title (string/upper-case type)} (icon)]
  566. [:div.ml-4.text-lg
  567. content]])))
  568. (rum/defcs catch-error
  569. < {:did-catch
  570. (fn [state error info]
  571. (js/console.dir error)
  572. (assoc state ::error error))}
  573. [{error ::error, c :rum/react-component} error-view view]
  574. (when error
  575. (js/console.error error)
  576. (log/error :ui/catch-error error))
  577. (if (and (not config/dev?) (some? error))
  578. error-view
  579. view))
  580. (rum/defc select
  581. [options on-change class]
  582. [:select.mt-1.block.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
  583. {:class (or class "form-select")
  584. :style {:padding "0 0 0 12px"}
  585. :on-change (fn [e]
  586. (let [value (util/evalue e)]
  587. (on-change value)))}
  588. (for [{:keys [label value selected]} options]
  589. [:option (cond->
  590. {:key label
  591. :value (or value label)}
  592. selected
  593. (assoc :selected selected))
  594. label])])
  595. (rum/defcs tippy < rum/static
  596. (rum/local false ::mounted?)
  597. [state {:keys [fixed-position? open?] :as opts} child]
  598. (let [*mounted? (::mounted? state)
  599. mounted? @*mounted?
  600. manual (not= open? nil)]
  601. (Tippy (->
  602. (merge {:arrow true
  603. :sticky true
  604. :theme "customized"
  605. :disabled (not (state/enable-tooltip?))
  606. :unmountHTMLWhenHide true
  607. :open (if manual open? @*mounted?)
  608. :trigger (if manual "manual" "mouseenter focus")
  609. ;; See https://github.com/tvkhoa/react-tippy/issues/13
  610. :popperOptions (if fixed-position?
  611. {:modifiers {:flip {:enabled false}
  612. :hide {:enabled false}
  613. :preventOverflow {:enabled false}}}
  614. {})
  615. :onShow #(reset! *mounted? true)
  616. :onHide #(reset! *mounted? false)}
  617. opts)
  618. (assoc :html (if (or open? mounted?)
  619. (when-let [html (:html opts)]
  620. (if (fn? html)
  621. (html)
  622. [:div.pr-3.py-1
  623. html]))
  624. [:div {:key "tippy"} ""])))
  625. child)))
  626. (defn slider
  627. [default-value {:keys [min max on-change]}]
  628. [:input.cursor-pointer
  629. {:type "range"
  630. :value (int default-value)
  631. :min min
  632. :max max
  633. :style {:width "100%"}
  634. :on-change #(let [value (util/evalue %)]
  635. (on-change value))}])