Giter Site home page Giter Site logo

web-layers's Introduction

Web Layers

Web Layers is an extension of the Web Components suite enabling complete component encapsulation including JavaScript, declarative definition, and a no build environment for the prototyping and development of web apps.

Use Cases

Encapsulation

The ability to compose your application with different frontend frameworks and build requirements.

An online store with

  • A navigation element written with React and built with Webpack.
  • A static details page written with Web Components.
  • A shopping cart application written with Svelte and built with Snowpack.

Declarative

something about not having a package.json file and a bunch of imports

No Build

Reduce the overhead of build system and configuration requirements by prototyping and developing applications without the need for complex development dependency installation or setup.

Start developing a new application with

  • Online sandbox
  • Local static server and network tunnel service like ngrok

Usage

  • Custom Element: A web layer is a custom element and can be declaratively defined in HTML or constructed with JavaScript.
  • Proxy Server: The proxy server allows cross origin resources to be fetched and managed by one window context, custom resource resolving, and same origin resource caching with service workers.
  • Prerendering:

Custom Element

Properties

WebLayer.customEvents

Attributes

src

The src attribute is a URL string used to source a layer and its resources. This is achieved by creating a hidden iframe, setting the src attribute of the iframe to the value of the src attribute on the layer, proxying if needed, and listening for the source event.

<web-layer src="https://github.io/my-layer/"></web-layer>

previewContent

When present the previewContent attribute will slot the layers content in the WebLayer.shadowRoot. The <slot> element will either be replaced with the sourced result if previewSource is present or replaced with the rendered content.

<web-layer previewContent></web-layer>

previewSource

When present the previewSource attribute indicates that once the layer is sourced to render the contents in the WebLayer.shadowRoot. This content will be replaced with the rendered result.

<web-layer previewSource></web-layer>

sharedContext

The sharedContext attribute prevents the layer from creating a new context window when rendering. All javascript will be evaluated within the current windows context.

Note

renderAdoptStart and renderAdoptComplete events will not be dispatched on layers with the sharedContext attribute.

<web-layer sharedContext></web-layer>

extend

The extend attribute will source and merge the target layers template with the parent layer. When there are multiple extending layers, the target layers resources are appended to the parents <head> or <body> in the same order as they appear in the content.

<web-layer>
  <web-layer extend></web-layer>
</web-layer>

template

The template attribute indicates that the web layer will be sourced but not rendered. This is used in prerendering and also allows for presourcing a layer.

<web-layer template></web-layer>

sourceEvent

The sourceEvent attribute defines a custom complete event that will be dispatched from the source iframe when it is done.

<web-layer sourceEvent="customSource">
  <template>
    <script>
      setTimeout(() =>
        window.dispatch(new Event('customSource')),
        3000
      )
    </script>
  </template>
</web-layer>

Events

Web Layer

Events dispatched on the WebLayer element

layerSourced

Event dispatched on the WebLayer element when it has finished sourcing

layerRendered

Event dispatched on the WebLayer element when it has finished rendering

layerPrerendered

Event dispatched on the WebLayer element when it has finished prerendering

Web Layer Window

Events dispatched on WebLayer iframe windows

sourceStart

CustomEvent dispatched when a source iframe is created and appended to the host. This event is dispatched with a WebLayerEventDetail instance as its CustomEvent.detail.

renderStart

CustomEvent dispatched when a render iframe is created and appended to the host. This event is dispatched with a WebLayerEventDetail instance as its CustomEvent.detail.

renderAdoptComplete

CustomEvent dispatched after the render iframes childnodes are adopted into the host layers document and appended to the host layers shadowRoot. This event is dispatched with a WebLayerEventDetail instance as its CustomEvent.detail.

Host Window

Events dispatched on the host window

sourceComplete

Dispatched when all the WebLayer instances have been sourced

renderComplete

Dispatched when all the WebLayer instances have been rendered

prerenderStart

Dispatched when a prerender has been started.

prerenderComplete

Dispatched when all the WebLayer instances have been prerendered

Interfaces

WebLayerEventDetail

The WebLayerEventDetail interface exposes the host window and host layer.

Properties

WebLayerEventDetail.window

A reference to the host window

WebLayerEventDetail.element

A reference to the host layer

Proxy

A writable property that is only assigned in the root host layer. The proxy is a URL string that is used to construct the layers source iframe src attribute only when the current window.location.origin does not match the layers src origin. To get an instance of it, use the window.webLayerProxy property.

TemplateRegistry

The TemplateRegistry is responsible for managing each layers source templates. It is a map of layer source URLs and their source node instances. To get an instance of it, use the window.webLayerTemplateRegistry property.

CustomEventRegistry

The CustomEventRegistry interface provides methods for registering and querying custom events. An instance of it will be passed as the event detail for sourceStart and renderStart events. It can also be accessed from the WebLayer.customEvents property.

Methods

customEvents.get(event) Returns the event name for the custom event.

customEvents.define(event, customName) Defines a new custom event.

customEvents.update(event, customName, options = {adopt: false}) Update a new custom event. Optionally adopt to new listeners.

Note

Calling update will cancel any current event listeners for that event and optionally add them as listeners to the new event name.

Examples

TBD

The default events for each lifecycle are:

  • source: DOMContentLoaded

  • render: load

  • prerender: renderComplete

Lifecycle

  • construct
  • connect
  • attribute change
    • source
    • render
  • prerender

CLI

Prerenders the target URL and prints the prerendered html to the console

~ npx web-layers prerender https://localhost:9000/layer/https%3A%2F%2Flocalhost%3A8080%2Fexamples%2Ftest

Optionaly write the output to a file

~ npx web-layers prerender https://localhost:9000/examples/test > index.html

Start a local proxy server

~ npx web-layers proxy

Development

  1. install deps
~ npm i
  1. get HTTPS working for localhost see HTTPS for localhost

Custom Element

  1. run static server
~ node static.js
  1. view an example e.g. https://localhost:8080/examples/nested.html

Proxy Server

  1. run proxy server
~ node index.mjs
  1. view an example
https://localhost:9000/layer/https%3A%2F%2Flocalhost%3A8080%2Fexamples%2Fperformance.html

web-layers's People

Watchers

Ben Sawyer avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.