summaryrefslogtreecommitdiffstats
path: root/toolkit/content/widgets/moz-toggle/moz-toggle.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /toolkit/content/widgets/moz-toggle/moz-toggle.mjs
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/content/widgets/moz-toggle/moz-toggle.mjs')
-rw-r--r--toolkit/content/widgets/moz-toggle/moz-toggle.mjs89
1 files changed, 89 insertions, 0 deletions
diff --git a/toolkit/content/widgets/moz-toggle/moz-toggle.mjs b/toolkit/content/widgets/moz-toggle/moz-toggle.mjs
new file mode 100644
index 0000000000..5aee2a7d42
--- /dev/null
+++ b/toolkit/content/widgets/moz-toggle/moz-toggle.mjs
@@ -0,0 +1,89 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at htp://mozilla.org/MPL/2.0/. */
+
+import { html, ifDefined } from "../vendor/lit.all.mjs";
+import { MozLitElement } from "../lit-utils.mjs";
+
+export default class MozToggle extends MozLitElement {
+ static properties = {
+ pressed: { type: Boolean, reflect: true },
+ disabled: { type: Boolean, reflect: true },
+ label: { type: String },
+ description: { type: String },
+ ariaLabel: { type: String, attribute: "aria-label" },
+ };
+
+ static get queries() {
+ return {
+ buttonEl: "#moz-toggle-button",
+ labelEl: "#moz-toggle-label",
+ descriptionEl: "#moz-toggle-description",
+ };
+ }
+
+ // Use a relative URL in storybook to get faster reloads on style changes.
+ static stylesheetUrl = window.IS_STORYBOOK
+ ? "./moz-toggle/moz-toggle.css"
+ : "chrome://global/content/elements/moz-toggle.css";
+
+ constructor() {
+ super();
+ this.pressed = false;
+ this.disabled = false;
+ }
+
+ handleClick() {
+ this.pressed = !this.pressed;
+ this.dispatchOnUpdateComplete(
+ new CustomEvent("toggle", {
+ bubbles: true,
+ composed: true,
+ })
+ );
+ }
+
+ labelTemplate() {
+ if (this.label) {
+ return html`
+ <label id="moz-toggle-label" part="label" for="moz-toggle-button">
+ ${this.label}
+ </label>
+ `;
+ }
+ return "";
+ }
+
+ descriptionTemplate() {
+ if (this.description) {
+ return html`
+ <p id="moz-toggle-description" part="description">
+ ${this.description}
+ </p>
+ `;
+ }
+ return "";
+ }
+
+ render() {
+ const { pressed, disabled, description, ariaLabel, handleClick } = this;
+ return html`
+ <link rel="stylesheet" href=${this.constructor.stylesheetUrl} />
+ ${this.labelTemplate()} ${this.descriptionTemplate()}
+ <button
+ id="moz-toggle-button"
+ part="button"
+ type="button"
+ class="toggle-button"
+ ?disabled=${disabled}
+ aria-pressed=${pressed}
+ aria-label=${ifDefined(ariaLabel ?? undefined)}
+ aria-describedby=${ifDefined(
+ description ? "moz-toggle-description" : undefined
+ )}
+ @click=${handleClick}
+ ></button>
+ `;
+ }
+}
+customElements.define("moz-toggle", MozToggle);