Fork me on GitHub
#garden
<
2016-04-25
>
mattsfrey21:04:32

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?

niamu21:04:22

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

niamu21:04:27

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.

mattsfrey21:04:46

Right, basically like radium

mattsfrey21:04:51

that’d be great to have

mattsfrey21:04:25

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

niamu21:04:30

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

niamu21:04:11

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

mattsfrey21:04:19

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 ?

niamu21:04:23

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.

mattsfrey21:04:43

ah I see, I will have to look into that

mattsfrey21:04:53

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

niamu21:04:23

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

mattsfrey21:04:53

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

mattsfrey21:04:12

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

niamu22:04:21

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

niamu22:04:27

(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]
        ]))))

mattsfrey22:04:18

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

niamu22:04:49

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

mattsfrey22:04:05

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

niamu22:04:14

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.

niamu22:04:44

Style information becomes pretty explicit then.

mattsfrey22:04:03

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

mattsfrey22:04:10

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

niamu22:04:36

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.

niamu22:04:13

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.

mattsfrey22:04:28

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

niamu22:04:52

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

niamu22:04:18

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.

niamu22:04:24

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