ion-col.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { proxyCustomElement, HTMLElement, forceUpdate, h, Host } from '@stencil/core/internal/client';
  5. import { b as getIonMode } from './ionic-global.js';
  6. const SIZE_TO_MEDIA = {
  7. xs: '(min-width: 0px)',
  8. sm: '(min-width: 576px)',
  9. md: '(min-width: 768px)',
  10. lg: '(min-width: 992px)',
  11. xl: '(min-width: 1200px)',
  12. };
  13. // Check if the window matches the media query
  14. // at the breakpoint passed
  15. // e.g. matchBreakpoint('sm') => true if screen width exceeds 576px
  16. const matchBreakpoint = (breakpoint) => {
  17. if (breakpoint === undefined || breakpoint === '') {
  18. return true;
  19. }
  20. if (window.matchMedia) {
  21. const mediaQuery = SIZE_TO_MEDIA[breakpoint];
  22. return window.matchMedia(mediaQuery).matches;
  23. }
  24. return false;
  25. };
  26. const colCss = ":host{-webkit-padding-start:var(--ion-grid-column-padding-xs, var(--ion-grid-column-padding, 5px));padding-inline-start:var(--ion-grid-column-padding-xs, var(--ion-grid-column-padding, 5px));-webkit-padding-end:var(--ion-grid-column-padding-xs, var(--ion-grid-column-padding, 5px));padding-inline-end:var(--ion-grid-column-padding-xs, var(--ion-grid-column-padding, 5px));padding-top:var(--ion-grid-column-padding-xs, var(--ion-grid-column-padding, 5px));padding-bottom:var(--ion-grid-column-padding-xs, var(--ion-grid-column-padding, 5px));margin-left:0;margin-right:0;margin-top:0;margin-bottom:0;-webkit-box-sizing:border-box;box-sizing:border-box;position:relative;-ms-flex-preferred-size:0;flex-basis:0;-ms-flex-positive:1;flex-grow:1;width:100%;max-width:100%;min-height:1px}@media (min-width: 576px){:host{-webkit-padding-start:var(--ion-grid-column-padding-sm, var(--ion-grid-column-padding, 5px));padding-inline-start:var(--ion-grid-column-padding-sm, var(--ion-grid-column-padding, 5px));-webkit-padding-end:var(--ion-grid-column-padding-sm, var(--ion-grid-column-padding, 5px));padding-inline-end:var(--ion-grid-column-padding-sm, var(--ion-grid-column-padding, 5px));padding-top:var(--ion-grid-column-padding-sm, var(--ion-grid-column-padding, 5px));padding-bottom:var(--ion-grid-column-padding-sm, var(--ion-grid-column-padding, 5px))}}@media (min-width: 768px){:host{-webkit-padding-start:var(--ion-grid-column-padding-md, var(--ion-grid-column-padding, 5px));padding-inline-start:var(--ion-grid-column-padding-md, var(--ion-grid-column-padding, 5px));-webkit-padding-end:var(--ion-grid-column-padding-md, var(--ion-grid-column-padding, 5px));padding-inline-end:var(--ion-grid-column-padding-md, var(--ion-grid-column-padding, 5px));padding-top:var(--ion-grid-column-padding-md, var(--ion-grid-column-padding, 5px));padding-bottom:var(--ion-grid-column-padding-md, var(--ion-grid-column-padding, 5px))}}@media (min-width: 992px){:host{-webkit-padding-start:var(--ion-grid-column-padding-lg, var(--ion-grid-column-padding, 5px));padding-inline-start:var(--ion-grid-column-padding-lg, var(--ion-grid-column-padding, 5px));-webkit-padding-end:var(--ion-grid-column-padding-lg, var(--ion-grid-column-padding, 5px));padding-inline-end:var(--ion-grid-column-padding-lg, var(--ion-grid-column-padding, 5px));padding-top:var(--ion-grid-column-padding-lg, var(--ion-grid-column-padding, 5px));padding-bottom:var(--ion-grid-column-padding-lg, var(--ion-grid-column-padding, 5px))}}@media (min-width: 1200px){:host{-webkit-padding-start:var(--ion-grid-column-padding-xl, var(--ion-grid-column-padding, 5px));padding-inline-start:var(--ion-grid-column-padding-xl, var(--ion-grid-column-padding, 5px));-webkit-padding-end:var(--ion-grid-column-padding-xl, var(--ion-grid-column-padding, 5px));padding-inline-end:var(--ion-grid-column-padding-xl, var(--ion-grid-column-padding, 5px));padding-top:var(--ion-grid-column-padding-xl, var(--ion-grid-column-padding, 5px));padding-bottom:var(--ion-grid-column-padding-xl, var(--ion-grid-column-padding, 5px))}}";
  27. const IonColStyle0 = colCss;
  28. const win = typeof window !== 'undefined' ? window : undefined;
  29. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  30. const SUPPORTS_VARS = win && !!(win.CSS && win.CSS.supports && win.CSS.supports('--a: 0'));
  31. const BREAKPOINTS = ['', 'xs', 'sm', 'md', 'lg', 'xl'];
  32. const Col = /*@__PURE__*/ proxyCustomElement(class Col extends HTMLElement {
  33. constructor() {
  34. super();
  35. this.__registerHost();
  36. this.__attachShadow();
  37. this.offset = undefined;
  38. this.offsetXs = undefined;
  39. this.offsetSm = undefined;
  40. this.offsetMd = undefined;
  41. this.offsetLg = undefined;
  42. this.offsetXl = undefined;
  43. this.pull = undefined;
  44. this.pullXs = undefined;
  45. this.pullSm = undefined;
  46. this.pullMd = undefined;
  47. this.pullLg = undefined;
  48. this.pullXl = undefined;
  49. this.push = undefined;
  50. this.pushXs = undefined;
  51. this.pushSm = undefined;
  52. this.pushMd = undefined;
  53. this.pushLg = undefined;
  54. this.pushXl = undefined;
  55. this.size = undefined;
  56. this.sizeXs = undefined;
  57. this.sizeSm = undefined;
  58. this.sizeMd = undefined;
  59. this.sizeLg = undefined;
  60. this.sizeXl = undefined;
  61. }
  62. onResize() {
  63. forceUpdate(this);
  64. }
  65. // Loop through all of the breakpoints to see if the media query
  66. // matches and grab the column value from the relevant prop if so
  67. getColumns(property) {
  68. let matched;
  69. for (const breakpoint of BREAKPOINTS) {
  70. const matches = matchBreakpoint(breakpoint);
  71. // Grab the value of the property, if it exists and our
  72. // media query matches we return the value
  73. const columns = this[property + breakpoint.charAt(0).toUpperCase() + breakpoint.slice(1)];
  74. if (matches && columns !== undefined) {
  75. matched = columns;
  76. }
  77. }
  78. // Return the last matched columns since the breakpoints
  79. // increase in size and we want to return the largest match
  80. return matched;
  81. }
  82. calculateSize() {
  83. const columns = this.getColumns('size');
  84. // If size wasn't set for any breakpoint
  85. // or if the user set the size without a value
  86. // it means we need to stick with the default and return
  87. // e.g. <ion-col size-md>
  88. if (!columns || columns === '') {
  89. return;
  90. }
  91. // If the size is set to auto then don't calculate a size
  92. const colSize = columns === 'auto'
  93. ? 'auto'
  94. : // If CSS supports variables we should use the grid columns var
  95. SUPPORTS_VARS
  96. ? `calc(calc(${columns} / var(--ion-grid-columns, 12)) * 100%)`
  97. : // Convert the columns to a percentage by dividing by the total number
  98. // of columns (12) and then multiplying by 100
  99. (columns / 12) * 100 + '%';
  100. return {
  101. flex: `0 0 ${colSize}`,
  102. width: `${colSize}`,
  103. 'max-width': `${colSize}`,
  104. };
  105. }
  106. // Called by push, pull, and offset since they use the same calculations
  107. calculatePosition(property, modifier) {
  108. const columns = this.getColumns(property);
  109. if (!columns) {
  110. return;
  111. }
  112. // If the number of columns passed are greater than 0 and less than
  113. // 12 we can position the column, else default to auto
  114. const amount = SUPPORTS_VARS
  115. ? // If CSS supports variables we should use the grid columns var
  116. `calc(calc(${columns} / var(--ion-grid-columns, 12)) * 100%)`
  117. : // Convert the columns to a percentage by dividing by the total number
  118. // of columns (12) and then multiplying by 100
  119. columns > 0 && columns < 12
  120. ? (columns / 12) * 100 + '%'
  121. : 'auto';
  122. return {
  123. [modifier]: amount,
  124. };
  125. }
  126. calculateOffset(isRTL) {
  127. return this.calculatePosition('offset', isRTL ? 'margin-right' : 'margin-left');
  128. }
  129. calculatePull(isRTL) {
  130. return this.calculatePosition('pull', isRTL ? 'left' : 'right');
  131. }
  132. calculatePush(isRTL) {
  133. return this.calculatePosition('push', isRTL ? 'right' : 'left');
  134. }
  135. render() {
  136. const isRTL = document.dir === 'rtl';
  137. const mode = getIonMode(this);
  138. return (h(Host, { key: '32ed75d81dd09d9bc8999f6d42e5b3cb99c84d91', class: {
  139. [mode]: true,
  140. }, style: Object.assign(Object.assign(Object.assign(Object.assign({}, this.calculateOffset(isRTL)), this.calculatePull(isRTL)), this.calculatePush(isRTL)), this.calculateSize()) }, h("slot", { key: '38f8d0440c20cc6d1b1d6a654d07f16de61d8134' })));
  141. }
  142. static get style() { return IonColStyle0; }
  143. }, [1, "ion-col", {
  144. "offset": [1],
  145. "offsetXs": [1, "offset-xs"],
  146. "offsetSm": [1, "offset-sm"],
  147. "offsetMd": [1, "offset-md"],
  148. "offsetLg": [1, "offset-lg"],
  149. "offsetXl": [1, "offset-xl"],
  150. "pull": [1],
  151. "pullXs": [1, "pull-xs"],
  152. "pullSm": [1, "pull-sm"],
  153. "pullMd": [1, "pull-md"],
  154. "pullLg": [1, "pull-lg"],
  155. "pullXl": [1, "pull-xl"],
  156. "push": [1],
  157. "pushXs": [1, "push-xs"],
  158. "pushSm": [1, "push-sm"],
  159. "pushMd": [1, "push-md"],
  160. "pushLg": [1, "push-lg"],
  161. "pushXl": [1, "push-xl"],
  162. "size": [1],
  163. "sizeXs": [1, "size-xs"],
  164. "sizeSm": [1, "size-sm"],
  165. "sizeMd": [1, "size-md"],
  166. "sizeLg": [1, "size-lg"],
  167. "sizeXl": [1, "size-xl"]
  168. }, [[9, "resize", "onResize"]]]);
  169. function defineCustomElement$1() {
  170. if (typeof customElements === "undefined") {
  171. return;
  172. }
  173. const components = ["ion-col"];
  174. components.forEach(tagName => { switch (tagName) {
  175. case "ion-col":
  176. if (!customElements.get(tagName)) {
  177. customElements.define(tagName, Col);
  178. }
  179. break;
  180. } });
  181. }
  182. const IonCol = Col;
  183. const defineCustomElement = defineCustomElement$1;
  184. export { IonCol, defineCustomElement };