ing-bank / lion

Fundamental white label web component features for your design system.
https://lion-web.netlify.app/
MIT License
1.75k stars 293 forks source link

ing-dialog doesnt close when you dispatch close event from a callback #2306

Closed Hazzle closed 3 months ago

Hazzle commented 3 months ago

Expected behavior

Closing of overlay after dispatching event from a callback method.

Actual Behavior

Close overlay event isnt picked up when dispatched from a callback method.

Additional context

ing-web version: 4.0.0 components used: ing-dialog, ing-dialog-frame, ing-button

from dialog: <ing-button id="cancelBtn" grey @click="${(e: Event) => this.closeModal(e)}">Cancel</ing-button>

works: private closeModal(e: Event) { e.target?.dispatchEvent(new Event('close-overlay', { bubbles: true })); }

doesnt work: private closeModal(e: Event) { setTimeout(() => e.target?.dispatchEvent(new Event('close-overlay', { bubbles: true }))); }

I added a timeout for demonstrative purposes, the same issue occurs from a promise (actual usecase).

LarsDenBakker commented 3 months ago

@Hazzle e.target has a different value when you access it directly in the event handler, or when it is accessed later. This is because the event continues to traverse up the tree. So by the team you dispatch your event, the target will be another element up the DOM tree.

You can store the target:

private closeModal(e: Event) { 
  const target = e.target;
  setTimeout(() => target?.dispatchEvent(new Event('close-overlay', { bubbles: true }))); 
}
Hazzle commented 3 months ago

Thanks, it works.