garden

mattsfrey 2016-04-25T21:02:32.000008Z

Hey I’m wondering if there has been any unofficial best practice established for achieving pseudo selectors with garden inline at the component level (using reagent). So far from research it seems that the only way to do it is to use the :style tag in a parent div or component?

niamu 2016-04-25T21:06:22.000009Z

That’s correct. The only way to compose pseudo selectors in CSS is through an external stylesheet or styles within a style tag.

niamu 2016-04-25T21:07:27.000010Z

Something that would be cool to build is a macro that could be used within components that compiles style information to a single external file. I’ve been meaning to find time to experiment with this, but haven’t yet.

mattsfrey 2016-04-25T21:13:46.000011Z

Right, basically like radium

mattsfrey 2016-04-25T21:13:51.000012Z

that’d be great to have

mattsfrey 2016-04-25T21:14:25.000013Z

I’m kind of hesitant to go down the road of building a large web app where <style> tags are being injected into the dom though, just “feels” wrong kind of

niamu 2016-04-25T21:32:30.000014Z

I know exactly what you mean. However, I can at least offer that it does work remarkably well.

niamu 2016-04-25T21:33:11.000015Z

And hopefully when such a macro exists, the refactoring and code cleanliness will be simple and straightforward.

mattsfrey 2016-04-25T21:33:19.000016Z

have you been using <style scoped> to try and contain it a bit more and remove the need for class names to some extent on child elements ?

niamu 2016-04-25T21:34:23.000017Z

Yes, unfortunately the support for the scoped attribute is still limited so as a fallback I’ve been using gensym on classes for the root element of components to scope styles.

mattsfrey 2016-04-25T21:34:43.000018Z

ah I see, I will have to look into that

mattsfrey 2016-04-25T21:34:53.000019Z

thanks a lot for the info, it’s been really helpful

niamu 2016-04-25T21:35:23.000020Z

No problem. Let me know what kind of solution you go with. I’d be interested to hear what works well for you.

mattsfrey 2016-04-25T21:35:53.000021Z

I’d like very much to roll inline styles as I feel thats moving more in the “right” direction and have seen a lot of compelling use cases with react

mattsfrey 2016-04-25T21:36:12.000022Z

just trying to really get my ducks in a row and get a feel for how it’s going to look code wise before pitching it to my team

niamu 2016-04-25T22:05:21.000023Z

This may help you out a little, even though it is using Om.

niamu 2016-04-25T22:05:27.000024Z

(defn component
  [data owner]
  (reify
    om/IInitState
    (init-state [_]
      {:root (keyword (str (name :div)
                      (gensym ".unique")))})
    om/IWillMount
    (will-mount [_]
      (om/set-state! owner :style
                     (css [(om/get-state owner :root)])))
    om/IRenderState
    (render-state [_ {:keys [root style]}]
      (html
       [root
        [:style {:scoped "scoped"} style]
        ]))))

mattsfrey 2016-04-25T22:07:18.000025Z

ah so they are using the unique identifier in conjunction with scoped

niamu 2016-04-25T22:07:49.000026Z

Yeah, I’ve done that so refactoring later when support for scoped is universal is simple.

mattsfrey 2016-04-25T22:08:05.000027Z

yeah that’s smart, this doesn’t look too bad really

niamu 2016-04-25T22:09:14.000028Z

I like that I get to separate CSS almost entirely into the IWillMount section. Then styles that rely on state become embedded in the HTML elements in IRenderState. Which feels nice.

niamu 2016-04-25T22:09:44.000029Z

Style information becomes pretty explicit then.

mattsfrey 2016-04-25T22:12:03.000030Z

I see, yeah I will have to try coding the equivalent in reagent with some sort of similar abstraction for pulling in the css def, maybe even just an import or declaring it in the component file above the component itself

mattsfrey 2016-04-25T22:13:10.000031Z

Is there some reason in particular you’re storing style definitions in state?

niamu 2016-04-25T22:15:36.000032Z

It hasn’t yet come in handy, but my initial reason for that was so that I style information is something that you can query from the component itself. It makes editing a single component entirely self-contained.

mattsfrey 2016-04-25T22:16:59.000033Z

ah i see

niamu 2016-04-25T22:18:13.000034Z

I’m still exploring as well, but this is what we’ve been building at my company so far and it has worked very well for us.

mattsfrey 2016-04-25T22:19:28.000035Z

Oh really, well that’s great to hear, I’d definitely like to be able to do likewise, but there is some resistance on the team from those that are more accustomed to using less etc so it needs to be a compelling sell

niamu 2016-04-25T22:22:52.000037Z

Yeah, that’s tough. Before I switched to garden I was only ever writing pure CSS.

niamu 2016-04-25T22:24:18.000038Z

What sold me was the build tools for generating CSS with garden. I really didn’t like the build tools outside of Clojure for handling CSS so I just stuck to writing it on my own rather than relying on pre-compilers.

niamu 2016-04-25T22:25:24.000039Z

Having figwheel re-render component CSS alongside the HTML is extremely handy.