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

cutom events #27

Open
echb opened this issue May 11, 2022 · 4 comments
Open

cutom events #27

echb opened this issue May 11, 2022 · 4 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@echb
Copy link

echb commented May 11, 2022

i've tried use custom events and i failed forthright, someone has used lego with custom events?

@vinyll
Copy link
Member

vinyll commented May 14, 2022

Hey @echb!
Because Lego is native HTML/JS, I'm pretty sure CustomEvent should work out of the box.
Do you have a piece of code to share your context?

@vinyll
Copy link
Member

vinyll commented May 15, 2022

The 3rd block at https://polight.github.io/lego-demo/ will show you a demo of a webcomponent updated from a CustomEvent, that's the most simplistic version.
The source of that component is here: https://github.com/Polight/lego-demo/blob/main/bricks/customevent-clock.html

@vinyll vinyll added the question Further information is requested label May 15, 2022
@echb
Copy link
Author

echb commented May 29, 2022

The component code

<template>
  <div class="container" @click="test">
    <div class="container">
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Aspernatur ut quos error totam reprehenderit fuga
        aperiam, explicabo quisquam, architecto at cumque assumenda illum corrupti natus facilis quasi, doloribus
        laborum
        odio.</p>
    </div>
</template>

<script>
  export default class extends Lego {

    connected() {
    }

    init() {
      this.state = {
      }

    }
    test() {
      const MessageEvent = new CustomEvent("message", {
        detail: { from: "Manz", message: "Hello!" },
        bubbles: true,
        composed: true
      });
      this.dispatchEvent(MessageEvent);
    }
  }
</script>

<style>
  .container {
    display: inline-flex;
    background-color: #9f1717;
    color: antiquewhite;
    padding: 2px 5px 2px 5px;
    border-radius: 10px;
  }

  ::slotted(*) {
    display: inline-block;
    /* background-color: green; */
    border: none;
    margin: 0;
    padding: 0;
  }
</style>

This is script calling outside component

  const prop = document.querySelector('lego-prop')
  prop.addEventListener('message', (e) => {
    console.log(e);
  })

The problem comes whe nest components

<template>
  <div class="container">
    <p>component with nested one</p>
    <div class="container">
      <lego-prop @message="test"></lego-prop>
    </div>
</template>

<script>
  export default class extends Lego {

    connected() {
      console.log('asd');
    }

    init() {
      this.state = {
      }

    }
    test(e) {
      console.log(e);
    }
  }
</script>

@vinyll
Copy link
Member

vinyll commented May 31, 2022

Hey @echb!
I'll set this up in the demo eventually and we can have a look together to get this running as expected.
We could ensure it works as basic HTML elements with the custom event as you are using it as a POC and then investigate what the issue is in this specific case.
I should find some time to get into it this week.

I'm pretty confident we should work this out :)

@vinyll vinyll added help wanted Extra attention is needed good first issue Good for newcomers and removed question Further information is requested labels May 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants