Shadow DOM layout compositions

Tomek Wytrębowicz

@tomalecpl

tomalec

Starcounter Tech Day 2016, 14 December, Stockholm, Sweden

Overview

@tomalecpl
@tomalecpl
@tomalecpl

What is the underlying technology?
The Web Components

@tomalecpl

Web Components specs:

  • Template Element
  • HTML Imports
  • Custom Elements
  • Shadow DOM
  • @tomalecpl

Shadow DOM

  • separate layout from semantics
  • modularity: compose one page from many documents
  • code clarity: hide implementation details of an element
  • CSS scoping/encapsulation
  •  
  • Distribute elements without touching their strucutre
@tomalecpl

In example

@tomalecpl

In example

App A

App B

Apps A x B

@tomalecpl

Benefits?

@tomalecpl

Individual app

Shadow DOM layouts

  • Same results as in writing monolithic front are available
  • Separates layout from functionality
  • Shorter and more readable code
  • Content is more discoverable to SEO and robots
  • Encapsule CSS
  • Declarative Shadow DOM
  • Simplified theming
  • A/B testing
  • Generating layouts

Tools

  • Change the layout using browser tools, then just click save
@tomalecpl

Echosystem

  • ...
  • Change the layout without touching funtional elements
  • Reuse layouts provided by app dev
  • Easy as moving lego blocks
  • Flexible as writing custom HTML page from top to bottom
  • Layouts are managable as any other data in Starcounter
@tomalecpl

How we can do that?

@tomalecpl

Insertion point

<starcounter-include/>
@tomalecpl

Prepare individual app

@tomalecpl

Default compositions

Given with the partial's HTML code

  • Separates layout from functionality
  • Shorter and more readable code
  • Content is more discoverable to SEO and robots
  • Encapsulate CSS
  • Declarative Shadow DOM
@tomalecpl

Default compositions

Migrate old-school code

As simple in code as it looks in pictures
identify functional elements
move the rest into composition
  • Same results as in writing monolithic front are available
@tomalecpl

Specific ones

  • Also HTML code
  • comes as data in JSON view-model
  • With same constrains freedom of HTML doc.
    DocumentFragment with any HTML+CSS structure and <slot>s
  • could be migrated between instances w/ Starcounter tools
  • Could be changed by server-side code
  • Simplified theming
  • A/B testing
  • Generating layouts
  • Layouts are managable as any other data in Starcounter
@tomalecpl

Rule them all!

@tomalecpl

Merged partial

Replace defaults with:
SELECT * FROM HTMLComposition
WHERE Key = "/People/Person.html&/Images/Concept.html"
  • Change the layout without touching funtional elements
  • Reuse layouts provided by app dev
  • Layouts are managable as any other data in Starcounter
@tomalecpl

What if there is none?

  • Use defaults
  • Magic is planned
  • You have many ways to provide those, anyway
@tomalecpl

What we can do with compositions for merged partials?

  • Whatever Web allows
    • Reorder inbetween between aps
    • hide
    • resize
    • ...
    • Apply any CSS rules
    • Build any HTML structure
    • Even add scripts if you're brave enough
  • Just use
    <slot name="App/el">
    to distribute an element
  • Flexible as writing custom HTML page from top to bottom
@tomalecpl

How to provide & create HTML composition?

@tomalecpl

Code Defaults

  • Way to provide defaults
  • Styling standalone
  • Provided by app devs
@tomalecpl

Populate Stacounter.HTMLComposition

  • (Usefull for) automated deployments and migrations
  • (should be delivered with) applications suites and mappers
  • Used for programmatically created Compositions (out of scope of this presentation)
  • curl
  • INSERT INTO ...
  • new Starcounter.HTMLComposition
  • ...
@tomalecpl

Editors

for humans

  • heavy-coders
  • any-coders
  • no-coders
  • designers
  • ... anyone who knows how to use a web browser
@tomalecpl

Live editor

  • Run Launcher / Layout App
  • Open the editor
  • Pick a partial to edit
  • Changes with live-preview
@tomalecpl

Live editor for coders

  • code editor
  • type any HTML needed
  • clear & save with single click
  • slot elements created automatically
  • Integration with browser's dev tools
  • Changes with live-preview
@tomalecpl

Live editor for non-coders

  • WYSIWYG editor
  • simple lego-blocks
  • move up-down
  • resize
  • group
  • hide
  • Inter-operates with advanced layouts
  • Changes with live-preview
@tomalecpl

AI generated layouts

(developement planned for Jan/Feb 2017)

  • Proposes composition
  • Re-generates & learns on demand
  • Takes many factors
    • Mapped and bound concepts
    • HTML semantincs
    • slot names
  • base the knowledge on entire echosystem
  • profiles to your needs
@tomalecpl

Guidelines

  • #UseThePlatform
  • Leave functional elements on root level
  • Put layouts into shadow
  • Keep them as plain and vanilla as possible
  • Do not expose TextNodes on root level (use <span>)
  • Prefix classnames, ids, slot names w/ App name
  • explicit slot names
  • do not use default slot in default compositions
@tomalecpl

Browser support

@tomalecpl

Shadow DOM v1 vs v0

V0

  • Initial spec
  • Currently in use
  • Currently supported by polyfills and Chrome
  • Supported by Starcounter until Polyfill@v1 release (~Q1 2017)

V1

  • Latest spec
  • Slightly polished API
  • All browser vendors agreed on
  • Suits our needs even better
<content select='[slot="App/name"]'>
<slot name="App/name">
@tomalecpl

Shadow DOM v1

Chrome/OperaPolyfill by Google team, supports V0, V1 in development.
Supports all browsers below
Chrome/OperaChrome V0 & V1
Chrome/OperaOpera V0 & V1
FirefoxFirefox ⚑ behind the flag dom.webcomponents.enabled
WebkitSafari V1 (Safari 10)
IE/EdgeIE/Edge "Priority: High — We intend to begin development soon."
@tomalecpl

Starcounter versions

2.2.1.3234

(Sep 2016)

@tomalecpl

Editors

Code editor
w/ dev tools integration
in Launcher & Layout App
as a custom element
<starcounter-layout-html-editor>
WYSIWYG editor Planned for this month, Dec 2016
AI generated layouts Prototype planned for Feb 2017
@tomalecpl

Thanks!

Tomek Wytrębowicz

tomalec

@tomalecpl

Articles:

This & other talks: