Giter Club home page Giter Club logo

web-components-poc's Introduction

Web Components PoC

Framework agnostic UI kit example based on Web Components

App

How to Start

  1. $ npm install
  2. $ npm start

How to Build

$ npm run build - generates static markup and builds for production

Standards and Libraries

  1. Autonomous Custom Elements
  2. NO Shadow DOM but Slots approach is implemented
  3. Preact as a reactive template engine
  4. HTM - temporal dependency; should be replaced with corresponding TS/Babel config

Code Structure

Utils:

  1. src/index.js - entry point with hydration ReactDOM.hydrate(<App />, rootElement);
  2. src/ssr.js - script which generates static markup ReactDOMServer.renderToString(React.createElement(App))
  3. src/utils/define.js - helper to simplify declaration of custom elements
    • embeds Preact into custom element life cycle as a template engine
    • implements Slots approach
  4. src/utils/reactWrapper.js - Web Component => React Component adapter
  5. src/utils/html.js - wrapper for temporal dependency HTM

Web Components:

  1. src/Button - rshb-button
  2. src/Checkbox - rshb-checkbox
  3. src/Dropdown - rshb-dropdown
  4. src/Input - rshb-input

Form example: App.js

Web Component Definition

import { define } from "../utils/define";
import { html } from "../utils/html";
import "./Checkbox.css";

export const Checkbox = define(
  class extends HTMLElement {
    // Custom element tag name
    static tag = "rshb-checkbox";

    /**
     * Custom element attributes definition
     *
     * type AttrName = string;
     * type AttrConverter<PropType = any> =(attrValue: string) => PropType;
     * type AttrsDefinition = Record<AttrName, AttrConverter>;
     */
    static props = {
      id: String,
      name: String,
      checked: Boolean,
      disabled: Boolean
    };

    /**
     * Custom element slots definition
     *
     * type SlotName = string;
     * type SlotCSSSelector = string;
     * type SlotsDefinition = Record<SlotName, SlotCSSSelector>;
     */
    static slots = {
      label: "label"
    };

    static events = {
      onClick: "click"
    };

    // Preact component used as a template (hooks could be used here to introduce some logic)
    static template = (props) =>
      html`<div class="form-check">
        <input class="form-check-input" type="checkbox" ...${props} />
        <label for=${props.id} class="form-check-label" />
      </div>`;
  }
);

https://developer.mozilla.org/ru/docs/Web/CSS/:defined

rshb-checkbox {
  display: inline;
}

rshb-checkbox:defined {
  opacity: 1;
}

rshb-checkbox:not(:defined) {
  opacity: 0.5;
}

Server-Side Rendering

  1. $ npm run ssr generates static markup and puts it (as REACT_APP_SSR environment variable) in .env file.
  2. $ npm start (as well as $ npm run build) uses generated REACT_APP_SSR environment variable to substitute it into <div id="root">%REACT_APP_SSR%</div> of public/index.html

web-components-poc's People

Contributors

redneckz avatar

Watchers

 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.