Skip to content

html literals? #416

@borkdude

Description

@borkdude

Experiment in html branch. Works with this input in playground:

Screenshot 2023-12-06 at 12 15 59 Screenshot 2023-12-05 at 19 50 46
(require
  '["https://cdn.jsdelivr.net/npm/lit-html@3.1.0/lit-html.js"
    :as lit])

(defn Ul []
  #html ^lit/html
  [:ul
   (mapv #(do #html ^lit/html [:li %])
     (shuffle (range 3)))])

(defn my-html
  []
  #html ^lit/html
  [:div "Hello"
   (Ul)])

(defn app-elt []
  (or
    (js/document.getElementById "app1")
    (doto (js/document.createElement "div")
      (set! -id "app1")
      (js/document.body.prepend))))

(lit/render (my-html) (app-elt))

#_(set! (.-innerHTML (app-elt)) my-html) 
(defn bar [ev]
  (js/console.log ev.target)
  (let [elt (js/document.getElementById "counter")
        v (-> elt .-innerHTML parse-long)
        v (inc v)]
    (set! elt -innerHTML v)))

(set! js/globalThis.bar bar)

(defn Foo []
  #html [:div
         [:div "Clicks: " [:span {:id "counter"} 0]]
         [:button {:onclick "bar(this)"}
          "Click me!"]
         (let [x "works"]
           #html [:div "Nesting " x "!"])])

(defn App [{:keys [name]}]
  #html [:div (str "Hello, " name)
         (Foo)])

#_(let [html (App {:name "Michiel Borkent"})
        elt (app-elt)]
    (set! elt.innerHTML html))

It could get annoying to have to write #html ^lit/html [....].

If users could write a macro to get rid of that boilerplate, e.g. (lit [:Ul ..]) this would be nice.
But then #html likely also has to be a macro instead of a reader tag (which is technically not impossible).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions