Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core/modal): fixes runtime error #902

Merged
merged 16 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/angular/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,8 @@ export class IxModalHeader {

export declare interface IxModalHeader extends Components.IxModalHeader {
/**
* Close icon is clicked
* Emits when close icon is clicked and closes the modal
Can be prevented, in which case only the event is triggered, and the modal remains open
*/
closeClick: EventEmitter<CustomEvent<MouseEvent>>;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/component-doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8133,7 +8133,7 @@
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": "Close icon is clicked",
"docs": "Emits when close icon is clicked and closes the modal\nCan be prevented, in which case only the event is triggered, and the modal remains open",
"docsTags": []
}
],
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4622,7 +4622,7 @@ declare namespace LocalJSX {
*/
"iconColor"?: string;
/**
* Close icon is clicked
* Emits when close icon is clicked and closes the modal Can be prevented, in which case only the event is triggered, and the modal remains open
*/
"onCloseClick"?: (event: IxModalHeaderCustomEvent<MouseEvent>) => void;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
position: relative;
overflow: auto;
padding: 0.125rem 0.625rem 0.125rem 0.625rem;
max-content: 60vh;
}
10 changes: 8 additions & 2 deletions packages/core/src/components/modal-header/modal-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Prop,
Watch,
} from '@stencil/core';
import { closestPassShadow } from '../utils/shadow-dom';

/**
* @since 2.0.0
Expand All @@ -38,6 +39,7 @@ export class ModalHeader {
* Icon of the Header
*/
@Prop() icon: string;

@Watch('icon')
onIconChange(icon: string) {
if (this.parentDialog) {
Expand All @@ -55,14 +57,18 @@ export class ModalHeader {
@Prop() iconColor: string;

/**
* Close icon is clicked
* Emits when close icon is clicked and closes the modal
* Can be prevented, in which case only the event is triggered, and the modal remains open
*/
@Event() closeClick: EventEmitter<MouseEvent>;

private parentDialog: HTMLIxModalElement;

componentDidLoad() {
this.parentDialog = this.hostElement.closest('ix-modal');
this.parentDialog = closestPassShadow(
this.hostElement,
'ix-modal'
) as HTMLIxModalElement;
this.onIconChange(this.icon);
}

Expand Down
25 changes: 24 additions & 1 deletion packages/core/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ export class Modal {
return this.hostElement.shadowRoot.querySelector('dialog');
}

waitForDialog(): Promise<HTMLDialogElement> {
return new Promise((resolve) => {
const startTime = Date.now();

const checkDialog = () => {
const dialog = this.hostElement.shadowRoot.querySelector('dialog');

if (dialog) {
resolve(dialog);
} else {
if (Date.now() - startTime < 3000) {
setTimeout(checkDialog, 100);
} else {
console.error('Modal not found');
resolve(null);
}
}
};

checkDialog();
});
}

private slideInModal() {
const duration = this.animation ? Animation.mediumTime : 0;

Expand Down Expand Up @@ -142,7 +165,7 @@ export class Modal {
*/
@Method()
async showModal() {
setTimeout(() => this.dialog.showModal());
setTimeout(async () => (await this.waitForDialog()).showModal());
}

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/components/utils/shadow-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ export function containsElement(target: Element, element: Element) {

return target.contains(element);
}

export function closestPassShadow(node: Node, selector: string) {
if (!node) {
return null;
}

if (node instanceof ShadowRoot) {
return closestPassShadow(node.host, selector);
}

if (node instanceof HTMLElement) {
if (node.matches(selector)) {
return node;
} else {
return closestPassShadow(node.parentNode, selector);
}
}

return closestPassShadow(node.parentNode, selector);
}
2 changes: 1 addition & 1 deletion packages/react-test-app/src/preview-examples/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function CustomModal() {

return (
<Modal ref={modalRef}>
<IxModalHeader onCloseClick={() => dismiss()}>
<IxModalHeader>
Message headline
</IxModalHeader>
<IxModalContent>Message text lorem ipsum</IxModalContent>
Expand Down