Skip to content

Commit

Permalink
Bug 1873534 - moz-card should emit a toggle event when expanding or c…
Browse files Browse the repository at this point in the history
…ollapsing. r=reusable-components-reviewers,mstriemer

Differential Revision: https://phabricator.services.mozilla.com/D210734
  • Loading branch information
Irene Ni committed May 22, 2024
1 parent d408e0a commit e7bb4d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
19 changes: 18 additions & 1 deletion toolkit/content/tests/widgets/test_moz_card.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<script>
let generatedSlotText = "generated slotted element";
let testHeading = "test heading";
const { BrowserTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/BrowserTestUtils.sys.mjs"
);

function assertBasicProperties(card, expectedValues) {
info(`Testing card with ID: ${card.id}`);
Expand Down Expand Up @@ -69,10 +72,24 @@

}

function assertAccordionCardProperties(card) {
async function assertAccordionCardProperties(card) {
ok(card.detailsEl, "The details element should exist");
ok(card.detailsEl.querySelector("summary"), "There should be a summary element within the details element");
ok(card.detailsEl.querySelector("summary").querySelector(".chevron-icon"), "There should be a chevron icon div within the summary element");

let cardToggled = BrowserTestUtils.waitForEvent(card, "toggle");
card.detailsEl.querySelector("summary").click();
let openEvent = await cardToggled;
is(openEvent.newState, "open", "Event shows new state is open");
is(openEvent.oldState, "closed", "Event shows old state is closed");
ok(card.detailsEl.open, "When the accordion is closed, it should expand when clicked");

cardToggled = BrowserTestUtils.waitForEvent(card, "toggle");
card.detailsEl.querySelector("summary").click();
let closeEvent = await cardToggled;
is(closeEvent.newState, "closed", "Event shows new state is closed");
is(closeEvent.oldState, "open", "Event shows old state is open");
ok(!card.detailsEl.open, "When the accordion is open, it should collapse when clicked");
}

function assertHeadingIconCardProperties(card) {
Expand Down
11 changes: 10 additions & 1 deletion toolkit/content/widgets/moz-card/moz-card.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class MozCard extends MozLitElement {
cardTemplate() {
if (this.type === "accordion") {
return html`
<details id="moz-card-details">
<details id="moz-card-details" @toggle="${this.onToggle}">
<summary>${this.headingTemplate()}</summary>
<div id="content"><slot></slot></div>
</details>
Expand Down Expand Up @@ -106,6 +106,15 @@ export default class MozCard extends MozLitElement {
this.detailsEl.open = force ?? !this.detailsEl.open;
}

onToggle() {
this.dispatchEvent(
new ToggleEvent("toggle", {
newState: this.detailsEl.open ? "open" : "closed",
oldState: this.detailsEl.open ? "closed" : "open",
})
);
}

render() {
return html`
<link
Expand Down

0 comments on commit e7bb4d9

Please sign in to comment.