pick-list-item.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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, Fragment } from '@stencil/core/internal/client';
  7. import { I as ICON_TYPES } from './resources.js';
  8. import { b as getSlotted, t as toAriaBoolean } from './dom.js';
  9. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot.js';
  10. import { u as updateHostInteraction } from './interactive.js';
  11. import { d as defineCustomElement$3 } from './action.js';
  12. import { d as defineCustomElement$2 } from './icon.js';
  13. import { d as defineCustomElement$1 } from './loader.js';
  14. const CSS = {
  15. actions: "actions",
  16. actionsEnd: "actions--end",
  17. actionsStart: "actions--start",
  18. description: "description",
  19. handle: "handle",
  20. handleActivated: "handle--activated",
  21. highlight: "highlight",
  22. icon: "icon",
  23. iconDot: "icon-dot",
  24. label: "label",
  25. remove: "remove",
  26. title: "title",
  27. textContainer: "text-container"
  28. };
  29. const ICONS = {
  30. checked: "check",
  31. remove: "x"
  32. };
  33. const SLOTS = {
  34. actionsEnd: "actions-end",
  35. actionsStart: "actions-start"
  36. };
  37. const TEXT = {
  38. remove: "Remove"
  39. };
  40. const pickListItemCss = "@charset \"UTF-8\";@-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{margin:0px;margin-bottom:1px;-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;-ms-flex-align:stretch;align-items:stretch;background-color:var(--calcite-ui-foreground-1);font-size:var(--calcite-font-size--1);line-height:1rem;color:var(--calcite-ui-text-1);--tw-shadow:0 1px 0 var(--calcite-ui-border-3);--tw-shadow-colored:0 1px 0 var(--tw-shadow-color);-webkit-box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);-webkit-transition:background-color var(--calcite-animation-timing);transition:background-color var(--calcite-animation-timing);-webkit-animation:calcite-fade-in var(--calcite-animation-timing);animation:calcite-fade-in var(--calcite-animation-timing)}:host *{-webkit-box-sizing:border-box;box-sizing:border-box}.label{display:-ms-flexbox;display:flex;-ms-flex:1 1 auto;flex:1 1 auto;cursor:pointer;-ms-flex-align:center;align-items:center;background-color:transparent;outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out}.label:focus{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}.label:hover{background-color:var(--calcite-ui-foreground-2)}:host([non-interactive]:hover){background-color:var(--calcite-ui-foreground-1)}:host([non-interactive]) .label,:host([non-interactive]) .icon{pointer-events:none}.icon{margin-top:0px;margin-bottom:0px;display:-ms-flexbox;display:flex;cursor:pointer;-ms-flex-align:center;align-items:center;padding:0.25rem;color:var(--calcite-ui-brand);-ms-flex:0 0 auto;flex:0 0 auto;line-height:0}.icon:hover{background-color:var(--calcite-ui-foreground-2)}.icon-dot{display:-ms-flexbox;display:flex;width:1.5rem;-ms-flex-align:center;align-items:center;padding:0.5rem}.icon-dot:before{opacity:0;content:\"•\"}.icon calcite-icon{opacity:0}:host([selected]) .icon-dot:before,:host([selected]) .icon calcite-icon{-webkit-transition:opacity var(--calcite-animation-timing);transition:opacity var(--calcite-animation-timing);opacity:1}.text-container{pointer-events:none;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;-ms-flex-wrap:nowrap;flex-wrap:nowrap;overflow:hidden;padding-top:0.5rem;padding-bottom:0.5rem;padding-left:0.75rem;padding-right:0.75rem;font-size:var(--calcite-font-size--2);line-height:1.375;word-wrap:break-word;word-break:break-word}.title{font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-1)}.description{margin-top:0.125rem;font-weight:var(--calcite-font-weight-normal);color:var(--calcite-ui-text-3)}.actions{margin:0px;display:-ms-flexbox;display:flex;-ms-flex:0 1 auto;flex:0 1 auto;-ms-flex-align:stretch;align-items:stretch;-ms-flex-pack:end;justify-content:flex-end}.actions--start~.label{-webkit-padding-start:0.25rem;padding-inline-start:0.25rem}: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}";
  41. const PickListItem = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  42. constructor() {
  43. super();
  44. this.__registerHost();
  45. this.__attachShadow();
  46. this.calciteListItemChange = createEvent(this, "calciteListItemChange", 7);
  47. this.calciteListItemRemove = createEvent(this, "calciteListItemRemove", 7);
  48. this.calciteListItemPropsChange = createEvent(this, "calciteListItemPropsChange", 7);
  49. this.calciteListItemValueChange = createEvent(this, "calciteListItemValueChange", 7);
  50. /**
  51. * When true, the item cannot be clicked and is visually muted.
  52. */
  53. this.disabled = false;
  54. /**
  55. * When false, the item cannot be deselected by user interaction.
  56. */
  57. this.disableDeselect = false;
  58. /**
  59. * @internal When true, the item cannot be selected by user interaction.
  60. */
  61. this.nonInteractive = false;
  62. /**
  63. * Determines the icon SVG symbol that will be shown. Options are circle, square, grip or null.
  64. * @see [ICON_TYPES](https://github.com/Esri/calcite-components/blob/master/src/components/pick-list/resources.ts#L5)
  65. */
  66. this.icon = null;
  67. /**
  68. * Set this to true to display a remove action that removes the item from the list.
  69. */
  70. this.removable = false;
  71. /**
  72. * Set this to true to pre-select an item. Toggles when an item is checked/unchecked.
  73. */
  74. this.selected = false;
  75. /**
  76. * Used as an accessible label (aria-label) for the "remove item" action. Only applicable if removable is true.
  77. * @default "Remove"
  78. */
  79. this.intlRemove = TEXT.remove;
  80. // --------------------------------------------------------------------------
  81. //
  82. // Private Methods
  83. //
  84. // --------------------------------------------------------------------------
  85. this.pickListClickHandler = (event) => {
  86. if (this.disabled || (this.disableDeselect && this.selected) || this.nonInteractive) {
  87. return;
  88. }
  89. this.shiftPressed = event.shiftKey;
  90. this.selected = !this.selected;
  91. };
  92. this.pickListKeyDownHandler = (event) => {
  93. if (event.key === " ") {
  94. event.preventDefault();
  95. if ((this.disableDeselect && this.selected) || this.nonInteractive) {
  96. return;
  97. }
  98. this.selected = !this.selected;
  99. }
  100. };
  101. this.removeClickHandler = () => {
  102. this.calciteListItemRemove.emit();
  103. };
  104. }
  105. descriptionWatchHandler() {
  106. this.calciteListItemPropsChange.emit();
  107. }
  108. labelWatchHandler() {
  109. this.calciteListItemPropsChange.emit();
  110. }
  111. metadataWatchHandler() {
  112. this.calciteListItemPropsChange.emit();
  113. }
  114. selectedWatchHandler() {
  115. this.calciteListItemChange.emit({
  116. item: this.el,
  117. value: this.value,
  118. selected: this.selected,
  119. shiftPressed: this.shiftPressed
  120. });
  121. this.shiftPressed = false;
  122. }
  123. valueWatchHandler(newValue, oldValue) {
  124. this.calciteListItemValueChange.emit({ oldValue, newValue });
  125. }
  126. // --------------------------------------------------------------------------
  127. //
  128. // Lifecycle
  129. //
  130. // --------------------------------------------------------------------------
  131. connectedCallback() {
  132. connectConditionalSlotComponent(this);
  133. }
  134. disconnectedCallback() {
  135. disconnectConditionalSlotComponent(this);
  136. }
  137. componentDidRender() {
  138. updateHostInteraction(this, this.el.closest("calcite-pick-list") ? "managed" : false);
  139. }
  140. // --------------------------------------------------------------------------
  141. //
  142. // Public Methods
  143. //
  144. // --------------------------------------------------------------------------
  145. /**
  146. * Used to toggle the selection state. By default this won't trigger an event.
  147. * The first argument allows the value to be coerced, rather than swapping values.
  148. */
  149. async toggleSelected(coerce) {
  150. this.selected = typeof coerce === "boolean" ? coerce : !this.selected;
  151. }
  152. /** Sets focus on the component. */
  153. async setFocus() {
  154. var _a;
  155. (_a = this.focusEl) === null || _a === void 0 ? void 0 : _a.focus();
  156. }
  157. // --------------------------------------------------------------------------
  158. //
  159. // Render Methods
  160. //
  161. // --------------------------------------------------------------------------
  162. renderIcon() {
  163. const { icon } = this;
  164. if (!icon) {
  165. return null;
  166. }
  167. return (h("span", { class: {
  168. [CSS.icon]: true,
  169. [CSS.iconDot]: icon === ICON_TYPES.circle
  170. }, onClick: this.pickListClickHandler }, icon === ICON_TYPES.square ? h("calcite-icon", { icon: ICONS.checked, scale: "s" }) : null));
  171. }
  172. renderRemoveAction() {
  173. return this.removable ? (h("calcite-action", { class: CSS.remove, icon: ICONS.remove, onCalciteActionClick: this.removeClickHandler, slot: SLOTS.actionsEnd, text: this.intlRemove })) : null;
  174. }
  175. renderActionsStart() {
  176. const { el } = this;
  177. const hasActionsStart = getSlotted(el, SLOTS.actionsStart);
  178. return hasActionsStart ? (h("div", { class: { [CSS.actions]: true, [CSS.actionsStart]: true } }, h("slot", { name: SLOTS.actionsStart }))) : null;
  179. }
  180. renderActionsEnd() {
  181. const { el, removable } = this;
  182. const hasActionsEnd = getSlotted(el, SLOTS.actionsEnd);
  183. return hasActionsEnd || removable ? (h("div", { class: { [CSS.actions]: true, [CSS.actionsEnd]: true } }, h("slot", { name: SLOTS.actionsEnd }), this.renderRemoveAction())) : null;
  184. }
  185. render() {
  186. const { description, label } = this;
  187. return (h(Fragment, null, this.renderIcon(), this.renderActionsStart(), h("label", { "aria-label": label, class: CSS.label, onClick: this.pickListClickHandler, onKeyDown: this.pickListKeyDownHandler, ref: (focusEl) => (this.focusEl = focusEl), tabIndex: 0 }, h("div", { "aria-checked": toAriaBoolean(this.selected), class: CSS.textContainer, role: "menuitemcheckbox" }, h("span", { class: CSS.title }, label), description ? h("span", { class: CSS.description }, description) : null)), this.renderActionsEnd()));
  188. }
  189. get el() { return this; }
  190. static get watchers() { return {
  191. "description": ["descriptionWatchHandler"],
  192. "label": ["labelWatchHandler"],
  193. "metadata": ["metadataWatchHandler"],
  194. "selected": ["selectedWatchHandler"],
  195. "value": ["valueWatchHandler"]
  196. }; }
  197. static get style() { return pickListItemCss; }
  198. }, [1, "calcite-pick-list-item", {
  199. "description": [513],
  200. "disabled": [516],
  201. "disableDeselect": [4, "disable-deselect"],
  202. "nonInteractive": [516, "non-interactive"],
  203. "icon": [513],
  204. "label": [513],
  205. "metadata": [16],
  206. "removable": [516],
  207. "selected": [1540],
  208. "intlRemove": [513, "intl-remove"],
  209. "value": [8],
  210. "toggleSelected": [64],
  211. "setFocus": [64]
  212. }]);
  213. function defineCustomElement() {
  214. if (typeof customElements === "undefined") {
  215. return;
  216. }
  217. const components = ["calcite-pick-list-item", "calcite-action", "calcite-icon", "calcite-loader"];
  218. components.forEach(tagName => { switch (tagName) {
  219. case "calcite-pick-list-item":
  220. if (!customElements.get(tagName)) {
  221. customElements.define(tagName, PickListItem);
  222. }
  223. break;
  224. case "calcite-action":
  225. if (!customElements.get(tagName)) {
  226. defineCustomElement$3();
  227. }
  228. break;
  229. case "calcite-icon":
  230. if (!customElements.get(tagName)) {
  231. defineCustomElement$2();
  232. }
  233. break;
  234. case "calcite-loader":
  235. if (!customElements.get(tagName)) {
  236. defineCustomElement$1();
  237. }
  238. break;
  239. } });
  240. }
  241. defineCustomElement();
  242. export { CSS as C, PickListItem as P, SLOTS as S, defineCustomElement as d };