shell-panel.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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, forceUpdate } from "@stencil/core";
  7. import { CSS, SLOTS, TEXT } from "./resources";
  8. import { getSlotted, getElementDir, isPrimaryPointerButton } from "../../utils/dom";
  9. import { clamp } from "../../utils/math";
  10. import { connectConditionalSlotComponent, disconnectConditionalSlotComponent } from "../../utils/conditionalSlot";
  11. /**
  12. * @slot - A slot for adding content to the component.
  13. * @slot action-bar - A slot for adding a `calcite-action-bar` to the component.
  14. */
  15. export class ShellPanel {
  16. constructor() {
  17. // --------------------------------------------------------------------------
  18. //
  19. // Properties
  20. //
  21. // --------------------------------------------------------------------------
  22. /**
  23. * When `true`, hides the component's content area.
  24. */
  25. this.collapsed = false;
  26. /**
  27. * When `true`, the content area displays like a floating panel.
  28. */
  29. this.detached = false;
  30. /**
  31. * When `detached`, specifies the maximum height of the component.
  32. */
  33. this.detachedHeightScale = "l";
  34. /**
  35. * Specifies the width of the component's content area.
  36. */
  37. this.widthScale = "m";
  38. /**
  39. * Accessible name for the resize separator.
  40. *
  41. * @default "Resize"
  42. */
  43. this.intlResize = TEXT.resize;
  44. /**
  45. * When `true` and not `detached`, the component's content area is resizable.
  46. */
  47. this.resizable = false;
  48. this.contentWidth = null;
  49. this.initialContentWidth = null;
  50. this.initialClientX = null;
  51. this.contentWidthMax = null;
  52. this.contentWidthMin = null;
  53. this.step = 1;
  54. this.stepMultiplier = 10;
  55. this.storeContentEl = (contentEl) => {
  56. this.contentEl = contentEl;
  57. };
  58. this.getKeyAdjustedWidth = (event) => {
  59. const { key } = event;
  60. const { el, step, stepMultiplier, contentWidthMin, contentWidthMax, initialContentWidth, position } = this;
  61. const multipliedStep = step * stepMultiplier;
  62. const MOVEMENT_KEYS = [
  63. "ArrowUp",
  64. "ArrowDown",
  65. "ArrowLeft",
  66. "ArrowRight",
  67. "Home",
  68. "End",
  69. "PageUp",
  70. "PageDown"
  71. ];
  72. if (MOVEMENT_KEYS.indexOf(key) > -1) {
  73. event.preventDefault();
  74. }
  75. const dir = getElementDir(el);
  76. const directionKeys = ["ArrowLeft", "ArrowRight"];
  77. const directionFactor = dir === "rtl" && directionKeys.includes(key) ? -1 : 1;
  78. const increaseKeys = key === "ArrowUp" ||
  79. (position === "end" ? key === directionKeys[0] : key === directionKeys[1]);
  80. if (increaseKeys) {
  81. const stepValue = event.shiftKey ? multipliedStep : step;
  82. return initialContentWidth + directionFactor * stepValue;
  83. }
  84. const decreaseKeys = key === "ArrowDown" ||
  85. (position === "end" ? key === directionKeys[1] : key === directionKeys[0]);
  86. if (decreaseKeys) {
  87. const stepValue = event.shiftKey ? multipliedStep : step;
  88. return initialContentWidth - directionFactor * stepValue;
  89. }
  90. if (typeof contentWidthMin === "number" && key === "Home") {
  91. return contentWidthMin;
  92. }
  93. if (typeof contentWidthMax === "number" && key === "End") {
  94. return contentWidthMax;
  95. }
  96. if (key === "PageDown") {
  97. return initialContentWidth - multipliedStep;
  98. }
  99. if (key === "PageUp") {
  100. return initialContentWidth + multipliedStep;
  101. }
  102. return null;
  103. };
  104. this.separatorKeyDown = (event) => {
  105. this.setInitialContentWidth();
  106. const width = this.getKeyAdjustedWidth(event);
  107. if (typeof width === "number") {
  108. this.setContentWidth(width);
  109. }
  110. };
  111. this.separatorPointerMove = (event) => {
  112. event.preventDefault();
  113. const { el, initialContentWidth, position, initialClientX } = this;
  114. const offset = event.clientX - initialClientX;
  115. const dir = getElementDir(el);
  116. const adjustmentDirection = dir === "rtl" ? -1 : 1;
  117. const adjustedOffset = position === "end" ? -adjustmentDirection * offset : adjustmentDirection * offset;
  118. const width = initialContentWidth + adjustedOffset;
  119. this.setContentWidth(width);
  120. };
  121. this.separatorPointerUp = (event) => {
  122. if (!isPrimaryPointerButton(event)) {
  123. return;
  124. }
  125. event.preventDefault();
  126. document.removeEventListener("pointerup", this.separatorPointerUp);
  127. document.removeEventListener("pointermove", this.separatorPointerMove);
  128. };
  129. this.setInitialContentWidth = () => {
  130. var _a;
  131. this.initialContentWidth = (_a = this.contentEl) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect().width;
  132. };
  133. this.separatorPointerDown = (event) => {
  134. if (!isPrimaryPointerButton(event)) {
  135. return;
  136. }
  137. event.preventDefault();
  138. const { separatorEl } = this;
  139. separatorEl && document.activeElement !== separatorEl && separatorEl.focus();
  140. this.setInitialContentWidth();
  141. this.initialClientX = event.clientX;
  142. document.addEventListener("pointerup", this.separatorPointerUp);
  143. document.addEventListener("pointermove", this.separatorPointerMove);
  144. };
  145. this.connectSeparator = (separatorEl) => {
  146. this.disconnectSeparator();
  147. this.separatorEl = separatorEl;
  148. separatorEl.addEventListener("pointerdown", this.separatorPointerDown);
  149. };
  150. this.disconnectSeparator = () => {
  151. var _a;
  152. (_a = this.separatorEl) === null || _a === void 0 ? void 0 : _a.removeEventListener("pointerdown", this.separatorPointerDown);
  153. };
  154. }
  155. watchHandler() {
  156. this.calciteShellPanelToggle.emit();
  157. }
  158. //--------------------------------------------------------------------------
  159. //
  160. // Lifecycle
  161. //
  162. //--------------------------------------------------------------------------
  163. connectedCallback() {
  164. connectConditionalSlotComponent(this);
  165. }
  166. disconnectedCallback() {
  167. disconnectConditionalSlotComponent(this);
  168. this.disconnectSeparator();
  169. }
  170. componentDidLoad() {
  171. this.updateAriaValues();
  172. }
  173. // --------------------------------------------------------------------------
  174. //
  175. // Render Methods
  176. //
  177. // --------------------------------------------------------------------------
  178. renderHeader() {
  179. const { el } = this;
  180. const hasHeader = getSlotted(el, SLOTS.header);
  181. return hasHeader ? (h("div", { class: CSS.contentHeader, key: "header" }, h("slot", { name: SLOTS.header }))) : null;
  182. }
  183. render() {
  184. const { collapsed, detached, position, initialContentWidth, contentWidth, contentWidthMax, contentWidthMin, intlResize, resizable } = this;
  185. const allowResizing = !detached && resizable;
  186. const contentNode = (h("div", { class: { [CSS.content]: true, [CSS.contentDetached]: detached }, hidden: collapsed, key: "content", ref: this.storeContentEl, style: allowResizing && contentWidth ? { width: `${contentWidth}px` } : null }, this.renderHeader(), h("div", { class: CSS.contentBody }, h("slot", null))));
  187. const separatorNode = allowResizing ? (h("div", { "aria-label": intlResize, "aria-orientation": "horizontal", "aria-valuemax": contentWidthMax, "aria-valuemin": contentWidthMin, "aria-valuenow": contentWidth !== null && contentWidth !== void 0 ? contentWidth : initialContentWidth, class: CSS.separator, key: "separator", onKeyDown: this.separatorKeyDown, ref: this.connectSeparator, role: "separator", tabIndex: 0, "touch-action": "none" })) : null;
  188. const actionBarNode = h("slot", { key: "action-bar", name: SLOTS.actionBar });
  189. const mainNodes = [actionBarNode, contentNode, separatorNode];
  190. if (position === "end") {
  191. mainNodes.reverse();
  192. }
  193. return h("div", { class: { [CSS.container]: true } }, mainNodes);
  194. }
  195. // --------------------------------------------------------------------------
  196. //
  197. // private Methods
  198. //
  199. // --------------------------------------------------------------------------
  200. setContentWidth(width) {
  201. const { contentWidthMax, contentWidthMin } = this;
  202. const roundedWidth = Math.round(width);
  203. this.contentWidth =
  204. typeof contentWidthMax === "number" && typeof contentWidthMin === "number"
  205. ? clamp(roundedWidth, contentWidthMin, contentWidthMax)
  206. : roundedWidth;
  207. }
  208. updateAriaValues() {
  209. const { contentEl } = this;
  210. const computedStyle = contentEl && getComputedStyle(contentEl);
  211. if (!computedStyle) {
  212. return;
  213. }
  214. const max = parseInt(computedStyle.getPropertyValue("max-width"), 10);
  215. const min = parseInt(computedStyle.getPropertyValue("min-width"), 10);
  216. const valueNow = parseInt(computedStyle.getPropertyValue("width"), 10);
  217. if (typeof valueNow === "number" && !isNaN(valueNow)) {
  218. this.initialContentWidth = valueNow;
  219. }
  220. if (typeof max === "number" && !isNaN(max)) {
  221. this.contentWidthMax = max;
  222. }
  223. if (typeof min === "number" && !isNaN(min)) {
  224. this.contentWidthMin = min;
  225. }
  226. forceUpdate(this);
  227. }
  228. static get is() { return "calcite-shell-panel"; }
  229. static get encapsulation() { return "shadow"; }
  230. static get originalStyleUrls() {
  231. return {
  232. "$": ["shell-panel.scss"]
  233. };
  234. }
  235. static get styleUrls() {
  236. return {
  237. "$": ["shell-panel.css"]
  238. };
  239. }
  240. static get properties() {
  241. return {
  242. "collapsed": {
  243. "type": "boolean",
  244. "mutable": false,
  245. "complexType": {
  246. "original": "boolean",
  247. "resolved": "boolean",
  248. "references": {}
  249. },
  250. "required": false,
  251. "optional": false,
  252. "docs": {
  253. "tags": [],
  254. "text": "When `true`, hides the component's content area."
  255. },
  256. "attribute": "collapsed",
  257. "reflect": true,
  258. "defaultValue": "false"
  259. },
  260. "detached": {
  261. "type": "boolean",
  262. "mutable": false,
  263. "complexType": {
  264. "original": "boolean",
  265. "resolved": "boolean",
  266. "references": {}
  267. },
  268. "required": false,
  269. "optional": false,
  270. "docs": {
  271. "tags": [],
  272. "text": "When `true`, the content area displays like a floating panel."
  273. },
  274. "attribute": "detached",
  275. "reflect": true,
  276. "defaultValue": "false"
  277. },
  278. "detachedHeightScale": {
  279. "type": "string",
  280. "mutable": false,
  281. "complexType": {
  282. "original": "Scale",
  283. "resolved": "\"l\" | \"m\" | \"s\"",
  284. "references": {
  285. "Scale": {
  286. "location": "import",
  287. "path": "../interfaces"
  288. }
  289. }
  290. },
  291. "required": false,
  292. "optional": false,
  293. "docs": {
  294. "tags": [],
  295. "text": "When `detached`, specifies the maximum height of the component."
  296. },
  297. "attribute": "detached-height-scale",
  298. "reflect": true,
  299. "defaultValue": "\"l\""
  300. },
  301. "widthScale": {
  302. "type": "string",
  303. "mutable": false,
  304. "complexType": {
  305. "original": "Scale",
  306. "resolved": "\"l\" | \"m\" | \"s\"",
  307. "references": {
  308. "Scale": {
  309. "location": "import",
  310. "path": "../interfaces"
  311. }
  312. }
  313. },
  314. "required": false,
  315. "optional": false,
  316. "docs": {
  317. "tags": [],
  318. "text": "Specifies the width of the component's content area."
  319. },
  320. "attribute": "width-scale",
  321. "reflect": true,
  322. "defaultValue": "\"m\""
  323. },
  324. "position": {
  325. "type": "string",
  326. "mutable": false,
  327. "complexType": {
  328. "original": "Position",
  329. "resolved": "\"end\" | \"start\"",
  330. "references": {
  331. "Position": {
  332. "location": "import",
  333. "path": "../interfaces"
  334. }
  335. }
  336. },
  337. "required": false,
  338. "optional": false,
  339. "docs": {
  340. "tags": [],
  341. "text": "Specifies the component's position. Will be flipped when the element direction is right-to-left (`\"rtl\"`)."
  342. },
  343. "attribute": "position",
  344. "reflect": true
  345. },
  346. "intlResize": {
  347. "type": "string",
  348. "mutable": false,
  349. "complexType": {
  350. "original": "string",
  351. "resolved": "string",
  352. "references": {}
  353. },
  354. "required": false,
  355. "optional": false,
  356. "docs": {
  357. "tags": [{
  358. "name": "default",
  359. "text": "\"Resize\""
  360. }],
  361. "text": "Accessible name for the resize separator."
  362. },
  363. "attribute": "intl-resize",
  364. "reflect": false,
  365. "defaultValue": "TEXT.resize"
  366. },
  367. "resizable": {
  368. "type": "boolean",
  369. "mutable": false,
  370. "complexType": {
  371. "original": "boolean",
  372. "resolved": "boolean",
  373. "references": {}
  374. },
  375. "required": false,
  376. "optional": false,
  377. "docs": {
  378. "tags": [],
  379. "text": "When `true` and not `detached`, the component's content area is resizable."
  380. },
  381. "attribute": "resizable",
  382. "reflect": true,
  383. "defaultValue": "false"
  384. }
  385. };
  386. }
  387. static get states() {
  388. return {
  389. "contentWidth": {}
  390. };
  391. }
  392. static get events() {
  393. return [{
  394. "method": "calciteShellPanelToggle",
  395. "name": "calciteShellPanelToggle",
  396. "bubbles": true,
  397. "cancelable": false,
  398. "composed": true,
  399. "docs": {
  400. "tags": [{
  401. "name": "deprecated",
  402. "text": "use a `ResizeObserver` on the component to listen for changes to its size."
  403. }],
  404. "text": "Emitted when collapse has been toggled."
  405. },
  406. "complexType": {
  407. "original": "void",
  408. "resolved": "void",
  409. "references": {}
  410. }
  411. }];
  412. }
  413. static get elementRef() { return "el"; }
  414. static get watchers() {
  415. return [{
  416. "propName": "collapsed",
  417. "methodName": "watchHandler"
  418. }];
  419. }
  420. }