rum

Simple, decomplected, isomorphic HTML UI library for Clojure and ClojureScript | 0.12.8 https://github.com/tonsky/rum/blob/gh-pages/CHANGELOG.md#0128
michihuber 2021-05-21T07:39:44.001800Z

Hi there, can anybody help me with a macro outputting a rum component? I want to add some sugar for standard components in my app, that always require the same set of mixins

(rum.core/defcs yo < editor.components.mixins/dispatching ,,,
  [a b]
  [:div "hello"])
Should become:
(defco yo [a b]
  [:div "hello"])
with this macro:
(defmacro defco [sym args & body]
  `(def
     ~sym
     (rum.core/build-defcs
       (fn ~(cons args body))
       [editor.components.mixins/dispatching ,,,]
       ~(str sym))))
the two forms above macroexpand-1 to exactly the same form, but my macro throws a react error, because :div is interpreted as a child (but react cannot render objects (keyword object in this instance)). any ideas?

Azzurite 2021-05-23T12:40:10.002700Z

I've also struggled with this and gave up (but mostly because my macro knowledge was terrible, I didn't run into the same issue)

Azzurite 2021-05-23T12:49:32.002900Z

I took you having this problem as well as a challenge and tried again, try this please:

(defmacro defco [sym args & body]
  `(rum.core/defc
     ~sym
     ~'< ALL_YOUR_MIXINS
     ~args
     ~@body))

Azzurite 2021-05-23T12:49:41.003100Z

^ @michihuber

Azzurite 2021-05-23T14:06:05.003300Z

I've also arrived myself at this version, which you can probably adjust to your liking:

(defn insert
  "Inserts an element into a collection at the given index"
  [coll idx elem]
  (concat
    (take idx coll)
    [elem]
    (drop idx coll)))

(defmacro def-styled
  "rum/defc but automatically adds the reactive mixin"
  [sym & args]
  (let [docstring (when (= (second args) '<)
                    (first args))
        filtered-args (if (= (first args) '<)
                        (rest args)
                        (if docstring
                          (nthrest args 2)
                          args))
        definition `(rum/defc
                      ~sym
                      ~'< reactive
                      ~@filtered-args)]
    (if docstring
      (insert definition 2 docstring)
      definition)))

michihuber 2021-05-26T07:17:21.005100Z

awesome, thank you, @azzurite

michihuber 2021-05-21T07:42:42.002500Z

when I change :div to "div" it works (showing div as a string in the dom, but no error)