tab-title.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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, Build, h, Host } from '@stencil/core/internal/client';
  7. import { g as guid } from './guid.js';
  8. import { g as getElementProp, t as toAriaBoolean, a as getElementDir } from './dom.js';
  9. import { c as createObserver } from './observers.js';
  10. import { u as updateHostInteraction } from './interactive.js';
  11. import { d as defineCustomElement$1 } from './icon.js';
  12. const tabTitleCss = "@-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{margin-right:1.25rem;display:block;-ms-flex:0 1 auto;flex:0 1 auto;outline:2px solid transparent;outline-offset:2px;-webkit-margin-start:0px;margin-inline-start:0px;-webkit-margin-end:1.25rem;margin-inline-end:1.25rem}:host([layout=center]){margin-top:0px;margin-bottom:0px;margin-left:1.25rem;margin-right:1.25rem;text-align:center;-ms-flex-preferred-size:12rem;flex-basis:12rem}:host([position=below]) a{border-bottom-width:0px;border-top-width:2px;border-top-color:transparent;border-top-style:solid}:host a{outline-offset:0;outline-color:transparent;-webkit-transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out;transition:outline-offset 100ms ease-in-out, outline-color 100ms ease-in-out}:host(:focus) a{outline:2px solid var(--calcite-ui-brand);outline-offset:-2px}:host(:active) a,:host(:focus) a,:host(:hover) a{border-color:var(--calcite-ui-border-2);color:var(--calcite-ui-text-1);-webkit-text-decoration-line:none;text-decoration-line:none}:host([active]) a{border-color:transparent;color:var(--calcite-ui-text-1)}:host([disabled]){pointer-events:none;cursor:default;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;opacity:var(--calcite-ui-opacity-disabled)}:host([disabled]) span,:host([disabled]) a{pointer-events:none;opacity:0.5}:host([disabled]) ::slotted([calcite-hydrated][disabled]),:host([disabled]) [calcite-hydrated][disabled]{opacity:1}:host([scale=s]){-webkit-margin-end:1rem;margin-inline-end:1rem}:host([scale=s]) a,:host([scale=s]) span{padding-top:0.25rem;padding-bottom:0.25rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=m]) a,:host([scale=m]) span{padding-top:0.5rem;padding-bottom:0.5rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=l]){-webkit-margin-end:1.5rem;margin-inline-end:1.5rem}:host([scale=l]) a,:host([scale=l]) span{padding-top:0.75rem;padding-bottom:0.75rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}a,span{-webkit-box-sizing:border-box;box-sizing:border-box;display:-ms-flexbox;display:flex;height:100%;width:100%;cursor:pointer;-webkit-appearance:none;-moz-appearance:none;appearance:none;-ms-flex-pack:center;justify-content:center;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;border-bottom-width:2px;padding-left:0px;padding-right:0px;padding-top:0.5rem;padding-bottom:0.5rem;font-size:var(--calcite-font-size--1);line-height:1rem;color:var(--calcite-ui-text-3);-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s;border-bottom-color:transparent;border-bottom-style:solid}span{cursor:default}.calcite-tab-title--icon{position:relative;margin:0px;display:-ms-inline-flexbox;display:inline-flex;-ms-flex-item-align:center;align-self:center}.calcite-tab-title--icon svg{-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}.container--has-text{padding:0.25rem}.container--has-text .calcite-tab-title--icon.icon-start{-webkit-margin-end:0.5rem;margin-inline-end:0.5rem}.container--has-text .calcite-tab-title--icon.icon-end{-webkit-margin-start:0.5rem;margin-inline-start:0.5rem}:host([icon-start][icon-end]) .calcite-tab-title--icon:first-child{-webkit-margin-end:0.5rem;margin-inline-end:0.5rem}:host([bordered]){-webkit-margin-end:0;margin-inline-end:0}:host([bordered][active]){-webkit-box-shadow:inset 0px -2px var(--calcite-ui-foreground-1);box-shadow:inset 0px -2px var(--calcite-ui-foreground-1)}:host([bordered][active][position=below]){-webkit-box-shadow:inset 0 2px 0 var(--calcite-ui-foreground-1);box-shadow:inset 0 2px 0 var(--calcite-ui-foreground-1)}:host([bordered]:hover) a,:host([bordered]:focus) a,:host([bordered]:active) a{position:relative}:host([bordered]:hover) a{background-color:var(--calcite-button-transparent-hover);-webkit-transition-property:all;transition-property:all;-webkit-transition-duration:var(--calcite-animation-timing);transition-duration:var(--calcite-animation-timing);-webkit-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out;-webkit-transition-delay:0s;transition-delay:0s}:host([bordered]) a{border-bottom-style:unset}:host([bordered][position=below]) a{border-top-style:unset}:host([active][bordered]) a{border-left:1px solid var(--calcite-ui-border-1);border-right:1px solid var(--calcite-ui-border-1)}:host([bordered]) a,:host([bordered]) span{padding-left:0.75rem;padding-right:0.75rem}:host([bordered][scale=s]) a,:host([bordered][scale=s]) span{padding-left:0.5rem;padding-right:0.5rem}:host([bordered][scale=l]) a,:host([bordered][scale=l]) span{padding-left:1rem;padding-right:1rem}";
  13. const TabTitle = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  14. constructor() {
  15. super();
  16. this.__registerHost();
  17. this.__attachShadow();
  18. this.calciteTabsActivate = createEvent(this, "calciteTabsActivate", 7);
  19. this.calciteInternalTabsActivate = createEvent(this, "calciteInternalTabsActivate", 7);
  20. this.calciteTabsFocusNext = createEvent(this, "calciteTabsFocusNext", 7);
  21. this.calciteTabsFocusPrevious = createEvent(this, "calciteTabsFocusPrevious", 7);
  22. this.calciteTabTitleRegister = createEvent(this, "calciteTabTitleRegister", 7);
  23. //--------------------------------------------------------------------------
  24. //
  25. // Properties
  26. //
  27. //--------------------------------------------------------------------------
  28. /** Show this tab title as selected */
  29. this.active = false;
  30. /** Disable this tab title */
  31. this.disabled = false;
  32. /** @internal Parent tabs component bordered value */
  33. this.bordered = false;
  34. //--------------------------------------------------------------------------
  35. //
  36. // Private State/Props
  37. //
  38. //--------------------------------------------------------------------------
  39. /** watches for changing text content **/
  40. this.mutationObserver = createObserver("mutation", () => this.updateHasText());
  41. /** determine if there is slotted text for styling purposes */
  42. this.hasText = false;
  43. this.guid = `calcite-tab-title-${guid()}`;
  44. }
  45. activeTabChanged() {
  46. if (this.active) {
  47. this.emitActiveTab(false);
  48. }
  49. }
  50. //--------------------------------------------------------------------------
  51. //
  52. // Lifecycle
  53. //
  54. //--------------------------------------------------------------------------
  55. connectedCallback() {
  56. this.setupTextContentObserver();
  57. this.parentTabNavEl = this.el.closest("calcite-tab-nav");
  58. this.parentTabsEl = this.el.closest("calcite-tabs");
  59. }
  60. disconnectedCallback() {
  61. var _a, _b;
  62. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  63. // Dispatching to body in order to be listened by other elements that are still connected to the DOM.
  64. (_b = document.body) === null || _b === void 0 ? void 0 : _b.dispatchEvent(new CustomEvent("calciteTabTitleUnregister", {
  65. detail: this.el
  66. }));
  67. }
  68. componentWillLoad() {
  69. if (Build.isBrowser) {
  70. this.updateHasText();
  71. }
  72. if (this.tab && this.active) {
  73. this.emitActiveTab(false);
  74. }
  75. }
  76. componentWillRender() {
  77. if (this.parentTabsEl) {
  78. this.layout = this.parentTabsEl.layout;
  79. this.position = this.parentTabsEl.position;
  80. this.scale = this.parentTabsEl.scale;
  81. this.bordered = this.parentTabsEl.bordered;
  82. }
  83. // handle case when tab-nav is only parent
  84. if (!this.parentTabsEl && this.parentTabNavEl) {
  85. this.position = getElementProp(this.parentTabNavEl, "position", this.position);
  86. this.scale = getElementProp(this.parentTabNavEl, "scale", this.scale);
  87. }
  88. }
  89. render() {
  90. const id = this.el.id || this.guid;
  91. const showSideBorders = this.bordered && !this.disabled && this.layout !== "center";
  92. const iconStartEl = (h("calcite-icon", { class: "calcite-tab-title--icon icon-start", flipRtl: this.iconFlipRtl === "start" || this.iconFlipRtl === "both", icon: this.iconStart, scale: "s" }));
  93. const iconEndEl = (h("calcite-icon", { class: "calcite-tab-title--icon icon-end", flipRtl: this.iconFlipRtl === "end" || this.iconFlipRtl === "both", icon: this.iconEnd, scale: "s" }));
  94. return (h(Host, { "aria-controls": this.controls, "aria-expanded": toAriaBoolean(this.active), id: id, role: "tab" }, h("a", { class: {
  95. container: true,
  96. "container--has-text": this.hasText
  97. }, style: showSideBorders && { width: `${this.parentTabNavEl.indicatorWidth}px` } }, this.iconStart ? iconStartEl : null, h("slot", null), this.iconEnd ? iconEndEl : null)));
  98. }
  99. async componentDidLoad() {
  100. this.calciteTabTitleRegister.emit(await this.getTabIdentifier());
  101. }
  102. componentDidRender() {
  103. updateHostInteraction(this, true);
  104. }
  105. //--------------------------------------------------------------------------
  106. //
  107. // Event Listeners
  108. //
  109. //--------------------------------------------------------------------------
  110. internalTabChangeHandler(event) {
  111. const targetTabsEl = event
  112. .composedPath()
  113. .find((el) => el.tagName === "CALCITE-TABS");
  114. if (targetTabsEl !== this.parentTabsEl) {
  115. return;
  116. }
  117. if (this.tab) {
  118. this.active = this.tab === event.detail.tab;
  119. }
  120. else {
  121. this.getTabIndex().then((index) => {
  122. this.active = index === event.detail.tab;
  123. });
  124. }
  125. event.stopPropagation();
  126. }
  127. onClick() {
  128. this.emitActiveTab();
  129. }
  130. keyDownHandler(e) {
  131. switch (e.key) {
  132. case " ":
  133. case "Enter":
  134. this.emitActiveTab();
  135. e.preventDefault();
  136. break;
  137. case "ArrowRight":
  138. if (getElementDir(this.el) === "ltr") {
  139. this.calciteTabsFocusNext.emit();
  140. }
  141. else {
  142. this.calciteTabsFocusPrevious.emit();
  143. }
  144. break;
  145. case "ArrowLeft":
  146. if (getElementDir(this.el) === "ltr") {
  147. this.calciteTabsFocusPrevious.emit();
  148. }
  149. else {
  150. this.calciteTabsFocusNext.emit();
  151. }
  152. break;
  153. }
  154. }
  155. //--------------------------------------------------------------------------
  156. //
  157. // Public Methods
  158. //
  159. //--------------------------------------------------------------------------
  160. /**
  161. * Return the index of this title within the nav
  162. */
  163. async getTabIndex() {
  164. return Array.prototype.indexOf.call(this.el.parentElement.querySelectorAll("calcite-tab-title"), this.el);
  165. }
  166. /**
  167. * @internal
  168. */
  169. async getTabIdentifier() {
  170. return this.tab ? this.tab : this.getTabIndex();
  171. }
  172. /**
  173. * @internal
  174. */
  175. async updateAriaInfo(tabIds = [], titleIds = []) {
  176. this.controls = tabIds[titleIds.indexOf(this.el.id)] || null;
  177. }
  178. updateHasText() {
  179. this.hasText = this.el.textContent.trim().length > 0;
  180. }
  181. setupTextContentObserver() {
  182. var _a;
  183. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  184. }
  185. emitActiveTab(userTriggered = true) {
  186. if (this.disabled) {
  187. return;
  188. }
  189. const payload = { tab: this.tab };
  190. this.calciteInternalTabsActivate.emit(payload);
  191. if (userTriggered) {
  192. this.calciteTabsActivate.emit(payload);
  193. }
  194. }
  195. get el() { return this; }
  196. static get watchers() { return {
  197. "active": ["activeTabChanged"]
  198. }; }
  199. static get style() { return tabTitleCss; }
  200. }, [1, "calcite-tab-title", {
  201. "active": [1540],
  202. "disabled": [516],
  203. "iconEnd": [513, "icon-end"],
  204. "iconFlipRtl": [513, "icon-flip-rtl"],
  205. "iconStart": [513, "icon-start"],
  206. "layout": [1537],
  207. "position": [1537],
  208. "scale": [1537],
  209. "bordered": [1540],
  210. "tab": [513],
  211. "controls": [32],
  212. "hasText": [32],
  213. "getTabIndex": [64],
  214. "getTabIdentifier": [64],
  215. "updateAriaInfo": [64]
  216. }, [[16, "calciteInternalTabChange", "internalTabChangeHandler"], [0, "click", "onClick"], [0, "keydown", "keyDownHandler"]]]);
  217. function defineCustomElement() {
  218. if (typeof customElements === "undefined") {
  219. return;
  220. }
  221. const components = ["calcite-tab-title", "calcite-icon"];
  222. components.forEach(tagName => { switch (tagName) {
  223. case "calcite-tab-title":
  224. if (!customElements.get(tagName)) {
  225. customElements.define(tagName, TabTitle);
  226. }
  227. break;
  228. case "calcite-icon":
  229. if (!customElements.get(tagName)) {
  230. defineCustomElement$1();
  231. }
  232. break;
  233. } });
  234. }
  235. defineCustomElement();
  236. export { TabTitle as T, defineCustomElement as d };