checkbox.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.82
  5. */
  6. import { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
  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 './label.js';
  10. import { u as updateHostInteraction } from './interactive.js';
  11. import { t as toAriaBoolean } from './dom.js';
  12. const checkboxCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 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;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}: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:-ms-inline-flexbox;display:inline-flex;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-webkit-tap-highlight-color:transparent}:host .check-svg{pointer-events:none;-webkit-box-sizing:border-box;box-sizing:border-box;display:block;overflow:hidden;background-color:var(--calcite-ui-foreground-1);fill:currentColor;stroke:currentColor;stroke-width:1;-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:150ms;transition-duration:150ms;-webkit-transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);width:var(--calcite-checkbox-size);height:var(--calcite-checkbox-size);-webkit-box-shadow:inset 0 0 0 1px var(--calcite-ui-border-input);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);-webkit-box-shadow:inset 0 0 0 1px 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{-webkit-box-shadow:inset 0 0 0 2px var(--calcite-ui-brand);box-shadow:inset 0 0 0 2px var(--calcite-ui-brand)}:host .toggle:focus .check-svg,:host .toggle:active .check-svg{-webkit-box-shadow:inset 0 0 0 1px var(--calcite-ui-brand), 0 0 0 2px var(--calcite-ui-foreground-1), 0 0 0 4px var(--calcite-ui-brand);box-shadow:inset 0 0 0 1px var(--calcite-ui-brand), 0 0 0 2px var(--calcite-ui-foreground-1), 0 0 0 4px var(--calcite-ui-brand);-webkit-transition:var(--calcite-animation-timing);transition:var(--calcite-animation-timing)}.toggle:focus-visible{outline:none}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}::slotted(input[slot=hidden-form-input]){bottom:0 !important;left:0 !important;margin:0 !important;opacity:0 !important;outline:none !important;padding:0 !important;position:absolute !important;right:0 !important;top:0 !important;-webkit-transform:none !important;transform:none !important;-webkit-appearance:none !important;z-index:-1 !important}";
  13. const Checkbox = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  14. constructor() {
  15. super();
  16. this.__registerHost();
  17. this.__attachShadow();
  18. this.calciteInternalCheckboxBlur = createEvent(this, "calciteInternalCheckboxBlur", 7);
  19. this.calciteCheckboxChange = createEvent(this, "calciteCheckboxChange", 7);
  20. this.calciteInternalCheckboxFocus = createEvent(this, "calciteInternalCheckboxFocus", 7);
  21. //--------------------------------------------------------------------------
  22. //
  23. // Properties
  24. //
  25. //--------------------------------------------------------------------------
  26. /** The checked state of the checkbox. */
  27. this.checked = false;
  28. /** True if the checkbox is disabled */
  29. this.disabled = false;
  30. /**
  31. * The hovered state of the checkbox.
  32. * @internal
  33. */
  34. this.hovered = false;
  35. /**
  36. * True if the checkbox is initially indeterminate,
  37. * which is independent from its checked state
  38. * https://css-tricks.com/indeterminate-checkboxes/
  39. * */
  40. this.indeterminate = false;
  41. /**
  42. * When true, makes the component required for form-submission.
  43. *
  44. * @internal
  45. */
  46. this.required = false;
  47. /** specify the scale of the checkbox, defaults to m */
  48. this.scale = "m";
  49. //--------------------------------------------------------------------------
  50. //
  51. // Private Properties
  52. //
  53. //--------------------------------------------------------------------------
  54. this.checkedPath = "M5.5 12L2 8.689l.637-.636L5.5 10.727l8.022-7.87.637.637z";
  55. this.indeterminatePath = "M13 8v1H3V8z";
  56. //--------------------------------------------------------------------------
  57. //
  58. // Private Methods
  59. //
  60. //--------------------------------------------------------------------------
  61. this.getPath = () => this.indeterminate ? this.indeterminatePath : this.checked ? this.checkedPath : "";
  62. this.toggle = () => {
  63. if (!this.disabled) {
  64. this.checked = !this.checked;
  65. this.setFocus();
  66. this.indeterminate = false;
  67. this.calciteCheckboxChange.emit();
  68. }
  69. };
  70. this.keyDownHandler = (event) => {
  71. if (event.key === " " || event.key === "Enter") {
  72. this.toggle();
  73. event.preventDefault();
  74. }
  75. };
  76. this.clickHandler = () => {
  77. this.toggle();
  78. };
  79. //--------------------------------------------------------------------------
  80. //
  81. // Event Listeners
  82. //
  83. //--------------------------------------------------------------------------
  84. this.onToggleBlur = () => {
  85. this.calciteInternalCheckboxBlur.emit(false);
  86. };
  87. this.onToggleFocus = () => {
  88. this.calciteInternalCheckboxFocus.emit(true);
  89. };
  90. this.onLabelClick = () => {
  91. this.toggle();
  92. };
  93. }
  94. //--------------------------------------------------------------------------
  95. //
  96. // Public Methods
  97. //
  98. //--------------------------------------------------------------------------
  99. /** Sets focus on the component. */
  100. async setFocus() {
  101. var _a;
  102. (_a = this.toggleEl) === null || _a === void 0 ? void 0 : _a.focus();
  103. }
  104. //--------------------------------------------------------------------------
  105. //
  106. // Lifecycle
  107. //
  108. //--------------------------------------------------------------------------
  109. connectedCallback() {
  110. this.guid = this.el.id || `calcite-checkbox-${guid()}`;
  111. connectLabel(this);
  112. connectForm(this);
  113. }
  114. disconnectedCallback() {
  115. disconnectLabel(this);
  116. disconnectForm(this);
  117. }
  118. componentDidRender() {
  119. updateHostInteraction(this);
  120. }
  121. // --------------------------------------------------------------------------
  122. //
  123. // Render Methods
  124. //
  125. // --------------------------------------------------------------------------
  126. render() {
  127. 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", { class: "check-svg", viewBox: "0 0 16 16" }, h("path", { d: this.getPath() })), h("slot", null)), h(HiddenFormInputSlot, { component: this })));
  128. }
  129. get el() { return this; }
  130. static get style() { return checkboxCss; }
  131. }, [1, "calcite-checkbox", {
  132. "checked": [1540],
  133. "disabled": [516],
  134. "guid": [1537],
  135. "hovered": [1540],
  136. "indeterminate": [1540],
  137. "label": [1],
  138. "name": [520],
  139. "required": [516],
  140. "scale": [513],
  141. "value": [8],
  142. "setFocus": [64]
  143. }]);
  144. function defineCustomElement() {
  145. if (typeof customElements === "undefined") {
  146. return;
  147. }
  148. const components = ["calcite-checkbox"];
  149. components.forEach(tagName => { switch (tagName) {
  150. case "calcite-checkbox":
  151. if (!customElements.get(tagName)) {
  152. customElements.define(tagName, Checkbox);
  153. }
  154. break;
  155. } });
  156. }
  157. defineCustomElement();
  158. export { Checkbox as C, defineCustomElement as d };