| OLD | NEW |
| (Empty) | |
| 1 # ShadyCSS |
| 2 |
| 3 ShadyCSS provides a library to simulate ShadowDOM style encapsulation (ScopingSh
im), a shim for the proposed CSS mixin `@apply` styntax (ApplyShim), and a libra
ry to integrate document-level stylesheets with both of the former libraries (Cu
stomStyleInterface). |
| 4 |
| 5 ## Requirements |
| 6 ShadyCSS requires support for the `<template>` element, ShadowDOM, MutationObser
ver, Promise, and Object.assign |
| 7 |
| 8 ## Loading |
| 9 |
| 10 ShadyCSS can be used by loading the ScopingShim, ApplyShim, CustomStyleInterface
, or any combination of those. |
| 11 |
| 12 The most-supported loading order is: |
| 13 1. ScopingShim |
| 14 1. ApplyShim |
| 15 1. CustomStyleInterface |
| 16 |
| 17 All libraries will expose an object on `window` named `ShadyCSS` with the follow
ing interface: |
| 18 |
| 19 ```js |
| 20 ShadyCSS = { |
| 21 prepareTemplate(templateElement, elementName, elementExtension){}, |
| 22 styleElement(element){}, |
| 23 styleSubtree(element, overrideProperties){}, |
| 24 styleDocument(overrideProperties){}, |
| 25 getComputedStyleValue(element, propertyName){ |
| 26 return // style value for property name on element |
| 27 }, |
| 28 nativeCss: Boolean, |
| 29 nativeShadow: Boolean |
| 30 } |
| 31 ``` |
| 32 |
| 33 ## About ScopingShim |
| 34 |
| 35 ScopingShim provides simulated ShadyDOM style encapsulation, and a shim for CSS
Custom Properties. |
| 36 |
| 37 ScopingShim works by rewriting style contents and transforming selectors to enfo
rce scoping. |
| 38 Additionally, if CSS Custom Properties is not detected, ScopingShim will replace
CSS Custom Property usage with realized values. |
| 39 |
| 40 ### Example: |
| 41 Here's an example of a custom element when Scoping Shim is not needed. |
| 42 |
| 43 ```html |
| 44 <my-element> |
| 45 <!-- shadow-root --> |
| 46 <style> |
| 47 :host { |
| 48 display: block; |
| 49 } |
| 50 #container slot::slotted(*) { |
| 51 color: gray; |
| 52 } |
| 53 #foo { |
| 54 color: black; |
| 55 } |
| 56 </style> |
| 57 <div id="foo">Shadow</div> |
| 58 <div id="container"> |
| 59 <slot> |
| 60 <!-- span distributed here --> |
| 61 </slot> |
| 62 </div> |
| 63 <!-- /shadow-root --> |
| 64 <span>Light</span> |
| 65 </my-element> |
| 66 ``` |
| 67 |
| 68 becomes: |
| 69 |
| 70 ```html |
| 71 <style scope="my-element"> |
| 72 my-element { |
| 73 display: block; |
| 74 } |
| 75 #container.my-element > * { |
| 76 color: gray; |
| 77 } |
| 78 #foo.my-element { |
| 79 color: black; |
| 80 } |
| 81 </style> |
| 82 <my-element> |
| 83 <div id="foo">Shadow</div> |
| 84 <div id="container"> |
| 85 <span>Light</span> |
| 86 </div> |
| 87 </my-element> |
| 88 ``` |
| 89 |
| 90 ## About ApplyShim |
| 91 |
| 92 ApplyShim provides a shim for the `@apply` syntax proposed at https://tabatkins.
github.io/specs/css-apply-rule/, which expands the definition CSS Custom Propert
ies to include objects that can be applied as a block. |
| 93 |
| 94 This is done by transforming the block definition into a set of CSS Custom Prope
rties, and replacing uses of `@apply` with consumption of those custom propertie
s. |
| 95 |
| 96 ### Example: |
| 97 |
| 98 Here we define a block called `--mixin` at the document level, and apply that bl
ock to `my-element` somewhere in the page. |
| 99 |
| 100 ```css |
| 101 html { |
| 102 --mixin: { |
| 103 border: 2px solid black; |
| 104 background-color: green; |
| 105 } |
| 106 } |
| 107 |
| 108 my-element { |
| 109 border: 1px dotted orange; |
| 110 @apply --mixin; |
| 111 } |
| 112 ``` |
| 113 |
| 114 becomes: |
| 115 |
| 116 ```css |
| 117 html { |
| 118 --mixin_-_border: 2px solid black; |
| 119 --mixin_-_background-color: green; |
| 120 } |
| 121 |
| 122 my-element { |
| 123 border: var(--mixin_-_border, 1px dotted orange); |
| 124 background-color: var(--mixin_-_background-color); |
| 125 } |
| 126 ``` |
| 127 |
| 128 ## About CustomStyleInterface |
| 129 |
| 130 CustomStyleInterface provides API to process `<style>` elements that are not ins
ide of |
| 131 ShadowRoots, and simulate upper-boundary style scoping for ShadyDOM. |
| 132 |
| 133 To add document-level styles to ShadyCSS, one can call `CustomStyleInterface.add
CustomStyle(styleElement)` or `CustomStyleInterface.addCustomStyle({getStyle: ()
=> styleElement})` |
| 134 |
| 135 An example usage of the document-level styling api can be found in `examples/doc
ument-style-lib.js`, and another example that uses a custom element wrapper can
be found in `examples/custom-style-element.js` |
| 136 |
| 137 ### Example: |
| 138 |
| 139 ```html |
| 140 <style class="document-style"> |
| 141 html { |
| 142 --content-color: brown; |
| 143 } |
| 144 </style> |
| 145 <my-element>This text will be brown!</my-element> |
| 146 <script> |
| 147 CustomStyleInterface.addCustomStyle(document.querySelector('style.document-style
')); |
| 148 </script> |
| 149 ``` |
| 150 |
| 151 Another example with a wrapper `<custom-style>` element |
| 152 |
| 153 ```html |
| 154 <custom-style> |
| 155 <style> |
| 156 html { |
| 157 --content-color: brown; |
| 158 } |
| 159 </style> |
| 160 </custom-style> |
| 161 <script> |
| 162 class CustomStyle extends HTMLElement { |
| 163 constructor() { |
| 164 CustomStyleInterface.addCustomStyle(this); |
| 165 } |
| 166 getStyle() { |
| 167 return this.querySelector('style'); |
| 168 } |
| 169 } |
| 170 </script> |
| 171 <my-element>This this text will be brown!</my-element> |
| 172 ``` |
| 173 |
| 174 Another example with a function that produces style elements |
| 175 |
| 176 ```html |
| 177 <my-element>This this text will be brown!</my-element> |
| 178 <script> |
| 179 CustomStyleInterface.addCustomStyle({ |
| 180 getStyle() { |
| 181 const s = document.createElement('style'); |
| 182 s.textContent = 'html{ --content-color: brown }'; |
| 183 return s; |
| 184 } |
| 185 }); |
| 186 </script> |
| 187 ``` |
| 188 |
| 189 ## Usage |
| 190 |
| 191 To use ShadyCSS: |
| 192 |
| 193 1. First, call `ShadyCSS.prepareTemplate(template, name)` on a |
| 194 `<template>` element that will be imported into a `shadowRoot`. |
| 195 |
| 196 2. When the element instance is connected, call `ShadyCSS.styleElement(element)` |
| 197 |
| 198 3. Create and stamp the element's shadowRoot |
| 199 |
| 200 4. Whenever dynamic updates are required, call `ShadyCSS.styleSubtree(element)`. |
| 201 |
| 202 5. If a styling change is made that may affect the whole document, call |
| 203 `ShadyCSS.styleDocument()`. |
| 204 |
| 205 The following example uses ShadyCSS and ShadyDOM to define a custom element. |
| 206 |
| 207 ```html |
| 208 <template id="myElementTemplate"> |
| 209 <style> |
| 210 :host { |
| 211 display: block; |
| 212 padding: 8px; |
| 213 } |
| 214 |
| 215 #content { |
| 216 background-color: var(--content-color); |
| 217 } |
| 218 |
| 219 .slot-container ::slotted(*) { |
| 220 border: 1px solid steelblue; |
| 221 margin: 4px; |
| 222 } |
| 223 </style> |
| 224 <div id="content">Content</div> |
| 225 <div class="slot-container"> |
| 226 <slot></slot> |
| 227 </div> |
| 228 </template> |
| 229 <script> |
| 230 ShadyCSS.prepareTemplate(myElementTemplate, 'my-element'); |
| 231 class MyElement extends HTMLElement { |
| 232 connectedCallback() { |
| 233 ShadyCSS.styleElement(this); |
| 234 if (!this.shadowRoot) { |
| 235 this.attachShadow({mode: 'open'}); |
| 236 this.shadowRoot.appendChild( |
| 237 document.importNode(myElementTemplate.content, true)); |
| 238 } |
| 239 } |
| 240 } |
| 241 |
| 242 customElements.define('my-element', MyElement); |
| 243 </script> |
| 244 ``` |
| 245 |
| 246 ## Type Extension elements |
| 247 |
| 248 ShadyCSS can also be used with type extension elements by supplying the base |
| 249 element name to `prepareTemplate` as a third argument. |
| 250 |
| 251 ### Example |
| 252 |
| 253 ```html |
| 254 <template id="myElementTemplate"> |
| 255 <style> |
| 256 :host { |
| 257 display: block; |
| 258 padding: 8px; |
| 259 } |
| 260 |
| 261 #content { |
| 262 background-color: var(--content-color); |
| 263 } |
| 264 |
| 265 .slot-container ::slotted(*) { |
| 266 border: 1px solid steelblue; |
| 267 margin: 4px; |
| 268 } |
| 269 </style> |
| 270 <div id="content">Content</div> |
| 271 <div class="slot-container"> |
| 272 <slot></slot> |
| 273 </div> |
| 274 </template> |
| 275 <script> |
| 276 ShadyCSS.prepareTemplate(myElementTemplate, 'my-element', 'div'); |
| 277 class MyElement extends HTMLDivElement { |
| 278 connectedCallback() { |
| 279 ShadyCSS.styleElement(this); |
| 280 if (!this.shadowRoot) { |
| 281 this.attachShadow({mode: 'open'}); |
| 282 this.shadowRoot.appendChild( |
| 283 document.importNode(myElementTemplate.content, true)); |
| 284 } |
| 285 } |
| 286 } |
| 287 |
| 288 customElements.define('my-element', MyElement, {extends: 'div'}); |
| 289 </script> |
| 290 ``` |
| 291 |
| 292 ## Imperative values for Custom properties |
| 293 |
| 294 To set the value of a CSS Custom Property imperatively, `ShadyCSS.styleSubtree` |
| 295 and `ShadyCSS.styleDocument` support an additional argument of an object mapping |
| 296 variable name to value. |
| 297 |
| 298 When using ApplyShim, defining new mixins or new values for current mixins imper
atively is not |
| 299 supported. |
| 300 |
| 301 ### Example |
| 302 ```html |
| 303 <my-element id="a">Text</my-element> |
| 304 <my-element>Text</my-element> |
| 305 <script> |
| 306 let el = document.querySelector('my-element#a'); |
| 307 // Set the color of all my-element instances to 'green' |
| 308 ShadyCSS.styleDocument({'--content-color' : 'green'}); |
| 309 // Set the color my-element#a's text to 'red' |
| 310 ShadyCSS.styleSubtree(el, {'--content-color' : 'red'}); |
| 311 </script> |
| 312 ``` |
| 313 |
| 314 ## Limitations |
| 315 |
| 316 ### Selector scoping |
| 317 |
| 318 You must have a selector to the left of the `::slotted` |
| 319 pseudo-element. |
| 320 |
| 321 ### Custom properties and `@apply` |
| 322 |
| 323 Dynamic changes are not automatically applied. If elements change such that they |
| 324 conditionally match selectors they did not previously, `ShadyCSS.styleDocument()
` |
| 325 must be called. |
| 326 |
| 327 For a given element's shadowRoot, only 1 value is allowed per custom properties. |
| 328 Properties cannot change from parent to child as they can under native custom |
| 329 properties; they can only change when a shadowRoot boundary is crossed. |
| 330 |
| 331 To receive a custom property, an element must directly match a selector that |
| 332 defines the property in its host's stylesheet. |
| 333 |
| 334 ### `<custom-style>` Flash of unstyled content |
| 335 |
| 336 If `ShadyCss.applyStyle` is never called, `<custom-style>` elements will process |
| 337 after HTML Imports have loaded, after the document loads, or after the next pain
t. |
| 338 This means that there may be a flash of unstyled content on the first load. |
| 339 |
| 340 ### Mixins do not cascade throught `<slot>` |
| 341 |
| 342 Crawling the DOM and updating styles is very expensive, and we found that trying
to |
| 343 update mixins through `<slot>` insertion points to be too expensive to justify f
or both |
| 344 polyfilled CSS Mixins and polyfilled CSS Custom Properties. |
| OLD | NEW |