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.

Allow dispatch of bubbling custom events when using `@angular/elements`

See original GitHub issue

šŸš€ feature request

Relevant Package

This feature request is for @angular/elements.

Description

When using @angular/elements, outputs are converted to CustomEvents: https://github.com/angular/angular/blob/eb9eebbb45352da6000560ee0988d9d59dce1254/packages/elements/src/create-custom-element.ts#L225-L228

There is no way of listening to the custom events from a grand parent element or component because these custom events don’t bubble as the bubbles option is set to false here:

https://github.com/angular/angular/blob/eb9eebbb45352da6000560ee0988d9d59dce1254/packages/elements/src/utils.ts#L55-L70

It would be nice to figure out a way of listening to custom events in grand parent elements.

Use Case

The app loads dynamic markdown content. This markdown is processed with a third party library like Marked where we add a special link handler that replaces links with our custom elements (using Angular Elements). These elements trigger custom events that we want to handle in the parent component (the one that loaded the markdown).

The general idea is that we can let a 3rd party (non-Angular) library control the DOM and inject our Angular Elements but we want to keep communicating with these elements without having to dispatch bubbling events manually or using an indirection like a provider or state management etc…

Solutions

Solution A

  1. Create a new CustomEventEmitter where we can configure bubbles and cancelable:
new CustomEventEmitter({bubbles: true, cancelable: false})

We can’t use EventEmitter because bubbles and cancelable will be simply ignored and confusing when not using Angular elements

  1. Change ComponentNgElementStrategy in order to handle CustomEventEmitter differently instead of merging all emitters: https://github.com/angular/angular/blob/eb9eebbb45352da6000560ee0988d9d59dce1254/packages/elements/src/component-factory-strategy.ts#L50

  2. Change createCustomElement in order to handle bubbles and cancelable: https://github.com/angular/angular/blob/eb9eebbb45352da6000560ee0988d9d59dce1254/packages/elements/src/create-custom-element.ts#L225-L228

  3. Change createCustomEvent to allow the control of bubbles and cancelable: https://github.com/angular/angular/blob/eb9eebbb45352da6000560ee0988d9d59dce1254/packages/elements/src/utils.ts#L55-L70

  4. [OPTIONAL] Should we deprecate EventEmitter usage with Angular elements? Allowing both can be confusing.

Solution B

Create a new CustomEventEmitter which is an EventEmitter of type EventEmitter<{bubbles?: boolean, cancelable?: boolean, detail?: T}> so every event can have a different bubbling strategy… but that would be a surprising behavior so I am not a big fan of this one.

Contribution

I’ll be more than happy to contribute with a PR if the solution is suitable.

Workaround

Trigger the event manually:

elementRef.nativeElement.dispatchEvent('myCustomEvent', {bubbles: true, detail: 'data'})

Somewhat related issues

https://github.com/angular/angular/issues/2296

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:22
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
csakiscommented, Jan 9, 2021

This issue was raised over 6 years ago. It seems that everybody agrees that this is a desirable feature to have and @yjaaidi’s solution is a good one. How long does it take to approve it by the Angular team?

0reactions
angular-robot[bot]commented, Jun 24, 2021

Thank you for submitting your feature request! Looks like during the polling process it didn’t collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular’s scope, we’d encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to bubble up angular2 custom event - Stack Overflow
Events from EventEmitter don't support bubbling. You can either use element.dispatchEvent() to fire a DOM event that bubbles, or use a shared service...
Read more >
Dispatching custom events - The Modern JavaScript Tutorial
Custom events can be used to create ā€œgraphical componentsā€. ... ...dispatch on elem! let event = new Event("hello", {bubbles: true});Ā ...
Read more >
Mechanics of Communication and Custom Events Binding in ...
The b event is a custom event. Custom events are created in Angular using its EventEmitter class. These events are used to communicate...
Read more >
DOM Events - The Angular Book
Let's update the main application component template with a top-most div element that is going to listen to the header-click event. That is...
Read more >
Angular custom event bubbling - David Votrubec
Custom events in Angular by default do not bubble up. It is because they are not really events, they are based on EventEmitter,Ā ......
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