Created
September 20, 2020 14:12
-
-
Save kommen/5ceeddeabc31ca133eb72345fb08cc3f to your computer and use it in GitHub Desktop.
poor/hiccup.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; "forked" from @plexus' https://github.com/plexus/hoc-schedule/blob/25f8cf59e57a1d2177fb99b79f1f6fad2c9fc942/src/poor/hiccup.cljs | |
(ns poor.hiccup | |
(:require [goog.dom :as gdom] | |
[clojure.string :as str])) | |
(defn split-tag [tag] | |
(let [tag (name tag) | |
parts (str/split tag #"\.")] | |
[(first parts) | |
(rest parts)])) | |
(defn split-el [[tag & tail]] | |
(let [[tag kls] (split-tag tag)] | |
[tag | |
(cond-> (if (map? (first tail)) | |
(first tail) | |
{}) | |
(seq kls) | |
(update :class str (str/join " " kls))) | |
(if (map? (first tail)) | |
(next tail) | |
tail)])) | |
(declare h) | |
(defn h* [hiccup] | |
(let [els (h hiccup)] | |
(if (seq? els) | |
els | |
(list els)))) | |
(defn h [hiccup] | |
(cond | |
(string? hiccup) | |
(gdom/createTextNode hiccup) | |
(vector? hiccup) | |
(let [[tag attrs children] (split-el hiccup) | |
el (gdom/createElement tag)] | |
(gdom/setProperties el (clj->js attrs)) | |
(apply gdom/append el (mapcat h* children)) | |
el) | |
(seq? hiccup) | |
(mapcat h* hiccup) | |
(instance? js/HTMLElement hiccup) | |
hiccup | |
:else | |
(h (str hiccup)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment