calcite-checkbox.entry.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-8ece2564.js';
  7. import { g as guid } from './guid-b4461004.js';
  8. import { c as connectForm, d as disconnectForm, H as HiddenFormInputSlot } from './form-bca481e1.js';
  9. import { c as connectLabel, d as disconnectLabel, g as getLabelText } from './label-50132b90.js';
  10. import { u as updateHostInteraction } from './interactive-cb5bf285.js';
  11. import { t as toAriaBoolean } from './dom-da697a3f.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 = class {
  14. constructor(hostRef) {
  15. registerInstance(this, hostRef);
  16. this.calciteInternalCheckboxBlur = createEvent(this, "calciteInternalCheckboxBlur", 7);
  17. this.calciteCheckboxChange = createEvent(this, "calciteCheckboxChange", 7);
  18. this.calciteInternalCheckboxFocus = createEvent(this, "calciteInternalCheckboxFocus", 7);
  19. //--------------------------------------------------------------------------
  20. //
  21. // Properties
  22. //
  23. //--------------------------------------------------------------------------
  24. /** The checked state of the checkbox. */
  25. this.checked = false;
  26. /** True if the checkbox is disabled */
  27. this.disabled = false;
  28. /**
  29. * The hovered state of the checkbox.
  30. * @internal
  31. */
  32. this.hovered = false;
  33. /**
  34. * True if the checkbox is initially indeterminate,
  35. * which is independent from its checked state
  36. * https://css-tricks.com/indeterminate-checkboxes/
  37. * */
  38. this.indeterminate = false;
  39. /**
  40. * When true, makes the component required for form-submission.
  41. *
  42. * @internal
  43. */
  44. this.required = false;
  45. /** specify the scale of the checkbox, defaults to m */
  46. this.scale = "m";
  47. //--------------------------------------------------------------------------
  48. //
  49. // Private Properties
  50. //
  51. //--------------------------------------------------------------------------
  52. this.checkedPath = "M5.5 12L2 8.689l.637-.636L5.5 10.727l8.022-7.87.637.637z";
  53. this.indeterminatePath = "M13 8v1H3V8z";
  54. //--------------------------------------------------------------------------
  55. //
  56. // Private Methods
  57. //
  58. //--------------------------------------------------------------------------
  59. this.getPath = () => this.indeterminate ? this.indeterminatePath : this.checked ? this.checkedPath : "";
  60. this.toggle = () => {
  61. if (!this.disabled) {
  62. this.checked = !this.checked;
  63. this.setFocus();
  64. this.indeterminate = false;
  65. this.calciteCheckboxChange.emit();
  66. }
  67. };
  68. this.keyDownHandler = (event) => {
  69. if (event.key === " " || event.key === "Enter") {
  70. this.toggle();
  71. event.preventDefault();
  72. }
  73. };
  74. this.clickHandler = () => {
  75. this.toggle();
  76. };
  77. //--------------------------------------------------------------------------
  78. //
  79. // Event Listeners
  80. //
  81. //--------------------------------------------------------------------------
  82. this.onToggleBlur = () => {
  83. this.calciteInternalCheckboxBlur.emit(false);
  84. };
  85. this.onToggleFocus = () => {
  86. this.calciteInternalCheckboxFocus.emit(true);
  87. };
  88. this.onLabelClick = () => {
  89. this.toggle();
  90. };
  91. }
  92. //--------------------------------------------------------------------------
  93. //
  94. // Public Methods
  95. //
  96. //--------------------------------------------------------------------------
  97. /** Sets focus on the component. */
  98. async setFocus() {
  99. var _a;
  100. (_a = this.toggleEl) === null || _a === void 0 ? void 0 : _a.focus();
  101. }
  102. //--------------------------------------------------------------------------
  103. //
  104. // Lifecycle
  105. //
  106. //--------------------------------------------------------------------------
  107. connectedCallback() {
  108. this.guid = this.el.id || `calcite-checkbox-${guid()}`;
  109. connectLabel(this);
  110. connectForm(this);
  111. }
  112. disconnectedCallback() {
  113. disconnectLabel(this);
  114. disconnectForm(this);
  115. }
  116. componentDidRender() {
  117. updateHostInteraction(this);
  118. }
  119. // --------------------------------------------------------------------------
  120. //
  121. // Render Methods
  122. //
  123. // --------------------------------------------------------------------------
  124. render() {
  125. 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 })));
  126. }
  127. get el() { return getElement(this); }
  128. };
  129. Checkbox.style = checkboxCss;
  130. export { Checkbox as calcite_checkbox };