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

Collocation in Modern Web

Collocation in Modern Web

Notes on using collocation in modern web applications.

Rahul Kadyan

October 28, 2017

Transcript

24 slides
  1. <!doctype html> <html> <head> <style> .title { color: red; }

    </style> </head> <body> <h1 class="title"> </h1> <script> document.querySelector('.title') .innerHTML = 'Collocation' </script> </body> </html> index.html
  2. C o l l o c a t i o

    n & R e a c t
  3. import React, { Component } from 'react' export default class

    Header extends Component { render () { return ( <header className='my-header'> {this.children} </header> ) } } header.jsx
  4. import React, { Component } from 'react' import StyleSheet from

    'some-css-in-js' const styles = StyleSheet.create({ header: { margin: '16px', fontSize: '16px', color: 'black' } }) export default class Header extends Component { render () { return ( <header className={styles.header}> {this.children} </header> ) } } header.jsx
  5. header.jsx import React, { Component } from 'react' import style

    from 'some-css-in-js' const myHeader = style` margin: 16px; font-size: 1.5rem; color: black; ` export default class Header extends Component { render () { return ( <header className={myHeader}> {this.children} </header> ) } }
  6. C o l l o c a t i o

    n & W e b C o m p o n e n t s
  7. header.html <template id="template"> <style> .my-header { margin: 16px; font-size: 1.5rem;

    color: black; } </style> <header class="my-header"> <slot> </slot> </header> </template> <script> class Header extends HTMLElement { connectedCallback () { const doc = document.currentScript.ownerDocument const template = doc.querySelector('#template') this.attachShadow({ mode: 'open' }) this.shadowRoot.appendChild( doc.importNode(template.content, true)) } } window.customElements.define('my-header', Header) </script>
  8. <template> <header class="my-header"> <slot /> </header> </template> <script> export default

    { name: 'MyHeader' } </script> <style> .my-header { margin: 16px; font-size: 1.5rem; color: black; } </style> header.vue
  9. p u s h i n g e v e

    n f u r t h e r
  10. <template> <header class="my-header"> <slot /> </header> </template> <script> export default

    { name: 'MyHeader' } </script> <style> .my-header { margin: 16px; font-size: 1.5rem; color: black; } </style> header.vue
  11. <template> <header class="my-header"> <slot /> </header> </template> <script> export default

    { name: 'MyHeader' } </script> <style scoped> .my-header { margin: 16px; font-size: 1.5rem; color: black; } </style> header.vue
  12. header.vue <template> <header :class="$style['my-header']"> <slot /> </header> </template> <script> export

    default { name: 'MyHeader' } </script> <style module> .my-header { margin: 16px; font-size: 1.5rem; color: black; } </style>
  13. header.vue <template> <header> <slot /> </header> </template> <script> export default

    { name: 'MyHeader' } </script> <docs> # MyHeader It renders a custom header element. ... </docs>
  14. header.vue <template> <header> <slot /> </header> </template> <script> export default

    { name: 'MyHeader' } </script> <test> import { mount } from 'vue-test-utils' test(`it renders children`, t => { const w = mount(Component, { slots: { default: '<h1>Hello World </h1>' } }) t.true(w.contains('h1')) }) </test>
  15. header.vue <template> <header> <slot /> </header> </template> <script> export default

    { name: 'MyHeader' } </script> <story> <MyComponent /> </story> <story name="With content"> <MyComponent> <h1>DevFest 17 </h1> </MyComponent> </story>
  16. Collocation — Review! Improved Readability Better CSS Easy Distribution Encapsulation

    Single Source Static Analysis Compile-time Optimisations Style Scoping And many more…

More Decks by Rahul Kadyan

Other Decks in Programming