tab-title.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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 { Build, h, Host } from "@stencil/core";
  7. import { guid } from "../../utils/guid";
  8. import { getElementDir, getElementProp, toAriaBoolean } from "../../utils/dom";
  9. import { createObserver } from "../../utils/observers";
  10. import { updateHostInteraction } from "../../utils/interactive";
  11. /**
  12. * @slot - A slot for adding text.
  13. */
  14. export class TabTitle {
  15. constructor() {
  16. //--------------------------------------------------------------------------
  17. //
  18. // Properties
  19. //
  20. //--------------------------------------------------------------------------
  21. /**
  22. * When `true`, the component and its respective `calcite-tab` contents are selected.
  23. *
  24. * Only one tab can be selected within the `calcite-tabs` parent.
  25. *
  26. * @deprecated Use `selected` instead.
  27. */
  28. this.active = false;
  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. this.selected = false;
  35. /** When `true`, interaction is prevented and the component is displayed with lower opacity. */
  36. this.disabled = false;
  37. /**
  38. * @internal
  39. */
  40. this.bordered = false;
  41. //--------------------------------------------------------------------------
  42. //
  43. // Private State/Props
  44. //
  45. //--------------------------------------------------------------------------
  46. /** watches for changing text content */
  47. this.mutationObserver = createObserver("mutation", () => this.updateHasText());
  48. /** determine if there is slotted text for styling purposes */
  49. this.hasText = false;
  50. this.resizeObserver = createObserver("resize", () => {
  51. this.calciteInternalTabIconChanged.emit();
  52. });
  53. this.guid = `calcite-tab-title-${guid()}`;
  54. }
  55. activeHandler(value) {
  56. this.selected = value;
  57. }
  58. selectedHandler(value) {
  59. this.active = value;
  60. if (this.selected) {
  61. this.emitActiveTab(false);
  62. }
  63. }
  64. //--------------------------------------------------------------------------
  65. //
  66. // Lifecycle
  67. //
  68. //--------------------------------------------------------------------------
  69. connectedCallback() {
  70. const { selected, active } = this;
  71. if (selected) {
  72. this.active = selected;
  73. }
  74. else if (active) {
  75. this.activeHandler(active);
  76. }
  77. this.setupTextContentObserver();
  78. this.parentTabNavEl = this.el.closest("calcite-tab-nav");
  79. this.parentTabsEl = this.el.closest("calcite-tabs");
  80. }
  81. disconnectedCallback() {
  82. var _a, _b, _c;
  83. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
  84. // Dispatching to body in order to be listened by other elements that are still connected to the DOM.
  85. (_b = document.body) === null || _b === void 0 ? void 0 : _b.dispatchEvent(new CustomEvent("calciteTabTitleUnregister", {
  86. detail: this.el
  87. }));
  88. (_c = this.resizeObserver) === null || _c === void 0 ? void 0 : _c.disconnect();
  89. }
  90. componentWillLoad() {
  91. if (Build.isBrowser) {
  92. this.updateHasText();
  93. }
  94. if (this.tab && this.selected) {
  95. this.emitActiveTab(false);
  96. }
  97. }
  98. componentWillRender() {
  99. if (this.parentTabsEl) {
  100. this.layout = this.parentTabsEl.layout;
  101. this.position = this.parentTabsEl.position;
  102. this.scale = this.parentTabsEl.scale;
  103. this.bordered = this.parentTabsEl.bordered;
  104. }
  105. // handle case when tab-nav is only parent
  106. if (!this.parentTabsEl && this.parentTabNavEl) {
  107. this.position = getElementProp(this.parentTabNavEl, "position", this.position);
  108. this.scale = getElementProp(this.parentTabNavEl, "scale", this.scale);
  109. }
  110. }
  111. render() {
  112. const id = this.el.id || this.guid;
  113. const iconStartEl = (h("calcite-icon", { class: "calcite-tab-title--icon icon-start", flipRtl: this.iconFlipRtl === "start" || this.iconFlipRtl === "both", icon: this.iconStart, scale: "s" }));
  114. const iconEndEl = (h("calcite-icon", { class: "calcite-tab-title--icon icon-end", flipRtl: this.iconFlipRtl === "end" || this.iconFlipRtl === "both", icon: this.iconEnd, scale: "s" }));
  115. return (h(Host, { "aria-controls": this.controls, "aria-selected": toAriaBoolean(this.selected), id: id, role: "tab", tabIndex: this.selected ? 0 : -1 }, h("div", { class: {
  116. container: true,
  117. "container--has-text": this.hasText
  118. }, 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)));
  119. }
  120. async componentDidLoad() {
  121. this.calciteInternalTabTitleRegister.emit(await this.getTabIdentifier());
  122. }
  123. componentDidRender() {
  124. updateHostInteraction(this, () => {
  125. return this.selected;
  126. });
  127. }
  128. //--------------------------------------------------------------------------
  129. //
  130. // Event Listeners
  131. //
  132. //--------------------------------------------------------------------------
  133. internalTabChangeHandler(event) {
  134. const targetTabsEl = event
  135. .composedPath()
  136. .find((el) => el.tagName === "CALCITE-TABS");
  137. if (targetTabsEl !== this.parentTabsEl) {
  138. return;
  139. }
  140. if (this.tab) {
  141. this.selected = this.tab === event.detail.tab;
  142. }
  143. else {
  144. this.getTabIndex().then((index) => {
  145. this.selected = index === event.detail.tab;
  146. });
  147. }
  148. event.stopPropagation();
  149. }
  150. onClick() {
  151. this.emitActiveTab();
  152. }
  153. keyDownHandler(event) {
  154. switch (event.key) {
  155. case " ":
  156. case "Enter":
  157. this.emitActiveTab();
  158. event.preventDefault();
  159. break;
  160. case "ArrowRight":
  161. event.preventDefault();
  162. if (getElementDir(this.el) === "ltr") {
  163. this.calciteInternalTabsFocusNext.emit();
  164. }
  165. else {
  166. this.calciteInternalTabsFocusPrevious.emit();
  167. }
  168. break;
  169. case "ArrowLeft":
  170. event.preventDefault();
  171. if (getElementDir(this.el) === "ltr") {
  172. this.calciteInternalTabsFocusPrevious.emit();
  173. }
  174. else {
  175. this.calciteInternalTabsFocusNext.emit();
  176. }
  177. break;
  178. }
  179. }
  180. //--------------------------------------------------------------------------
  181. //
  182. // Public Methods
  183. //
  184. //--------------------------------------------------------------------------
  185. /**
  186. * Returns the index of the title within the `calcite-tab-nav`.
  187. */
  188. async getTabIndex() {
  189. return Array.prototype.indexOf.call(this.el.parentElement.querySelectorAll("calcite-tab-title"), this.el);
  190. }
  191. /**
  192. * @internal
  193. */
  194. async getTabIdentifier() {
  195. return this.tab ? this.tab : this.getTabIndex();
  196. }
  197. /**
  198. * @param tabIds
  199. * @param titleIds
  200. * @internal
  201. */
  202. async updateAriaInfo(tabIds = [], titleIds = []) {
  203. this.controls = tabIds[titleIds.indexOf(this.el.id)] || null;
  204. }
  205. updateHasText() {
  206. this.hasText = this.el.textContent.trim().length > 0;
  207. }
  208. setupTextContentObserver() {
  209. var _a;
  210. (_a = this.mutationObserver) === null || _a === void 0 ? void 0 : _a.observe(this.el, { childList: true, subtree: true });
  211. }
  212. emitActiveTab(userTriggered = true) {
  213. if (this.disabled) {
  214. return;
  215. }
  216. const payload = { tab: this.tab };
  217. this.calciteInternalTabsActivate.emit(payload);
  218. if (userTriggered) {
  219. this.calciteTabsActivate.emit(payload);
  220. }
  221. }
  222. static get is() { return "calcite-tab-title"; }
  223. static get encapsulation() { return "shadow"; }
  224. static get originalStyleUrls() {
  225. return {
  226. "$": ["tab-title.scss"]
  227. };
  228. }
  229. static get styleUrls() {
  230. return {
  231. "$": ["tab-title.css"]
  232. };
  233. }
  234. static get properties() {
  235. return {
  236. "active": {
  237. "type": "boolean",
  238. "mutable": true,
  239. "complexType": {
  240. "original": "boolean",
  241. "resolved": "boolean",
  242. "references": {}
  243. },
  244. "required": false,
  245. "optional": false,
  246. "docs": {
  247. "tags": [{
  248. "name": "deprecated",
  249. "text": "Use `selected` instead."
  250. }],
  251. "text": "When `true`, the component and its respective `calcite-tab` contents are selected.\n\nOnly one tab can be selected within the `calcite-tabs` parent."
  252. },
  253. "attribute": "active",
  254. "reflect": true,
  255. "defaultValue": "false"
  256. },
  257. "selected": {
  258. "type": "boolean",
  259. "mutable": true,
  260. "complexType": {
  261. "original": "boolean",
  262. "resolved": "boolean",
  263. "references": {}
  264. },
  265. "required": false,
  266. "optional": false,
  267. "docs": {
  268. "tags": [],
  269. "text": "When `true`, the component and its respective `calcite-tab` contents are selected.\n\nOnly one tab can be selected within the `calcite-tabs` parent."
  270. },
  271. "attribute": "selected",
  272. "reflect": true,
  273. "defaultValue": "false"
  274. },
  275. "disabled": {
  276. "type": "boolean",
  277. "mutable": false,
  278. "complexType": {
  279. "original": "boolean",
  280. "resolved": "boolean",
  281. "references": {}
  282. },
  283. "required": false,
  284. "optional": false,
  285. "docs": {
  286. "tags": [],
  287. "text": "When `true`, interaction is prevented and the component is displayed with lower opacity."
  288. },
  289. "attribute": "disabled",
  290. "reflect": true,
  291. "defaultValue": "false"
  292. },
  293. "iconEnd": {
  294. "type": "string",
  295. "mutable": false,
  296. "complexType": {
  297. "original": "string",
  298. "resolved": "string",
  299. "references": {}
  300. },
  301. "required": false,
  302. "optional": true,
  303. "docs": {
  304. "tags": [],
  305. "text": "Specifies an icon to display at the end of the component."
  306. },
  307. "attribute": "icon-end",
  308. "reflect": true
  309. },
  310. "iconFlipRtl": {
  311. "type": "string",
  312. "mutable": false,
  313. "complexType": {
  314. "original": "FlipContext",
  315. "resolved": "\"both\" | \"end\" | \"start\"",
  316. "references": {
  317. "FlipContext": {
  318. "location": "import",
  319. "path": "../interfaces"
  320. }
  321. }
  322. },
  323. "required": false,
  324. "optional": true,
  325. "docs": {
  326. "tags": [],
  327. "text": "When `true`, the icon will be flipped when the element direction is right-to-left (`\"rtl\"`)."
  328. },
  329. "attribute": "icon-flip-rtl",
  330. "reflect": true
  331. },
  332. "iconStart": {
  333. "type": "string",
  334. "mutable": false,
  335. "complexType": {
  336. "original": "string",
  337. "resolved": "string",
  338. "references": {}
  339. },
  340. "required": false,
  341. "optional": true,
  342. "docs": {
  343. "tags": [],
  344. "text": "Specifies an icon to display at the start of the component."
  345. },
  346. "attribute": "icon-start",
  347. "reflect": true
  348. },
  349. "layout": {
  350. "type": "string",
  351. "mutable": true,
  352. "complexType": {
  353. "original": "TabLayout",
  354. "resolved": "\"center\" | \"inline\"",
  355. "references": {
  356. "TabLayout": {
  357. "location": "import",
  358. "path": "../tabs/interfaces"
  359. }
  360. }
  361. },
  362. "required": false,
  363. "optional": false,
  364. "docs": {
  365. "tags": [{
  366. "name": "internal",
  367. "text": undefined
  368. }],
  369. "text": ""
  370. },
  371. "attribute": "layout",
  372. "reflect": true
  373. },
  374. "position": {
  375. "type": "string",
  376. "mutable": true,
  377. "complexType": {
  378. "original": "TabPosition",
  379. "resolved": "\"above\" | \"below\" | \"bottom\" | \"top\"",
  380. "references": {
  381. "TabPosition": {
  382. "location": "import",
  383. "path": "../tabs/interfaces"
  384. }
  385. }
  386. },
  387. "required": false,
  388. "optional": false,
  389. "docs": {
  390. "tags": [{
  391. "name": "internal",
  392. "text": undefined
  393. }],
  394. "text": ""
  395. },
  396. "attribute": "position",
  397. "reflect": true
  398. },
  399. "scale": {
  400. "type": "string",
  401. "mutable": true,
  402. "complexType": {
  403. "original": "Scale",
  404. "resolved": "\"l\" | \"m\" | \"s\"",
  405. "references": {
  406. "Scale": {
  407. "location": "import",
  408. "path": "../interfaces"
  409. }
  410. }
  411. },
  412. "required": false,
  413. "optional": false,
  414. "docs": {
  415. "tags": [{
  416. "name": "internal",
  417. "text": undefined
  418. }],
  419. "text": ""
  420. },
  421. "attribute": "scale",
  422. "reflect": true
  423. },
  424. "bordered": {
  425. "type": "boolean",
  426. "mutable": true,
  427. "complexType": {
  428. "original": "boolean",
  429. "resolved": "boolean",
  430. "references": {}
  431. },
  432. "required": false,
  433. "optional": false,
  434. "docs": {
  435. "tags": [{
  436. "name": "internal",
  437. "text": undefined
  438. }],
  439. "text": ""
  440. },
  441. "attribute": "bordered",
  442. "reflect": true,
  443. "defaultValue": "false"
  444. },
  445. "tab": {
  446. "type": "string",
  447. "mutable": false,
  448. "complexType": {
  449. "original": "string",
  450. "resolved": "string",
  451. "references": {}
  452. },
  453. "required": false,
  454. "optional": true,
  455. "docs": {
  456. "tags": [],
  457. "text": "Specifies a unique name for the component.\n\nWhen specified, use the same value on the `calcite-tab`."
  458. },
  459. "attribute": "tab",
  460. "reflect": true
  461. }
  462. };
  463. }
  464. static get states() {
  465. return {
  466. "controls": {},
  467. "hasText": {}
  468. };
  469. }
  470. static get events() {
  471. return [{
  472. "method": "calciteTabsActivate",
  473. "name": "calciteTabsActivate",
  474. "bubbles": true,
  475. "cancelable": false,
  476. "composed": true,
  477. "docs": {
  478. "tags": [{
  479. "name": "see",
  480. "text": "[TabChangeEventDetail](https://github.com/Esri/calcite-components/blob/master/src/components/tab/interfaces.ts#L1)"
  481. }],
  482. "text": "Fires when a `calcite-tab` is selected. Emits the `tab` property, or the index position."
  483. },
  484. "complexType": {
  485. "original": "TabChangeEventDetail",
  486. "resolved": "TabChangeEventDetail",
  487. "references": {
  488. "TabChangeEventDetail": {
  489. "location": "import",
  490. "path": "../tab/interfaces"
  491. }
  492. }
  493. }
  494. }, {
  495. "method": "calciteInternalTabsActivate",
  496. "name": "calciteInternalTabsActivate",
  497. "bubbles": true,
  498. "cancelable": false,
  499. "composed": true,
  500. "docs": {
  501. "tags": [{
  502. "name": "see",
  503. "text": "[TabChangeEventDetail](https://github.com/Esri/calcite-components/blob/master/src/components/tab/interfaces.ts#L1)"
  504. }, {
  505. "name": "internal",
  506. "text": undefined
  507. }],
  508. "text": "Fires when a `calcite-tab` is selected (`event.details`)."
  509. },
  510. "complexType": {
  511. "original": "TabChangeEventDetail",
  512. "resolved": "TabChangeEventDetail",
  513. "references": {
  514. "TabChangeEventDetail": {
  515. "location": "import",
  516. "path": "../tab/interfaces"
  517. }
  518. }
  519. }
  520. }, {
  521. "method": "calciteInternalTabsFocusNext",
  522. "name": "calciteInternalTabsFocusNext",
  523. "bubbles": true,
  524. "cancelable": false,
  525. "composed": true,
  526. "docs": {
  527. "tags": [{
  528. "name": "internal",
  529. "text": undefined
  530. }],
  531. "text": ""
  532. },
  533. "complexType": {
  534. "original": "void",
  535. "resolved": "void",
  536. "references": {}
  537. }
  538. }, {
  539. "method": "calciteInternalTabsFocusPrevious",
  540. "name": "calciteInternalTabsFocusPrevious",
  541. "bubbles": true,
  542. "cancelable": false,
  543. "composed": true,
  544. "docs": {
  545. "tags": [{
  546. "name": "internal",
  547. "text": undefined
  548. }],
  549. "text": ""
  550. },
  551. "complexType": {
  552. "original": "void",
  553. "resolved": "void",
  554. "references": {}
  555. }
  556. }, {
  557. "method": "calciteInternalTabTitleRegister",
  558. "name": "calciteInternalTabTitleRegister",
  559. "bubbles": true,
  560. "cancelable": false,
  561. "composed": true,
  562. "docs": {
  563. "tags": [{
  564. "name": "internal",
  565. "text": undefined
  566. }],
  567. "text": ""
  568. },
  569. "complexType": {
  570. "original": "TabID",
  571. "resolved": "number | string",
  572. "references": {
  573. "TabID": {
  574. "location": "import",
  575. "path": "../tabs/interfaces"
  576. }
  577. }
  578. }
  579. }, {
  580. "method": "calciteInternalTabIconChanged",
  581. "name": "calciteInternalTabIconChanged",
  582. "bubbles": true,
  583. "cancelable": false,
  584. "composed": true,
  585. "docs": {
  586. "tags": [{
  587. "name": "internal",
  588. "text": undefined
  589. }],
  590. "text": ""
  591. },
  592. "complexType": {
  593. "original": "void",
  594. "resolved": "void",
  595. "references": {}
  596. }
  597. }];
  598. }
  599. static get methods() {
  600. return {
  601. "getTabIndex": {
  602. "complexType": {
  603. "signature": "() => Promise<number>",
  604. "parameters": [],
  605. "references": {
  606. "Promise": {
  607. "location": "global"
  608. }
  609. },
  610. "return": "Promise<number>"
  611. },
  612. "docs": {
  613. "text": "Returns the index of the title within the `calcite-tab-nav`.",
  614. "tags": []
  615. }
  616. },
  617. "getTabIdentifier": {
  618. "complexType": {
  619. "signature": "() => Promise<TabID>",
  620. "parameters": [],
  621. "references": {
  622. "Promise": {
  623. "location": "global"
  624. },
  625. "TabID": {
  626. "location": "import",
  627. "path": "../tabs/interfaces"
  628. }
  629. },
  630. "return": "Promise<TabID>"
  631. },
  632. "docs": {
  633. "text": "",
  634. "tags": [{
  635. "name": "internal",
  636. "text": undefined
  637. }]
  638. }
  639. },
  640. "updateAriaInfo": {
  641. "complexType": {
  642. "signature": "(tabIds?: string[], titleIds?: string[]) => Promise<void>",
  643. "parameters": [{
  644. "tags": [{
  645. "name": "param",
  646. "text": "tabIds"
  647. }],
  648. "text": ""
  649. }, {
  650. "tags": [{
  651. "name": "param",
  652. "text": "titleIds"
  653. }],
  654. "text": ""
  655. }],
  656. "references": {
  657. "Promise": {
  658. "location": "global"
  659. }
  660. },
  661. "return": "Promise<void>"
  662. },
  663. "docs": {
  664. "text": "",
  665. "tags": [{
  666. "name": "param",
  667. "text": "tabIds"
  668. }, {
  669. "name": "param",
  670. "text": "titleIds"
  671. }, {
  672. "name": "internal",
  673. "text": undefined
  674. }]
  675. }
  676. }
  677. };
  678. }
  679. static get elementRef() { return "el"; }
  680. static get watchers() {
  681. return [{
  682. "propName": "active",
  683. "methodName": "activeHandler"
  684. }, {
  685. "propName": "selected",
  686. "methodName": "selectedHandler"
  687. }];
  688. }
  689. static get listeners() {
  690. return [{
  691. "name": "calciteInternalTabChange",
  692. "method": "internalTabChangeHandler",
  693. "target": "body",
  694. "capture": false,
  695. "passive": false
  696. }, {
  697. "name": "click",
  698. "method": "onClick",
  699. "target": undefined,
  700. "capture": false,
  701. "passive": false
  702. }, {
  703. "name": "keydown",
  704. "method": "keyDownHandler",
  705. "target": undefined,
  706. "capture": false,
  707. "passive": false
  708. }];
  709. }
  710. }