Hello!
Recently I’ve been controlling the view state of components with attribute values in the top-level element. Here’s a minimal example of the structure I’ve been using:
HTML:
<body data-active-view="1">
<section data-view="1">1</section>
<section data-view="2">2</section>
<section data-view="3">3</section>
</body>
CSS:
[data-view] { display: none }
[data-active-view="1"] [data-view="1"] { display: block }
[data-active-view="2"] [data-view="2"] { display: block }
[data-active-view="3"] [data-view="3"] { display: block }
The only problem is that the CSS displaying logic is static – adding more views would require either adding more CSS rules dynamically, or doing it preemptively by adding an arbitrary amount of rules for more views (for a 4th view, 5th, 6th, etc).
I think a smarter way to do this would be to abstract the value of an attribute into a variable, something like:
[data-view] { display: none }
[data-active-view=@value] [data-view=@value] { display: block }
This syntax would result in writing less CSS, while also explaining the purpose of the CSS logic more clearly.
Playing around with this proposed syntax, you can also do cool things like
- Using a variable with advanced attribute value selectors
[attr~=@value], [attr|=@value], [attr^=@value], etc
- Using multiple variables in the same selector string
[data-active-view=@value][other-attr=@other-val] [data-view=@value][other-attr=@other-val] { display: block }
I haven’t researched too much into the practicality of a syntax like this – so I’m totally open to different proposals
Let me know what you think!
Update 10/2/16 Here’s a better organized description of the feature and the proposed CSS syntax: github.com/tvler/Matching-Attribute-Values-CSS-Selector/blob/master/explainer.md