UNPKG

@synergy-design-system/components

Version:

This package provides the base of the Synergy Design System as native web components. It uses [lit](https://www.lit.dev) and parts of [shoelace](https://shoelace.style/). Synergy officially supports the latest two versions of all major browsers (as define

259 lines (253 loc) 7.5 kB
import { switch_custom_styles_default } from "./chunk.T7PPSROA.js"; import { switch_styles_default } from "./chunk.4TB35TYG.js"; import { defaultValue } from "./chunk.3NXKLKWH.js"; import { form_control_custom_styles_default, form_control_styles_default } from "./chunk.G4URZQCL.js"; import { FormControlController } from "./chunk.HP2LEQRU.js"; import { HasSlotController } from "./chunk.WVVQK5TE.js"; import { enableDefaultSettings } from "./chunk.HYFCK7MM.js"; import { watch } from "./chunk.BVZQ6QSY.js"; import { component_styles_default } from "./chunk.NLYVOJGK.js"; import { SynergyElement } from "./chunk.3THJTCRO.js"; import { __decorateClass } from "./chunk.Z4XV3SMG.js"; // src/components/switch/switch.component.ts import { classMap } from "lit/directives/class-map.js"; import { html } from "lit"; import { ifDefined } from "lit/directives/if-defined.js"; import { live } from "lit/directives/live.js"; import { property, query, state } from "lit/decorators.js"; var SynSwitch = class extends SynergyElement { constructor() { super(...arguments); this.formControlController = new FormControlController(this, { value: (control) => control.checked ? control.value || "on" : void 0, defaultValue: (control) => control.defaultChecked, setValue: (control, checked) => control.checked = checked }); this.hasSlotController = new HasSlotController(this, "help-text"); this.hasFocus = false; this.title = ""; this.name = ""; this.size = "medium"; this.disabled = false; this.checked = false; this.defaultChecked = false; this.form = ""; this.required = false; this.helpText = ""; } /** Gets the validity state object */ get validity() { return this.input.validity; } /** Gets the validation message */ get validationMessage() { return this.input.validationMessage; } firstUpdated() { this.formControlController.updateValidity(); } handleBlur() { this.hasFocus = false; this.emit("syn-blur"); } handleInput() { this.emit("syn-input"); } handleInvalid(event) { this.formControlController.setValidity(false); this.formControlController.emitInvalidEvent(event); } handleClick() { this.checked = !this.checked; this.emit("syn-change"); } handleFocus() { this.hasFocus = true; this.emit("syn-focus"); } handleKeyDown(event) { if (event.key === "ArrowLeft") { event.preventDefault(); this.checked = false; this.emit("syn-change"); this.emit("syn-input"); } if (event.key === "ArrowRight") { event.preventDefault(); this.checked = true; this.emit("syn-change"); this.emit("syn-input"); } } handleCheckedChange() { this.input.checked = this.checked; this.formControlController.updateValidity(); } handleDisabledChange() { this.formControlController.setValidity(true); } /** Simulates a click on the switch. */ click() { this.input.click(); } /** Sets focus on the switch. */ focus(options) { this.input.focus(options); } /** Removes focus from the switch. */ blur() { this.input.blur(); } /** Checks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid. */ checkValidity() { return this.input.checkValidity(); } /** Gets the associated form, if one exists. */ getForm() { return this.formControlController.getForm(); } /** Checks for validity and shows the browser's validation message if the control is invalid. */ reportValidity() { return this.input.reportValidity(); } /** Sets a custom validation message. Pass an empty string to restore validity. */ setCustomValidity(message) { this.input.setCustomValidity(message); this.formControlController.updateValidity(); } render() { const hasHelpTextSlot = this.hasSlotController.test("help-text"); const hasHelpText = this.helpText ? true : !!hasHelpTextSlot; return html` <div class=${classMap({ "form-control": true, "form-control--small": this.size === "small", "form-control--medium": this.size === "medium", "form-control--large": this.size === "large", "form-control--has-help-text": hasHelpText })} > <label part="base" class=${classMap({ switch: true, "switch--checked": this.checked, "switch--disabled": this.disabled, "switch--focused": this.hasFocus, "switch--small": this.size === "small", "switch--medium": this.size === "medium", "switch--large": this.size === "large" })} > <input class="switch__input" type="checkbox" title=${this.title} name=${this.name} value=${ifDefined(this.value)} .checked=${live(this.checked)} .disabled=${this.disabled} .required=${this.required} role="switch" aria-checked=${this.checked ? "true" : "false"} aria-describedby="help-text" @click=${this.handleClick} @input=${this.handleInput} @invalid=${this.handleInvalid} @blur=${this.handleBlur} @focus=${this.handleFocus} @keydown=${this.handleKeyDown} /> <span part="control" class="switch__control"> <span part="thumb" class="switch__thumb"></span> </span> <div part="label" class="switch__label"> <slot></slot> </div> </label> <div aria-hidden=${hasHelpText ? "false" : "true"} class="form-control__help-text" id="help-text" part="form-control-help-text" > <slot name="help-text">${this.helpText}</slot> </div> </div> `; } }; SynSwitch.styles = [component_styles_default, form_control_styles_default, switch_styles_default, form_control_custom_styles_default, switch_custom_styles_default]; __decorateClass([ query('input[type="checkbox"]') ], SynSwitch.prototype, "input", 2); __decorateClass([ state() ], SynSwitch.prototype, "hasFocus", 2); __decorateClass([ property({ reflect: true }) ], SynSwitch.prototype, "title", 2); __decorateClass([ property() ], SynSwitch.prototype, "name", 2); __decorateClass([ property() ], SynSwitch.prototype, "value", 2); __decorateClass([ property({ reflect: true }) ], SynSwitch.prototype, "size", 2); __decorateClass([ property({ type: Boolean, reflect: true }) ], SynSwitch.prototype, "disabled", 2); __decorateClass([ property({ type: Boolean, reflect: true }) ], SynSwitch.prototype, "checked", 2); __decorateClass([ defaultValue("checked") ], SynSwitch.prototype, "defaultChecked", 2); __decorateClass([ property({ reflect: true }) ], SynSwitch.prototype, "form", 2); __decorateClass([ property({ type: Boolean, reflect: true }) ], SynSwitch.prototype, "required", 2); __decorateClass([ property({ attribute: "help-text" }) ], SynSwitch.prototype, "helpText", 2); __decorateClass([ watch("checked", { waitUntilFirstUpdate: true }) ], SynSwitch.prototype, "handleCheckedChange", 1); __decorateClass([ watch("disabled", { waitUntilFirstUpdate: true }) ], SynSwitch.prototype, "handleDisabledChange", 1); SynSwitch = __decorateClass([ enableDefaultSettings("SynSwitch") ], SynSwitch); export { SynSwitch }; //# sourceMappingURL=chunk.PATEVJCK.js.map