Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience functions #10

Open
tatut opened this issue Nov 13, 2017 · 3 comments
Open

Convenience functions #10

tatut opened this issue Nov 13, 2017 · 3 comments

Comments

@tatut
Copy link
Contributor

tatut commented Nov 13, 2017

Currently combining style information with other attributes seems quite verbose, like:

[:a (merge (stylefy/use-style some-style-ns/link-style)
                  {:on-click #(...)})
  "click me"]

A more convenient shorthand would be nice (though I don't yet know what would be the best).

Perhaps defining a pre-styled element, like:

;; define pre-styled constructor
(def my-link (stylefy/with-style :a some-style-ns/link-style))

(defn my-component []
 ;; use it like any reagent component
  [my-link {:on-click #(...)} "click me"])

This can obviously be done separately without adding it to stylefy, but I think a recommended way would be nice.

@Jarzka
Copy link
Owner

Jarzka commented Nov 17, 2017

Neat, this idea is worth testing!

@Jarzka
Copy link
Owner

Jarzka commented Apr 20, 2018

I think this problem got a bit less painful after use-style started accepting HTML attributes as the second parameter.

@nijk
Copy link

nijk commented Apr 25, 2018

Agreed, and further convenience wrappers can be provided by wrapping the component in an HOC that does a lot of the hard work for you, e.g.

[with-style [:a {...} "click me"]]

(defn- normalise
  [attrs-and-children]
  (if (and (not (second attrs-and-children))
           (sequential? (first attrs-and-children))
           (coll? (ffirst attrs-and-children)))
    (first attrs-and-children)
    attrs-and-children))

;; Merge new attrs map into Attrs & Children sequence
(defn merge-attrs
  [default-attrs [attrs & children :as attrs-and-children]]
  (if (map? attrs)
    (cons (merge default-attrs attrs) children)
    (cons default-attrs attrs-and-children)))

;; Gernerate Hiccup with Stylefy styles and variadic args
(defn with-style
  [style-map style [element & attrs-and-children]]
  (let [style (get (get style-map @theme) style)
        [attrs & children] (->> (normalise attrs-and-children)
                                (merge-attrs {}))
        attrs' (or (use-style style attrs) attrs)]
    (into [element] (cons attrs' children))))

Disclaimer:
This is untested code and has no guarantees associated with it. It is intended here to spark debate about the possible ways to solve the original poster's question and help close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants