checkbox.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client/index.js';
  7. import { g as guid } from './guid.js';
  8. import { c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form.js';
  9. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label2.js';
  10. import { u as updateHostInteraction } from './interactive.js';
  11. import { t as toAriaBoolean } from './dom.js';
  12. import { i as isActivationKey } from './key.js';
  13. const checkboxCss = "@keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in-down{0%{opacity:0;transform:translate3D(0, -5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;transform:translate3D(0, 5px, 0)}100%{opacity:1;transform:translate3D(0, 0, 0)}}@keyframes in-scale{0%{opacity:0;transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;transform:scale3D(1, 1, 1)}}:root{--calcite-animation-timing:calc(150ms * var(--calcite-internal-duration-factor));--calcite-internal-duration-factor:var(--calcite-duration-factor, 1);--calcite-internal-animation-timing-fast:calc(100ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-medium:calc(200ms * var(--calcite-internal-duration-factor));--calcite-internal-animation-timing-slow:calc(300ms * var(--calcite-internal-duration-factor))}.calcite-animate{opacity:0;animation-fill-mode:both;animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{animation-name:in}.calcite-animate__in-down{animation-name:in-down}.calcite-animate__in-up{animation-name:in-up}.calcite-animate__in-scale{animation-name:in-scale}@media (prefers-reduced-motion: reduce){:root{--calcite-internal-duration-factor:0.01}}:root{--calcite-floating-ui-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([scale=s]){--calcite-checkbox-size:0.75rem}:host([scale=m]){--calcite-checkbox-size:var(--calcite-font-size--1)}:host([scale=l]){--calcite-checkbox-size:1rem}:host{position:relative;display:inline-flex;cursor:pointer;-webkit-user-select:none;user-select:none;-webkit-tap-highlight-color:transparent}:host .check-svg,:host .toggle{inline-size:var(--calcite-checkbox-size);block-size:var(--calcite-checkbox-size)}:host .check-svg{pointer-events:none;box-sizing:border-box;display:block;overflow:hidden;background-color:var(--calcite-ui-foreground-1);fill:currentColor;stroke:currentColor;stroke-width:1;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;box-shadow:inset 0 0 0 1px var(--calcite-ui-border-input);color:var(--calcite-ui-background)}:host([checked]) .check-svg,:host([indeterminate]) .check-svg{background-color:var(--calcite-ui-brand);box-shadow:inset 0 0 0 1px var(--calcite-ui-brand)}:host([hovered]) .toggle .check-svg,:host .toggle:hover .check-svg{box-shadow:inset 0 0 0 2px var(--calcite-ui-brand)}.toggle{outline-color:transparent}.toggle:active,.toggle:focus,.toggle:focus-visible{outline:2px solid var(--calcite-ui-brand);outline-offset:2px}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}::slotted(input[slot=hidden-form-input]){margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;inset:0 !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
  14. const Checkbox = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  15. constructor() {
  16. super();
  17. this.__registerHost();
  18. this.__attachShadow();
  19. this.calciteInternalCheckboxBlur = createEvent(this, "calciteInternalCheckboxBlur", 6);
  20. this.calciteCheckboxChange = createEvent(this, "calciteCheckboxChange", 6);
  21. this.calciteInternalCheckboxFocus = createEvent(this, "calciteInternalCheckboxFocus", 6);
  22. //--------------------------------------------------------------------------
  23. //
  24. // Properties
  25. //
  26. //--------------------------------------------------------------------------
  27. /** When `true`, the component is checked. */
  28. this.checked = false;
  29. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  30. this.disabled = false;
  31. /**
  32. * The hovered state of the checkbox.
  33. *
  34. * @internal
  35. */
  36. this.hovered = false;
  37. /**
  38. * When `true`, the component is initially indeterminate, which is independent from its `checked` value.
  39. *
  40. * The state is visual only, and can look different across browsers.
  41. *
  42. * @mdn [indeterminate](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#indeterminate_state_checkboxes)
  43. */
  44. this.indeterminate = false;
  45. /**
  46. * When `true`, the component must have a value in order for the form to submit.
  47. *
  48. * @internal
  49. */
  50. this.required = false;
  51. /** Specifies the size of the component. */
  52. this.scale = "m";
  53. //--------------------------------------------------------------------------
  54. //
  55. // Private Properties
  56. //
  57. //--------------------------------------------------------------------------
  58. this.checkedPath = "M5.5 12L2 8.689l.637-.636L5.5 10.727l8.022-7.87.637.637z";
  59. this.indeterminatePath = "M13 8v1H3V8z";
  60. //--------------------------------------------------------------------------
  61. //
  62. // Private Methods
  63. //
  64. //--------------------------------------------------------------------------
  65. this.getPath = () => this.indeterminate ? this.indeterminatePath : this.checked ? this.checkedPath : "";
  66. this.toggle = () => {
  67. if (!this.disabled) {
  68. this.checked = !this.checked;
  69. this.setFocus();
  70. this.indeterminate = false;
  71. this.calciteCheckboxChange.emit();
  72. }
  73. };
  74. this.keyDownHandler = (event) => {
  75. if (isActivationKey(event.key)) {
  76. this.toggle();
  77. event.preventDefault();
  78. }
  79. };
  80. this.clickHandler = () => {
  81. this.toggle();
  82. };
  83. //--------------------------------------------------------------------------
  84. //
  85. // Event Listeners
  86. //
  87. //--------------------------------------------------------------------------
  88. this.onToggleBlur = () => {
  89. this.calciteInternalCheckboxBlur.emit(false);
  90. };
  91. this.onToggleFocus = () => {
  92. this.calciteInternalCheckboxFocus.emit(true);
  93. };
  94. this.onLabelClick = () => {
  95. this.toggle();
  96. };
  97. }
  98. //--------------------------------------------------------------------------
  99. //
  100. // Public Methods
  101. //
  102. //--------------------------------------------------------------------------
  103. /** Sets focus on the component. */
  104. async setFocus() {
  105. var _a;
  106. (_a = this.toggleEl) === null || _a === void 0 ? void 0 : _a.focus();
  107. }
  108. //--------------------------------------------------------------------------
  109. //
  110. // Lifecycle
  111. //
  112. //--------------------------------------------------------------------------
  113. connectedCallback() {
  114. this.guid = this.el.id || `calcite-checkbox-${guid()}`;
  115. connectLabel(this);
  116. connectForm(this);
  117. }
  118. disconnectedCallback() {
  119. disconnectLabel(this);
  120. disconnectForm(this);
  121. }
  122. componentDidRender() {
  123. updateHostInteraction(this);
  124. }
  125. // --------------------------------------------------------------------------
  126. //
  127. // Render Methods
  128. //
  129. // --------------------------------------------------------------------------
  130. render() {
  131. return (h(Host, { onClick: this.clickHandler, onKeyDown: this.keyDownHandler }, h("div", { "aria-checked": toAriaBoolean(this.checked), "aria-label": getLabelText(this), class: "toggle", onBlur: this.onToggleBlur, onFocus: this.onToggleFocus, ref: (toggleEl) => (this.toggleEl = toggleEl), role: "checkbox", tabIndex: this.disabled ? undefined : 0 }, h("svg", { "aria-hidden": "true", class: "check-svg", viewBox: "0 0 16 16" }, h("path", { d: this.getPath() })), h("slot", null)), h(HiddenFormInputSlot, { component: this })));
  132. }
  133. get el() { return this; }
  134. static get style() { return checkboxCss; }
  135. }, [1, "calcite-checkbox", {
  136. "checked": [1540],
  137. "disabled": [516],
  138. "guid": [1537],
  139. "hovered": [1540],
  140. "indeterminate": [1540],
  141. "label": [1],
  142. "name": [520],
  143. "required": [516],
  144. "scale": [513],
  145. "value": [8],
  146. "setFocus": [64]
  147. }]);
  148. function defineCustomElement() {
  149. if (typeof customElements === "undefined") {
  150. return;
  151. }
  152. const components = ["calcite-checkbox"];
  153. components.forEach(tagName => { switch (tagName) {
  154. case "calcite-checkbox":
  155. if (!customElements.get(tagName)) {
  156. customElements.define(tagName, Checkbox);
  157. }
  158. break;
  159. } });
  160. }
  161. defineCustomElement();
  162. export { Checkbox as C, defineCustomElement as d };