handle.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 { h } from "@stencil/core";
  7. import { toAriaBoolean } from "../../utils/dom";
  8. import { CSS, ICONS } from "./resources";
  9. export class Handle {
  10. constructor() {
  11. // --------------------------------------------------------------------------
  12. //
  13. // Properties
  14. //
  15. // --------------------------------------------------------------------------
  16. /**
  17. * @internal
  18. */
  19. this.activated = false;
  20. /**
  21. * Value for the button title attribute
  22. */
  23. this.textTitle = "handle";
  24. // --------------------------------------------------------------------------
  25. //
  26. // Private Methods
  27. //
  28. // --------------------------------------------------------------------------
  29. this.handleKeyDown = (event) => {
  30. switch (event.key) {
  31. case " ":
  32. this.activated = !this.activated;
  33. event.preventDefault();
  34. break;
  35. case "ArrowUp":
  36. case "ArrowDown":
  37. if (!this.activated) {
  38. return;
  39. }
  40. event.preventDefault();
  41. const direction = event.key.toLowerCase().replace("arrow", "");
  42. this.calciteHandleNudge.emit({ handle: this.el, direction });
  43. break;
  44. }
  45. };
  46. this.handleBlur = () => {
  47. this.activated = false;
  48. };
  49. }
  50. // --------------------------------------------------------------------------
  51. //
  52. // Methods
  53. //
  54. // --------------------------------------------------------------------------
  55. /** Sets focus on the component. */
  56. async setFocus() {
  57. var _a;
  58. (_a = this.handleButton) === null || _a === void 0 ? void 0 : _a.focus();
  59. }
  60. // --------------------------------------------------------------------------
  61. //
  62. // Render Methods
  63. //
  64. // --------------------------------------------------------------------------
  65. render() {
  66. return (
  67. // Needs to be a span because of https://github.com/SortableJS/Sortable/issues/1486
  68. h("span", { "aria-pressed": toAriaBoolean(this.activated), class: { [CSS.handle]: true, [CSS.handleActivated]: this.activated }, onBlur: this.handleBlur, onKeyDown: this.handleKeyDown, ref: (el) => {
  69. this.handleButton = el;
  70. }, role: "button", tabindex: "0", title: this.textTitle }, h("calcite-icon", { icon: ICONS.drag, scale: "s" })));
  71. }
  72. static get is() { return "calcite-handle"; }
  73. static get encapsulation() { return "shadow"; }
  74. static get originalStyleUrls() {
  75. return {
  76. "$": ["handle.scss"]
  77. };
  78. }
  79. static get styleUrls() {
  80. return {
  81. "$": ["handle.css"]
  82. };
  83. }
  84. static get properties() {
  85. return {
  86. "activated": {
  87. "type": "boolean",
  88. "mutable": true,
  89. "complexType": {
  90. "original": "boolean",
  91. "resolved": "boolean",
  92. "references": {}
  93. },
  94. "required": false,
  95. "optional": false,
  96. "docs": {
  97. "tags": [{
  98. "name": "internal",
  99. "text": undefined
  100. }],
  101. "text": ""
  102. },
  103. "attribute": "activated",
  104. "reflect": true,
  105. "defaultValue": "false"
  106. },
  107. "textTitle": {
  108. "type": "string",
  109. "mutable": false,
  110. "complexType": {
  111. "original": "string",
  112. "resolved": "string",
  113. "references": {}
  114. },
  115. "required": false,
  116. "optional": false,
  117. "docs": {
  118. "tags": [],
  119. "text": "Value for the button title attribute"
  120. },
  121. "attribute": "text-title",
  122. "reflect": true,
  123. "defaultValue": "\"handle\""
  124. }
  125. };
  126. }
  127. static get events() {
  128. return [{
  129. "method": "calciteHandleNudge",
  130. "name": "calciteHandleNudge",
  131. "bubbles": true,
  132. "cancelable": false,
  133. "composed": true,
  134. "docs": {
  135. "tags": [],
  136. "text": "Emitted when the handle is activated and the up or down arrow key is pressed.\n\n**Note:**: The `handle` event payload prop is deprecated, please use the event's `target`/`currentTarget` instead"
  137. },
  138. "complexType": {
  139. "original": "DeprecatedEventPayload",
  140. "resolved": "any",
  141. "references": {
  142. "DeprecatedEventPayload": {
  143. "location": "import",
  144. "path": "../interfaces"
  145. }
  146. }
  147. }
  148. }];
  149. }
  150. static get methods() {
  151. return {
  152. "setFocus": {
  153. "complexType": {
  154. "signature": "() => Promise<void>",
  155. "parameters": [],
  156. "references": {
  157. "Promise": {
  158. "location": "global"
  159. }
  160. },
  161. "return": "Promise<void>"
  162. },
  163. "docs": {
  164. "text": "Sets focus on the component.",
  165. "tags": []
  166. }
  167. }
  168. };
  169. }
  170. static get elementRef() { return "el"; }
  171. }