UNPKG

@shoelace-style/shoelace

Version:

A forward-thinking library of web components.

114 lines (110 loc) 3.14 kB
import { radio_styles_default } from "./chunk.FKMWLPHV.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/radio/radio.component.ts import { classMap } from "lit/directives/class-map.js"; import { html } from "lit"; import { property, state } from "lit/decorators.js"; var SlRadio = class extends ShoelaceElement { constructor() { super(); this.checked = false; this.hasFocus = false; this.size = "medium"; this.disabled = false; this.handleBlur = () => { this.hasFocus = false; this.emit("sl-blur"); }; this.handleClick = () => { if (!this.disabled) { this.checked = true; } }; this.handleFocus = () => { this.hasFocus = true; this.emit("sl-focus"); }; this.addEventListener("blur", this.handleBlur); this.addEventListener("click", this.handleClick); this.addEventListener("focus", this.handleFocus); } connectedCallback() { super.connectedCallback(); this.setInitialAttributes(); } setInitialAttributes() { this.setAttribute("role", "radio"); this.setAttribute("tabindex", "-1"); this.setAttribute("aria-disabled", this.disabled ? "true" : "false"); } handleCheckedChange() { this.setAttribute("aria-checked", this.checked ? "true" : "false"); this.setAttribute("tabindex", this.checked ? "0" : "-1"); } handleDisabledChange() { this.setAttribute("aria-disabled", this.disabled ? "true" : "false"); } render() { return html` <span part="base" class=${classMap({ radio: true, "radio--checked": this.checked, "radio--disabled": this.disabled, "radio--focused": this.hasFocus, "radio--small": this.size === "small", "radio--medium": this.size === "medium", "radio--large": this.size === "large" })} > <span part="${`control${this.checked ? " control--checked" : ""}`}" class="radio__control"> ${this.checked ? html` <sl-icon part="checked-icon" class="radio__checked-icon" library="system" name="radio"></sl-icon> ` : ""} </span> <slot part="label" class="radio__label"></slot> </span> `; } }; SlRadio.styles = [component_styles_default, radio_styles_default]; SlRadio.dependencies = { "sl-icon": SlIcon }; __decorateClass([ state() ], SlRadio.prototype, "checked", 2); __decorateClass([ state() ], SlRadio.prototype, "hasFocus", 2); __decorateClass([ property() ], SlRadio.prototype, "value", 2); __decorateClass([ property({ reflect: true }) ], SlRadio.prototype, "size", 2); __decorateClass([ property({ type: Boolean, reflect: true }) ], SlRadio.prototype, "disabled", 2); __decorateClass([ watch("checked") ], SlRadio.prototype, "handleCheckedChange", 1); __decorateClass([ watch("disabled", { waitUntilFirstUpdate: true }) ], SlRadio.prototype, "handleDisabledChange", 1); export { SlRadio };