tab-nav.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 { proxyCustomElement, HTMLElement, createEvent, h, Host } from '@stencil/core/internal/client';
  7. import { a as getElementDir, h as filterDirectChildren } from './dom.js';
  8. import { c as createObserver } from './observers.js';
  9. const tabNavCss = "@-webkit-keyframes in{0%{opacity:0}100%{opacity:1}}@keyframes in{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-down{0%{opacity:0;-webkit-transform:translate3D(0, -5px, 0);transform:translate3D(0, -5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@keyframes in-up{0%{opacity:0;-webkit-transform:translate3D(0, 5px, 0);transform:translate3D(0, 5px, 0)}100%{opacity:1;-webkit-transform:translate3D(0, 0, 0);transform:translate3D(0, 0, 0)}}@-webkit-keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 1);transform:scale3D(1, 1, 1)}}@keyframes in-scale{0%{opacity:0;-webkit-transform:scale3D(0.95, 0.95, 1);transform:scale3D(0.95, 0.95, 1)}100%{opacity:1;-webkit-transform:scale3D(1, 1, 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;-webkit-animation-fill-mode:both;animation-fill-mode:both;-webkit-animation-duration:var(--calcite-animation-timing);animation-duration:var(--calcite-animation-timing)}.calcite-animate__in{-webkit-animation-name:in;animation-name:in}.calcite-animate__in-down{-webkit-animation-name:in-down;animation-name:in-down}.calcite-animate__in-up{-webkit-animation-name:in-up;animation-name:in-up}.calcite-animate__in-scale{-webkit-animation-name:in-scale;animation-name:in-scale}:root{--calcite-popper-transition:var(--calcite-animation-timing)}:host([hidden]){display:none}:host{position:relative;display:-ms-flexbox;display:flex}:host([scale=s]){min-height:1.5rem}:host([scale=m]){min-height:2rem}:host([scale=l]){min-height:2.75rem}.tab-nav{display:-ms-flexbox;display:flex;width:100%;-ms-flex-pack:start;justify-content:flex-start;overflow:auto}:host([layout=center]) .tab-nav{-ms-flex-pack:center;justify-content:center}.tab-nav-active-indicator-container{position:absolute;left:0px;right:0px;bottom:0px;height:0.125rem;width:100%;overflow:hidden}.tab-nav-active-indicator{position:absolute;bottom:0px;display:block;height:0.125rem;background-color:var(--calcite-ui-brand);-webkit-transition-property:all;transition-property:all;-webkit-transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);transition-timing-function:cubic-bezier(0.4, 0, 0.2, 1);-webkit-transition-duration:150ms;transition-duration:150ms;-webkit-transition-timing-function:cubic-bezier(0, 0, 0.2, 1);transition-timing-function:cubic-bezier(0, 0, 0.2, 1)}:host([position=below]) .tab-nav-active-indicator{bottom:unset;top:0px}:host([position=below]) .tab-nav-active-indicator-container{bottom:unset;top:0px}:host([bordered]) .tab-nav-active-indicator-container{bottom:unset}:host([bordered][position=below]) .tab-nav-active-indicator-container{bottom:0;top:unset}";
  10. const TabNav = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  11. constructor() {
  12. super();
  13. this.__registerHost();
  14. this.__attachShadow();
  15. this.calciteTabChange = createEvent(this, "calciteTabChange", 7);
  16. this.calciteInternalTabChange = createEvent(this, "calciteInternalTabChange", 7);
  17. /** @internal Parent tabs component scale value */
  18. this.scale = "m";
  19. /** @internal Parent tabs component layout value */
  20. this.layout = "inline";
  21. /** @internal Parent tabs component position value */
  22. this.position = "below";
  23. /** @internal Parent tabs component bordered value when layout is "inline" */
  24. this.bordered = false;
  25. this.animationActiveDuration = 0.3;
  26. this.resizeObserver = createObserver("resize", () => {
  27. // remove active indicator transition duration during resize to prevent wobble
  28. this.activeIndicatorEl.style.transitionDuration = "0s";
  29. this.updateActiveWidth();
  30. this.updateOffsetPosition();
  31. });
  32. //--------------------------------------------------------------------------
  33. //
  34. // Private Methods
  35. //
  36. //--------------------------------------------------------------------------
  37. this.handleContainerScroll = () => {
  38. // remove active indicator transition duration while container is scrolling to prevent wobble
  39. this.activeIndicatorEl.style.transitionDuration = "0s";
  40. this.updateOffsetPosition();
  41. };
  42. }
  43. async selectedTabChanged() {
  44. if (localStorage &&
  45. this.storageId &&
  46. this.selectedTab !== undefined &&
  47. this.selectedTab !== null) {
  48. localStorage.setItem(`calcite-tab-nav-${this.storageId}`, JSON.stringify(this.selectedTab));
  49. }
  50. this.calciteInternalTabChange.emit({
  51. tab: this.selectedTab
  52. });
  53. this.selectedTabEl = await this.getTabTitleById(this.selectedTab);
  54. }
  55. selectedTabElChanged() {
  56. this.updateOffsetPosition();
  57. this.updateActiveWidth();
  58. // reset the animation time on tab selection
  59. this.activeIndicatorEl.style.transitionDuration = `${this.animationActiveDuration}s`;
  60. }
  61. //--------------------------------------------------------------------------
  62. //
  63. // Lifecycle
  64. //
  65. //--------------------------------------------------------------------------
  66. connectedCallback() {
  67. var _a;
  68. this.parentTabsEl = this.el.closest("calcite-tabs");
  69. (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el);
  70. }
  71. disconnectedCallback() {
  72. var _a;
  73. (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  74. }
  75. componentWillLoad() {
  76. const storageKey = `calcite-tab-nav-${this.storageId}`;
  77. if (localStorage && this.storageId && localStorage.getItem(storageKey)) {
  78. const storedTab = JSON.parse(localStorage.getItem(storageKey));
  79. this.selectedTab = storedTab;
  80. }
  81. }
  82. componentWillRender() {
  83. const { parentTabsEl } = this;
  84. this.layout = parentTabsEl === null || parentTabsEl === void 0 ? void 0 : parentTabsEl.layout;
  85. this.position = parentTabsEl === null || parentTabsEl === void 0 ? void 0 : parentTabsEl.position;
  86. this.scale = parentTabsEl === null || parentTabsEl === void 0 ? void 0 : parentTabsEl.scale;
  87. this.bordered = parentTabsEl === null || parentTabsEl === void 0 ? void 0 : parentTabsEl.bordered;
  88. // fix issue with active tab-title not lining up with blue indicator
  89. if (this.selectedTabEl) {
  90. this.updateOffsetPosition();
  91. }
  92. }
  93. componentDidRender() {
  94. // if every tab title is active select the first tab.
  95. if (this.tabTitles.length &&
  96. this.tabTitles.every((title) => !title.active) &&
  97. !this.selectedTab) {
  98. this.tabTitles[0].getTabIdentifier().then((tab) => {
  99. this.calciteInternalTabChange.emit({
  100. tab
  101. });
  102. });
  103. }
  104. }
  105. render() {
  106. const dir = getElementDir(this.el);
  107. const width = `${this.indicatorWidth}px`;
  108. const offset = `${this.indicatorOffset}px`;
  109. const indicatorStyle = dir !== "rtl" ? { width, left: offset } : { width, right: offset };
  110. return (h(Host, { role: "tablist" }, h("div", { class: "tab-nav", onScroll: this.handleContainerScroll, ref: (el) => (this.tabNavEl = el) }, h("div", { class: "tab-nav-active-indicator-container", ref: (el) => (this.activeIndicatorContainerEl = el) }, h("div", { class: "tab-nav-active-indicator", ref: (el) => (this.activeIndicatorEl = el), style: indicatorStyle })), h("slot", null))));
  111. }
  112. //--------------------------------------------------------------------------
  113. //
  114. // Event Listeners
  115. //
  116. //--------------------------------------------------------------------------
  117. focusPreviousTabHandler(e) {
  118. const currentIndex = this.getIndexOfTabTitle(e.target, this.enabledTabTitles);
  119. const previousTab = this.enabledTabTitles[currentIndex - 1] ||
  120. this.enabledTabTitles[this.enabledTabTitles.length - 1];
  121. previousTab.focus();
  122. e.stopPropagation();
  123. e.preventDefault();
  124. }
  125. focusNextTabHandler(e) {
  126. const currentIndex = this.getIndexOfTabTitle(e.target, this.enabledTabTitles);
  127. const nextTab = this.enabledTabTitles[currentIndex + 1] || this.enabledTabTitles[0];
  128. nextTab.focus();
  129. e.stopPropagation();
  130. e.preventDefault();
  131. }
  132. internalActivateTabHandler(e) {
  133. this.selectedTab = e.detail.tab
  134. ? e.detail.tab
  135. : this.getIndexOfTabTitle(e.target);
  136. e.stopPropagation();
  137. e.preventDefault();
  138. }
  139. activateTabHandler(e) {
  140. this.calciteTabChange.emit({
  141. tab: this.selectedTab
  142. });
  143. e.stopPropagation();
  144. e.preventDefault();
  145. }
  146. /**
  147. * Check for active tabs on register and update selected
  148. */
  149. updateTabTitles(e) {
  150. if (e.target.active) {
  151. this.selectedTab = e.detail;
  152. }
  153. }
  154. globalInternalTabChangeHandler(e) {
  155. if (this.syncId &&
  156. e.target !== this.el &&
  157. e.target.syncId === this.syncId &&
  158. this.selectedTab !== e.detail.tab) {
  159. this.selectedTab = e.detail.tab;
  160. e.stopPropagation();
  161. }
  162. }
  163. updateOffsetPosition() {
  164. var _a, _b, _c, _d, _e;
  165. const dir = getElementDir(this.el);
  166. const navWidth = (_a = this.activeIndicatorContainerEl) === null || _a === void 0 ? void 0 : _a.offsetWidth;
  167. const tabLeft = (_b = this.selectedTabEl) === null || _b === void 0 ? void 0 : _b.offsetLeft;
  168. const tabWidth = (_c = this.selectedTabEl) === null || _c === void 0 ? void 0 : _c.offsetWidth;
  169. const offsetRight = navWidth - (tabLeft + tabWidth);
  170. this.indicatorOffset =
  171. dir !== "rtl" ? tabLeft - ((_d = this.tabNavEl) === null || _d === void 0 ? void 0 : _d.scrollLeft) : offsetRight + ((_e = this.tabNavEl) === null || _e === void 0 ? void 0 : _e.scrollLeft);
  172. }
  173. updateActiveWidth() {
  174. var _a;
  175. this.indicatorWidth = (_a = this.selectedTabEl) === null || _a === void 0 ? void 0 : _a.offsetWidth;
  176. }
  177. getIndexOfTabTitle(el, tabTitles = this.tabTitles) {
  178. // In most cases, since these indexes correlate with tab contents, we want to consider all tab titles.
  179. // However, when doing relative index operations, it makes sense to pass in this.enabledTabTitles as the 2nd arg.
  180. return tabTitles.indexOf(el);
  181. }
  182. async getTabTitleById(id) {
  183. return Promise.all(this.tabTitles.map((el) => el.getTabIdentifier())).then((ids) => {
  184. return this.tabTitles[ids.indexOf(id)];
  185. });
  186. }
  187. get tabTitles() {
  188. return filterDirectChildren(this.el, "calcite-tab-title");
  189. }
  190. get enabledTabTitles() {
  191. return filterDirectChildren(this.el, "calcite-tab-title:not([disabled])");
  192. }
  193. get el() { return this; }
  194. static get watchers() { return {
  195. "selectedTab": ["selectedTabChanged"],
  196. "selectedTabEl": ["selectedTabElChanged"]
  197. }; }
  198. static get style() { return tabNavCss; }
  199. }, [1, "calcite-tab-nav", {
  200. "storageId": [1, "storage-id"],
  201. "syncId": [1, "sync-id"],
  202. "scale": [1537],
  203. "layout": [1537],
  204. "position": [1537],
  205. "bordered": [1540],
  206. "indicatorOffset": [1026, "indicator-offset"],
  207. "indicatorWidth": [1026, "indicator-width"],
  208. "selectedTab": [32],
  209. "selectedTabEl": [32]
  210. }, [[0, "calciteTabsFocusPrevious", "focusPreviousTabHandler"], [0, "calciteTabsFocusNext", "focusNextTabHandler"], [0, "calciteInternalTabsActivate", "internalActivateTabHandler"], [0, "calciteTabsActivate", "activateTabHandler"], [0, "calciteTabTitleRegister", "updateTabTitles"], [16, "calciteInternalTabChange", "globalInternalTabChangeHandler"]]]);
  211. function defineCustomElement() {
  212. if (typeof customElements === "undefined") {
  213. return;
  214. }
  215. const components = ["calcite-tab-nav"];
  216. components.forEach(tagName => { switch (tagName) {
  217. case "calcite-tab-nav":
  218. if (!customElements.get(tagName)) {
  219. customElements.define(tagName, TabNav);
  220. }
  221. break;
  222. } });
  223. }
  224. defineCustomElement();
  225. export { TabNav as T, defineCustomElement as d };