A presentation at CSS Day + UX Special in in Amsterdam, Netherlands by bruce lawson
@brucel
@brucel
@brucel
@brucel
@brucel
@brucel
@brucel
@brucel CSS-in-JS
@brucel
"20pt" ;
@brucel Cascading HTML style sheets proposal font.family = times font.size = 12pt h1.font.size = font.size * 3 h2.font.size = font.size * 2.5 h3.font.size = font.size * 2 strong.font.weight = bold space.above = 4pt space.below = 4pt space.first = space.left + 0.5cm # i.e. some extra indentation head.space.above = 15pt # straying from the 4pt default above https://www.w3.org/People/howcome/p/cascade.html
@brucel
@brucel Håkon Wium Lie When I first proposed CSS, JavaScript was not around so I couldn’t borrow it from there. Rather, the syntax was inspired by X resources from the X11 Window System. The reason for changing the CSS syntax from font.size to font- size was twofold.
First, the hyphen makes it look more like written English, which improves human readability.
Second, DSSSL and DSSSL-Lite used hyphenated property names. https://dev.opera.com/articles/css-twenty-years-hakon/
@brucel Culture Clash
@brucel
@brucel John Allsopp
@brucel
Gandallsopp:
“Question after question, of course, is “how do I?”. But beneath
questions like “how do I make my pages look the same on every
platform” and “how can I make my fonts appear identical on the
Macintosh and Windows” is an underlying question – “how do I
control the user’s browser?”
Indeed, the word
control
turns up
with surprising frequency.
Underpinning all this is the belief that
designers are controllers
.
@brucel Håkon One quite specific goal of CSS was to not create a programming language. DSSSL was emerging from the SGML world and that scared us. So did the Turing- completeness. In short, you can't tell if a program will terminate or not -- you could end up in endless loops. Therefore we chose a simpler model where you just made declarations.
@brucel DSSSL DSSSL did, unfortunately, have the fatal flaw which would plague all Scheme like languages: too many parenthesis. Additionally, it was arguably too complete of a spec when it was finally published , making it intimidating to browser developers. The DSSSL spec included over 210 separate styleable properties. The Languages Which Almost Became CSS - Zack Bloom
@brucel Y U NO GIVE PARENT SELECTORS? A parent selector would mean that styles would have to be updated as the HTML document loads. Languages like DSSSL were completely out, as they could perform operations on the document itself, which would not be entirely available when the rendering is to begin. https://eager.io/blog/the-languages-which-almost-were-css/
@brucel Sir Uncle Timbo’s design principles I chose HTML not to be a programming language because I wanted different programs to do different things with it: present it differently, extract tables of contents, index it, and so on. Sir Uncle Timbo https://www.w3.org/DesignIssues/Principles.html#PLP
@brucel Principle of Least Power The choice of language is a common design choice. The low power end of the scale is typically simpler to design, implement and use , but the high power end of the scale has all the attraction of being an open-ended hook into which anything can be placed : a door to uses bounded only by the imagination of the programmer. Sir Uncle Timbo https://www.w3.org/DesignIssues/Principles.html#PLP
@brucel
@brucel Why does this conundrum resonate so universally with people who have used CSS? CSS is hard because its properties interact, often in unexpected ways. Because when you set one of them, you're never just setting that one thing.
That one thing combines and bounces off of and contradicts with a dozen other things, including default things that you never actually set yourself. https://css-tricks.com/css-is-awesome/
@brucel Design creator “at the time it just blew my mind that someone thought the default behavior should be to just have the text honk right out of the box, instead of just making the box bigger like my nice, sensible tables had always done.”
@brucel CSS is bad “The picture fails to capture all the ways in which CSS is bad. You are right. overflow is easy to deal with. Others ( specificity , alignment, box sizing models, maintainability of large stylesheets, obscure hidden rules and assumptions , etc.) are numerous and overwhelming.” https://twitter.com/JohnHargrove/status/985630830704852998
@brucel
Inheritance and Cascade
We also added [a]
fairly complex mechanism
to CSS:
inheritance and cascading. Inheritance was/is is fairly well
understood, it's used in many other computer languages as
well.
Cascading, however, was something new
and in some ways
it competes with inheritance. I can see reasons for not liking it,
but to me the benefit of not having to provide complete style
sheets makes up for that.
Howcome, June 2018, private conversation
@brucel CSS is awesome! One rule of thumb for mitigating this is, never be more explicit than you need to be ... Work with CSS, instead of against it. CSS is weird. It's unlike any other code, and that makes a lot of programmers uncomfortable. But used wisely it can, in fact, be awesome. Brandon Smith https://css-tricks.com/css-is-awesome/
@brucel The era of components
@brucel
@brucel
@brucel
@brucel
@brucel • Something about being convinced by component-based architecture
@brucel Because of • Re-usability
• Composability
• Testability
• Shareability
@brucel Thanks Cristiano Rastelli
(@areaweb)
@brucel
@brucel Why is CSS for components a PITA? • Everything is global
• Order affects cascade
• Dependency system - @import
• Relies on markup structure (classes, IDs, descendant/ strucural combinators)
• Breaks silently
@brucel
@brucel
@brucel • There is a tremendous amount of memorization of unexpected behaviors , rules, names and other idiosyncrasies. It's amazingly powerful, for sure, but it is loaded with cruft
https://twitter.com/JohnHargrove/status/985631372764221440
@brucel Et tu, Specificity? As we all (should) probably know by now, specificity is is one of CSS’ most troublesome features , and is an area that soon becomes hard to manage on projects of any reasonable size. Specificity is a trait best avoided, which is why we don’t use IDs in CSS, and we don’t nest selectors unless absolutely necessary . Harry Roberts, October 2014 https://csswizardry.com/2014/10/the-specificity-graph/
@brucel Good old CSS: • IDE support
• syntax highlighting
• code completions
• validations
• CSS generators
• huge community
@brucel
@brucel
@brucel Designers, designers, designers, designers
@brucel What’s your biggest gripe w/ CSS-in-JS? “Always having to remember to camelCase properties then spending 10min pulling hair out when you do forget” “the cryptic domain-specific languages that each of the frameworks do just ever so slightly differently”
@brucel https://twitter.com/_philpl/status/935656757942251520 “When I test look and feel in browser, then I copy paste from inspector, only to have to re-write it as a JSON object” “Lack of linting, autocomplete, and css plug-ins for colors/ incrementing/ etc”. “Not a gripe. But a concern: Performance.”
@brucel CSS-in-JS costs us! • Loading JS library to handle CSS creation at runtime
• Creating / modifying stylesheets at runtime
• Server-Side Rendering = CSS × 2
@brucel
@brucel
@brucel Totez!
@brucel Totez!
@brucel Totez !
@brucel
@brucel
@brucel Ido’s dream • Modular
• Dynamic
• Performant
• CSS capabilities
• Tooling
• Customisation
@brucel Introducing Stylable is a CSS preprocessor for styling components with typed CSS.
@brucel
@brucel Introducing Mr. Potato Mr Potato is a (React) component.
@brucel Sane APIs We use React and Typescript to provide an API for component logic
@brucel A bucket of parts The great thing about Mr. Potato is that all of his parts are reusable and customizable
@brucel Personalizing a Potato-Bruce Extending a component’s interface
@brucel {Mohawk: required;}
@brucel
@brucel Custom Pseudo-Elements Exposing internal parts all the way down
@brucel Custom States Use custom states with parameters
@brucel Custom States Use custom states with parameters
@brucel CSSWG
“A future version of this specification may allow multiple pseudo-elements per selector."
What's the reason for that? What's the reason it's not currently allowed? Pseudo-elements aren't just filtering which element is selected: they actually create structure in the box tree. So if we have a pseudo element attached to a pseudo-element, it creates a box structure that doesn't currently exist, and pseudo-element box structures can be particularly intricate and complicated to implement
since many of them don't fit into the box's tree structure. Therefore we intend to only allow specific combinations--those that are needed for particular use cases.
@brucel Latest Spec (30 May 18) https://drafts.csswg.org/selectors-4/#pseudo-element-structure
@brucel Mix it all together
@brucel
@brucel Extra happy in Amsterdam .backdrop:in( amsterdam ) ~ .potatoBruce:mood(happy) ::mouth::after { content:” ?????? ” }
@brucel
@brucel
@brucel
@brucel Try it out! potatobruce.io
stylable.io
@stylableio
@brucel A type system for CSS • §
@brucel A type system for CSS • CSS Superset
• Inner parts as Pseudo-Elements
• Create custom Pseudo-Classes
• Type aware dependencies
• IDE & Build support
• Optimization - "ruleset shaking"
@brucel Status of Stylable • Pseudo- Elements & Classes
• Imports
• Mixins
• VSCode Extension
• Integration - React, Webpack, Jest, Node ...
@brucel W.I.P. • Declaration Interfaces (allowing/ disallowing overwriting of styles)
• MOAR Integrations
• Dogfooding and battle-testing
• Web Components Compatibility, Houdini (Roadmap)
@brucel Number One Priority • 120 million registered users
• CSS Grid
@brucel James Kyle “pretty sure all the CSS-in-JS libraries just died for me ... CSS could be perfectly static if given the right tools, that's exactly what stylable does. It gives you the tools you need in CSS so that you don't need to do a bunch of dynamic shit in JS. Making it static is a huge performance win ” core team member of Yarn, Babel and TC39
https://twitter.com/jamiebuilds/status/929675977067655170
@brucel
@brucel
@brucel
@brucel Get ‘em while they’re hot • Stylable docs - Stylable.io
• Stylable repo - github.com/wix/stylable
@brucel
@brucel James Kyle again https://twitter.com/jamiebuilds/status/988839821887107072
@brucel
@brucel
“CSS Blocks implements a
strict subset of CSS
.”
This means we've intentionally restricted some of the features
you're allowed to use in a Block file to ensure we can optimize
your stylesheets as much as possible!
https://github.com/linkedin/css-blocks#its-just-cs
s
-mostly
@brucel “Stylable is 100% CSS, and then some” “ In case I didn’t say it before, I’ll say it again: with Stylable, you get CSS, and every part of CSS. This seems like a “duh” observation, but this is significant if you’ve ever battled with a CSS-in-JS framework over a lost or “hacky” implementation of a basic CSS feature. If this is all you’re looking for, stop reading here and try it out!” Drew Powers https://blog.envylabs.com/stylable-the-good-the-bad-the-weird-f1ee137311a4
@brucel
@brucel
@brucel Goodbye, :shadow and
/deep/ These were shadow-piercing selectors that allowed you to target any node in an element’s Shadow DOM. Apart from being terribad for performance , they also required the user of an element to be intimately familiar with some random element’s implementation , which was unlikely and lead to them just breaking the whole element by accident. Monica Dinculescu
@brucel Hello, ::part and ::theme Unlike :shadow and /deep/, they don’t allow you to style arbitrary elements inside a shadow tree: they only allow you to style elements that an author has tagged as being eligible for styling This is a lot of effort on the element author, but easy on the theme user. https://meowni.ca/posts/part-theme-explainer/
@brucel
@brucel
@brucel
@brucel Houdini: JS-in-your-CSS! Custom Paint:
.myThing {background: paint (nice-thing);} Custom Layout:
.myMasonry div {display: layout (masonry);} .. and many more! ( Ask Greg !)
@brucel
@brucel b • web comping
@brucel
@brucel Thank you! Håkon Wium Lie, John Allsopp, @wixeng Stylable team (especially Tom and Lichtash) ! https://pixabay.com/en/potatoes-vegetables-erdfrucht-bio-1585060/ ! https://www.pexels.com/photo/photo-of-eiffel-tower-paris-811715/ ! https://thenounproject.com/icon/38411/ ! https://pixabay.com/en/eiffel-tower-long-exposure-lights-1156146/ No potatoes were harmed during the making of this presentation.
Everyone's talking about CSS-in-JS. It's the Kim Kardashian of web development. And, as with Kimmie, opinions are polarised. To some, CSS in JS just makes sense: it's local to your component, it can't leak and, hey, I know how to write JavaScript and CSS is weird.
To others, CSS-in-JS is an abomination that makes them want to emulate Kimmie and "release a fragrance" in disdain. Why are scripters so afraid of the cascade? Why the hesitance about inheritance?
Let's look at what CSS seems to lack, what the CSS-in-JS libraries can teach us, so we don't do as Kim's buttocks did and "Break the Internet".
The following resources were mentioned during the presentation or are useful additional information.
Stylable documentation
Potato Bruce site. Clone the repo and tweak to see how Stylable works - the main styles are in /src/components/app.st.css