Skip to content

Commit

Permalink
fix(modal): fix modal events
Browse files Browse the repository at this point in the history
fixes: #70
  • Loading branch information
Sukaato committed Dec 14, 2024
1 parent 5c4cebb commit 95fe769
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ pop-modal,prop,showBackdrop,boolean,undefined,false,true
pop-modal,prop,trigger,string,undefined,false,false
pop-modal,method,dismiss,dismiss(data: any) => Promise<boolean>
pop-modal,method,present,present() => Promise<boolean>
pop-modal,event,didDismiss,void,true
pop-modal,event,didPresent,void,true
pop-modal,event,dismiss,void,true
pop-modal,event,present,void,true
pop-modal,part,actions
pop-modal,part,backdrop
pop-modal,part,content
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1742,8 +1742,8 @@ declare global {
new (): HTMLPopMaskElement;
};
interface HTMLPopModalElementEventMap {
"didPresent": void;
"didDismiss": void;
"present": void;
"dismiss": void;
}
/**
* Modal is used to show a dialog or a box when you click on the trigger element.
Expand Down Expand Up @@ -2744,11 +2744,11 @@ declare namespace LocalJSX {
/**
* Emitted after the modal has dismissed.
*/
"onDidDismiss"?: (event: PopModalCustomEvent<void>) => void;
"onDismiss"?: (event: PopModalCustomEvent<void>) => void;
/**
* Emitted after the modal has presented.
*/
"onDidPresent"?: (event: PopModalCustomEvent<void>) => void;
"onPresent"?: (event: PopModalCustomEvent<void>) => void;
/**
* If `true`, the modal will open. If `false`, the modal will close. Use this if you need finer grained control over presentation, otherwise just use the modalController or the `trigger` property. Note: `open` will automatically be set back to `false` when the modal dismisses.
* @config
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/components/avatar/avatar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
--color: #{theme.use_color("neutral.content")};

display: grid;
place-content: center;

position: relative;
aspect-ratio: 1;
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/components/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ export class Modal implements ComponentInterface, OverlayInterface {
if (isOpen) {
this.present();
} else {
this.dismiss(null);
this.dismiss('close');
}
}

/**
* Emitted after the modal has presented.
*/
@Event() didPresent: EventEmitter<void>;
@Event({ eventName: 'present' }) didPresent: EventEmitter<void>;

/**
* Emitted after the modal has dismissed.
*/
@Event() didDismiss: EventEmitter<void>;
@Event({ eventName: 'dismiss' }) didDismiss: EventEmitter<void>;

connectedCallback(): void {
const { trigger } = this;
Expand Down Expand Up @@ -162,6 +162,7 @@ export class Modal implements ComponentInterface, OverlayInterface {
}

this.dialog.showModal();
this.didPresent.emit();
return true;
}

Expand All @@ -177,6 +178,7 @@ export class Modal implements ComponentInterface, OverlayInterface {
if (!open) return false;

this.dialog.close(data);
this.didDismiss.emit();
return true;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/components/modal/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ Modal is used to show a dialog or a box when you click on the trigger element.

## Events

| Event | Description | Type |
| ------------ | -------------------------------------- | ------------------- |
| `didDismiss` | Emitted after the modal has dismissed. | `CustomEvent<void>` |
| `didPresent` | Emitted after the modal has presented. | `CustomEvent<void>` |
| Event | Description | Type |
| --------- | -------------------------------------- | ------------------- |
| `dismiss` | Emitted after the modal has dismissed. | `CustomEvent<void>` |
| `present` | Emitted after the modal has presented. | `CustomEvent<void>` |


## Methods
Expand Down
100 changes: 100 additions & 0 deletions packages/core/src/components/modal/tests/basic/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dropdown | Poppy-ui</title>
<link rel="stylesheet" href="/dist/poppy/poppy.css">
<script type="module" src="/dist/poppy/poppy.esm.js"></script>
<script nomodule src="/dist/poppy/poppy.js"></script>
<style>
main {
width: 100vw;
height: 100dvh;
display: flex;
flex-direction: column;
gap: 1rem;
padding: 1rem;

background-color: var(--base-300);
}

section {
display: flex;
flex-direction: column;
justify-content: center;
gap: .35rem;
}

div {
display: flex;
justify-content: center;
gap: .5rem;
}
</style>
</head>

<body>
<main>
<section>
<h2>Dropdown - basic</h2>
<div>
<pop-button id="default-btn">default</pop-button>
<pop-modal id="default" trigger="default-btn">
basic
<pop-button slot="actions" class="close">close</pop-button>
</pop-modal>
</div>
</section>
<section>
<h2>Dropdown - Show backdrop</h2>
<div>
<pop-button id="show-backdrop-btn">show-backdrop</pop-button>
<pop-modal id="show-backdrop" trigger="show-backdrop-btn" show-backdrop>
show backdrop
<pop-button slot="actions" class="close">close</pop-button>
</pop-modal>
</div>
</section>
<section>
<h2>Dropdown - Backdrop</h2>
<div>
<pop-button id="backdrop-dismiss-btn">backdrop-dismiss</pop-button>
<pop-modal id="backdrop-dismiss" trigger="backdrop-dismiss-btn" backdrop-dismiss>
backdrop dismiss
<pop-button slot="actions" class="close">close</pop-button>
</pop-modal>
</div>
</section>
<section>
<h2>Dropdown - Open</h2>
<div>
<pop-button id="open-btn">open</pop-button>
<pop-modal id="open" trigger="open-btn" open>
Open by default
<pop-button slot="actions" class="close">close</pop-button>
</pop-modal>
</div>
</section>
</main>

<script>
document.querySelectorAll('.close').forEach(btn => {
btn.addEventListener('click', () => {
btn.parentElement.dismiss();
});
});

document.querySelectorAll('pop-modal').forEach(modal => {
modal.addEventListener('present', () => {
console.log('present', modal.id)
});
modal.addEventListener('dismiss', () => {
console.log('dismiss', modal.id)
});
});
</script>
</body>

</html>
8 changes: 4 additions & 4 deletions packages/vue/src/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ export const PopModal = /*@__PURE__*/ globalThis.window ? defineContainer<JSX.Po
'showBackdrop',
'backdropDismiss',
'open',
'didPresent',
'didDismiss'
'present',
'dismiss'
]) : defineStencilSSRComponent({
tagName: 'pop-modal',
hydrateModule: import('@poppy-ui/core/hydrate'),
Expand All @@ -505,8 +505,8 @@ export const PopModal = /*@__PURE__*/ globalThis.window ? defineContainer<JSX.Po
'showBackdrop': [Boolean, "show-backdrop"],
'backdropDismiss': [Boolean, "backdrop-dismiss"],
'open': [Boolean, "open"],
'onDidPresent': [Function],
'onDidDismiss': [Function]
'onPresent': [Function],
'onDismiss': [Function]
}
});

Expand Down

0 comments on commit 95fe769

Please sign in to comment.