Skip to content

Instantly share code, notes, and snippets.

@kommen
Created September 20, 2020 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kommen/5ceeddeabc31ca133eb72345fb08cc3f to your computer and use it in GitHub Desktop.
Save kommen/5ceeddeabc31ca133eb72345fb08cc3f to your computer and use it in GitHub Desktop.
poor/hiccup.cljs
;; "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