handle.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 { Component, Element, Event, Method, Prop, 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 - stores the activated state of the drag handle.
  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. break;
  34. case "ArrowUp":
  35. case "ArrowDown":
  36. if (!this.activated) {
  37. return;
  38. }
  39. const direction = event.key.toLowerCase().replace("arrow", "");
  40. this.calciteHandleNudge.emit({ handle: this.el, direction });
  41. break;
  42. }
  43. };
  44. this.handleBlur = () => {
  45. this.activated = false;
  46. };
  47. }
  48. // --------------------------------------------------------------------------
  49. //
  50. // Methods
  51. //
  52. // --------------------------------------------------------------------------
  53. /** Sets focus on the component. */
  54. async setFocus() {
  55. this.handleButton.focus();
  56. }
  57. // --------------------------------------------------------------------------
  58. //
  59. // Render Methods
  60. //
  61. // --------------------------------------------------------------------------
  62. render() {
  63. return (
  64. // Needs to be a span because of https://github.com/SortableJS/Sortable/issues/1486
  65. h("span", { "aria-pressed": toAriaBoolean(this.activated), class: { [CSS.handle]: true, [CSS.handleActivated]: this.activated }, onBlur: this.handleBlur, onKeyDown: this.handleKeyDown, ref: (el) => {
  66. this.handleButton = el;
  67. }, role: "button", tabindex: "0", title: this.textTitle },
  68. h("calcite-icon", { icon: ICONS.drag, scale: "s" })));
  69. }
  70. static get is() { return "calcite-handle"; }
  71. static get encapsulation() { return "shadow"; }
  72. static get originalStyleUrls() { return {
  73. "$": ["handle.scss"]
  74. }; }
  75. static get styleUrls() { return {
  76. "$": ["handle.css"]
  77. }; }
  78. static get properties() { return {
  79. "activated": {
  80. "type": "boolean",
  81. "mutable": true,
  82. "complexType": {
  83. "original": "boolean",
  84. "resolved": "boolean",
  85. "references": {}
  86. },
  87. "required": false,
  88. "optional": false,
  89. "docs": {
  90. "tags": [{
  91. "name": "internal",
  92. "text": "- stores the activated state of the drag handle."
  93. }],
  94. "text": ""
  95. },
  96. "attribute": "activated",
  97. "reflect": true,
  98. "defaultValue": "false"
  99. },
  100. "textTitle": {
  101. "type": "string",
  102. "mutable": false,
  103. "complexType": {
  104. "original": "string",
  105. "resolved": "string",
  106. "references": {}
  107. },
  108. "required": false,
  109. "optional": false,
  110. "docs": {
  111. "tags": [],
  112. "text": "Value for the button title attribute"
  113. },
  114. "attribute": "text-title",
  115. "reflect": true,
  116. "defaultValue": "\"handle\""
  117. }
  118. }; }
  119. static get events() { return [{
  120. "method": "calciteHandleNudge",
  121. "name": "calciteHandleNudge",
  122. "bubbles": true,
  123. "cancelable": true,
  124. "composed": true,
  125. "docs": {
  126. "tags": [],
  127. "text": "Emitted when the the handle is activated and the up or down arrow key is pressed."
  128. },
  129. "complexType": {
  130. "original": "any",
  131. "resolved": "any",
  132. "references": {}
  133. }
  134. }]; }
  135. static get methods() { return {
  136. "setFocus": {
  137. "complexType": {
  138. "signature": "() => Promise<void>",
  139. "parameters": [],
  140. "references": {
  141. "Promise": {
  142. "location": "global"
  143. }
  144. },
  145. "return": "Promise<void>"
  146. },
  147. "docs": {
  148. "text": "Sets focus on the component.",
  149. "tags": []
  150. }
  151. }
  152. }; }
  153. static get elementRef() { return "el"; }
  154. }