calcite-shell-panel.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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, forceUpdate } from '@stencil/core/internal/client/index.js';
  7. import { c as getElementDir, i as isPrimaryPointerButton, b as getSlotted } from './dom.js';
  8. import { c as clamp } from './math.js';
  9. import { c as connectConditionalSlotComponent, d as disconnectConditionalSlotComponent } from './conditionalSlot.js';
  10. const CSS = {
  11. container: "container",
  12. content: "content",
  13. contentHeader: "content__header",
  14. contentBody: "content__body",
  15. contentDetached: "content--detached",
  16. separator: "separator"
  17. };
  18. const SLOTS = {
  19. actionBar: "action-bar",
  20. header: "header"
  21. };
  22. const TEXT = {
  23. resize: "Resize"
  24. };
  25. const shellPanelCss = "@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{pointer-events:none;display:flex;flex:0 1 auto;align-items:stretch;--calcite-shell-panel-detached-max-height:unset}.container{pointer-events:none;box-sizing:border-box;display:flex;flex:1 1 auto;align-items:stretch;background-color:transparent;font-size:var(--calcite-font-size--1);color:var(--calcite-ui-text-2)}.container *{box-sizing:border-box}:host(:hover) .separator:not(:hover):not(:focus),:host(:focus-within) .separator:not(:hover):not(:focus){opacity:1;background-color:var(--calcite-ui-border-3)}.separator{pointer-events:auto;position:absolute;inset-block:0px;display:flex;block-size:100%;inline-size:0.125rem;background-color:transparent;opacity:0;transition:all var(--calcite-animation-timing) ease-in-out 0s, outline 0s, outline-offset 0s;cursor:col-resize;outline:none}.separator:hover{opacity:1;background-color:var(--calcite-ui-border-2)}.separator:focus{background-color:var(--calcite-ui-brand);opacity:1}:host([position=start]) .separator{inset-inline-end:-2px}:host([position=end]) .separator{inset-inline-start:-2px}::slotted(calcite-panel),::slotted(calcite-flow){block-size:100%;inline-size:100%;flex:1 1 auto;max-block-size:unset;max-inline-size:unset}::slotted(.calcite-match-height){display:flex;flex:1 1 auto;overflow:hidden}.content{pointer-events:auto;display:flex;flex-direction:column;flex-wrap:nowrap;align-items:stretch;align-self:stretch;background-color:var(--calcite-ui-background);padding:0px;inline-size:var(--calcite-shell-panel-width);max-inline-size:var(--calcite-shell-panel-max-width);min-inline-size:var(--calcite-shell-panel-min-width);transition:max-block-size var(--calcite-animation-timing), max-inline-size var(--calcite-animation-timing)}.content__header{display:flex;flex:0 1 auto;flex-direction:column;flex-wrap:nowrap;align-items:stretch}.content__body{display:flex;flex:1 1 auto;flex-direction:column;overflow:hidden}:host([width-scale=s]) .content{--calcite-shell-panel-width:calc(var(--calcite-panel-width-multiplier) * 12vw);--calcite-shell-panel-max-width:calc(var(--calcite-panel-width-multiplier) * 300px);--calcite-shell-panel-min-width:calc(var(--calcite-panel-width-multiplier) * 150px)}:host([width-scale=m]) .content{--calcite-shell-panel-width:calc(var(--calcite-panel-width-multiplier) * 20vw);--calcite-shell-panel-max-width:calc(var(--calcite-panel-width-multiplier) * 420px);--calcite-shell-panel-min-width:calc(var(--calcite-panel-width-multiplier) * 240px)}:host([width-scale=l]) .content{--calcite-shell-panel-width:calc(var(--calcite-panel-width-multiplier) * 45vw);--calcite-shell-panel-max-width:calc(var(--calcite-panel-width-multiplier) * 680px);--calcite-shell-panel-min-width:calc(var(--calcite-panel-width-multiplier) * 340px)}:host([detached-height-scale=s]) .content--detached{--calcite-shell-panel-detached-max-height:40vh}:host([detached-height-scale=m]) .content--detached{--calcite-shell-panel-detached-max-height:60vh}:host([detached-height-scale=l]) .content--detached{--calcite-shell-panel-detached-max-height:80vh}.content--detached{margin-inline:0.5rem;margin-block:0.5rem auto;block-size:auto;overflow:hidden;border-radius:0.25rem;--tw-shadow:0 4px 8px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -1px rgba(0, 0, 0, 0.04);--tw-shadow-colored:0 4px 8px -1px var(--tw-shadow-color), 0 2px 4px -1px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);max-block-size:var(--calcite-shell-panel-detached-max-height)}.content--detached ::slotted(calcite-panel),.content--detached ::slotted(calcite-flow){max-block-size:unset}:host([position=start]) .content--detached ::slotted(calcite-panel),:host([position=start]) .content--detached ::slotted(calcite-flow),:host([position=end]) .content--detached ::slotted(calcite-panel),:host([position=end]) .content--detached ::slotted(calcite-flow){border-style:none}.content[hidden]{display:none}slot[name=action-bar]::slotted(calcite-action-bar),.content ::slotted(calcite-flow),.content ::slotted(calcite-panel:not([dismissed])){border-width:1px;border-style:solid;border-color:var(--calcite-ui-border-3)}:host([position=start]) slot[name=action-bar]::slotted(calcite-action-bar),:host([position=start]) .content ::slotted(calcite-flow),:host([position=start]) .content ::slotted(calcite-panel){border-inline-start:none}:host([position=end]) slot[name=action-bar]::slotted(calcite-action-bar),:host([position=end]) .content ::slotted(calcite-flow),:host([position=end]) .content ::slotted(calcite-panel){border-inline-end:none}";
  26. const ShellPanel = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  27. constructor() {
  28. super();
  29. this.__registerHost();
  30. this.__attachShadow();
  31. this.calciteShellPanelToggle = createEvent(this, "calciteShellPanelToggle", 6);
  32. // --------------------------------------------------------------------------
  33. //
  34. // Properties
  35. //
  36. // --------------------------------------------------------------------------
  37. /**
  38. * When `true`, hides the component's content area.
  39. */
  40. this.collapsed = false;
  41. /**
  42. * When `true`, the content area displays like a floating panel.
  43. */
  44. this.detached = false;
  45. /**
  46. * When `detached`, specifies the maximum height of the component.
  47. */
  48. this.detachedHeightScale = "l";
  49. /**
  50. * Specifies the width of the component's content area.
  51. */
  52. this.widthScale = "m";
  53. /**
  54. * Accessible name for the resize separator.
  55. *
  56. * @default "Resize"
  57. */
  58. this.intlResize = TEXT.resize;
  59. /**
  60. * When `true` and not `detached`, the component's content area is resizable.
  61. */
  62. this.resizable = false;
  63. this.contentWidth = null;
  64. this.initialContentWidth = null;
  65. this.initialClientX = null;
  66. this.contentWidthMax = null;
  67. this.contentWidthMin = null;
  68. this.step = 1;
  69. this.stepMultiplier = 10;
  70. this.storeContentEl = (contentEl) => {
  71. this.contentEl = contentEl;
  72. };
  73. this.getKeyAdjustedWidth = (event) => {
  74. const { key } = event;
  75. const { el, step, stepMultiplier, contentWidthMin, contentWidthMax, initialContentWidth, position } = this;
  76. const multipliedStep = step * stepMultiplier;
  77. const MOVEMENT_KEYS = [
  78. "ArrowUp",
  79. "ArrowDown",
  80. "ArrowLeft",
  81. "ArrowRight",
  82. "Home",
  83. "End",
  84. "PageUp",
  85. "PageDown"
  86. ];
  87. if (MOVEMENT_KEYS.indexOf(key) > -1) {
  88. event.preventDefault();
  89. }
  90. const dir = getElementDir(el);
  91. const directionKeys = ["ArrowLeft", "ArrowRight"];
  92. const directionFactor = dir === "rtl" && directionKeys.includes(key) ? -1 : 1;
  93. const increaseKeys = key === "ArrowUp" ||
  94. (position === "end" ? key === directionKeys[0] : key === directionKeys[1]);
  95. if (increaseKeys) {
  96. const stepValue = event.shiftKey ? multipliedStep : step;
  97. return initialContentWidth + directionFactor * stepValue;
  98. }
  99. const decreaseKeys = key === "ArrowDown" ||
  100. (position === "end" ? key === directionKeys[1] : key === directionKeys[0]);
  101. if (decreaseKeys) {
  102. const stepValue = event.shiftKey ? multipliedStep : step;
  103. return initialContentWidth - directionFactor * stepValue;
  104. }
  105. if (typeof contentWidthMin === "number" && key === "Home") {
  106. return contentWidthMin;
  107. }
  108. if (typeof contentWidthMax === "number" && key === "End") {
  109. return contentWidthMax;
  110. }
  111. if (key === "PageDown") {
  112. return initialContentWidth - multipliedStep;
  113. }
  114. if (key === "PageUp") {
  115. return initialContentWidth + multipliedStep;
  116. }
  117. return null;
  118. };
  119. this.separatorKeyDown = (event) => {
  120. this.setInitialContentWidth();
  121. const width = this.getKeyAdjustedWidth(event);
  122. if (typeof width === "number") {
  123. this.setContentWidth(width);
  124. }
  125. };
  126. this.separatorPointerMove = (event) => {
  127. event.preventDefault();
  128. const { el, initialContentWidth, position, initialClientX } = this;
  129. const offset = event.clientX - initialClientX;
  130. const dir = getElementDir(el);
  131. const adjustmentDirection = dir === "rtl" ? -1 : 1;
  132. const adjustedOffset = position === "end" ? -adjustmentDirection * offset : adjustmentDirection * offset;
  133. const width = initialContentWidth + adjustedOffset;
  134. this.setContentWidth(width);
  135. };
  136. this.separatorPointerUp = (event) => {
  137. if (!isPrimaryPointerButton(event)) {
  138. return;
  139. }
  140. event.preventDefault();
  141. document.removeEventListener("pointerup", this.separatorPointerUp);
  142. document.removeEventListener("pointermove", this.separatorPointerMove);
  143. };
  144. this.setInitialContentWidth = () => {
  145. var _a;
  146. this.initialContentWidth = (_a = this.contentEl) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect().width;
  147. };
  148. this.separatorPointerDown = (event) => {
  149. if (!isPrimaryPointerButton(event)) {
  150. return;
  151. }
  152. event.preventDefault();
  153. const { separatorEl } = this;
  154. separatorEl && document.activeElement !== separatorEl && separatorEl.focus();
  155. this.setInitialContentWidth();
  156. this.initialClientX = event.clientX;
  157. document.addEventListener("pointerup", this.separatorPointerUp);
  158. document.addEventListener("pointermove", this.separatorPointerMove);
  159. };
  160. this.connectSeparator = (separatorEl) => {
  161. this.disconnectSeparator();
  162. this.separatorEl = separatorEl;
  163. separatorEl.addEventListener("pointerdown", this.separatorPointerDown);
  164. };
  165. this.disconnectSeparator = () => {
  166. var _a;
  167. (_a = this.separatorEl) === null || _a === void 0 ? void 0 : _a.removeEventListener("pointerdown", this.separatorPointerDown);
  168. };
  169. }
  170. watchHandler() {
  171. this.calciteShellPanelToggle.emit();
  172. }
  173. //--------------------------------------------------------------------------
  174. //
  175. // Lifecycle
  176. //
  177. //--------------------------------------------------------------------------
  178. connectedCallback() {
  179. connectConditionalSlotComponent(this);
  180. }
  181. disconnectedCallback() {
  182. disconnectConditionalSlotComponent(this);
  183. this.disconnectSeparator();
  184. }
  185. componentDidLoad() {
  186. this.updateAriaValues();
  187. }
  188. // --------------------------------------------------------------------------
  189. //
  190. // Render Methods
  191. //
  192. // --------------------------------------------------------------------------
  193. renderHeader() {
  194. const { el } = this;
  195. const hasHeader = getSlotted(el, SLOTS.header);
  196. return hasHeader ? (h("div", { class: CSS.contentHeader, key: "header" }, h("slot", { name: SLOTS.header }))) : null;
  197. }
  198. render() {
  199. const { collapsed, detached, position, initialContentWidth, contentWidth, contentWidthMax, contentWidthMin, intlResize, resizable } = this;
  200. const allowResizing = !detached && resizable;
  201. 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))));
  202. 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;
  203. const actionBarNode = h("slot", { key: "action-bar", name: SLOTS.actionBar });
  204. const mainNodes = [actionBarNode, contentNode, separatorNode];
  205. if (position === "end") {
  206. mainNodes.reverse();
  207. }
  208. return h("div", { class: { [CSS.container]: true } }, mainNodes);
  209. }
  210. // --------------------------------------------------------------------------
  211. //
  212. // private Methods
  213. //
  214. // --------------------------------------------------------------------------
  215. setContentWidth(width) {
  216. const { contentWidthMax, contentWidthMin } = this;
  217. const roundedWidth = Math.round(width);
  218. this.contentWidth =
  219. typeof contentWidthMax === "number" && typeof contentWidthMin === "number"
  220. ? clamp(roundedWidth, contentWidthMin, contentWidthMax)
  221. : roundedWidth;
  222. }
  223. updateAriaValues() {
  224. const { contentEl } = this;
  225. const computedStyle = contentEl && getComputedStyle(contentEl);
  226. if (!computedStyle) {
  227. return;
  228. }
  229. const max = parseInt(computedStyle.getPropertyValue("max-width"), 10);
  230. const min = parseInt(computedStyle.getPropertyValue("min-width"), 10);
  231. const valueNow = parseInt(computedStyle.getPropertyValue("width"), 10);
  232. if (typeof valueNow === "number" && !isNaN(valueNow)) {
  233. this.initialContentWidth = valueNow;
  234. }
  235. if (typeof max === "number" && !isNaN(max)) {
  236. this.contentWidthMax = max;
  237. }
  238. if (typeof min === "number" && !isNaN(min)) {
  239. this.contentWidthMin = min;
  240. }
  241. forceUpdate(this);
  242. }
  243. get el() { return this; }
  244. static get watchers() { return {
  245. "collapsed": ["watchHandler"]
  246. }; }
  247. static get style() { return shellPanelCss; }
  248. }, [1, "calcite-shell-panel", {
  249. "collapsed": [516],
  250. "detached": [516],
  251. "detachedHeightScale": [513, "detached-height-scale"],
  252. "widthScale": [513, "width-scale"],
  253. "position": [513],
  254. "intlResize": [1, "intl-resize"],
  255. "resizable": [516],
  256. "contentWidth": [32]
  257. }]);
  258. function defineCustomElement$1() {
  259. if (typeof customElements === "undefined") {
  260. return;
  261. }
  262. const components = ["calcite-shell-panel"];
  263. components.forEach(tagName => { switch (tagName) {
  264. case "calcite-shell-panel":
  265. if (!customElements.get(tagName)) {
  266. customElements.define(tagName, ShellPanel);
  267. }
  268. break;
  269. } });
  270. }
  271. defineCustomElement$1();
  272. const CalciteShellPanel = ShellPanel;
  273. const defineCustomElement = defineCustomElement$1;
  274. export { CalciteShellPanel, defineCustomElement };