ion-accordion.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
  5. import { r as raf, t as transitionEndAsync, a as addEventListener, b as removeEventListener, g as getElementRoot } from './helpers.js';
  6. import { c as chevronDown } from './index7.js';
  7. import { c as config } from './index4.js';
  8. import { b as getIonMode } from './ionic-global.js';
  9. import { d as defineCustomElement$2 } from './icon.js';
  10. const accordionIosCss = ":host{display:block;position:relative;width:100%;background-color:var(--ion-background-color, #ffffff);overflow:hidden;z-index:0}:host(.accordion-expanding) ::slotted(ion-item[slot=header]),:host(.accordion-expanded) ::slotted(ion-item[slot=header]){--border-width:0px}:host(.accordion-animated){-webkit-transition:all 300ms cubic-bezier(0.25, 0.8, 0.5, 1);transition:all 300ms cubic-bezier(0.25, 0.8, 0.5, 1)}:host(.accordion-animated) #content{-webkit-transition:max-height 300ms cubic-bezier(0.25, 0.8, 0.5, 1);transition:max-height 300ms cubic-bezier(0.25, 0.8, 0.5, 1)}#content{overflow:hidden;will-change:max-height}:host(.accordion-collapsing) #content{max-height:0 !important}:host(.accordion-collapsed) #content{display:none}:host(.accordion-expanding) #content{max-height:0}:host(.accordion-expanding) #content-wrapper{overflow:auto}:host(.accordion-disabled) #header,:host(.accordion-readonly) #header,:host(.accordion-disabled) #content,:host(.accordion-readonly) #content{pointer-events:none}:host(.accordion-disabled) #header,:host(.accordion-disabled) #content{opacity:0.4}@media (prefers-reduced-motion: reduce){:host,#content{-webkit-transition:none !important;transition:none !important}}:host(.accordion-next) ::slotted(ion-item[slot=header]){--border-width:0.55px 0px 0.55px 0px}";
  11. const IonAccordionIosStyle0 = accordionIosCss;
  12. const accordionMdCss = ":host{display:block;position:relative;width:100%;background-color:var(--ion-background-color, #ffffff);overflow:hidden;z-index:0}:host(.accordion-expanding) ::slotted(ion-item[slot=header]),:host(.accordion-expanded) ::slotted(ion-item[slot=header]){--border-width:0px}:host(.accordion-animated){-webkit-transition:all 300ms cubic-bezier(0.25, 0.8, 0.5, 1);transition:all 300ms cubic-bezier(0.25, 0.8, 0.5, 1)}:host(.accordion-animated) #content{-webkit-transition:max-height 300ms cubic-bezier(0.25, 0.8, 0.5, 1);transition:max-height 300ms cubic-bezier(0.25, 0.8, 0.5, 1)}#content{overflow:hidden;will-change:max-height}:host(.accordion-collapsing) #content{max-height:0 !important}:host(.accordion-collapsed) #content{display:none}:host(.accordion-expanding) #content{max-height:0}:host(.accordion-expanding) #content-wrapper{overflow:auto}:host(.accordion-disabled) #header,:host(.accordion-readonly) #header,:host(.accordion-disabled) #content,:host(.accordion-readonly) #content{pointer-events:none}:host(.accordion-disabled) #header,:host(.accordion-disabled) #content{opacity:0.4}@media (prefers-reduced-motion: reduce){:host,#content{-webkit-transition:none !important;transition:none !important}}";
  13. const IonAccordionMdStyle0 = accordionMdCss;
  14. const Accordion = /*@__PURE__*/ proxyCustomElement(class Accordion extends HTMLElement {
  15. constructor() {
  16. super();
  17. this.__registerHost();
  18. this.__attachShadow();
  19. this.updateListener = () => this.updateState(false);
  20. this.setItemDefaults = () => {
  21. const ionItem = this.getSlottedHeaderIonItem();
  22. if (!ionItem) {
  23. return;
  24. }
  25. /**
  26. * For a11y purposes, we make
  27. * the ion-item a button so users
  28. * can tab to it and use keyboard
  29. * navigation to get around.
  30. */
  31. ionItem.button = true;
  32. ionItem.detail = false;
  33. /**
  34. * By default, the lines in an
  35. * item should be full here, but
  36. * only do that if a user has
  37. * not explicitly overridden them
  38. */
  39. if (ionItem.lines === undefined) {
  40. ionItem.lines = 'full';
  41. }
  42. };
  43. this.getSlottedHeaderIonItem = () => {
  44. const { headerEl } = this;
  45. if (!headerEl) {
  46. return;
  47. }
  48. /**
  49. * Get the first ion-item
  50. * slotted in the header slot
  51. */
  52. const slot = headerEl.querySelector('slot');
  53. if (!slot) {
  54. return;
  55. }
  56. // This is not defined in unit tests
  57. if (slot.assignedElements === undefined)
  58. return;
  59. return slot.assignedElements().find((el) => el.tagName === 'ION-ITEM');
  60. };
  61. this.setAria = (expanded = false) => {
  62. const ionItem = this.getSlottedHeaderIonItem();
  63. if (!ionItem) {
  64. return;
  65. }
  66. /**
  67. * Get the native <button> element inside of
  68. * ion-item because that is what will be focused
  69. */
  70. const root = getElementRoot(ionItem);
  71. const button = root.querySelector('button');
  72. if (!button) {
  73. return;
  74. }
  75. button.setAttribute('aria-expanded', `${expanded}`);
  76. };
  77. this.slotToggleIcon = () => {
  78. const ionItem = this.getSlottedHeaderIonItem();
  79. if (!ionItem) {
  80. return;
  81. }
  82. const { toggleIconSlot, toggleIcon } = this;
  83. /**
  84. * Check if there already is a toggle icon.
  85. * If so, do not add another one.
  86. */
  87. const existingToggleIcon = ionItem.querySelector('.ion-accordion-toggle-icon');
  88. if (existingToggleIcon) {
  89. return;
  90. }
  91. const iconEl = document.createElement('ion-icon');
  92. iconEl.slot = toggleIconSlot;
  93. iconEl.lazy = false;
  94. iconEl.classList.add('ion-accordion-toggle-icon');
  95. iconEl.icon = toggleIcon;
  96. iconEl.setAttribute('aria-hidden', 'true');
  97. ionItem.appendChild(iconEl);
  98. };
  99. this.expandAccordion = (initialUpdate = false) => {
  100. const { contentEl, contentElWrapper } = this;
  101. if (initialUpdate || contentEl === undefined || contentElWrapper === undefined) {
  102. this.state = 4 /* AccordionState.Expanded */;
  103. return;
  104. }
  105. if (this.state === 4 /* AccordionState.Expanded */) {
  106. return;
  107. }
  108. if (this.currentRaf !== undefined) {
  109. cancelAnimationFrame(this.currentRaf);
  110. }
  111. if (this.shouldAnimate()) {
  112. raf(() => {
  113. this.state = 8 /* AccordionState.Expanding */;
  114. this.currentRaf = raf(async () => {
  115. const contentHeight = contentElWrapper.offsetHeight;
  116. const waitForTransition = transitionEndAsync(contentEl, 2000);
  117. contentEl.style.setProperty('max-height', `${contentHeight}px`);
  118. await waitForTransition;
  119. this.state = 4 /* AccordionState.Expanded */;
  120. contentEl.style.removeProperty('max-height');
  121. });
  122. });
  123. }
  124. else {
  125. this.state = 4 /* AccordionState.Expanded */;
  126. }
  127. };
  128. this.collapseAccordion = (initialUpdate = false) => {
  129. const { contentEl } = this;
  130. if (initialUpdate || contentEl === undefined) {
  131. this.state = 1 /* AccordionState.Collapsed */;
  132. return;
  133. }
  134. if (this.state === 1 /* AccordionState.Collapsed */) {
  135. return;
  136. }
  137. if (this.currentRaf !== undefined) {
  138. cancelAnimationFrame(this.currentRaf);
  139. }
  140. if (this.shouldAnimate()) {
  141. this.currentRaf = raf(async () => {
  142. const contentHeight = contentEl.offsetHeight;
  143. contentEl.style.setProperty('max-height', `${contentHeight}px`);
  144. raf(async () => {
  145. const waitForTransition = transitionEndAsync(contentEl, 2000);
  146. this.state = 2 /* AccordionState.Collapsing */;
  147. await waitForTransition;
  148. this.state = 1 /* AccordionState.Collapsed */;
  149. contentEl.style.removeProperty('max-height');
  150. });
  151. });
  152. }
  153. else {
  154. this.state = 1 /* AccordionState.Collapsed */;
  155. }
  156. };
  157. /**
  158. * Helper function to determine if
  159. * something should animate.
  160. * If prefers-reduced-motion is set
  161. * then we should not animate, regardless
  162. * of what is set in the config.
  163. */
  164. this.shouldAnimate = () => {
  165. if (typeof window === 'undefined') {
  166. return false;
  167. }
  168. const prefersReducedMotion = matchMedia('(prefers-reduced-motion: reduce)').matches;
  169. if (prefersReducedMotion) {
  170. return false;
  171. }
  172. const animated = config.get('animated', true);
  173. if (!animated) {
  174. return false;
  175. }
  176. if (this.accordionGroupEl && !this.accordionGroupEl.animated) {
  177. return false;
  178. }
  179. return true;
  180. };
  181. this.updateState = async (initialUpdate = false) => {
  182. const accordionGroup = this.accordionGroupEl;
  183. const accordionValue = this.value;
  184. if (!accordionGroup) {
  185. return;
  186. }
  187. const value = accordionGroup.value;
  188. const shouldExpand = Array.isArray(value) ? value.includes(accordionValue) : value === accordionValue;
  189. if (shouldExpand) {
  190. this.expandAccordion(initialUpdate);
  191. this.isNext = this.isPrevious = false;
  192. }
  193. else {
  194. this.collapseAccordion(initialUpdate);
  195. /**
  196. * When using popout or inset,
  197. * the collapsed accordion items
  198. * may need additional border radius
  199. * applied. Check to see if the
  200. * next or previous accordion is selected.
  201. */
  202. const nextAccordion = this.getNextSibling();
  203. const nextAccordionValue = nextAccordion === null || nextAccordion === void 0 ? void 0 : nextAccordion.value;
  204. if (nextAccordionValue !== undefined) {
  205. this.isPrevious = Array.isArray(value) ? value.includes(nextAccordionValue) : value === nextAccordionValue;
  206. }
  207. const previousAccordion = this.getPreviousSibling();
  208. const previousAccordionValue = previousAccordion === null || previousAccordion === void 0 ? void 0 : previousAccordion.value;
  209. if (previousAccordionValue !== undefined) {
  210. this.isNext = Array.isArray(value) ? value.includes(previousAccordionValue) : value === previousAccordionValue;
  211. }
  212. }
  213. };
  214. this.getNextSibling = () => {
  215. if (!this.el) {
  216. return;
  217. }
  218. const nextSibling = this.el.nextElementSibling;
  219. if ((nextSibling === null || nextSibling === void 0 ? void 0 : nextSibling.tagName) !== 'ION-ACCORDION') {
  220. return;
  221. }
  222. return nextSibling;
  223. };
  224. this.getPreviousSibling = () => {
  225. if (!this.el) {
  226. return;
  227. }
  228. const previousSibling = this.el.previousElementSibling;
  229. if ((previousSibling === null || previousSibling === void 0 ? void 0 : previousSibling.tagName) !== 'ION-ACCORDION') {
  230. return;
  231. }
  232. return previousSibling;
  233. };
  234. this.state = 1 /* AccordionState.Collapsed */;
  235. this.isNext = false;
  236. this.isPrevious = false;
  237. this.value = `ion-accordion-${accordionIds++}`;
  238. this.disabled = false;
  239. this.readonly = false;
  240. this.toggleIcon = chevronDown;
  241. this.toggleIconSlot = 'end';
  242. }
  243. valueChanged() {
  244. this.updateState();
  245. }
  246. connectedCallback() {
  247. var _a;
  248. const accordionGroupEl = (this.accordionGroupEl = (_a = this.el) === null || _a === void 0 ? void 0 : _a.closest('ion-accordion-group'));
  249. if (accordionGroupEl) {
  250. this.updateState(true);
  251. addEventListener(accordionGroupEl, 'ionValueChange', this.updateListener);
  252. }
  253. }
  254. disconnectedCallback() {
  255. const accordionGroupEl = this.accordionGroupEl;
  256. if (accordionGroupEl) {
  257. removeEventListener(accordionGroupEl, 'ionValueChange', this.updateListener);
  258. }
  259. }
  260. componentDidLoad() {
  261. this.setItemDefaults();
  262. this.slotToggleIcon();
  263. /**
  264. * We need to wait a tick because we
  265. * just set ionItem.button = true and
  266. * the button has not have been rendered yet.
  267. */
  268. raf(() => {
  269. /**
  270. * Set aria label on button inside of ion-item
  271. * once the inner content has been rendered.
  272. */
  273. const expanded = this.state === 4 /* AccordionState.Expanded */ || this.state === 8 /* AccordionState.Expanding */;
  274. this.setAria(expanded);
  275. });
  276. }
  277. toggleExpanded() {
  278. const { accordionGroupEl, disabled, readonly, value, state } = this;
  279. if (disabled || readonly)
  280. return;
  281. if (accordionGroupEl) {
  282. /**
  283. * Because the accordion group may or may
  284. * not allow multiple accordions open, we
  285. * need to request the toggling of this
  286. * accordion and the accordion group will
  287. * make the decision on whether or not
  288. * to allow it.
  289. */
  290. const expand = state === 1 /* AccordionState.Collapsed */ || state === 2 /* AccordionState.Collapsing */;
  291. accordionGroupEl.requestAccordionToggle(value, expand);
  292. }
  293. }
  294. render() {
  295. const { disabled, readonly } = this;
  296. const mode = getIonMode(this);
  297. const expanded = this.state === 4 /* AccordionState.Expanded */ || this.state === 8 /* AccordionState.Expanding */;
  298. const headerPart = expanded ? 'header expanded' : 'header';
  299. const contentPart = expanded ? 'content expanded' : 'content';
  300. this.setAria(expanded);
  301. return (h(Host, { key: '073e1d02c18dcbc20c68648426e87c14750c031d', class: {
  302. [mode]: true,
  303. 'accordion-expanding': this.state === 8 /* AccordionState.Expanding */,
  304. 'accordion-expanded': this.state === 4 /* AccordionState.Expanded */,
  305. 'accordion-collapsing': this.state === 2 /* AccordionState.Collapsing */,
  306. 'accordion-collapsed': this.state === 1 /* AccordionState.Collapsed */,
  307. 'accordion-next': this.isNext,
  308. 'accordion-previous': this.isPrevious,
  309. 'accordion-disabled': disabled,
  310. 'accordion-readonly': readonly,
  311. 'accordion-animated': this.shouldAnimate(),
  312. } }, h("div", { key: '9b4cf326de8bb6b4033992903c0c1bfd7eea9bcc', onClick: () => this.toggleExpanded(), id: "header", part: headerPart, "aria-controls": "content", ref: (headerEl) => (this.headerEl = headerEl) }, h("slot", { key: '464c32a37f64655eacf4218284214f5f30b14a1e', name: "header" })), h("div", { key: '8bb52e6a62d7de0106b253201a89a32e79d9a594', id: "content", part: contentPart, role: "region", "aria-labelledby": "header", ref: (contentEl) => (this.contentEl = contentEl) }, h("div", { key: '1d9dfd952ad493754aaeea7a8f625b33c2dd90a0', id: "content-wrapper", ref: (contentElWrapper) => (this.contentElWrapper = contentElWrapper) }, h("slot", { key: '970dfbc55a612d739d0ca3b7b1a08e5c96d0c479', name: "content" })))));
  313. }
  314. static get delegatesFocus() { return true; }
  315. get el() { return this; }
  316. static get watchers() { return {
  317. "value": ["valueChanged"]
  318. }; }
  319. static get style() { return {
  320. ios: IonAccordionIosStyle0,
  321. md: IonAccordionMdStyle0
  322. }; }
  323. }, [49, "ion-accordion", {
  324. "value": [1],
  325. "disabled": [4],
  326. "readonly": [4],
  327. "toggleIcon": [1, "toggle-icon"],
  328. "toggleIconSlot": [1, "toggle-icon-slot"],
  329. "state": [32],
  330. "isNext": [32],
  331. "isPrevious": [32]
  332. }, undefined, {
  333. "value": ["valueChanged"]
  334. }]);
  335. let accordionIds = 0;
  336. function defineCustomElement$1() {
  337. if (typeof customElements === "undefined") {
  338. return;
  339. }
  340. const components = ["ion-accordion", "ion-icon"];
  341. components.forEach(tagName => { switch (tagName) {
  342. case "ion-accordion":
  343. if (!customElements.get(tagName)) {
  344. customElements.define(tagName, Accordion);
  345. }
  346. break;
  347. case "ion-icon":
  348. if (!customElements.get(tagName)) {
  349. defineCustomElement$2();
  350. }
  351. break;
  352. } });
  353. }
  354. const IonAccordion = Accordion;
  355. const defineCustomElement = defineCustomElement$1;
  356. export { IonAccordion, defineCustomElement };