0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-02 20:39:09 -05:00
penpot/frontend/uxbox/ui/lightbox.cljs

69 lines
1.8 KiB
Text
Raw Normal View History

2015-12-14 09:50:19 +02:00
(ns uxbox.ui.lightbox
2015-12-14 10:48:50 +02:00
(:require [sablono.core :as html :refer-macros [html]]
[rum.core :as rum]
2015-12-17 16:43:58 +02:00
[uxbox.ui.util :as util]
2015-12-14 09:50:19 +02:00
[uxbox.ui.keyboard :as k]
[goog.events :as events])
(:import goog.events.EventType))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; State Management
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defonce +current+ (atom nil))
(defn open!
([kind]
(open! kind nil))
([kind params]
(reset! +current+ (merge {:name kind} params))))
2015-12-14 09:50:19 +02:00
(defn close!
[]
(reset! +current+ nil))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; UI
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmulti render-lightbox :name)
2015-12-14 09:50:19 +02:00
(defmethod render-lightbox :default [_] nil)
(defn- on-esc-clicked
[e]
(when (k/esc? e)
(close!)))
(defn- lightbox-will-mount
[own]
(let [key (events/listen js/document
EventType.KEYDOWN
on-esc-clicked)]
(assoc own ::eventkey key)))
2015-12-14 09:50:19 +02:00
(defn- lightbox-will-umount
[own]
(let [key (::eventkey own)]
(events/unlistenByKey key)
(dissoc own ::eventkey)))
(defn- lightbox-transfer-state
[old-own own]
(assoc own ::eventkey (::eventkey old-own)))
2015-12-14 09:50:19 +02:00
(defn- lightbox-render
[own]
(let [params (rum/react +current+)]
2015-12-14 10:48:50 +02:00
(html
[:div.lightbox {:class (when (nil? params) "hide")}
(render-lightbox params)])))
2015-12-14 09:50:19 +02:00
2015-12-14 10:48:50 +02:00
(def ^:static lightbox
2015-12-14 09:50:19 +02:00
(util/component
{:name "lightbox"
:render lightbox-render
:transfer-state lightbox-transfer-state
2015-12-14 09:50:19 +02:00
:will-mount lightbox-will-mount
:will-unmount lightbox-will-umount
:mixins [rum/reactive]}))