calcite-combobox-item.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 getElementProp, b as getSlotted } from './dom.js';
  8. import { g as guid } from './guid.js';
  9. import { d as getAncestors, e as getDepth } from './utils2.js';
  10. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot.js';
  11. import { u as updateHostInteraction } from './interactive.js';
  12. import { d as defineCustomElement$2 } from './icon.js';
  13. const CSS = {
  14. icon: "icon",
  15. iconActive: "icon--active",
  16. custom: "icon--custom",
  17. dot: "icon--dot",
  18. single: "label--single",
  19. label: "label",
  20. active: "label--active",
  21. selected: "label--selected",
  22. title: "title",
  23. textContainer: "text-container"
  24. };
  25. const comboboxItemCss = "@charset \"UTF-8\";@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)}.scale--s{font-size:var(--calcite-font-size--2);line-height:1rem;--calcite-combobox-item-spacing-unit-l:0.5rem;--calcite-combobox-item-spacing-unit-s:0.25rem;--calcite-combobox-item-spacing-indent:0.5rem}.scale--m{font-size:var(--calcite-font-size--1);line-height:1rem;--calcite-combobox-item-spacing-unit-l:0.75rem;--calcite-combobox-item-spacing-unit-s:0.5rem;--calcite-combobox-item-spacing-indent:0.75rem}.scale--l{font-size:var(--calcite-font-size-0);line-height:1.25rem;--calcite-combobox-item-spacing-unit-l:1rem;--calcite-combobox-item-spacing-unit-s:0.75rem;--calcite-combobox-item-spacing-indent:1rem}.container{--calcite-combobox-item-indent-value:calc(\n var(--calcite-combobox-item-spacing-indent) * var(--calcite-combobox-item-spacing-indent-multiplier)\n )}:host(:focus){--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host,ul{margin:0px;display:flex;flex-direction:column;padding:0px}:host(:focus),ul:focus{outline:2px solid transparent;outline-offset:2px}.label{position:relative;box-sizing:border-box;display:flex;inline-size:100%;min-inline-size:100%;cursor:pointer;align-items:center;color:var(--calcite-ui-text-3);text-decoration-line:none;transition-duration:150ms;transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);outline-color:transparent;padding-block:var(--calcite-combobox-item-spacing-unit-s);padding-inline:var(--calcite-combobox-item-spacing-unit-l)}:host([disabled]) .label{cursor:default}.label--selected{font-weight:var(--calcite-font-weight-medium);color:var(--calcite-ui-text-1)}.label--active{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}.label:hover,.label:active{background-color:var(--calcite-ui-foreground-2);color:var(--calcite-ui-text-1);text-decoration-line:none;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow)}.title{padding-block:0;padding-inline:var(--calcite-combobox-item-spacing-unit-l)}.icon{display:inline-flex;opacity:0;transition-duration:150ms;transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);color:var(--calcite-ui-border-1)}.icon--indent{padding-inline-start:var(--calcite-combobox-item-indent-value)}.icon--custom{margin-block-start:-1px;color:var(--calcite-ui-text-3)}.icon--active{color:var(--calcite-ui-text-1)}.icon--dot{display:flex;justify-content:flex-end;min-inline-size:var(--calcite-combobox-item-spacing-unit-l)}.icon--dot:before{text-align:start;content:\"•\"}.label--active .icon{opacity:1}.label--selected .icon{opacity:1;color:var(--calcite-ui-brand)}:host(:hover[disabled]) .icon{opacity:1}";
  26. const ComboboxItem = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  27. constructor() {
  28. super();
  29. this.__registerHost();
  30. this.__attachShadow();
  31. this.calciteComboboxItemChange = createEvent(this, "calciteComboboxItemChange", 6);
  32. // --------------------------------------------------------------------------
  33. //
  34. // Properties
  35. //
  36. // --------------------------------------------------------------------------
  37. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  38. this.disabled = false;
  39. /**
  40. * When `true`, the component is selected.
  41. */
  42. this.selected = false;
  43. /** When `true`, the component is active. */
  44. this.active = false;
  45. /** The `id` attribute of the component. When omitted, a globally unique identifier is used. */
  46. this.guid = guid();
  47. this.scale = "m";
  48. // --------------------------------------------------------------------------
  49. //
  50. // Private Methods
  51. //
  52. // --------------------------------------------------------------------------
  53. this.itemClickHandler = (event) => {
  54. event.preventDefault();
  55. if (this.disabled) {
  56. return;
  57. }
  58. this.selected = !this.selected;
  59. };
  60. }
  61. selectedWatchHandler() {
  62. this.calciteComboboxItemChange.emit(this.el);
  63. }
  64. // --------------------------------------------------------------------------
  65. //
  66. // Lifecycle
  67. //
  68. // --------------------------------------------------------------------------
  69. connectedCallback() {
  70. this.ancestors = getAncestors(this.el);
  71. this.scale = getElementProp(this.el, "scale", this.scale);
  72. connectConditionalSlotComponent(this);
  73. }
  74. disconnectedCallback() {
  75. disconnectConditionalSlotComponent(this);
  76. }
  77. componentDidRender() {
  78. updateHostInteraction(this);
  79. }
  80. // --------------------------------------------------------------------------
  81. //
  82. // Public Methods
  83. //
  84. // --------------------------------------------------------------------------
  85. /**
  86. * Used to toggle the selection state. By default this won't trigger an event.
  87. * The first argument allows the value to be coerced, rather than swapping values.
  88. *
  89. * @param coerce
  90. */
  91. async toggleSelected(coerce) {
  92. if (this.disabled) {
  93. return;
  94. }
  95. this.selected = typeof coerce === "boolean" ? coerce : !this.selected;
  96. }
  97. // --------------------------------------------------------------------------
  98. //
  99. // Render Methods
  100. //
  101. // --------------------------------------------------------------------------
  102. renderIcon(isSingle) {
  103. const { icon, disabled, selected } = this;
  104. const level = `${CSS.icon}--indent`;
  105. const defaultIcon = isSingle ? "dot" : "check";
  106. const iconPath = disabled ? "circle-disallowed" : defaultIcon;
  107. const showDot = isSingle && !icon && !disabled;
  108. return showDot ? (h("span", { class: {
  109. [CSS.icon]: true,
  110. [CSS.dot]: true,
  111. [level]: true
  112. } })) : (h("calcite-icon", { class: {
  113. [CSS.icon]: !icon,
  114. [CSS.custom]: !!icon,
  115. [CSS.iconActive]: icon && selected,
  116. [level]: true
  117. }, icon: icon || iconPath, scale: "s" }));
  118. }
  119. renderChildren() {
  120. if (getSlotted(this.el)) {
  121. return (h("ul", { key: "default-slot-container" }, h("slot", null)));
  122. }
  123. return null;
  124. }
  125. render() {
  126. const isSingleSelect = getElementProp(this.el, "selection-mode", "multi") === "single";
  127. const classes = {
  128. [CSS.label]: true,
  129. [CSS.selected]: this.selected,
  130. [CSS.active]: this.active,
  131. [CSS.single]: isSingleSelect
  132. };
  133. const depth = getDepth(this.el);
  134. return (h(Host, { "aria-hidden": "true" }, h("div", { class: `container scale--${this.scale}`, style: { "--calcite-combobox-item-spacing-indent-multiplier": `${depth}` } }, h("li", { class: classes, id: this.guid, onClick: this.itemClickHandler }, this.renderIcon(isSingleSelect), h("span", { class: CSS.title }, this.textLabel)), this.renderChildren())));
  135. }
  136. get el() { return this; }
  137. static get watchers() { return {
  138. "selected": ["selectedWatchHandler"]
  139. }; }
  140. static get style() { return comboboxItemCss; }
  141. }, [1, "calcite-combobox-item", {
  142. "disabled": [516],
  143. "selected": [1540],
  144. "active": [516],
  145. "ancestors": [1040],
  146. "guid": [513],
  147. "icon": [513],
  148. "textLabel": [513, "text-label"],
  149. "value": [8],
  150. "constant": [516],
  151. "filterDisabled": [516, "filter-disabled"],
  152. "toggleSelected": [64]
  153. }]);
  154. function defineCustomElement$1() {
  155. if (typeof customElements === "undefined") {
  156. return;
  157. }
  158. const components = ["calcite-combobox-item", "calcite-icon"];
  159. components.forEach(tagName => { switch (tagName) {
  160. case "calcite-combobox-item":
  161. if (!customElements.get(tagName)) {
  162. customElements.define(tagName, ComboboxItem);
  163. }
  164. break;
  165. case "calcite-icon":
  166. if (!customElements.get(tagName)) {
  167. defineCustomElement$2();
  168. }
  169. break;
  170. } });
  171. }
  172. defineCustomElement$1();
  173. const CalciteComboboxItem = ComboboxItem;
  174. const defineCustomElement = defineCustomElement$1;
  175. export { CalciteComboboxItem, defineCustomElement };