question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Are webcomponents working?

See original GitHub issue

I’ve checked in the builder and demo and I cannot verify but the same component that is working perfect in react or vue is not working with webcomponents, nothing happens with props and the slot.

https://codesandbox.io/s/papanasi-webcomponents-27zsfr?file=/src/index.js

<pa-button variant="red">Test</pa-button>

original code:

/**
 * Usage:
 *
 *  <pa-button></pa-button>
 *
 */
export default class Button extends HTMLElement {
  constructor() {
    super();
    const self = this;

    this.state = {};
    this.props = {};

    // used to keep track of all nodes created by show/for
    this.nodesToDestroy = [];
    // batch updates
    this.pendingUpdate = false;

    if (undefined) {
      this.attachShadow({ mode: 'open' });
    }
  }

  destroyAnyNodes() {
    // destroy current view template refs before rendering again
    this.nodesToDestroy.forEach((el) => el.remove());
    this.nodesToDestroy = [];
  }

  get _root() {
    return this.shadowRoot || this;
  }

  connectedCallback() {
    this._root.innerHTML = `
      <button data-test1="1" data-name="button-1">
        <slot></slot>
      </button>
      <style></style>`;
    this.pendingUpdate = true;
    this.render();

    this.onMount();
    this.pendingUpdate = false;
    this.update();
  }

  onMount() {}

  onUpdate() {}

  update() {
    if (this.pendingUpdate === true) {
      return;
    }
    this.pendingUpdate = true;

    this.render();
    this.onUpdate();

    this.pendingUpdate = false;
  }

  render() {
    // re-rendering needs to ensure that all nodes generated by for/show are refreshed
    this.destroyAnyNodes();
    this.updateBindings();
  }

  updateBindings() {
    this._root.querySelectorAll("[data-name='button-1']").forEach((el) => {
      el.className = `pa-button ${this.props.variant ? `pa-button--${this.props.variant}` : ''} `;
      if (el.props) {
        el.props.class = `pa-button ${this.props.variant ? `pa-button--${this.props.variant}` : ''} `;
        el.update();
      }
    });
  }
  renderTextNode(el, text) {
    const textNode = document.createTextNode(text);
    el.after(textNode);
    this.nodesToDestroy.push(el.nextSibling);
  }
}
customElements.get("pa-button") || customElements.define("pa-button", Button);

result: image

Expected: To have “test” inside the button and “pa-button–red” in the classlist.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
PatrickJScommented, May 26, 2022

no worries issues are welcomed. we didn’t document this so it’s our fault. I’ve been working on the webcomponent output so anything in that area I’ll address

1reaction
PatrickJScommented, May 27, 2022

@CKGrafico can you make a new issue about this.

Attributes aren’t being converted to props on load

The way I’m using mitosis/webcomponents is having the whole app in it so I never actually ran into this issue haha

Read more comments on GitHub >

github_iconTop Results From Across the Web

What happened to web components? - LogRocket Blog
Originally, web components had a standalone W3C specification called Custom Elements. Custom Elements was deprecated in 2018 and its parts ...
Read more >
Are Web Components Dead? - Level Up Coding
Despite the promising advantages, Web Components continuously struggle to find industrywide adoption. Many developers say they have already died. Who's right?
Read more >
A Complete Introduction to Web Components in 2022 - Kinsta
Web Components are a standard way to create reusable and modular HTML elements without using a JavaScript framework.
Read more >
Web Components - MDN Web Docs - Mozilla
A guide showing how to use the features of custom elements to create simple web components, as well as looking into lifecycle callbacks...
Read more >
The State Of Web Components in 2022 - DEV Community ‍ ‍
The Web Components Community Group aids with "collaboration between people working on web components libraries, tools, documentation, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found