On Github philipwalton / html5devconf
I don't really know. Nobody knows.
Web Components haven't been around long enough to really get a feel for what works and what doesn't.
All we can do is learn from history and do our best to avoid repeating the mistakes of the past.
As it turns out, CSS is harder than it looks.
It's not because:
CSS isn't hard because of the tricks and the hacks, or because it doesn't support X or Y.
Getting your rules to match the elements you want without them accidentally matching the elements you don't.
All CSS rules are global
It’s hard to write predictable code when any rule you write could potentially conflict with another rule you didn’t know existed.
And, for whatever reason, most people who write CSS like to live dangerously!
#main #content div div div { float: left; width: 50%; }
Accomplishing the first part without writing too much code.
Implementation details cannot be easily abstracted away
Are all these classes needed? What do they do? If I remove one of them will everything break? Wtf is going on? #FML
All of these attempt to solve CSS's two hard problems:
/* CSS not found… */
…
The main document:
Newman
The element's shadow DOM:
Hello, ****
~~~~ ~~
~~ ~~Hello, ~~ Newman ~~~~ ~~
~~// Create an object that will be the new element's prototype. var HelloWorld = Object.create(HTMLElement.prototype); // Add a callback to run whenever a new tag is created. HelloWorld.createdCallback = function() { this.createShadowRoot().innerHTML = '' + '
Hello,
'; } // Register the element. document.registerElement('hello-world', { prototype: HelloWorld }); Callbacks reference →How it's done today (using BEM):
…
**Notice how div.Media-body serves no semantic purpose.
The main DOM:
…
Shadow DOM:
****
~~<style>…</style>~~ ~~~~ ~~~~ ~~
…
~~What if I don't like how the media object looks. How can I make it better?
The <author-card> imports the <media-object> and composes it within its own shadow DOM.
<link href="/path/to/media-object.html" rel="import"> <style>…</style> **** ****Author card demo →
Sometimes CSS doesn't do exactly what you want, so you have to resort to hacks.
With shadow DOM, you can keep these hacks out of sight, where they belong.
Flex grid broken | Flex grid fixed<flex-line> and <flex-grid> are very presentational, so you might not want to have them appear in your main document source.
Luckily, you don't have to. You can use <flex-line> to compose other layouts.
Composed tree:
<body> ~~~~ ~~
Check out polymer-project.org as well as chromestatus.com. Both are built entirely with Polymer.
I've also put together a simple toy site that I was using to validate some of these concepts. You can find it on Github.
With the platform.js polyfill you can get most of these features in all modern browsers (IE 10+).
Some features (like full style encapsulation) cannot be polyfilled, and so native support is required (Chrome 36+ only at this point).
Yes, the selectors ::shadow and /deep/ allow you to do this.
But in general you should never use these selectors on your own elements, only on third-party components that aren't sufficiently extensible.
To quote the open/closed principle:
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.In general, dynamic content should be in the light DOM so your components can be static, bundleable, and cacheable.
Yes. Screen readers see the rendered tree in the same way that you see it (visually) when using your browser.
And search engines that can run JavaScript have access to all content, including shadow content.