All Projects → alesgenova → stenciljs-in-react

alesgenova / stenciljs-in-react

Licence: other
Stenciljs components in React: step by step guide

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
CSS
56736 projects

Projects that are alternatives of or similar to stenciljs-in-react

anywhere-webcomponents
A UI work in progress based on custom elements (web components) for use in anywhere.
Stars: ✭ 17 (-22.73%)
Mutual labels:  webcomponents, stenciljs
solar-components
Web Components Implementation of Solar Design System
Stars: ✭ 16 (-27.27%)
Mutual labels:  webcomponents, stenciljs
app-starter
Angular mono-repo (Ionic/Capacitor/StencilJS/Web Component) app starter for supporting cross platform apps.
Stars: ✭ 75 (+240.91%)
Mutual labels:  webcomponents, stenciljs
Ionicons
Premium hand-crafted icons built by Ionic, for Ionic apps and web apps everywhere 🌎
Stars: ✭ 15,802 (+71727.27%)
Mutual labels:  webcomponents, stenciljs
web-photo-filter
A Web Component to apply Instagram-like WebGL filters to photos
Stars: ✭ 105 (+377.27%)
Mutual labels:  webcomponents, stenciljs
Stencil
A toolchain for building scalable, enterprise-ready component systems on top of TypeScript and Web Component standards. Stencil components can be distributed natively to React, Angular, Vue, and traditional web developers from a single, framework-agnostic codebase.
Stars: ✭ 9,880 (+44809.09%)
Mutual labels:  webcomponents, stenciljs
mm-components
Music Markups components
Stars: ✭ 50 (+127.27%)
Mutual labels:  webcomponents, stenciljs
Ionic Framework
A powerful cross-platform UI toolkit for building native-quality iOS, Android, and Progressive Web Apps with HTML, CSS, and JavaScript.
Stars: ✭ 45,802 (+208090.91%)
Mutual labels:  webcomponents, stenciljs
Atoms
Atoms for Blaze UI
Stars: ✭ 1,505 (+6740.91%)
Mutual labels:  webcomponents, stenciljs
gatsby-remark-highlight-code
Adds stylish cards and syntax highlighting to code blocks in markdown files
Stars: ✭ 63 (+186.36%)
Mutual labels:  webcomponents, stenciljs
stencil-mobx
No description or website provided.
Stars: ✭ 26 (+18.18%)
Mutual labels:  stenciljs
web-controls-library
A library of reusable custom elements for GeneXus web applications
Stars: ✭ 14 (-36.36%)
Mutual labels:  stenciljs
revodropdown
Minimalistic dropdown and auto-complete component with filtering and keyboard support
Stars: ✭ 20 (-9.09%)
Mutual labels:  stenciljs
vscode-exts
Visual Studio Code Extensions
Stars: ✭ 33 (+50%)
Mutual labels:  webcomponents
svelte-webcomponent-boilerplate
🏗 Create your HTML5 Web Component with Svelte. Made your web components with this user-friendly boilerplate
Stars: ✭ 58 (+163.64%)
Mutual labels:  webcomponents
material-webcomponents
Material Design implemented in Web Components (Custom Elements v1)
Stars: ✭ 110 (+400%)
Mutual labels:  webcomponents
fuco
Functional Component like React, but for Web Components.
Stars: ✭ 71 (+222.73%)
Mutual labels:  webcomponents
zkit
zKit, components for modern web.
Stars: ✭ 38 (+72.73%)
Mutual labels:  webcomponents
polytimer
polytimer.rocks
Stars: ✭ 24 (+9.09%)
Mutual labels:  webcomponents
ui5con-app-vue
The Smart Store app is a Vue.js sample application, demonstrating the usage of the UI5 Web Components. You can find a step by step tutorial below on how to build the app by yourself. You don't have to clone the repo, the app will be built from scratch.
Stars: ✭ 31 (+40.91%)
Mutual labels:  webcomponents

Stencil components in React

Stencil is not a JS framework. It is a compiler that produces a reusable web component that can be embedded anywhere else.

This is a step by step guide to consume a non-trivial stencil component in a React app.

The starter react app was created with create-react-app.

Similar guides

Table of contents

0: Build a stenciljs component and publish it to npm

Creating your first stencil component is very easy and it is well documented here.

This example will consume two components:

1: Add the component(s) to the dependencies

Add the component to the app dependencies in package.json

// package.json

"dependencies": {
  ...
  "@openchemistry/molecule-vtkjs": "^0.3.2",
  "split-me": "^1.1.4"
}

2: Import the component(s)

Import the component in the index.js of the app:

import { defineCustomElements as defineMolecule } from '@openchemistry/molecule-vtkjs/dist/loader';
import { defineCustomElements as defineSplitMe } from 'split-me/dist/loader';

defineMolecule(window);
defineSplitMe(window);

3: Consume the component

It is now possible to use the tag provided by the stencil component in the render function of any react component.

render() {
    return (
      <split-me n="2">
        <oc-molecule-vtkjs slot="0"></oc-molecule-vtkjs>
        <oc-molecule-vtkjs slot="1"></oc-molecule-vtkjs>
      </split-me>
    )
}

Appendix: Attribute vs Prop

oc-molecule-vtkjs has a property named cjson that expects an object (or a JSON.stringified object).

Strings and numbers can be passed directly as attributes to a stencil component.

One way to pass a complex object to a component could be to JSON.stringify() the object and then JSON.parse() it inside the component. But this round trip can be expensive, and it would be a good idea to pass the object directly as a prop.

React doesn't provide a convenient way to distinguish between attribute and prop, so a little work is needed to achieve this.

It just boils down to saving a reference to the element of the stencil component, and then set the property directly in the javascript code.

To make this operation easier, it can be convenient to create a reusable utility function wc.

export function wc(customEvents = {}, props = {}) {
  let storedEl;

  return function (el) {
    for (let name in customEvents) {
      let value = customEvents[name] ;
      // If we have an element then add event listeners
      // otherwise remove the event listener
      const action = (el) ? el.addEventListener : storedEl.removeEventListener;
      if (typeof value === 'function') {
        action(name, value);
        return;
      }
    }
    
    // If we have an element then set props
    if (el) {
      for (let name in props) {
        let value = props[name] ;
        el[name] = value;
      }
    }
    storedEl = el;
  };
}

And then use it in the jsx to bind events and properties to the webcomponent this way:

import React, { Component } from 'react';
import { wc } from './utils/webcomponent';

class SomeComponent extends Component {

  render() {
    return (
      <div style={{width: "100%", height: "20rem", position: "relative"}}>
        <oc-molecule-vtkjs
          ref={wc(
            // Events
            {},
            // Props
            {
              cjson: molecule
            }
          )}
        />
      </div>
    );
  }

}

export default SomeComponent;
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].