76 lines
2.2 KiB
JavaScript
76 lines
2.2 KiB
JavaScript
/* 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 { MozBaseInputElement } from "../lit-utils.mjs";
|
|
// eslint-disable-next-line import/no-unassigned-import
|
|
import "chrome://global/content/elements/moz-label.mjs";
|
|
|
|
/**
|
|
* A simple toggle element that can be used to switch between two states.
|
|
*
|
|
* @tagname moz-toggle
|
|
* @property {boolean} pressed - Whether or not the element is pressed.
|
|
* @property {boolean} disabled - Whether or not the element is disabled.
|
|
* @property {string} label - The label text.
|
|
* @property {string} description - The description text.
|
|
* @property {string} ariaLabel
|
|
* The aria-label text for cases where there is no visible label.
|
|
* @slot support-link - Used to append a moz-support-link to the description.
|
|
* @fires toggle
|
|
* Custom event indicating that the toggle's pressed state has changed.
|
|
*/
|
|
export default class MozToggle extends MozBaseInputElement {
|
|
static properties = {
|
|
ariaLabel: { type: String, mapped: true },
|
|
pressed: { type: Boolean, reflect: true },
|
|
};
|
|
|
|
static activatedProperty = "pressed";
|
|
|
|
get buttonEl() {
|
|
return this.inputEl;
|
|
}
|
|
|
|
constructor() {
|
|
super();
|
|
this.pressed = false;
|
|
}
|
|
|
|
handleClick() {
|
|
this.pressed = !this.pressed;
|
|
this.dispatchOnUpdateComplete(
|
|
new CustomEvent("toggle", {
|
|
bubbles: true,
|
|
composed: true,
|
|
})
|
|
);
|
|
}
|
|
|
|
inputTemplate() {
|
|
const { pressed, disabled, ariaLabel, handleClick } = this;
|
|
return html`<button
|
|
id="input"
|
|
part="button"
|
|
type="button"
|
|
class="toggle-button"
|
|
name=${this.name}
|
|
value=${this.value}
|
|
?disabled=${disabled}
|
|
aria-pressed=${pressed}
|
|
aria-label=${ifDefined(ariaLabel ?? undefined)}
|
|
aria-describedby="description"
|
|
accesskey=${ifDefined(this.accessKey)}
|
|
@click=${handleClick}
|
|
></button>`;
|
|
}
|
|
|
|
inputStylesTemplate() {
|
|
return html`<link
|
|
rel="stylesheet"
|
|
href="chrome://global/content/elements/moz-toggle.css"
|
|
/>`;
|
|
}
|
|
}
|
|
customElements.define("moz-toggle", MozToggle);
|