adwaveui
Version:
Interactive Web Components inspired by the Gtk Adwaita theme.
183 lines (181 loc) • 8.9 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/components/switch/switch.tsx
import { jsx as _jsx, jsxs as _jsxs } from "jsxte/jsx-runtime";
import { Switch } from "adwavecss";
import { Attribute, CustomElement } from "jsxte-wc";
import { BaseElement } from "../../base-elements.mjs";
import "../../index.style.mjs";
import { cls } from "../../utils/cls.mjs";
import { CustomKeyboardEvent, CustomMouseEvent } from "../../utils/events.mjs";
import { stopEvent } from "../../utils/prevent-default.mjs";
import "./switch.style.mjs";
var __esDecorate = function(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) {
if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected");
return f;
}
__name(accept, "accept");
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function(f) {
if (done) throw new TypeError("Cannot add initializers after decoration has completed");
extraInitializers.push(accept(f || null));
};
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
} else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};
var __runInitializers = function(thisArg, initializers, value) {
var useValue = arguments.length > 2;
for (var i = 0; i < initializers.length; i++) {
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
}
return useValue ? value : void 0;
};
var SwitchChangeEvent = class extends CustomEvent {
static {
__name(this, "SwitchChangeEvent");
}
constructor(active) {
super("change", {
detail: {
active
}
});
}
};
var ADWaveSwitchElement = (() => {
let _classDecorators = [CustomElement("adw-switch")];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
let _classSuper = BaseElement;
let _active_decorators;
let _active_initializers = [];
let _active_extraInitializers = [];
let _disabled_decorators;
let _disabled_initializers = [];
let _disabled_extraInitializers = [];
let _name_decorators;
let _name_initializers = [];
let _name_extraInitializers = [];
let _form_decorators;
let _form_initializers = [];
let _form_extraInitializers = [];
var ADWaveSwitchElement2 = class extends _classSuper {
static {
__name(this, "ADWaveSwitchElement");
}
static {
_classThis = this;
}
static {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
_active_decorators = [Attribute({ type: "boolean", nullable: false })];
_disabled_decorators = [Attribute({ type: "boolean", nullable: false, default: false })];
_name_decorators = [Attribute({ nullable: true })];
_form_decorators = [Attribute({ nullable: true })];
__esDecorate(this, null, _active_decorators, { kind: "accessor", name: "active", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "active" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.active, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.active = value;
}, "set") }, metadata: _metadata }, _active_initializers, _active_extraInitializers);
__esDecorate(this, null, _disabled_decorators, { kind: "accessor", name: "disabled", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "disabled" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.disabled, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.disabled = value;
}, "set") }, metadata: _metadata }, _disabled_initializers, _disabled_extraInitializers);
__esDecorate(this, null, _name_decorators, { kind: "accessor", name: "name", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "name" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.name, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.name = value;
}, "set") }, metadata: _metadata }, _name_initializers, _name_extraInitializers);
__esDecorate(this, null, _form_decorators, { kind: "accessor", name: "form", static: false, private: false, access: { has: /* @__PURE__ */ __name((obj) => "form" in obj, "has"), get: /* @__PURE__ */ __name((obj) => obj.form, "get"), set: /* @__PURE__ */ __name((obj, value) => {
obj.form = value;
}, "set") }, metadata: _metadata }, _form_initializers, _form_extraInitializers);
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
ADWaveSwitchElement2 = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
}
#active_accessor_storage;
get active() {
return this.#active_accessor_storage;
}
set active(value) {
this.#active_accessor_storage = value;
}
#disabled_accessor_storage;
get disabled() {
return this.#disabled_accessor_storage;
}
set disabled(value) {
this.#disabled_accessor_storage = value;
}
#name_accessor_storage;
get name() {
return this.#name_accessor_storage;
}
set name(value) {
this.#name_accessor_storage = value;
}
#form_accessor_storage;
get form() {
return this.#form_accessor_storage;
}
set form(value) {
this.#form_accessor_storage = value;
}
constructor() {
super();
this.#active_accessor_storage = __runInitializers(this, _active_initializers, false);
this.#disabled_accessor_storage = (__runInitializers(this, _active_extraInitializers), __runInitializers(this, _disabled_initializers, false));
this.#name_accessor_storage = (__runInitializers(this, _disabled_extraInitializers), __runInitializers(this, _name_initializers, null));
this.#form_accessor_storage = (__runInitializers(this, _name_extraInitializers), __runInitializers(this, _form_initializers, null));
this.handleClick = (__runInitializers(this, _form_extraInitializers), (e) => {
e.stopPropagation();
const shouldContinue = this.dispatchEvent(new CustomMouseEvent("click", {}, e));
if (this.disabled || !shouldContinue)
return;
this.active = !this.active;
});
this.handleKeyDown = (e) => {
e.stopPropagation();
const shouldContinue = this.dispatchEvent(new CustomKeyboardEvent("keydown", {}, e));
if (shouldContinue && e.key === " ") {
if (this.disabled || !shouldContinue)
return;
this.active = !this.active;
}
};
this.effect(({ isFirstMount }) => {
if (isFirstMount)
return;
this.dispatchEvent(new SwitchChangeEvent(this.active));
}, (s) => [s.active]);
}
render() {
return _jsxs("div", { class: cls({
[Switch.switch]: true,
[Switch.disabled]: this.disabled,
[Switch.active]: this.active
}), onclick: this.handleClick, onkeydown: this.handleKeyDown, tabindex: "0", role: "switch", "aria-checked": this.active, "aria-disabled": this.disabled ? "true" : "false", children: [_jsx("div", { class: Switch.knob }), _jsx("input", { type: "checkbox", class: "_adw_hidden", disabled: this.disabled, checked: this.active, name: this.name ?? void 0, form: this.form ?? void 0, onclick: this.handleClick, onchange: stopEvent, "aria-hidden": "true" })] });
}
};
return ADWaveSwitchElement2 = _classThis;
})();
export {
ADWaveSwitchElement
};