@shoelace-style/shoelace
Version:
A forward-thinking library of web components.
146 lines (143 loc) • 4.16 kB
JavaScript
import {
option_styles_default
} from "./chunk.FXXKMG2P.js";
import {
LocalizeController
} from "./chunk.WLV3FVBR.js";
import {
SlIcon
} from "./chunk.E6QAPUBK.js";
import {
watch
} from "./chunk.CCJUT23E.js";
import {
component_styles_default
} from "./chunk.TUVJKY7S.js";
import {
ShoelaceElement
} from "./chunk.UYAO2JRR.js";
import {
__decorateClass
} from "./chunk.B3BW2AY6.js";
// src/components/option/option.component.ts
import { classMap } from "lit/directives/class-map.js";
import { html } from "lit";
import { property, query, state } from "lit/decorators.js";
var SlOption = class extends ShoelaceElement {
constructor() {
super(...arguments);
// @ts-expect-error - Controller is currently unused
this.localize = new LocalizeController(this);
this.current = false;
this.selected = false;
this.hasHover = false;
this.value = "";
this.disabled = false;
}
connectedCallback() {
super.connectedCallback();
this.setAttribute("role", "option");
this.setAttribute("aria-selected", "false");
}
handleDefaultSlotChange() {
const textLabel = this.getTextLabel();
if (typeof this.cachedTextLabel === "undefined") {
this.cachedTextLabel = textLabel;
return;
}
if (textLabel !== this.cachedTextLabel) {
this.cachedTextLabel = textLabel;
this.emit("slotchange", { bubbles: true, composed: false, cancelable: false });
}
}
handleMouseEnter() {
this.hasHover = true;
}
handleMouseLeave() {
this.hasHover = false;
}
handleDisabledChange() {
this.setAttribute("aria-disabled", this.disabled ? "true" : "false");
}
handleSelectedChange() {
this.setAttribute("aria-selected", this.selected ? "true" : "false");
}
handleValueChange() {
if (typeof this.value !== "string") {
this.value = String(this.value);
}
if (this.value.includes(" ")) {
console.error(`Option values cannot include a space. All spaces have been replaced with underscores.`, this);
this.value = this.value.replace(/ /g, "_");
}
}
/** Returns a plain text label based on the option's content. */
getTextLabel() {
const nodes = this.childNodes;
let label = "";
[...nodes].forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
if (!node.hasAttribute("slot")) {
label += node.textContent;
}
}
if (node.nodeType === Node.TEXT_NODE) {
label += node.textContent;
}
});
return label.trim();
}
render() {
return html`
<div
part="base"
class=${classMap({
option: true,
"option--current": this.current,
"option--disabled": this.disabled,
"option--selected": this.selected,
"option--hover": this.hasHover
})}
=${this.handleMouseEnter}
=${this.handleMouseLeave}
>
<sl-icon part="checked-icon" class="option__check" name="check" library="system" aria-hidden="true"></sl-icon>
<slot part="prefix" name="prefix" class="option__prefix"></slot>
<slot part="label" class="option__label" =${this.handleDefaultSlotChange}></slot>
<slot part="suffix" name="suffix" class="option__suffix"></slot>
</div>
`;
}
};
SlOption.styles = [component_styles_default, option_styles_default];
SlOption.dependencies = { "sl-icon": SlIcon };
__decorateClass([
query(".option__label")
], SlOption.prototype, "defaultSlot", 2);
__decorateClass([
state()
], SlOption.prototype, "current", 2);
__decorateClass([
state()
], SlOption.prototype, "selected", 2);
__decorateClass([
state()
], SlOption.prototype, "hasHover", 2);
__decorateClass([
property({ reflect: true })
], SlOption.prototype, "value", 2);
__decorateClass([
property({ type: Boolean, reflect: true })
], SlOption.prototype, "disabled", 2);
__decorateClass([
watch("disabled")
], SlOption.prototype, "handleDisabledChange", 1);
__decorateClass([
watch("selected")
], SlOption.prototype, "handleSelectedChange", 1);
__decorateClass([
watch("value")
], SlOption.prototype, "handleValueChange", 1);
export {
SlOption
};