Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSS-in-JS: FTW || WTF?

bruce lawson
June 15, 2018
140

CSS-in-JS: FTW || WTF?

CSS Day, Amsterdam, June 2018.

bruce lawson

June 15, 2018
Tweet

Transcript

  1. @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
  2. @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/
  3. @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.
  4. @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. - personal correspondace, Bruce and Håkon Wium Lie
  5. @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
  6. @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/
  7. @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
  8. @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
  9. @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/
  10. @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.” - Steven Frank https://css-tricks.com/css-is-awesome/#comment-1609829
  11. @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
  12. @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
  13. @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/
  14. @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
  15. @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
  16. @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/
  17. @brucel Good old CSS: • IDE support • syntax highlighting

    • code completions • validations • CSS generators • huge community
  18. @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”
  19. @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.”
  20. @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
  21. @brucel Ido’s dream • Modular • Dynamic • Performant •

    CSS capabilities • Tooling • Customisation
  22. @brucel A bucket of parts The great thing about Mr.

    Potato is that all of his parts are reusable and customizable
  23. @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.
  24. @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"
  25. @brucel Status of Stylable • Pseudo- Elements & Classes •

    Imports • Mixins • VSCode Extension • Integration - React, Webpack, Jest, Node ...
  26. @brucel W.I.P. • Declaration Interfaces (allowing/ disallowing overwriting of styles)

    • MOAR Integrations • Dogfooding and battle-testing • Web Components Compatibility, Houdini (Roadmap)
  27. @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
  28. @brucel Get ‘em while they’re hot • Stylable docs -

    Stylable.io • Stylable repo - github.com/wix/stylable
  29. @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-css-mostly
  30. @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
  31. @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
  32. @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/
  33. @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!)
  34. @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.