ion-split-pane.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { proxyCustomElement, HTMLElement, createEvent, Build, h, Host } from '@stencil/core/internal/client';
  5. import { a as printIonWarning } from './index4.js';
  6. import { b as getIonMode } from './ionic-global.js';
  7. const splitPaneIosCss = ":host{--side-width:100%;left:0;right:0;top:0;bottom:0;display:-ms-flexbox;display:flex;position:absolute;-ms-flex-direction:row;flex-direction:row;-ms-flex-wrap:nowrap;flex-wrap:nowrap;contain:strict}:host(.split-pane-visible) ::slotted(.split-pane-main){left:0;right:0;top:0;bottom:0;position:relative;-ms-flex:1;flex:1;-webkit-box-shadow:none;box-shadow:none;overflow:hidden;z-index:0}::slotted(.split-pane-side:not(ion-menu)){display:none}:host{--border:0.55px solid var(--ion-item-border-color, var(--ion-border-color, var(--ion-color-step-250, var(--ion-background-color-step-250, #c8c7cc))));--side-min-width:270px;--side-max-width:28%}";
  8. const IonSplitPaneIosStyle0 = splitPaneIosCss;
  9. const splitPaneMdCss = ":host{--side-width:100%;left:0;right:0;top:0;bottom:0;display:-ms-flexbox;display:flex;position:absolute;-ms-flex-direction:row;flex-direction:row;-ms-flex-wrap:nowrap;flex-wrap:nowrap;contain:strict}:host(.split-pane-visible) ::slotted(.split-pane-main){left:0;right:0;top:0;bottom:0;position:relative;-ms-flex:1;flex:1;-webkit-box-shadow:none;box-shadow:none;overflow:hidden;z-index:0}::slotted(.split-pane-side:not(ion-menu)){display:none}:host{--border:1px solid var(--ion-item-border-color, var(--ion-border-color, var(--ion-color-step-150, var(--ion-background-color-step-150, rgba(0, 0, 0, 0.13)))));--side-min-width:270px;--side-max-width:28%}";
  10. const IonSplitPaneMdStyle0 = splitPaneMdCss;
  11. // TODO(FW-2832): types
  12. const SPLIT_PANE_MAIN = 'split-pane-main';
  13. const SPLIT_PANE_SIDE = 'split-pane-side';
  14. const QUERY = {
  15. xs: '(min-width: 0px)',
  16. sm: '(min-width: 576px)',
  17. md: '(min-width: 768px)',
  18. lg: '(min-width: 992px)',
  19. xl: '(min-width: 1200px)',
  20. never: '',
  21. };
  22. const SplitPane = /*@__PURE__*/ proxyCustomElement(class SplitPane extends HTMLElement {
  23. constructor() {
  24. super();
  25. this.__registerHost();
  26. this.__attachShadow();
  27. this.ionSplitPaneVisible = createEvent(this, "ionSplitPaneVisible", 7);
  28. this.visible = false;
  29. this.contentId = undefined;
  30. this.disabled = false;
  31. this.when = QUERY['lg'];
  32. }
  33. visibleChanged(visible) {
  34. this.ionSplitPaneVisible.emit({ visible });
  35. }
  36. /**
  37. * @internal
  38. */
  39. async isVisible() {
  40. return Promise.resolve(this.visible);
  41. }
  42. async connectedCallback() {
  43. // TODO: connectedCallback is fired in CE build
  44. // before WC is defined. This needs to be fixed in Stencil.
  45. if (typeof customElements !== 'undefined' && customElements != null) {
  46. await customElements.whenDefined('ion-split-pane');
  47. }
  48. this.styleMainElement();
  49. this.updateState();
  50. }
  51. disconnectedCallback() {
  52. if (this.rmL) {
  53. this.rmL();
  54. this.rmL = undefined;
  55. }
  56. }
  57. updateState() {
  58. if (!Build.isBrowser) {
  59. return;
  60. }
  61. if (this.rmL) {
  62. this.rmL();
  63. this.rmL = undefined;
  64. }
  65. // Check if the split-pane is disabled
  66. if (this.disabled) {
  67. this.visible = false;
  68. return;
  69. }
  70. // When query is a boolean
  71. const query = this.when;
  72. if (typeof query === 'boolean') {
  73. this.visible = query;
  74. return;
  75. }
  76. // When query is a string, let's find first if it is a shortcut
  77. const mediaQuery = QUERY[query] || query;
  78. // Media query is empty or null, we hide it
  79. if (mediaQuery.length === 0) {
  80. this.visible = false;
  81. return;
  82. }
  83. // Listen on media query
  84. const callback = (q) => {
  85. this.visible = q.matches;
  86. };
  87. const mediaList = window.matchMedia(mediaQuery);
  88. // TODO FW-5869
  89. mediaList.addListener(callback);
  90. this.rmL = () => mediaList.removeListener(callback);
  91. this.visible = mediaList.matches;
  92. }
  93. /**
  94. * Attempt to find the main content
  95. * element inside of the split pane.
  96. * If found, set it as the main node.
  97. *
  98. * We assume that the main node
  99. * is available in the DOM on split
  100. * pane load.
  101. */
  102. styleMainElement() {
  103. if (!Build.isBrowser) {
  104. return;
  105. }
  106. const contentId = this.contentId;
  107. const children = this.el.children;
  108. const nu = this.el.childElementCount;
  109. let foundMain = false;
  110. for (let i = 0; i < nu; i++) {
  111. const child = children[i];
  112. const isMain = contentId !== undefined && child.id === contentId;
  113. if (isMain) {
  114. if (foundMain) {
  115. printIonWarning('[ion-split-pane] - Cannot have more than one main node.');
  116. return;
  117. }
  118. else {
  119. setPaneClass(child, isMain);
  120. foundMain = true;
  121. }
  122. }
  123. }
  124. if (!foundMain) {
  125. printIonWarning('[ion-split-pane] - Does not have a specified main node.');
  126. }
  127. }
  128. render() {
  129. const mode = getIonMode(this);
  130. return (h(Host, { key: '098801b5a318e2fc6913fb0d9079b1552927b99b', class: {
  131. [mode]: true,
  132. // Used internally for styling
  133. [`split-pane-${mode}`]: true,
  134. 'split-pane-visible': this.visible,
  135. } }, h("slot", { key: '8cbc6a942ecba54fc3c62027d46917db067b65c8' })));
  136. }
  137. get el() { return this; }
  138. static get watchers() { return {
  139. "visible": ["visibleChanged"],
  140. "disabled": ["updateState"],
  141. "when": ["updateState"]
  142. }; }
  143. static get style() { return {
  144. ios: IonSplitPaneIosStyle0,
  145. md: IonSplitPaneMdStyle0
  146. }; }
  147. }, [33, "ion-split-pane", {
  148. "contentId": [513, "content-id"],
  149. "disabled": [4],
  150. "when": [8],
  151. "visible": [32],
  152. "isVisible": [64]
  153. }, undefined, {
  154. "visible": ["visibleChanged"],
  155. "disabled": ["updateState"],
  156. "when": ["updateState"]
  157. }]);
  158. const setPaneClass = (el, isMain) => {
  159. let toAdd;
  160. let toRemove;
  161. if (isMain) {
  162. toAdd = SPLIT_PANE_MAIN;
  163. toRemove = SPLIT_PANE_SIDE;
  164. }
  165. else {
  166. toAdd = SPLIT_PANE_SIDE;
  167. toRemove = SPLIT_PANE_MAIN;
  168. }
  169. const classList = el.classList;
  170. classList.add(toAdd);
  171. classList.remove(toRemove);
  172. };
  173. function defineCustomElement$1() {
  174. if (typeof customElements === "undefined") {
  175. return;
  176. }
  177. const components = ["ion-split-pane"];
  178. components.forEach(tagName => { switch (tagName) {
  179. case "ion-split-pane":
  180. if (!customElements.get(tagName)) {
  181. customElements.define(tagName, SplitPane);
  182. }
  183. break;
  184. } });
  185. }
  186. const IonSplitPane = SplitPane;
  187. const defineCustomElement = defineCustomElement$1;
  188. export { IonSplitPane, defineCustomElement };