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

Web Component Design -- Maintainability

Web Component Design -- Maintainability

Have you ever tried to develop front end code that can be easily used in multiple projects? Reinventing the wheel is no fun. In this talk, I will use an example to share her design process for developing web components that are accessible, pretty, and easy to reuse and will also share her tips and tricks for maintaining and growing frontends over time without breaking existing code.

Joy Heron

July 12, 2019
Tweet

More Decks by Joy Heron

Other Decks in Technology

Transcript

  1. 5 Web Component Design Spring 2016 Summer 2016 Spring 2017

    Winter 2017 Fall 2018 … and in the future??
  2. 7 Web Component Design Would I do this in the

    backend? Joy Heron / @iamjoyheron
  3. 8 Web Component Design Abstraction - Rule of Three Joy

    Heron / @iamjoyheron Step 1: copy Step 2: copy Step 3: Abstract!
  4. 9 Time to find an abstraction! Web Component Design GET

    /search?col1=x&col2=y 200 OK <table>…</table>
  5. And now let’s build it with web components! 10 Web

    Component Design That’s what I did. It’s called: Tabelle https://github.com/innoq/tabelle
  6. 11 Web Component Design What is a web component? Joy

    Heron / @iamjoyheron HTML CSS probably JavaScript possibly make it pretty make it better make it work FOR ALL USERS!
  7. 12 Web Component Design Single Responsibility Principle Joy Heron /

    @iamjoyheron Each Component should do ONE thing REALLY WELL If you need more functionality, write a new component and compose them! Benefit of HTML First: HTML composes naturally!
  8. 13 Web Component Design How to find web components? Joy

    Heron / @iamjoyheron Atomic Design: http://atomicdesign.bradfrost.com/
  9. 16 Web Component Design Filter Field Component Joy Heron /

    @iamjoyheron Step 1: make it work GET /search? col1=my+search+str& col2=Option+1 What we need <input type=“text” name=“col1” /> <select name=“col2”> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select> <form action=“/search”> <button type=“submit”>Search</button> </form> How to get it
  10. 18 Web Component Design Tip #1 If you want to

    make sure you are accessible ACTUALLY USE A SCREEN READER Read this book for more tips!
  11. 19 Web Component Design Joy Heron / @iamjoyheron Currently <input

    type=“text” name=“col1” /> Edit Text.
 You are currently on a text field inside of web content… Solution Filter Field Component Step 1.2: make it accessible <input type=“text” name=“col1” aria-label=“Filter Column 1” /> Filter Column 1, Edit Text.
 You are currently on a text field inside of web content… ❤ Let’s test our component with a screen reader to see how it sounds!
  12. 22 CSS is harder to OVERRIDE than it is to

    WRITE Web Component Design Joy Heron / @iamjoyheron
  13. 23 Web Component Design Tip #2 Use minimal CSS to

    make it easy to override your styles later! Joy Heron / @iamjoyheron
  14. 24 Web Component Design Joy Heron / @iamjoyheron <input class=“tabelle-input”

    type=“text” name=“col1” aria-label=“Filter Column 1”/> <select class=“tabelle-input” name=“col2” aria-label=“Filter Column 1”> … </select> Add a CSS class to our component to allow users to write their own styles later! Filter Field Component Step 2: make it pretty
  15. 25 Web Component Design Joy Heron / @iamjoyheron HTML it

    works FOR ALL USERS! CSS someone else can make it pretty! JS? make it better? ?
  16. 26 Web Component Design Joy Heron / @iamjoyheron CSS someone

    else can make it pretty! JS no need! HTML it works FOR ALL USERS!
  17. 29 Web Component Design Sort Button Component Joy Heron /

    @iamjoyheron Step 1: make it work GET /search?sort=col1_asc What we need <input id=“col1_asc” type=“radio” name=“sort” value=“col1_asc” /> <label for=“col1_asc”>Sort Column 1 Ascending</label> <form action=“/search”> <button type=“submit”>Search</button> </form> How to get it HTML Element? <input id=“col1_desc” type=“radio” name=“sort” value=“col1_desc” /> <label for=“col1_desc”>Sort Column 2 Descending</label>
  18. 30 Web Component Design Joy Heron / @iamjoyheron Sort Column

    1 Ascending, radio button, 1 of 2
 You are currently on a radio button, 1 of 2, … ❤ Sort Button Component Step 1.2: make it accessible
  19. 33 Web Component Design Tip #3 You can select an

    input element by clicking on it’s HTML label Joy Heron / @iamjoyheron
  20. 34 Web Component Design Joy Heron / @iamjoyheron Visually hide

    radio buttons and style their labels. Sort Button Component Step 2: make it pretty %visually-hidden { position: absolute !important; height: 1px; width: 1px; overflow: hidden; clip: rect(1px 1px 1px 1px); /* IE6, IE7 */ clip: rect(1px, 1px, 1px, 1px); } .visually-hidden { @extend %visually-hidden; } Source: https://snook.ca/archives/html_and_css/hiding-content-for-accessibility Move content offscreen so it is not visible but can still be read by a screen reader. SCSS Placeholder Selector & Util CSS Class
  21. 35 Web Component Design Joy Heron / @iamjoyheron <input class=“arrow”

    id=“col1_asc” … /> <label class=“arrow—asc” for=“col1_asc”> Sort Column 1 Ascending </label> <input class=“arrow” id=“col1_desc” … /> <label class=“arrow—desc” for=“col1_desc”> Sort Column 2 Descending </label> Visually hide radio buttons and style their labels. .arrow { @extend %visually-hidden; } .arrow—asc { @include icon-before(‘up.svg’, #acacac); cursor: pointer; } .arrow—desc { @include icon-before(‘down.svg’, #acacac); cursor: pointer; } So far, so good: Sort Button Component Step 2: make it pretty
  22. 36 Web Component Design Joy Heron / @iamjoyheron • Style

    checked, focused, and hover input states! • Because our label follows our input, we can style this using pure CSS with a ‘+’! .arrow:checked + .arrow—asc::before, .arrow:checked + .arrow—desc::before { background-color: #535353; } .arrow:focus + .arrow—asc::before, .arrow:focus + .arrow—desc::before, .arrow:hover + .arrow—asc::before, .arrow:hover + .arrow—desc::before { background-color: #6882cb; } Result: focused: checked: Sort Button Component Step 2: make it pretty
  23. 37 Web Component Design Joy Heron / @iamjoyheron Hide our

    labels visually <input class=“arrow” id=“col1_asc” type=“radio” name=“sort” value=“col1_asc” /> <label class=“arrow—asc” for=“col1_asc”> <span class=“visually-hidden”>Sort Column 1 Ascending<span/> </label> <input class=“arrow” id=“col1_desc” type=“radio” name=“sort” value=“col1_desc” /> <label class=“arrow—desc” for=“col1_desc”> <span class=“visually-hidden”>Sort Column 1 Descending<span/> </label> base: checked: focused: Sort Button Component Step 2: make it pretty
  24. 38 Web Component Design Joy Heron / @iamjoyheron Sort Button

    Component Step 2.2: Double-check accessibility Sort Column 1 Ascending, radio button, 1 of 2
 You are currently on a radio button, 1 of 2, … ❤
  25. 39 Web Component Design Joy Heron / @iamjoyheron HTML it

    works FOR ALL USERS! CSS it’s pretty! JS? make it better? ?
  26. 40 Web Component Design Joy Heron / @iamjoyheron HTML it

    works FOR ALL USERS! CSS it’s pretty! JS we’re ok for now!
  27. 41 Web Component Design We’ve written some components… But how

    can we MAINTAIN them over time? Joy Heron / @iamjoyheron
  28. 42 Web Component Design Tip #4 Use a templating engine

    as an abstraction for your component 
 Possible templating engines: JSX / complate, Handlebars Joy Heron / @iamjoyheron
  29. 43 Web Component Design Benefits of HTML Templates for web

    components Joy Heron / @iamjoyheron Change ONCE, modify all instances Easier to MAINTAIN components over time PUBLISH templates as an npm library for use in other projects
  30. Title Text 44 Web Component Design Photo by Jens Lelie

    on Unsplash We need a big mental shift: From “Let’s Build a Website” to “Let’s maintain a product which other products use as a dependency” - Brad Frost
  31. input and select components 45 Web Component Design Joy Heron

    / @iamjoyheron /* input.jsx */ function Input ({ name, label }, …kids) { return <input class=“tabelle-input” type=“text” name={name} aria-label={label} /> } /* select.jsx */ function Select ({ name, label }, …kids) { return <select class=“tabelle-input” name={name} aria-label=“Filter {label}”> {kids} </select> <Input name=“col1” label=“Column 1” > <Select name=“col2” label=“Column 2”> <option>A</option> <option>B</option> … <option>Z</option> </Select>
  32. arrows component 46 Web Component Design Joy Heron / @iamjoyheron

    /* arrows.jsx */ function Input ({ name, label }, …kids) { return <> <input class=“arrow” id=“{name}_asc” type=“radio” name=“sort” value=“{name}_asc” /> <label class=“arrow—asc” for=“{name}_asc”> <span class=“visually-hidden”> Sort {label} Ascending <span/> </label> <input class=“arrow” id=“{name}_desc” type=“radio” name=“sort” value=“{name}_desc” /> <label class=“arrow—desc” for=“{name}_desc”> <span class=“visually-hidden”> Sort {label} Descending <span/> </label> </> } <Arrows name=“col3” label=“Column 3” />
  33. 47 Web Component Design Joy Heron / @iamjoyheron Tip #5

    Develop your components using a pattern library or living styleguide (patternlab.io, fractal) BONUS POINTS:
 publish your pattern library as reusable templates
  34. 48 Web Component Design How does this work in a

    team? Joy Heron / @iamjoyheron
  35. 50 Web Component Design Joy Heron / @iamjoyheron A new

    product with the same design system? But what happens when we have to split our teams?
  36. Web components provide a language between backend and frontend 51

    Web Component Design Cross-functional Team Joy Heron / @iamjoyheron Backend Database Platform Frontend Design & UX Design- Dev Fullstack-Dev Fullstack-Dev Frontend- Dev Backend-Dev Devops-Dev
  37. 52 Web Component Design Joy Heron / @iamjoyheron Collaboration with

    split teams AVOID throwing components over the wall! Namespace .star-… Namespace .heart-… Namespace .global-…
  38. 53 Web Component Design Joy Heron / @iamjoyheron There are

    loads of different team constellations but in all of them we want… to RELEASE new versions of our component library quickly as we develop new ideas to UPGRADE to new versions of our component library quickly WITHOUT worrying about breaking changes
  39. 54 Web Component Design Tip #6 When publishing a library

    of templates, keep the components backward compatible to make it easy to upgrade! Joy Heron / @iamjoyheron
  40. 55 Web Component Design Joy Heron / @iamjoyheron How to

    make a component backward compatible When adding new parameters, make them OPTIONAL or add a default value Instead of breaking a component, create a COPY of the existing implementation and give it a NEW NAME Avoid DEPENDING too strongly on the structure of the component when using it in other components or in UI Tests
  41. 59 Web Component Design Table Header Component Joy Heron /

    @iamjoyheron Step 1: make it work <div> Column 1 <Arrows name=“col1” label=“Column 1” /> <Input name=“col1” label=“Column 1” /> </div>
  42. 60 Web Component Design Table Header Component Joy Heron /

    @iamjoyheron Step 1.2: make it accessible Column 1 → Sort Column 1 Ascending, radio button 1 of 6 Sort Column 1 Ascending → → Sort Column 1 Descending, radio button 2 of 6 Sort Column 1 Descending → Filter Column 1, edit text → → Column 2 → Sort Column 2 Ascending, radio button 3 of 6 Sort Column 2 Ascending → → Sort Column 2 Descending, radio button 4 of 6 → …
  43. 61 Web Component Design Tip #7 Wrap input fields in

    a role=group to add a navigation level for a screen reader Joy Heron / @iamjoyheron Note: The built-in HTML Element fieldset provides similar behavior but is extremely difficult to style
  44. 62 Web Component Design Table Header Component Joy Heron /

    @iamjoyheron Step 1.2: make it accessible <div role=“group” aria-labelledby=“col1_group” > <span id=“col1_group” >Column 1</span> <Arrows name=“col1” label=“Column 1” /> <Input name=“col1” label=“Column 1” /> </div> <div> Column 1 <Arrows name=“col1” label=“Column 1” /> <Input name=“col1” label=“Column 1” /> </div>
  45. 63 Web Component Design Table Header Component Joy Heron /

    @iamjoyheron Step 1.2: make it accessible Column 1, group VO+→ Column 2, group VO + ↓ Column 2 → Sort Column 2 Ascending, radio button 3 of 6 Sort Column 2 Ascending → → Sort Column 2 Descending, radio button 4 of 6 Sort Column 2 Descending → Filter Column 2, edit text → Navigation via groups with a screen reader! ❤
  46. 67 Web Component Design Tip #8 Design CSS like a

    box of chocolates Design your container with love and place your components inside it. (use Flexbox or CSS Grid) Joy Heron / @iamjoyheron
  47. 68 Web Component Design Table Header Component Joy Heron /

    @iamjoyheron Step 2: make it pretty Define a CSS Grid for Layout HEADER ▲ ▼ SEARCH .tabelle-header { display: grid; grid-template-areas: “header arrow-asc” “header arrow-desc” “search search”; } 1fr Takes up a proportional amount of grid auto grid-template-columns: 1fr auto;
  48. 69 Web Component Design Table Header Component Joy Heron /

    @iamjoyheron Step 2: make it pretty Place children items in grid HEADER ▲ ▼ SEARCH .tabelle-header { .header { grid-area: header; } .arrow—asc { grid-area: arrow-asc; } .arrow—desc { grid-area: arrow-desc; } .tabelle-input { grid-area: search; } } .tabelle-header { grid-template-areas: “header arrow-asc” “header arrow-desc” “search search”; }
  49. 70 Web Component Design Table Header Component Joy Heron /

    @iamjoyheron Step 2: make it pretty Add new styling classes <table> <th class=“tabelle-header” role=“group” aria-labelledby=“col2g”> <span class=“header” id=“col2g”>Column 1</span> … </th> </table> Almost there!!!
  50. 71 Web Component Design Tip #9 With CSS Grid or

    Flexbox, vertical alignment is finally easy! Joy Heron / @iamjoyheron
  51. 72 Web Component Design Table Header Component Joy Heron /

    @iamjoyheron Step 2: make it pretty Center table header vertically .tabelle-header { .header { align-self: center; } } ❤
  52. 73 Web Component Design Table Header Component Joy Heron /

    @iamjoyheron Our HTML Template for reuse ❤ /* tabelle-header.jsx */ function TabelleHeader ({ name, label }, …kids) { return <div class=“tabelle-header” role=“group” aria-labelledby=“{name}_group”> <span class=“header” id=“{name}_group” > {label} </span> <Arrows name={name} label={name} /> {kids ? <Select name={name} label={name}>{kids}<Select> : <Input name={name} label={name} />} </div> }
  53. 74 Web Component Design Joy Heron / @iamjoyheron HTML it

    works FOR ALL USERS! CSS it’s pretty! JS? make it better? ?
  54. 75 Web Component Design Joy Heron / @iamjoyheron Joy Heron

    / @iamjoyheron HTML it works FOR ALL USERS! CSS it’s pretty! JS no need!
  55. 78 Web Component Design Tabelle Joy Heron / @iamjoyheron Step

    1: make it work <form action=“/search”> <table> <thead> <tr> <th> <TabelleHeader name=“col1” label=“Column 1” /> </th> <th> <TabelleHeader name=“col2” label=“Column 2” /> </th> <th> <TabelleHeader name=“col3” label=“Column 3” /> </th> </tr> </thead> <tbody>…</tbody> </table> <button type=“submit”>Perform Search</button> </form> Wrap in a form so that we can submit user filter queries
  56. 79 Web Component Design Tabelle Joy Heron / @iamjoyheron Step

    1: make it work ❤ The magic of FORMS
  57. 80 Web Component Design Joy Heron / @iamjoyheron HTML it

    works FOR ALL USERS! CSS? make it pretty? ?
  58. 81 Web Component Design Joy Heron / @iamjoyheron HTML it

    works FOR ALL USERS! CSS someone else can make it pretty!
  59. 83 Web Component Design Tip #10 Submit forms asynchronously and

    replace your DOM with the result from the server Joy Heron / @iamjoyheron
  60. 84 Web Component Design Tabelle Joy Heron / @iamjoyheron Step

    3: make it better Submitting forms asynchronously // stringify form data as `application/x-www-form-urlencoded` function serializeForm (form) { … } function submit (form) { const uri = serializeForm(form) return fetch(uri) .then(response => response.text()) } ⚠ Only the application happy path is being considered here. Source: https://github.com/FND/uitil
  61. 85 Web Component Design Tabelle Joy Heron / @iamjoyheron Step

    3: make it better Replacing the tbody with response HTML function template2dom (htmlString, selector) { let tmp = document.createElement(‘template’) tmp.innerHTML = htmlString.trim() return tmp.content.querySelector(selector) } function replaceTbody (tbody, htmlResponse) { let newTbody = template2dom(htmlResponse, ‘tbody’) tbody.innerHTML = newTBody.innerHTML } ⚠ Only the application happy path is being considered here.
  62. 86 Web Component Design Tabelle Joy Heron / @iamjoyheron Step

    3: make it better Override default submit behavior function doSubmit (component, form) { let tbody = component.querySelector(‘tbody’) submit(form) .then(html => replaceTBody(tbody, html)) } function initializeSubmit (component, form) { form.addEventListener(‘submit’, ev => { doSubmit(component, form) ev.preventDefault() }) } ⚠ Only the application happy path is being considered here. Who calls the initialize function?
  63. 87 Web Component Design Historically: adding a component dynamically used

    to require BOTH the HTML Markup and a function to initialize it Joy Heron / @iamjoyheron
  64. 88 Web Component Design Tip #11 Use Custom Elements to

    define a custom HTML Element AND define how to initialize it. The browser will then initialize your component for you! Joy Heron / @iamjoyheron
  65. 89 Web Component Design Tabelle Joy Heron / @iamjoyheron Step

    3: make it better Custom Elements allow you to define how the component needs to be initialized. class Tabelle extends HTMLElement { connectedCallback () { let form = this.form initializeSubmit(this, form) } get form () { return this.querySelector(‘form’) } } customElements.define('ta-belle', Tabelle) <ta-belle> … </ta-belle> As soon as ta-belle appears in the DOM, connectedCallback will be called! ✨
  66. 90 Web Component Design Tabelle Joy Heron / @iamjoyheron Step

    3: make it better Custom Elements provide scope: this is bound to the HTML Element itself and it’s native JavaScript API class Tabelle extends HTMLElement { connectedCallback () { let form = this.form initializeSubmit(this, form) } get form () { return this.querySelector(‘form’) } } customElements.define('ta-belle', Tabelle)
  67. 91 Web Component Design Tabelle Joy Heron / @iamjoyheron Step

    3: make it better Make the implementation more dynamic by triggering submits when an input element changes import { debounce } from ‘util’ class Tabelle extends HTMLElement { connectedCallback () { … form.addEventListener(‘change’, () => doSubmit(this, form)) form.addEventListener(‘keyup’, debounce(300, ev => doSubmit(this, form))) } } Make sure to debounce ‘keyups’ for input[type=text] elements! https://davidwalsh.name/javascript-debounce-function ⚠ Only the application happy path is being considered here.
  68. 94 Web Component Design Joy Heron / @iamjoyheron HTML it

    works FOR ALL USERS! CSS it’s pretty! JS faster & dynamic