@utrecht/web-component-library-stencil
Version:
Stencil component library bundle for the Municipality of Utrecht based on the NL Design System architecture
155 lines (154 loc) • 5.81 kB
JavaScript
/**
* @license EUPL-1.2
* Copyright (c) 2020-2024 Frameless B.V.
* Copyright (c) 2021-2024 Gemeente Utrecht
*/
import { h } from "@stencil/core";
import clsx from "clsx";
export class FormToggle {
constructor() {
this.disabled = false;
this.checked = false;
}
render() {
const { checked, disabled } = this;
const toggleInteraction = () => {
if (!this.disabled) {
this.checked = !this.checked;
this.utrechtInput.emit(this);
this.utrechtChange.emit(this);
}
};
const handleClick = () => {
toggleInteraction();
};
const handleKeyPress = (evt) => {
if (evt.code === 'Space' || evt.key === ' ') {
// Do not scroll the page using Space when the toggle is focussed
evt.preventDefault();
}
};
const handleKeyUp = (evt) => {
if (evt.code === 'Space' || evt.key === ' ') {
toggleInteraction();
}
};
return (h("div", { key: 'be20682f616eb7b0d14a213f46612a8a09292e2e', class: clsx('utrecht-form-toggle', checked && 'utrecht-form-toggle--checked', !checked && 'utrecht-form-toggle--not-checked', disabled && 'utrecht-form-toggle--disabled'), tabIndex: disabled ? null : 0, role: "switch", "aria-disabled": disabled ? 'true' : null, onClick: handleClick, onKeyPress: handleKeyPress, onKeyUp: handleKeyUp }, h("div", { key: 'a39f6ea33b38051b0cc321a85d830839d8f526db', class: "utrecht-form-toggle__label utrecht-form-toggle__label--off" }), h("div", { key: '2a817a7018427997e3ec410076aa58ac27f1b93e', class: "utrecht-form-toggle__label utrecht-form-toggle__label--on" }), h("div", { key: '9db9333631a9392b5af650bd40febc6d559e80a9', class: clsx('utrecht-form-toggle__track', checked && 'utrecht-form-toggle__track--checked', !checked && 'utrecht-form-toggle__track--not-checked', disabled && 'utrecht-form-toggle__track--disabled') }, h("div", { key: 'd468c947a90b4aec924c658b8d36f84f8eb27bc7', class: clsx('utrecht-form-toggle__thumb', checked && 'utrecht-form-toggle__thumb--checked', !checked && 'utrecht-form-toggle__thumb--not-checked', disabled && 'utrecht-form-toggle__thumb--disabled') }))));
}
static get is() { return "utrecht-form-toggle"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["form-toggle.scss"]
};
}
static get styleUrls() {
return {
"$": ["form-toggle.css"]
};
}
static get properties() {
return {
"disabled": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": ""
},
"attribute": "disabled",
"reflect": true,
"defaultValue": "false"
},
"checked": {
"type": "boolean",
"mutable": false,
"complexType": {
"original": "boolean",
"resolved": "boolean",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": ""
},
"attribute": "checked",
"reflect": true,
"defaultValue": "false"
}
};
}
static get events() {
return [{
"method": "utrechtBlur",
"name": "utrechtBlur",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": ""
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}, {
"method": "utrechtChange",
"name": "utrechtChange",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": ""
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}, {
"method": "utrechtFocus",
"name": "utrechtFocus",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": ""
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}, {
"method": "utrechtInput",
"name": "utrechtInput",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": ""
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}];
}
}
//# sourceMappingURL=form-toggle.js.map