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

Coach Mark: Emit event when user hits the close button #733

Merged
merged 4 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading