Skip to content

Commit

Permalink
documents modal and adds example
Browse files Browse the repository at this point in the history
  • Loading branch information
slagyr committed Dec 19, 2013
1 parent a177f54 commit 7080f6f
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 26 deletions.
74 changes: 73 additions & 1 deletion example/example.css
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,6 @@
/* --------------------------------------
* Autocomplete Field Styles
* -------------------------------------- */
.autocomplete-dropdown { .autocomplete-dropdown {
background-color: white; background-color: white;
border: 1px solid #25a8e0; border: 1px solid #25a8e0;
Expand Down Expand Up @@ -27,4 +30,73 @@
.autocomplete-dropdown .highlighted { .autocomplete-dropdown .highlighted {
background-color: #25a8e0; background-color: #25a8e0;
color: white; color: white;
} }

/* --------------------------------------
* Modal Styles
* -------------------------------------- */

.modal-backdrop {
position: fixed;
overflow-y: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 9000;
background-color: rgba(0, 0, 0, 0.5);
padding: 10px;
}

.modal {
outline: none;
aria-hidden: true;
z-index: 9001;
overflow: visible;
max-width: 800px;
width: 90%;
margin: 100px auto 0 auto;
background-color: #ffffff;

/*IE6-7*/
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
-webkit-box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
box-shadow: 0 3px 7px rgba(0, 0, 0, 0.3);
-webkit-background-clip: padding-box;
-moz-background-clip: padding-box;
background-clip: padding-box;
}

.modal-header {
padding: 1em 1em 0;
}

.modal-header button {
float: right;
font-size: 1.2em;
font-weight: 600;
background: transparent;
padding: 0;
margin: 0;
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
border: none;
cursor: pointer;
color: #333;
}

.modal-header h3 {
width: 95%;
display: inline-block;
}

.modal-body {
clear: both;
max-height: 85%;
overflow-y: auto;
padding: 1em;
}
6 changes: 4 additions & 2 deletions example/src/cljs/example/main.cljs
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns example.main (ns example.main
(:require [example.autocomplete-demo :refer [demo-autocomplete]])) (:require [example.autocomplete-demo :refer [demo-autocomplete]]
[example.modal-demo :refer [demo-modal]]))


(defn ^:export init [] (defn ^:export init []
(demo-autocomplete)) (demo-autocomplete)
(demo-modal))
19 changes: 19 additions & 0 deletions example/src/cljs/example/modal_demo.cljs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns example.modal-demo
(:require-macros [hiccup.core :as h])
(:require [com.eighthlight.filament.modal :as modal]
[com.eighthlight.filament.util :as util]
[domina :as dom]
[hiccup.core]))

(def modal (modal/create-modal "modal"))

(defn show-modal []
(modal/populate! modal {:title "Modal" :content "This is how you generate a modal using Filament."})
(modal/show! modal))

(defn arm-button []
(util/override-click! (dom/by-id "click-me") show-modal))

(defn demo-modal []
(dom/append! (.-body js/document) (h/html [:p [:button {:id "click-me"} "Click me!"]]))
(arm-button))
71 changes: 48 additions & 23 deletions src/cljs/com/eighthlight/filament/modal.cljs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
(:require [com.eighthlight.filament.util :as util] (:require [com.eighthlight.filament.util :as util]
[com.eighthlight.filament.fx :as fx] [com.eighthlight.filament.fx :as fx]
[domina :as dom] [domina :as dom]
[domina.css :as css]
[domina.events :as event] [domina.events :as event]
[hiccup.core])) [hiccup.core]))


(defn modal-view (defn modal-view
"Returns a hiccup data structure representing the modal DOM."
([id] (modal-view id "modal")) ([id] (modal-view id "modal"))
([id class] ([id class]
[:div {:tabindex "0" :id id :class (str class "-backdrop")} ; tabindex is necessary to grant focus [:div {:tabindex "0" :id id :class (str class "-backdrop")} ; tabindex is necessary to grant focus
Expand All @@ -18,61 +18,86 @@
[:h3]] [:h3]]
[:div {:class (str class "-body")}]]])) [:div {:class (str class "-body")}]]]))


(defn title-node [modal] (css/sel modal "h3")) (defn modal-node [modal] (first (dom/children modal)))
(defn header-node [modal] (first (dom/children (modal-node modal))))
(defn title-node [modal] (second (dom/children (header-node modal))))
(defn close-button [modal] (first (dom/children (header-node modal))))
(defn body [modal] (second (dom/children (modal-node modal))))


(defn show! [modal] (defn show!
"Attaches the modal to the DOM and makes it visible."
[modal]
(dom/set-style! modal :opacity 0) (dom/set-style! modal :opacity 0)
(dom/append! (css/sel "body") modal) (dom/append! (.-body js/document) modal)
(fx/show! modal) (fx/show! modal)
(.focus modal) (.focus modal)
(fx/fade-in modal (fx/fade-in modal
:duration 100 :duration 100
:on-end (fn [e] (dom/set-style! modal :opacity 100)))) :on-end (fn [e] (dom/set-style! modal :opacity 100))))


(defn hide! [modal] (defn hide!
"Hides the modal and dataches it from the DOM."
[modal]
(fx/fade-out modal (fx/fade-out modal
:duration 100 :duration 100
:on-finish #(do :on-finish #(do
(dom/detach! modal) (dom/detach! modal)
(fx/hide! modal)))) (fx/hide! modal))))


(defn set-title! [modal title] (defn set-title!
"Populates the title of the modal."
[modal title]
(dom/set-text! (title-node modal) title)) (dom/set-text! (title-node modal) title))


(defn title [modal] (defn title
"Returns the title of the modal."
[modal]
(dom/text (title-node modal))) (dom/text (title-node modal)))


(defn set-content! [modal content] (defn set-content!
(dom/set-html! (css/sel modal ".modal-body") content)) "Populates the body of the modal."
[modal content]
(dom/set-html! (body modal) content))


(defn body [modal] (defn populate!
(css/sel modal ".modal-body")) "Populates the modal. The second parameter should be map containing:

:title - a title for the modal
(defn populate! [modal {:keys [title content]}] :content - the main content of the modal"
[modal {:keys [title content]}]
(set-title! modal title) (set-title! modal title)
(set-content! modal content)) (set-content! modal content))


(defn clear! [modal] (defn clear!
"Clears out any content that is currently in the modal dom."
[modal]
(populate! modal {:title "" :content ""})) (populate! modal {:title "" :content ""}))


(defn process-keyup [modal e] (defn- process-keyup [modal e]
(when (and (util/ESC? e) (when (and (util/ESC? e)
(not= "SELECT" (.-tagName (event/target e)))) (not= "SELECT" (.-tagName (event/target e))))
(hide! modal))) (hide! modal)))


(defn bind-listeners [modal class] (defn- bind-listeners [modal class]
(event/listen! modal :keyup (partial process-keyup modal)) (event/listen! modal :keyup (partial process-keyup modal))
(event/listen! modal :click (fn [e] (hide! modal))) (util/override-click! modal (fn [e] (hide! modal)))
(event/listen! (util/override-click! (close-button modal) (fn [e] (hide! modal)))
(css/sel modal (str "." (str class "-close-button"))) (util/override-click! (modal-node modal) #(event/stop-propagation %)))
:click (fn [e] (hide! modal)))
(let [modal-node (css/sel modal (str "." class))]
(event/listen! modal-node :click (fn [e] (event/stop-propagation e)))))


(defn create-modal (defn create-modal
"Returns a datached modal DOM element with no content. This is typically used to create the modal at a high level
that can be reused. See populate!, show!, and hide! for usage.
The id parameter is added to the root DOM element so that it can be retreived easily.
The optional class parameter will be prefixed onto all classes applied to the elements. Defaults to 'modal'.
CSS: Presuming the default 'modal' class, the following selectors should be styled:
.modal-backdrop - covers the page and provide background for the foreground div
.modal - centered window for modal content
.modal-header - div at the top of the modal
.modal-header button - close button
.modal-header h3 - header/title for the modal
.modal-body - container for the modal content"
([id] (create-modal id "modal")) ([id] (create-modal id "modal"))
([id class] ([id class]
(let [modal (dom/single-node (h/html (modal-view id class)))] (let [modal (dom/single-node (h/html (modal-view id class)))]
(bind-listeners modal class) (bind-listeners modal class)
; (hide! modal)
modal))) modal)))

0 comments on commit 7080f6f

Please sign in to comment.