tab-title.js 15 KB

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