Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: watch decorator doesn't work if we change the original tag name #3554

Closed
3 tasks done
urielblanco opened this issue Aug 25, 2022 · 4 comments
Closed
3 tasks done
Labels
Bug: Validated This PR or Issue is verified to be a bug within Stencil

Comments

@urielblanco
Copy link

urielblanco commented Aug 25, 2022

Prerequisites

Stencil Version

2.13.0

Current Behavior

My problem is that the @Watch decorator only works if I keep the original tag name.

  • If I import a custom element from my stencil library and define it as a custom element like this:

My vanilla js app

import { MyComponent } from '@my-lib/dist/components/my-component.js';

customElements.define('my-component', MyComponent);

My simple component

import { Component, Prop, h, Watch } from '@stencil/core';

@Component({
  tag: 'my-component',
  styleUrl: 'my-component.css',
  shadow: true,
})
export class MyComponent {
  /**
   * The first name
   */
  @Prop({ reflect: true, mutable: true }) checked?: boolean = false;

  @Watch('checked')
  onValueChange(): void {
    console.log('trigger from watch', this.checked);
  }

  private readonly onChange = (e: InputEvent): void => {
    const isElementChecked = (e.target as HTMLInputElement).checked;
    if (isElementChecked !== this.checked) {
      this.checked = isElementChecked;
    }
  };

  render() {
    return <input onChange={this.onChange} type="checkbox" name="test" id="" />;
  }
}

With this example, I can see in the console that the watch func triggered 🙌🏽

Expected Behavior

Suppose I change the original tag name when I define my component in customElement.define() I hope that the @Watch decorator keeps working but it doesn't.

Steps to Reproduce

You can recreate my example in Current behavior or download it and run the scripts like this:

npm i
npm run build
npm run start:test

And that's it! If you change the tag name from "my-component" to "my-component-test" the watch func stop working and you can't see in the console my message.

Code Reproduction URL

https://github.com/urielblanco/test-watch

Additional Information

No response

@ionitron-bot ionitron-bot bot added the triage label Aug 25, 2022
@urielblanco
Copy link
Author

urielblanco commented Aug 25, 2022

The community in slack was help me and one guy said that the problem is here

https://github.dev/ionic-team/stencil/blob/c05cebec911ca0ef4acf7e4673e24e7801316b0d/src/runtime/initialize-component.ts#L87-L88

customElements.whenDefined(cmpMeta.$tagName$).then(() => (hostRef.$flags$ |= 128 /* isWatchReady */));

$tagName$ is defined in your file as "my-component", so this callback never fires

Then

https://github.dev/ionic-team/stencil/blob/c05cebec911ca0ef4acf7e4673e24e7801316b0d/src/runtime/set-value.ts#L53-L54

      if (BUILD.watchCallback && cmpMeta.$watchers$ && flags & HOST_FLAGS.isWatchReady) {

is always false (meaning the watch is never triggered

Looks like changing:

customElements.whenDefined(cmpMeta.$tagName$).then(() => (hostRef.$flags$ |= 128 /* isWatchReady */));

to

customElements.whenDefined(elm.localName).then(() => (hostRef.$flags$ |= 128 /* isWatchReady */));

makes it work, but maybe it breaks things elsewhere

I hope this could help!

@tanner-reits tanner-reits self-assigned this Aug 25, 2022
@tanner-reits
Copy link
Member

Hi @urielblanco 👋

Thanks for reporting this issue and the additional context to problematic code! I was able to verify the problem in your reproduction repo. I'll go ahead and get this labeled appropriately so we can get this refined and into our backlog. Thanks again!

@tanner-reits tanner-reits added Resolution: Refine This PR is marked for Jira refinement. We're not working on it - we're talking it through. and removed triage labels Aug 25, 2022
@tanner-reits tanner-reits removed their assignment Aug 25, 2022
@rwaskiewicz rwaskiewicz added Bug: Validated This PR or Issue is verified to be a bug within Stencil and removed Resolution: Refine This PR is marked for Jira refinement. We're not working on it - we're talking it through. labels Aug 25, 2022
@christian-bromann
Copy link
Member

@urielblanco thanks for providing the solution to this bug. I raised a PR in #5767 to bring this into Stencil.

@tanner-reits
Copy link
Member

A fix for this was released as a part of Stencil's v4.18.2 release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug: Validated This PR or Issue is verified to be a bug within Stencil
Projects
None yet
Development

No branches or pull requests

4 participants