cljss icon indicating copy to clipboard operation
cljss copied to clipboard

CSS specificity and order of declaration

Open knubie opened this issue 5 years ago • 3 comments

In some cases, the order of declaration matters, for example when applying :hover and :active pseudoselectors. Because an element can be in both a :hover and :active state at the same time, it's important to declare :active after :hover, so that it takes precedence. Cljss doesn't seem to take order of declaration into account, which can make it impossible to correctly apply certain pseudoselectors like the ones above on a single element.

knubie avatar Jan 26 '19 02:01 knubie

Happy to take a PR! I don't have time for this at the moment.

roman01la avatar Jan 26 '19 08:01 roman01la

I think the best way to go about this would be to add a new type of psuedo-selector to the syntax. As CSS supports cascading styles through order, and clojure maps do not guarantee their keywords will remain in the same order as definition. This leads us to a bit of a contradiction. This is what I propose.

(defstyles my-style []
  {:&:hover {#_"some style definition"}
   :&:active {#_"some other style"}
   :*:order [:&:hover :&:active]})

The idea being that anything you have added to the syntax is designated by a star instead of an ampersand.

Let me know what you think about the changes.

MichaelStergianis avatar Jul 17 '19 19:07 MichaelStergianis

@MichaelStergianis Interesting proposal. I think if we take that approach it may make sense to use a "vendor prefix," something like

(defstyles my-style []
  {:&:hover {#_"some style definition"}
   :&:active {#_"some other style"}
   :-cljss-order [:&:hover :&:active]})

Another idea is to have defstyles and defstyled take a single map or a vector of styles:

(defstyles my-style []
  [{:font-size "12px"
    :color "black"}
   {:&:hover {#_"some style definition"}
   {:&:active {#_"some other style"}}])

This is similar to how Reactive Native works.

knubie avatar Jul 19 '19 23:07 knubie