Skip to content

feat: node.eventListeners/getEventListeners(node) alternative to conditionally render internal elements, when an @Event has listeners. #5538

@finikwashere

Description

@finikwashere

Prerequisites

Describe the Feature Request

Add an ability to check whether the @event decorated property has any event listeners/subscribers.
There is currently no way to check if a callback function was assigned to this event prop.

Describe the Use Case

The original idea was to create a resuable component, containing a conditionally-rendered button as part of the component template outside of <slot>. The button needs to have an onClick action, but since it's inside the template, an @event decorator will be used on <Host> and we link onClick handler with action.emit().
But the conditional rendering has to be done with @Prop() hasAction:boolean
<SomeComponent hasAction={true} onAction={handler}/>

Describe Preferred Solution

A preferred solution would be to be able to check if the action has listeners/callback subscribed and based on that, render or hide the button in the template.

Angular offers this soluton using this.click.observers.length > 0

then the result with button would look like: <SomeComponent onAction={handler}/>
without button: <SomeComponent />

Describe Alternatives

An alternative could be a custom attribute added to the <Host> after rendering, e.g. reflect-event="action", which we could access via elementRef.
On the other hand it can be also done via children inside of , but then wiring between internal logic of and will be harder

Chromium Devtools offers node.eventListeners(); and getEventListeners(node), but of course it's not part of native DOM API.

Related Code

No response

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature: Want this? Upvote it!This PR or Issue may be a great consideration for a future idea.Resolution: RefineThis PR is marked for Jira refinement. We're not working on it - we're talking it through.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions