Skip to content

Commit

Permalink
Coach Mark: Emit event when user hits the close button (#733)
Browse files Browse the repository at this point in the history
* feat(coach-mark): emit event when user hits the close button

* feat(coach-mark): add changeset

* feat(coach-mark): add event to documentation
  • Loading branch information
michael-iden committed Mar 21, 2024
1 parent 169152f commit 20a8596
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-cows-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ithaka/pharos": minor
---

Emit `pharos-coach-mark-closed` event when coach mark is dismissed
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ describe('pharos-coach-mark', () => {
expect(component.hide).to.be.true;
});

it('closes when the close button is clicked', async () => {
it('closes when the close button is clicked and emits closed event', async () => {
let wasFired = false;
const handleClose = (): void => {
wasFired = true;
};
component.addEventListener('pharos-coach-mark-closed', handleClose);

component.hide = false;
await component.updateComplete;

Expand All @@ -56,6 +62,7 @@ describe('pharos-coach-mark', () => {
await component.updateComplete;

expect(component.hide).to.be.true;
expect(wasFired).to.be.true;
});

it('displays the header set in the element attribute', async () => {
Expand Down
14 changes: 13 additions & 1 deletion packages/pharos/src/components/coach-mark/pharos-coach-mark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type Variant = 'light' | 'dark';
*
* @tag pharos-coach-mark
*
* @fires pharos-coach-mark-closed - Fires when the coach mark is closed
*
*/
export class PharosCoachMark extends ScopedRegistryMixin(PharosElement) {
static elementDefinitions = {
Expand Down Expand Up @@ -92,6 +94,16 @@ export class PharosCoachMark extends ScopedRegistryMixin(PharosElement) {
autoUpdate(document.documentElement, this, () => this.requestUpdate());
}

private _hideCoachMark(): void {
this.hide = true;

const details = {
bubbles: true,
composed: true,
};
this.dispatchEvent(new CustomEvent('pharos-coach-mark-closed', details));
}

private setOffset() {
const id: string = this.getAttribute('id') || '';
const targetElement: Element | null = document.querySelector(`[data-coach-mark="${id}"]`);
Expand Down Expand Up @@ -131,7 +143,7 @@ export class PharosCoachMark extends ScopedRegistryMixin(PharosElement) {
variant="${this.variant === 'light' ? 'subtle' : 'overlay'}"
icon="close"
a11y-label="Close"
@click="${() => (this.hide = true)}"
@click="${this._hideCoachMark}"
></pharos-button>
<pharos-heading
id="coach-mark-heading"
Expand Down

0 comments on commit 20a8596

Please sign in to comment.