Event communication between web components - Lit University (Advanced)

Поделиться
HTML-код
  • Опубликовано: 1 авг 2024
  • Follow along as Lit Software Engineer Elliott Marquez shares the pros, cons, and use cases of communicating with events. Learn how a web component can communicate in any type of environment with the Mediator Method and more in this advanced Lit University episode!
    Resources
    Lit documentation site → goo.gle/Lit-devsite
    Learn more about events → goo.gle/3xvNIXE
    Extended reading
    WCCG Context API → goo.gle/3ry2nO0
    Shadow DOM Event Visualization → goo.gle/3KWnotp
    Code samples
    Trom-bone → goo.gle/3vLZN8o
    Gist → goo.gle/3koeE3K
    Listening to events playground link→ goo.gle/3KyWd74
    Dispatching events/Mediator Method playground link→ goo.gle/3vShLGC
    State Management and Lit
    MobX → goo.gle/3jNhoaL
    Apollo GraphQL → goo.gle/3McDIGL
    Redux → goo.gle/3MWKv87
    Chapters
    0:00 - Introduction
    1:02 - Why events?
    2:33 - Parent child
    4:30 - Siblings and the Mediator Method
    7:45 - Pros and cons
    9:12 - Wrap up
    Have any lingering questions? Tweet at us with the hashtag AskLitDev or join the Lit & Friends Discord!
    Join the community → goo.gle/Lit-Community
    Twitter → goo.gle/Lit-Twitter
    Discord → goo.gle/Lit-Discord
    Watch more Lit University → goo.gle/LitUniversity
    Subscribe to never miss a video on Lit → goo.gle/Lit
    #Lit #WebComponents #WebDevelopment
  • РазвлеченияРазвлечения

Комментарии • 30

  • @buildWithLit
    @buildWithLit  2 года назад +5

    Subscribe to never miss a video on Lit! → goo.gle/Lit

  • @christophegeiser2426
    @christophegeiser2426 2 года назад +9

    Thanks - the trombone example and code is brilliant !

    • @ElliottMarquez
      @ElliottMarquez 2 года назад

      Thanks! I had a lot of fun making it as well as the mini-synth!

  • @anarki1234567
    @anarki1234567 4 месяца назад

    A really cool set of videos. They are easy to grasp despite they touch deep fundamentals. Awesome.

  • @Sage125
    @Sage125 2 года назад +6

    Man this is great, love Lit! Hope you guys keep on growing the channel with video's like these!

    • @ElliottMarquez
      @ElliottMarquez 2 года назад +2

      Thanks! Check in in a few weeks for the next Build It With Lit video!

  • @offroaders123
    @offroaders123 2 года назад +2

    Using events to communicate with parent and children elements is genius! Thanks for mentioning it, I forgot how useful that really could be.

    • @ElliottMarquez
      @ElliottMarquez 2 года назад +2

      Awesome! In the world of web components we like to return to the roots of what the browser has to offer. I think it's easy to forget about new browser features because you're waiting for cross-browser support.
      Like dis you know CSS :is() is very well supported across browsers now?

    • @offroaders123
      @offroaders123 2 года назад

      @@ElliottMarquez Exactly! I really like to take advantage of everything that the modern web supports today. I think a combination of ES Modules in the browser and Web Components can make for a powerful yet simple workflow without the use of a bundler.
      *Edit: And I have heard of :is(), it's great! I've used :where() a few times too, that one is great to hide some complex selectors for default Web Component styles that you want to make overridable by the component user :)

  • @nenameijer2282
    @nenameijer2282 Год назад

    Looking dashing Elliott! Thanks for the vid😁

  • @peachesfruitella
    @peachesfruitella 3 месяца назад +1

    Working with MFE's and finding that sibling to sibling relationships can be needed - but being on different parts of the HTML page, means that they cannot all be under the same parent, and so the mediator pattern although agreeable, doesn't seem to fit that scenario. A good example is a shopping cart update (denoting number of items in basket) also existing in two very different places in the DOM. The only way I can see this working i then falling back to SPA style ... which I thought we were breaking away from.
    Would love any thoughts on this ....

  • @tongrui316
    @tongrui316 2 года назад +3

    I like the trombone demo, waiting for the state management

    • @ElliottMarquez
      @ElliottMarquez 2 года назад

      some things on state management are coming 👀

  • @punkkabestia
    @punkkabestia 2 года назад +3

    We're waiting the state manager full production grade implementation from Lit team video !!!! : )

    • @ElliottMarquez
      @ElliottMarquez 2 года назад

      We have some state manager videos in the pipeline!

  • @genzthegreat
    @genzthegreat Год назад +1

    I am looking for routing and other stuffs which JavaScript framework already give. Also how it differs from qwik framework?

  • @AlanChandler42
    @AlanChandler42 2 года назад +2

    I want to make three comments having been developing a relatively largish application single handedly since 2016 (initially with Polymer - now with Lit - no other frameworks involved). This is slowly replacing a large Microsoft Access application that I have been maintaining since 2009.
    Firstly - events only work when the elements are actually in the dom. In my application I actually page out (normally with the cache directive) all the large subtrees that are not active at the current time, and then further down the tree hierachy a second layer of subtrees. Nevertheless, although a database is the formal master datastore there is need for the cross tree updating of elements when they are paged out to avoid a re-read of the database when they are paged back in. In that case I developed a publish subscribe type model using callbacks, so that key data entities that have been updated in one subtree that can effect data for paged out subtrees elsewhere in the application.
    Secondly - although I use a lot of the mediator pattern - child to child communication can also take place by dispatching events and adding event listeners to document.body. In fact, I have developed a custom element that provides a subscribe service for such events, and that adds an event listener to document body and dispatches a local event upwards when when it receives the document.body event. This subscribe element is then a child of its parent who can then receive the event as though it was emitted locally.
    Thirdly - a design pattern I use a lot - mainly for dialog boxes that pop up to get information from the user, for sub sub elements which can be used all over the application, is to fire an event upwards with some data to a "dialog" element placed high in the hierarchy. It receives the info and remembers the sender. In fact it can often position itself close by the sender. Then when the information is gathered from the user it sends an event back to the element that dispatched it (by dispatching the event on the sender element), where it can be picked up via the @event declaration. Because this is a common pattern I developed a mixin to handle all the communication (I have a 'xxxx-request' and 'xxxx-reply' convention for the event names, with 'xxxx' being the value of a property I call eventBase that the mixin uses)
    Note I have a few mixins for common patterns, so for example I have a "page-manager" mixin that works in combination with a distributed routing module so that different urls (at different url hierarchy levels) control which page is switched in and out that I mention in the first comment above

    • @ElliottMarquez
      @ElliottMarquez 2 года назад

      Lots of really great info! Lots of agreement coming from me, I wish I had the time in the video to go deep into these topics!
      A cool pattern I've been toying around with was to communicate by passing around an instance to `new EventTarget()` now that it's supported in Safari 14. A bit more portable and private than document.body. problem is still getting a reference to it, but can be possible using a ReactiveController which Andrew will be covering in 2 videos from now

  • @cs_devel
    @cs_devel 2 года назад +3

    I would like to have a video explanation on the Lit lifecycle, because it's confusing where to put loading of remote data depending on changing of a certain property.
    Maybe someone can explain such a specialty.

    • @ElliottMarquez
      @ElliottMarquez 2 года назад

      interesting one! I might tackle that one soon! Though for now you might want to take a look at @lit-labs/task and give us feedback on our GitHub discussions and GitHub issues so you can shape how it looks coming out of Labs!

  • @notpinoy
    @notpinoy 10 месяцев назад

    What if my web components will be used by another application. One component is a filter. Another component is a label that will need to refresh when the filter changed. But the container application doesn't know how these web components should communicate. I just import the lit project js to my other ui application, add a filter component and a label component. How will the label component know when the filter has changed?

  • @alexgrinberg1888
    @alexgrinberg1888 2 года назад

    Would it be possible to cover extending the Lit Component in your next video lessons? Thanks!

    • @ElliottMarquez
      @ElliottMarquez 2 года назад

      That is exactly what we're tackling in the next Lit U video!

  • @christopherderrell8470
    @christopherderrell8470 2 года назад

    Thanks Elliot - 💪🏽. Quick question, why did you use new Event('eventname') instead of new CustomEvent('eventname',{...detail}). Is there a reason for using the one you did?

    • @ElliottMarquez
      @ElliottMarquez 2 года назад

      Simply because I had no need for a detail!
      Some people on our team also like extending Event for type safety rather than using CustomEvent

    • @christopherderrell8470
      @christopherderrell8470 2 года назад

      @@ElliottMarquez is there any benefit for extending Event?

    • @ElliottMarquez
      @ElliottMarquez 2 года назад

      @@christopherderrell8470 mostly type safety in TypeScript and adding methods to the class

    • @christopherderrell8470
      @christopherderrell8470 2 года назад +1

      @@ElliottMarquez Coolio. Thanks for answering

  • @shafesp
    @shafesp Год назад

    what is that intro music?
    Its COOOOLLLL....

  • @hugodsa89
    @hugodsa89 7 месяцев назад

    dude can any of these videos actually use meaningful examples... last time i used a trombone renderer was... never