ui.cljs 29 KB

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