tab-nav.js 12 KB

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