Skip to content

Commit

Permalink
fix(modal): closing is prevented with legacy event (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arturo Castillo Delgado authored Nov 9, 2021
1 parent 15288ee commit e712c3a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/components/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Modal {
/** (optional) Modal size */
@Prop() size?: string = 'default';
/** (optional) If `true`, the Modal is open. */
@Prop({ reflect: true }) opened?: boolean = false;
@Prop({ reflect: true, mutable: true }) opened?: boolean = false;
/** (optional) Transition duration */
@Prop() duration?: number = 200;
/** (optional) Label for close button */
Expand Down Expand Up @@ -129,7 +129,9 @@ export class Modal {
}

emitBeforeClose(trigger: CloseEventTrigger) {
if (!this.scaleBeforeClose.emit({ trigger }).defaultPrevented) {
const emittedEvents = emitEvent(this, 'scaleBeforeClose', { trigger });
const prevented = emittedEvents.some((event) => event.defaultPrevented);
if (!prevented) {
this.opened = false;
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/html/modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ <h3>Modal with header</h3>
modal.addEventListener('scaleClose', () => {
modal.removeAttribute('opened');
});

modal.addEventListener('scaleBeforeClose', (event) => {
// event.preventDefault()
});
</script>
</body>
</html>
12 changes: 9 additions & 3 deletions packages/components/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,23 @@ export const isPseudoClassSupported = (pseudoClass) => {
* @param instance {ComponentInterface} - The component instance, aka `this`
* @param eventKey {string} - The event property, e.g. `scaleChange`
* @param detail {any} - The custom event `detail`
* @returns {CustomEvent[]} - The events emitted
*/
export function emitEvent(
instance: ComponentInterface,
eventKey: string,
detail?: any
) {
): CustomEvent[] {
const legacyKey = eventKey + 'Legacy';
const emitted = [];
if (typeof instance[legacyKey] !== 'undefined') {
instance[legacyKey].emit(detail);
// Emit legacy camel case event, e.g. `scaleClose`
emitted.push(instance[legacyKey].emit(detail));
}
instance[eventKey].emit(detail);
// Emit now-standard kebab-case event, e.g. `scale-close`
emitted.push(instance[eventKey].emit(detail));
// Return both
return emitted;
}

export function isClickOutside(event: MouseEvent, host: HTMLElement) {
Expand Down

0 comments on commit e712c3a

Please sign in to comment.