tab.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 { g as guid } from './guid.js';
  8. import { t as toAriaBoolean, n as nodeListToArray } from './dom.js';
  9. const tabCss = "@-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([active]) section{display:block}:host{display:none;height:100%;width:100%}:host([active]){display:block;height:100%;width:100%;overflow:auto}section{display:none;height:100%;width:100%}:host([scale=s]){padding-top:0.25rem;padding-bottom:0.25rem;font-size:var(--calcite-font-size--2);line-height:1rem}:host([scale=m]){padding-top:0.5rem;padding-bottom:0.5rem;font-size:var(--calcite-font-size--1);line-height:1rem}:host([scale=l]){padding-top:0.75rem;padding-bottom:0.75rem;font-size:var(--calcite-font-size-0);line-height:1.25rem}";
  10. const Tab = /*@__PURE__*/ proxyCustomElement(class extends HTMLElement {
  11. constructor() {
  12. super();
  13. this.__registerHost();
  14. this.__attachShadow();
  15. this.calciteTabRegister = createEvent(this, "calciteTabRegister", 7);
  16. /**
  17. * Show this tab
  18. */
  19. this.active = false;
  20. /** @internal Parent tabs component scale value */
  21. this.scale = "m";
  22. this.guid = `calcite-tab-title-${guid()}`;
  23. }
  24. //--------------------------------------------------------------------------
  25. //
  26. // Lifecycle
  27. //
  28. //--------------------------------------------------------------------------
  29. render() {
  30. const id = this.el.id || this.guid;
  31. return (h(Host, { "aria-expanded": toAriaBoolean(this.active), "aria-labelledby": this.labeledBy, id: id, role: "tabpanel" }, h("section", null, h("slot", null))));
  32. }
  33. connectedCallback() {
  34. this.parentTabsEl = this.el.closest("calcite-tabs");
  35. }
  36. componentDidLoad() {
  37. this.calciteTabRegister.emit();
  38. }
  39. componentWillRender() {
  40. var _a;
  41. this.scale = (_a = this.parentTabsEl) === null || _a === void 0 ? void 0 : _a.scale;
  42. }
  43. disconnectedCallback() {
  44. var _a;
  45. // Dispatching to body in order to be listened by other elements that are still connected to the DOM.
  46. (_a = document.body) === null || _a === void 0 ? void 0 : _a.dispatchEvent(new CustomEvent("calciteTabUnregister", {
  47. detail: this.el
  48. }));
  49. }
  50. //--------------------------------------------------------------------------
  51. //
  52. // Event Listeners
  53. //
  54. //--------------------------------------------------------------------------
  55. internalTabChangeHandler(event) {
  56. const targetTabsEl = event
  57. .composedPath()
  58. .find((el) => el.tagName === "CALCITE-TABS");
  59. // to allow `<calcite-tabs>` to be nested we need to make sure this
  60. // `calciteTabChange` event was actually fired from a within the same
  61. // `<calcite-tabs>` that is the a parent of this tab.
  62. if (targetTabsEl !== this.parentTabsEl) {
  63. return;
  64. }
  65. if (this.tab) {
  66. this.active = this.tab === event.detail.tab;
  67. }
  68. else {
  69. this.getTabIndex().then((index) => {
  70. this.active = index === event.detail.tab;
  71. });
  72. }
  73. }
  74. //--------------------------------------------------------------------------
  75. //
  76. // Public Methods
  77. //
  78. //--------------------------------------------------------------------------
  79. /**
  80. * Return the index of this tab within the tab array
  81. */
  82. async getTabIndex() {
  83. return Array.prototype.indexOf.call(nodeListToArray(this.el.parentElement.children).filter((e) => e.matches("calcite-tab")), this.el);
  84. }
  85. //--------------------------------------------------------------------------
  86. //
  87. // Private Methods
  88. //
  89. //--------------------------------------------------------------------------
  90. /**
  91. * @internal
  92. */
  93. async updateAriaInfo(tabIds = [], titleIds = []) {
  94. this.labeledBy = titleIds[tabIds.indexOf(this.el.id)] || null;
  95. }
  96. get el() { return this; }
  97. static get style() { return tabCss; }
  98. }, [1, "calcite-tab", {
  99. "tab": [513],
  100. "active": [1540],
  101. "scale": [1537],
  102. "labeledBy": [32],
  103. "getTabIndex": [64],
  104. "updateAriaInfo": [64]
  105. }, [[16, "calciteInternalTabChange", "internalTabChangeHandler"]]]);
  106. function defineCustomElement() {
  107. if (typeof customElements === "undefined") {
  108. return;
  109. }
  110. const components = ["calcite-tab"];
  111. components.forEach(tagName => { switch (tagName) {
  112. case "calcite-tab":
  113. if (!customElements.get(tagName)) {
  114. customElements.define(tagName, Tab);
  115. }
  116. break;
  117. } });
  118. }
  119. defineCustomElement();
  120. export { Tab as T, defineCustomElement as d };