calcite-tile-select.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 } from '@stencil/core/internal/client/index.js';
  7. import { g as guid } from './guid.js';
  8. import { u as updateHostInteraction } from './interactive.js';
  9. import { d as defineCustomElement$4 } from './icon.js';
  10. import { d as defineCustomElement$3 } from './link.js';
  11. import { d as defineCustomElement$2 } from './tile.js';
  12. const CSS = {
  13. checked: "checked",
  14. description: "description",
  15. descriptionOnly: "description-only",
  16. disabled: "disabled",
  17. heading: "heading",
  18. headingOnly: "heading-only",
  19. icon: "icon",
  20. iconOnly: "icon-only",
  21. inputAlignmentEnd: "input-alignment-end",
  22. inputAlignmentStart: "input-alignment-start",
  23. inputEnabled: "input-enabled",
  24. largeVisual: "large-visual",
  25. widthAuto: "width-auto",
  26. widthFull: "width-full"
  27. };
  28. const tileSelectCss = "@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{display:block}:host .container{background-color:var(--calcite-ui-foreground-1);box-shadow:0 0 0 1px var(--calcite-ui-border-2);box-sizing:border-box;cursor:pointer;display:inline-block;block-size:100%;max-inline-size:300px;padding:0.75rem;position:relative;vertical-align:top;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s}:host .container.checked{z-index:1;box-shadow:0 0 0 1px var(--calcite-ui-brand)}:host .container.heading-only{align-items:center}:host .container:not(.input-enabled) ::slotted(calcite-checkbox),:host .container:not(.input-enabled) ::slotted(calcite-radio-button){position:absolute;inline-size:1px;block-size:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0}:host .container.focused{outline-color:transparent}:host .container.focused:not(.disabled):not(.input-enabled){outline:2px solid var(--calcite-ui-brand);outline-offset:-4px;box-shadow:0 0 0 1px var(--calcite-ui-brand), inset 0 0 0 2px var(--calcite-ui-foreground-1)}:host .container.input-enabled.input-alignment-start.width-auto.heading-only,:host .container.input-enabled.input-alignment-start.width-auto.icon-only,:host .container.input-enabled.input-alignment-start.width-auto.description-only,:host .container.input-enabled.input-alignment-start.width-auto.heading.description,:host .container.input-enabled.input-alignment-start.width-auto.icon.description,:host .container.input-enabled.input-alignment-start.width-auto.heading.icon.description{display:inline-grid;grid-template-columns:max-content 1fr}:host .container.input-enabled.input-alignment-start.heading-only,:host .container.input-enabled.input-alignment-start.icon-only,:host .container.input-enabled.input-alignment-start.description-only,:host .container.input-enabled.input-alignment-start.heading.description,:host .container.input-enabled.input-alignment-start.icon.description,:host .container.input-enabled.input-alignment-start.heading.icon.description{gap:0.75rem}:host .container.input-enabled.input-alignment-start calcite-tile{order:1}:host .container.input-enabled.input-alignment-start.large-visual ::slotted(calcite-checkbox),:host .container.input-enabled.input-alignment-start.large-visual ::slotted(calcite-radio-button){position:absolute;inset-block-start:0.75rem;inset-inline-start:0.75rem}:host .container.input-enabled.input-alignment-end.width-auto.heading-only,:host .container.input-enabled.input-alignment-end.width-auto.icon-only{display:inline-grid;grid-gap:0.75rem;grid-template-columns:max-content 1fr}:host .container.input-enabled.input-alignment-end.heading-only,:host .container.input-enabled.input-alignment-end.icon-only{gap:0.75rem}:host .container.input-enabled.input-alignment-end.description-only ::slotted(calcite-checkbox),:host .container.input-enabled.input-alignment-end.description-only ::slotted(calcite-radio-button),:host .container.input-enabled.input-alignment-end.heading.description ::slotted(calcite-checkbox),:host .container.input-enabled.input-alignment-end.heading.description ::slotted(calcite-radio-button),:host .container.input-enabled.input-alignment-end.icon.description ::slotted(calcite-checkbox),:host .container.input-enabled.input-alignment-end.icon.description ::slotted(calcite-radio-button),:host .container.input-enabled.input-alignment-end.heading.icon.description ::slotted(calcite-checkbox),:host .container.input-enabled.input-alignment-end.heading.icon.description ::slotted(calcite-radio-button){position:absolute;inset-block-start:0.75rem;inset-inline-end:0.75rem}:host .container.input-enabled.input-alignment-end.large-visual ::slotted(calcite-checkbox),:host .container.input-enabled.input-alignment-end.large-visual ::slotted(calcite-radio-button){position:absolute;inset-block-start:0.75rem;inset-inline-end:0.75rem}:host .container.width-full{display:flex;max-inline-size:none}:host .container.width-full calcite-tile{flex:1 1 auto}:host(:hover) .container:not(.input-enabled){box-shadow:0 0 0 1px var(--calcite-ui-brand)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}";
  29. const TileSelect = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  30. constructor() {
  31. super();
  32. this.__registerHost();
  33. this.__attachShadow();
  34. this.calciteTileSelectChange = createEvent(this, "calciteTileSelectChange", 6);
  35. //--------------------------------------------------------------------------
  36. //
  37. // Properties
  38. //
  39. //--------------------------------------------------------------------------
  40. /** When `true`, the component is checked. */
  41. this.checked = false;
  42. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  43. this.disabled = false;
  44. /** When `true`, the component is not displayed and is not focusable or checkable. */
  45. this.hidden = false;
  46. /** When `true`, displays an interactive input based on the `type` property. */
  47. this.inputEnabled = false;
  48. /** When `inputEnabled` is `true`, specifies the placement of the interactive input on the component. */
  49. this.inputAlignment = "start";
  50. /**
  51. * The selection mode of the component.
  52. *
  53. * Use radio for single selection, and checkbox for multiple selections.
  54. */
  55. this.type = "radio";
  56. /** Specifies the width of the component. */
  57. this.width = "auto";
  58. this.guid = `calcite-tile-select-${guid()}`;
  59. //--------------------------------------------------------------------------
  60. //
  61. // State
  62. //
  63. //--------------------------------------------------------------------------
  64. /** The focused state of the tile-select. */
  65. this.focused = false;
  66. }
  67. checkedChanged(newChecked) {
  68. this.input.checked = newChecked;
  69. }
  70. nameChanged(newName) {
  71. this.input.name = newName;
  72. }
  73. //--------------------------------------------------------------------------
  74. //
  75. // Public Methods
  76. //
  77. //--------------------------------------------------------------------------
  78. /** Sets focus on the component. */
  79. async setFocus() {
  80. var _a;
  81. (_a = this.input) === null || _a === void 0 ? void 0 : _a.setFocus();
  82. }
  83. //--------------------------------------------------------------------------
  84. //
  85. // Event Listeners
  86. //
  87. //--------------------------------------------------------------------------
  88. checkboxChangeHandler(event) {
  89. const checkbox = event.target;
  90. if (checkbox === this.input) {
  91. this.checked = checkbox.checked;
  92. }
  93. event.stopPropagation();
  94. this.calciteTileSelectChange.emit();
  95. }
  96. checkboxFocusBlurHandler(event) {
  97. const checkbox = event.target;
  98. if (checkbox === this.input) {
  99. this.focused = event.detail;
  100. }
  101. event.stopPropagation();
  102. }
  103. radioButtonChangeHandler(event) {
  104. const radioButton = event.target;
  105. if (radioButton === this.input) {
  106. this.checked = radioButton.checked;
  107. }
  108. event.stopPropagation();
  109. this.calciteTileSelectChange.emit();
  110. }
  111. radioButtonCheckedChangeHandler(event) {
  112. const radioButton = event.target;
  113. if (radioButton === this.input) {
  114. this.checked = radioButton.checked;
  115. }
  116. event.stopPropagation();
  117. }
  118. radioButtonFocusBlurHandler(event) {
  119. const radioButton = event.target;
  120. if (radioButton === this.input) {
  121. this.focused = radioButton.focused;
  122. }
  123. event.stopPropagation();
  124. }
  125. click(event) {
  126. const target = event.target;
  127. const targets = ["calcite-tile", "calcite-tile-select"];
  128. if (targets.includes(target.localName)) {
  129. this.input.click();
  130. }
  131. }
  132. mouseenter() {
  133. if (this.input.localName === "calcite-radio-button") {
  134. this.input.hovered = true;
  135. }
  136. if (this.input.localName === "calcite-checkbox") {
  137. this.input.hovered = true;
  138. }
  139. }
  140. mouseleave() {
  141. if (this.input.localName === "calcite-radio-button") {
  142. this.input.hovered = false;
  143. }
  144. if (this.input.localName === "calcite-checkbox") {
  145. this.input.hovered = false;
  146. }
  147. }
  148. //--------------------------------------------------------------------------
  149. //
  150. // Lifecycle
  151. //
  152. //--------------------------------------------------------------------------
  153. connectedCallback() {
  154. this.renderInput();
  155. }
  156. disconnectedCallback() {
  157. this.input.parentNode.removeChild(this.input);
  158. }
  159. componentDidRender() {
  160. updateHostInteraction(this);
  161. }
  162. // --------------------------------------------------------------------------
  163. //
  164. // Render Methods
  165. //
  166. // --------------------------------------------------------------------------
  167. renderInput() {
  168. this.input = document.createElement(this.type === "radio" ? "calcite-radio-button" : "calcite-checkbox");
  169. this.input.checked = this.checked;
  170. this.input.disabled = this.disabled;
  171. this.input.hidden = this.hidden;
  172. this.input.id = this.guid;
  173. this.input.label = this.heading || this.name || "";
  174. if (this.name) {
  175. this.input.name = this.name;
  176. }
  177. if (this.value) {
  178. this.input.value = this.value != null ? this.value.toString() : "";
  179. }
  180. this.el.insertAdjacentElement("beforeend", this.input);
  181. }
  182. render() {
  183. const { checked, description, disabled, focused, heading, icon, inputAlignment, inputEnabled, width } = this;
  184. return (h("div", { class: {
  185. checked,
  186. container: true,
  187. [CSS.description]: Boolean(description),
  188. [CSS.descriptionOnly]: Boolean(!heading && !icon && description),
  189. disabled,
  190. focused,
  191. [CSS.heading]: Boolean(heading),
  192. [CSS.headingOnly]: heading && !icon && !description,
  193. [CSS.icon]: Boolean(icon),
  194. [CSS.iconOnly]: !heading && icon && !description,
  195. [CSS.inputAlignmentEnd]: inputAlignment === "end",
  196. [CSS.inputAlignmentStart]: inputAlignment === "start",
  197. [CSS.inputEnabled]: inputEnabled,
  198. [CSS.largeVisual]: heading && icon && !description,
  199. [CSS.widthAuto]: width === "auto",
  200. [CSS.widthFull]: width === "full"
  201. } }, h("calcite-tile", { active: checked, description: description, embed: true, heading: heading, icon: icon }), h("slot", null)));
  202. }
  203. get el() { return this; }
  204. static get watchers() { return {
  205. "checked": ["checkedChanged"],
  206. "name": ["nameChanged"]
  207. }; }
  208. static get style() { return tileSelectCss; }
  209. }, [1, "calcite-tile-select", {
  210. "checked": [1540],
  211. "description": [513],
  212. "disabled": [516],
  213. "heading": [513],
  214. "hidden": [516],
  215. "icon": [513],
  216. "name": [520],
  217. "inputEnabled": [516, "input-enabled"],
  218. "inputAlignment": [513, "input-alignment"],
  219. "type": [513],
  220. "value": [8],
  221. "width": [513],
  222. "focused": [32],
  223. "setFocus": [64]
  224. }, [[0, "calciteCheckboxChange", "checkboxChangeHandler"], [0, "calciteInternalCheckboxFocus", "checkboxFocusBlurHandler"], [0, "calciteInternalCheckboxBlur", "checkboxFocusBlurHandler"], [0, "calciteRadioButtonChange", "radioButtonChangeHandler"], [0, "calciteInternalRadioButtonCheckedChange", "radioButtonCheckedChangeHandler"], [0, "calciteInternalRadioButtonFocus", "radioButtonFocusBlurHandler"], [0, "calciteInternalRadioButtonBlur", "radioButtonFocusBlurHandler"], [0, "click", "click"], [1, "pointerenter", "mouseenter"], [1, "pointerleave", "mouseleave"]]]);
  225. function defineCustomElement$1() {
  226. if (typeof customElements === "undefined") {
  227. return;
  228. }
  229. const components = ["calcite-tile-select", "calcite-icon", "calcite-link", "calcite-tile"];
  230. components.forEach(tagName => { switch (tagName) {
  231. case "calcite-tile-select":
  232. if (!customElements.get(tagName)) {
  233. customElements.define(tagName, TileSelect);
  234. }
  235. break;
  236. case "calcite-icon":
  237. if (!customElements.get(tagName)) {
  238. defineCustomElement$4();
  239. }
  240. break;
  241. case "calcite-link":
  242. if (!customElements.get(tagName)) {
  243. defineCustomElement$3();
  244. }
  245. break;
  246. case "calcite-tile":
  247. if (!customElements.get(tagName)) {
  248. defineCustomElement$2();
  249. }
  250. break;
  251. } });
  252. }
  253. defineCustomElement$1();
  254. const CalciteTileSelect = TileSelect;
  255. const defineCustomElement = defineCustomElement$1;
  256. export { CalciteTileSelect, defineCustomElement };