pick-list-item.js 12 KB

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