0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-10 00:58:26 -05:00

Add own defc macro for more easy define components with less boilerplate.

This commit is contained in:
Andrey Antukh 2016-07-09 16:36:30 +03:00
parent d8057f3c83
commit 4f7e1f0a98
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95

40
src/uxbox/util/mixins.clj Normal file
View file

@ -0,0 +1,40 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.util.mixins
(:require [rum.core :as rum]
[sablono.compiler :as s]))
(defn- parse-defc
[args]
(loop [r {}
s 0
v (first args)
n (rest args)]
(case s
0 (if (symbol? v)
(recur (assoc r :name v) (inc s) (first n) (rest n))
(throw (ex-info "Invalid" {})))
1 (if (string? v)
(recur (assoc r :doc v) (inc s) (first n) (rest n))
(recur r (inc s) v n))
2 (if (map? v)
(if-let [mixins (:mixins v)]
(let [spec (dissoc v :mixins)
mixins (if (empty? spec)
mixins
(conj mixins spec))]
(recur (assoc r :mixins mixins) (inc s) (first n) (rest n)))
(recur (assoc r :mixins [v]) (inc s) (first n) (rest n)))
(recur r (inc s) v n))
3 (let [sym (:name r)
func `(fn ~sym ~v ~(s/compile-html `(do ~@n)))]
[func (:doc r) (:mixins r) sym]))))
(defmacro defc
[& args]
(let [[render doc mixins cname] (parse-defc args)]
`(def ~cname ~doc (rum/build-defc ~render ~mixins ~(str cname)))))