GithubHelp home page GithubHelp logo

jhony-v / stencil-shared-library Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 76 KB

Demo using StencilJs components in mono repo and micro frontends through core initialization

CSS 26.86% TypeScript 58.82% HTML 14.32%
microfrontends react stenciljs

stencil-shared-library's Introduction

Stencil shared library

Demo using StencilJs components in mono repo and micro frontends through core initialization

1. Creation of engine using stencil

// packages/engine/src/index.ts
import ReactDOM from 'react-dom/client';

export class EngineApplication {
  constructor(private readonly container: string, private readonly app: JSX.Element) {
    import('design/loader').then(this.setupCustomElements);
    import('market/loader').then(this.setupCustomElements);
  }

  setupCustomElements({ defineCustomElements, applyPolyfills }) {
    applyPolyfills().then(() => {
      defineCustomElements(window);
    });
  }

  start() {
    const el = document.getElementById(this.container) as HTMLElement;
    ReactDOM.createRoot(el).render(this.app);
  }
}

2. Usage of engine

// apps/dashboard/src/main.tsx

import { EngineApplication } from 'engine';
import App from './App';
import './index.css';

const engine = new EngineApplication('root', <App />);
engine.start();

3. Using web components created in StencilJs

// apps/dashboard/src/App.tsx

//@ts-nocheck
import './App.css';

function App() {
  return (
    <div className="App">
      <h1>HELLO</h1>
      <my-widget></my-widget>
      <my-component first="Stencil" last="'Don't call me a framework' JS"></my-component>
    </div>
  );
}

export default App;

4. Accessing to events in web components

4.1 Creating reusable hook to bind Stencil Js components

import { useEffect, useRef } from 'react';

type UseWebComponentRefProps<On> = {
  on: On;
};

export default function useWebComponentRef<T>({ on }: UseWebComponentRefProps<T>) {
  const ref = useRef<HTMLElement>();

  useEffect(() => {
    const events = Object.keys(on).map(name => {
      return {
        name,
        handler: ev => on[name](ev.detail),
      };
    });

    events.forEach(({ name, handler }) => {
      ref.current?.addEventListener(name, handler);
    });

    return () => {
      events.forEach(({ name, handler }) => {
        ref.current?.removeEventListener(name, handler);
      });
    };
  }, []);

  return [ref];
}

4.2 Using in react components

import { useWebComponentRef } from 'engine';

const WebComponent = ({ username }) => {
  const [widget] = useWebComponentRef({
    on: {
      changeUsername(value) {
        console.log(`${username} prints ${value}`);
      },
    },
  });

  return (
    <design-widget username={username} ref={widget}>
      <i>HELLO {username}</i>
    </design-widget>
  );
};

stencil-shared-library's People

Contributors

jhony-v avatar

Stargazers

 avatar

Watchers

 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.