input-shims-279903e2.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { w as win, d as doc } from './index-a5d50daf.js';
  5. import { g as getScrollElement, c as scrollByPoint, f as findClosestIonContent } from './index-9a17db3d.js';
  6. import { a as addEventListener, b as removeEventListener, r as raf, c as componentOnReady } from './helpers-d94bc8ad.js';
  7. import { a as KeyboardResize, K as Keyboard } from './keyboard-73175e24.js';
  8. import './index-cfd9c1f2.js';
  9. import './capacitor-59395cbd.js';
  10. const cloneMap = new WeakMap();
  11. const relocateInput = (componentEl, inputEl, shouldRelocate, inputRelativeY = 0, disabledClonedInput = false) => {
  12. if (cloneMap.has(componentEl) === shouldRelocate) {
  13. return;
  14. }
  15. if (shouldRelocate) {
  16. addClone(componentEl, inputEl, inputRelativeY, disabledClonedInput);
  17. }
  18. else {
  19. removeClone(componentEl, inputEl);
  20. }
  21. };
  22. const isFocused = (input) => {
  23. /**
  24. * https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode
  25. * Calling getRootNode on an element in standard web page will return HTMLDocument.
  26. * Calling getRootNode on an element inside of the Shadow DOM will return the associated ShadowRoot.
  27. * Calling getRootNode on an element that is not attached to a document/shadow tree will return
  28. * the root of the DOM tree it belongs to.
  29. * isFocused is used for the hide-caret utility which only considers input/textarea elements
  30. * that are present in the DOM, so we don't set types for that final case since it does not apply.
  31. */
  32. return input === input.getRootNode().activeElement;
  33. };
  34. const addClone = (componentEl, inputEl, inputRelativeY, disabledClonedInput = false) => {
  35. // this allows for the actual input to receive the focus from
  36. // the user's touch event, but before it receives focus, it
  37. // moves the actual input to a location that will not screw
  38. // up the app's layout, and does not allow the native browser
  39. // to attempt to scroll the input into place (messing up headers/footers)
  40. // the cloned input fills the area of where native input should be
  41. // while the native input fakes out the browser by relocating itself
  42. // before it receives the actual focus event
  43. // We hide the focused input (with the visible caret) invisible by making it scale(0),
  44. const parentEl = inputEl.parentNode;
  45. // DOM WRITES
  46. const clonedEl = inputEl.cloneNode(false);
  47. clonedEl.classList.add('cloned-input');
  48. clonedEl.tabIndex = -1;
  49. /**
  50. * Making the cloned input disabled prevents
  51. * Chrome for Android from still scrolling
  52. * the entire page since this cloned input
  53. * will briefly be hidden by the keyboard
  54. * even though it is not focused.
  55. *
  56. * This is not needed on iOS. While this
  57. * does not cause functional issues on iOS,
  58. * the input still appears slightly dimmed even
  59. * if we set opacity: 1.
  60. */
  61. if (disabledClonedInput) {
  62. clonedEl.disabled = true;
  63. }
  64. parentEl.appendChild(clonedEl);
  65. cloneMap.set(componentEl, clonedEl);
  66. const doc = componentEl.ownerDocument;
  67. const tx = doc.dir === 'rtl' ? 9999 : -9999;
  68. componentEl.style.pointerEvents = 'none';
  69. inputEl.style.transform = `translate3d(${tx}px,${inputRelativeY}px,0) scale(0)`;
  70. };
  71. const removeClone = (componentEl, inputEl) => {
  72. const clone = cloneMap.get(componentEl);
  73. if (clone) {
  74. cloneMap.delete(componentEl);
  75. clone.remove();
  76. }
  77. componentEl.style.pointerEvents = '';
  78. inputEl.style.transform = '';
  79. };
  80. /**
  81. * Factoring in 50px gives us some room
  82. * in case the keyboard shows password/autofill bars
  83. * asynchronously.
  84. */
  85. const SCROLL_AMOUNT_PADDING = 50;
  86. const enableHideCaretOnScroll = (componentEl, inputEl, scrollEl) => {
  87. if (!scrollEl || !inputEl) {
  88. return () => {
  89. return;
  90. };
  91. }
  92. const scrollHideCaret = (shouldHideCaret) => {
  93. if (isFocused(inputEl)) {
  94. relocateInput(componentEl, inputEl, shouldHideCaret);
  95. }
  96. };
  97. const onBlur = () => relocateInput(componentEl, inputEl, false);
  98. const hideCaret = () => scrollHideCaret(true);
  99. const showCaret = () => scrollHideCaret(false);
  100. addEventListener(scrollEl, 'ionScrollStart', hideCaret);
  101. addEventListener(scrollEl, 'ionScrollEnd', showCaret);
  102. inputEl.addEventListener('blur', onBlur);
  103. return () => {
  104. removeEventListener(scrollEl, 'ionScrollStart', hideCaret);
  105. removeEventListener(scrollEl, 'ionScrollEnd', showCaret);
  106. inputEl.removeEventListener('blur', onBlur);
  107. };
  108. };
  109. const SKIP_SELECTOR = 'input, textarea, [no-blur], [contenteditable]';
  110. const enableInputBlurring = () => {
  111. let focused = true;
  112. let didScroll = false;
  113. const doc = document;
  114. const onScroll = () => {
  115. didScroll = true;
  116. };
  117. const onFocusin = () => {
  118. focused = true;
  119. };
  120. const onTouchend = (ev) => {
  121. // if app did scroll return early
  122. if (didScroll) {
  123. didScroll = false;
  124. return;
  125. }
  126. const active = doc.activeElement;
  127. if (!active) {
  128. return;
  129. }
  130. // only blur if the active element is a text-input or a textarea
  131. if (active.matches(SKIP_SELECTOR)) {
  132. return;
  133. }
  134. // if the selected target is the active element, do not blur
  135. const tapped = ev.target;
  136. if (tapped === active) {
  137. return;
  138. }
  139. if (tapped.matches(SKIP_SELECTOR) || tapped.closest(SKIP_SELECTOR)) {
  140. return;
  141. }
  142. focused = false;
  143. // TODO FW-2796: find a better way, why 50ms?
  144. setTimeout(() => {
  145. if (!focused) {
  146. active.blur();
  147. }
  148. }, 50);
  149. };
  150. addEventListener(doc, 'ionScrollStart', onScroll);
  151. doc.addEventListener('focusin', onFocusin, true);
  152. doc.addEventListener('touchend', onTouchend, false);
  153. return () => {
  154. removeEventListener(doc, 'ionScrollStart', onScroll, true);
  155. doc.removeEventListener('focusin', onFocusin, true);
  156. doc.removeEventListener('touchend', onTouchend, false);
  157. };
  158. };
  159. const SCROLL_ASSIST_SPEED = 0.3;
  160. const getScrollData = (componentEl, contentEl, keyboardHeight, platformHeight) => {
  161. var _a;
  162. const itemEl = (_a = componentEl.closest('ion-item,[ion-item]')) !== null && _a !== void 0 ? _a : componentEl;
  163. return calcScrollData(itemEl.getBoundingClientRect(), contentEl.getBoundingClientRect(), keyboardHeight, platformHeight);
  164. };
  165. const calcScrollData = (inputRect, contentRect, keyboardHeight, platformHeight) => {
  166. // compute input's Y values relative to the body
  167. const inputTop = inputRect.top;
  168. const inputBottom = inputRect.bottom;
  169. // compute visible area
  170. const visibleAreaTop = contentRect.top;
  171. const visibleAreaBottom = Math.min(contentRect.bottom, platformHeight - keyboardHeight);
  172. // compute safe area
  173. const safeAreaTop = visibleAreaTop + 15;
  174. const safeAreaBottom = visibleAreaBottom - SCROLL_AMOUNT_PADDING;
  175. // figure out if each edge of the input is within the safe area
  176. const distanceToBottom = safeAreaBottom - inputBottom;
  177. const distanceToTop = safeAreaTop - inputTop;
  178. // desiredScrollAmount is the negated distance to the safe area according to our calculations.
  179. const desiredScrollAmount = Math.round(distanceToBottom < 0 ? -distanceToBottom : distanceToTop > 0 ? -distanceToTop : 0);
  180. // our calculations make some assumptions that aren't always true, like the keyboard being closed when an input
  181. // gets focus, so make sure we don't scroll the input above the visible area
  182. const scrollAmount = Math.min(desiredScrollAmount, inputTop - visibleAreaTop);
  183. const distance = Math.abs(scrollAmount);
  184. const duration = distance / SCROLL_ASSIST_SPEED;
  185. const scrollDuration = Math.min(400, Math.max(150, duration));
  186. return {
  187. scrollAmount,
  188. scrollDuration,
  189. scrollPadding: keyboardHeight,
  190. inputSafeY: -(inputTop - safeAreaTop) + 4,
  191. };
  192. };
  193. const PADDING_TIMER_KEY = '$ionPaddingTimer';
  194. /**
  195. * Scroll padding adds additional padding to the bottom
  196. * of ion-content so that there is enough scroll space
  197. * for an input to be scrolled above the keyboard. This
  198. * is needed in environments where the webview does not
  199. * resize when the keyboard opens.
  200. *
  201. * Example: If an input at the bottom of ion-content is
  202. * focused, there is no additional scrolling space below
  203. * it, so the input cannot be scrolled above the keyboard.
  204. * Scroll padding fixes this by adding padding equal to the
  205. * height of the keyboard to the bottom of the content.
  206. *
  207. * Common environments where this is needed:
  208. * - Mobile Safari: The keyboard overlays the content
  209. * - Capacitor/Cordova on iOS: The keyboard overlays the content
  210. * when the KeyboardResize mode is set to 'none'.
  211. */
  212. const setScrollPadding = (contentEl, paddingAmount, clearCallback) => {
  213. const timer = contentEl[PADDING_TIMER_KEY];
  214. if (timer) {
  215. clearTimeout(timer);
  216. }
  217. if (paddingAmount > 0) {
  218. contentEl.style.setProperty('--keyboard-offset', `${paddingAmount}px`);
  219. }
  220. else {
  221. contentEl[PADDING_TIMER_KEY] = setTimeout(() => {
  222. contentEl.style.setProperty('--keyboard-offset', '0px');
  223. if (clearCallback) {
  224. clearCallback();
  225. }
  226. }, 120);
  227. }
  228. };
  229. /**
  230. * When an input is about to be focused,
  231. * set a timeout to clear any scroll padding
  232. * on the content. Note: The clearing
  233. * is done on a timeout so that if users
  234. * are moving focus from one input to the next
  235. * then re-adding scroll padding to the new
  236. * input with cancel the timeout to clear the
  237. * scroll padding.
  238. */
  239. const setClearScrollPaddingListener = (inputEl, contentEl, doneCallback) => {
  240. const clearScrollPadding = () => {
  241. if (contentEl) {
  242. setScrollPadding(contentEl, 0, doneCallback);
  243. }
  244. };
  245. inputEl.addEventListener('focusout', clearScrollPadding, { once: true });
  246. };
  247. let currentPadding = 0;
  248. const SKIP_SCROLL_ASSIST = 'data-ionic-skip-scroll-assist';
  249. const enableScrollAssist = (componentEl, inputEl, contentEl, footerEl, keyboardHeight, enableScrollPadding, keyboardResize, disableClonedInput = false) => {
  250. /**
  251. * Scroll padding should only be added if:
  252. * 1. The global scrollPadding config option
  253. * is set to true.
  254. * 2. The native keyboard resize mode is either "none"
  255. * (keyboard overlays webview) or undefined (resize
  256. * information unavailable)
  257. * Resize info is available on Capacitor 4+
  258. */
  259. const addScrollPadding = enableScrollPadding && (keyboardResize === undefined || keyboardResize.mode === KeyboardResize.None);
  260. /**
  261. * This tracks whether or not the keyboard has been
  262. * presented for a single focused text field. Note
  263. * that it does not track if the keyboard is open
  264. * in general such as if the keyboard is open for
  265. * a different focused text field.
  266. */
  267. let hasKeyboardBeenPresentedForTextField = false;
  268. /**
  269. * When adding scroll padding we need to know
  270. * how much of the viewport the keyboard obscures.
  271. * We do this by subtracting the keyboard height
  272. * from the platform height.
  273. *
  274. * If we compute this value when switching between
  275. * inputs then the webview may already be resized.
  276. * At this point, `win.innerHeight` has already accounted
  277. * for the keyboard meaning we would then subtract
  278. * the keyboard height again. This will result in the input
  279. * being scrolled more than it needs to.
  280. */
  281. const platformHeight = win !== undefined ? win.innerHeight : 0;
  282. /**
  283. * Scroll assist is run when a text field
  284. * is focused. However, it may need to
  285. * re-run when the keyboard size changes
  286. * such that the text field is now hidden
  287. * underneath the keyboard.
  288. * This function re-runs scroll assist
  289. * when that happens.
  290. *
  291. * One limitation of this is on a web browser
  292. * where native keyboard APIs do not have cross-browser
  293. * support. `ionKeyboardDidShow` relies on the Visual Viewport API.
  294. * This means that if the keyboard changes but does not change
  295. * geometry, then scroll assist will not re-run even if
  296. * the user has scrolled the text field under the keyboard.
  297. * This is not a problem when running in Cordova/Capacitor
  298. * because `ionKeyboardDidShow` uses the native events
  299. * which fire every time the keyboard changes.
  300. */
  301. const keyboardShow = (ev) => {
  302. /**
  303. * If the keyboard has not yet been presented
  304. * for this text field then the text field has just
  305. * received focus. In that case, the focusin listener
  306. * will run scroll assist.
  307. */
  308. if (hasKeyboardBeenPresentedForTextField === false) {
  309. hasKeyboardBeenPresentedForTextField = true;
  310. return;
  311. }
  312. /**
  313. * Otherwise, the keyboard has already been presented
  314. * for the focused text field.
  315. * This means that the keyboard likely changed
  316. * geometry, and we need to re-run scroll assist.
  317. * This can happen when the user rotates their device
  318. * or when they switch keyboards.
  319. *
  320. * Make sure we pass in the computed keyboard height
  321. * rather than the estimated keyboard height.
  322. *
  323. * Since the keyboard is already open then we do not
  324. * need to wait for the webview to resize, so we pass
  325. * "waitForResize: false".
  326. */
  327. jsSetFocus(componentEl, inputEl, contentEl, footerEl, ev.detail.keyboardHeight, addScrollPadding, disableClonedInput, platformHeight, false);
  328. };
  329. /**
  330. * Reset the internal state when the text field loses focus.
  331. */
  332. const focusOut = () => {
  333. hasKeyboardBeenPresentedForTextField = false;
  334. win === null || win === void 0 ? void 0 : win.removeEventListener('ionKeyboardDidShow', keyboardShow);
  335. componentEl.removeEventListener('focusout', focusOut);
  336. };
  337. /**
  338. * When the input is about to receive
  339. * focus, we need to move it to prevent
  340. * mobile Safari from adjusting the viewport.
  341. */
  342. const focusIn = async () => {
  343. /**
  344. * Scroll assist should not run again
  345. * on inputs that have been manually
  346. * focused inside of the scroll assist
  347. * implementation.
  348. */
  349. if (inputEl.hasAttribute(SKIP_SCROLL_ASSIST)) {
  350. inputEl.removeAttribute(SKIP_SCROLL_ASSIST);
  351. return;
  352. }
  353. jsSetFocus(componentEl, inputEl, contentEl, footerEl, keyboardHeight, addScrollPadding, disableClonedInput, platformHeight);
  354. win === null || win === void 0 ? void 0 : win.addEventListener('ionKeyboardDidShow', keyboardShow);
  355. componentEl.addEventListener('focusout', focusOut);
  356. };
  357. componentEl.addEventListener('focusin', focusIn);
  358. return () => {
  359. componentEl.removeEventListener('focusin', focusIn);
  360. win === null || win === void 0 ? void 0 : win.removeEventListener('ionKeyboardDidShow', keyboardShow);
  361. componentEl.removeEventListener('focusout', focusOut);
  362. };
  363. };
  364. /**
  365. * Use this function when you want to manually
  366. * focus an input but not have scroll assist run again.
  367. */
  368. const setManualFocus = (el) => {
  369. /**
  370. * If element is already focused then
  371. * a new focusin event will not be dispatched
  372. * to remove the SKIL_SCROLL_ASSIST attribute.
  373. */
  374. if (document.activeElement === el) {
  375. return;
  376. }
  377. el.setAttribute(SKIP_SCROLL_ASSIST, 'true');
  378. el.focus();
  379. };
  380. const jsSetFocus = async (componentEl, inputEl, contentEl, footerEl, keyboardHeight, enableScrollPadding, disableClonedInput = false, platformHeight = 0, waitForResize = true) => {
  381. if (!contentEl && !footerEl) {
  382. return;
  383. }
  384. const scrollData = getScrollData(componentEl, (contentEl || footerEl), keyboardHeight, platformHeight);
  385. if (contentEl && Math.abs(scrollData.scrollAmount) < 4) {
  386. // the text input is in a safe position that doesn't
  387. // require it to be scrolled into view, just set focus now
  388. setManualFocus(inputEl);
  389. /**
  390. * Even though the input does not need
  391. * scroll assist, we should preserve the
  392. * the scroll padding as users could be moving
  393. * focus from an input that needs scroll padding
  394. * to an input that does not need scroll padding.
  395. * If we remove the scroll padding now, users will
  396. * see the page jump.
  397. */
  398. if (enableScrollPadding && contentEl !== null) {
  399. setScrollPadding(contentEl, currentPadding);
  400. setClearScrollPaddingListener(inputEl, contentEl, () => (currentPadding = 0));
  401. }
  402. return;
  403. }
  404. // temporarily move the focus to the focus holder so the browser
  405. // doesn't freak out while it's trying to get the input in place
  406. // at this point the native text input still does not have focus
  407. relocateInput(componentEl, inputEl, true, scrollData.inputSafeY, disableClonedInput);
  408. setManualFocus(inputEl);
  409. /**
  410. * Relocating/Focusing input causes the
  411. * click event to be cancelled, so
  412. * manually fire one here.
  413. */
  414. raf(() => componentEl.click());
  415. /**
  416. * If enabled, we can add scroll padding to
  417. * the bottom of the content so that scroll assist
  418. * has enough room to scroll the input above
  419. * the keyboard.
  420. */
  421. if (enableScrollPadding && contentEl) {
  422. currentPadding = scrollData.scrollPadding;
  423. setScrollPadding(contentEl, currentPadding);
  424. }
  425. if (typeof window !== 'undefined') {
  426. let scrollContentTimeout;
  427. const scrollContent = async () => {
  428. // clean up listeners and timeouts
  429. if (scrollContentTimeout !== undefined) {
  430. clearTimeout(scrollContentTimeout);
  431. }
  432. window.removeEventListener('ionKeyboardDidShow', doubleKeyboardEventListener);
  433. window.removeEventListener('ionKeyboardDidShow', scrollContent);
  434. // scroll the input into place
  435. if (contentEl) {
  436. await scrollByPoint(contentEl, 0, scrollData.scrollAmount, scrollData.scrollDuration);
  437. }
  438. // the scroll view is in the correct position now
  439. // give the native text input focus
  440. relocateInput(componentEl, inputEl, false, scrollData.inputSafeY);
  441. // ensure this is the focused input
  442. setManualFocus(inputEl);
  443. /**
  444. * When the input is about to be blurred
  445. * we should set a timeout to remove
  446. * any scroll padding.
  447. */
  448. if (enableScrollPadding) {
  449. setClearScrollPaddingListener(inputEl, contentEl, () => (currentPadding = 0));
  450. }
  451. };
  452. const doubleKeyboardEventListener = () => {
  453. window.removeEventListener('ionKeyboardDidShow', doubleKeyboardEventListener);
  454. window.addEventListener('ionKeyboardDidShow', scrollContent);
  455. };
  456. if (contentEl) {
  457. const scrollEl = await getScrollElement(contentEl);
  458. /**
  459. * scrollData will only consider the amount we need
  460. * to scroll in order to properly bring the input
  461. * into view. It will not consider the amount
  462. * we can scroll in the content element.
  463. * As a result, scrollData may request a greater
  464. * scroll position than is currently available
  465. * in the DOM. If this is the case, we need to
  466. * wait for the webview to resize/the keyboard
  467. * to show in order for additional scroll
  468. * bandwidth to become available.
  469. */
  470. const totalScrollAmount = scrollEl.scrollHeight - scrollEl.clientHeight;
  471. if (waitForResize && scrollData.scrollAmount > totalScrollAmount - scrollEl.scrollTop) {
  472. /**
  473. * On iOS devices, the system will show a "Passwords" bar above the keyboard
  474. * after the initial keyboard is shown. This prevents the webview from resizing
  475. * until the "Passwords" bar is shown, so we need to wait for that to happen first.
  476. */
  477. if (inputEl.type === 'password') {
  478. // Add 50px to account for the "Passwords" bar
  479. scrollData.scrollAmount += SCROLL_AMOUNT_PADDING;
  480. window.addEventListener('ionKeyboardDidShow', doubleKeyboardEventListener);
  481. }
  482. else {
  483. window.addEventListener('ionKeyboardDidShow', scrollContent);
  484. }
  485. /**
  486. * This should only fire in 2 instances:
  487. * 1. The app is very slow.
  488. * 2. The app is running in a browser on an old OS
  489. * that does not support Ionic Keyboard Events
  490. */
  491. scrollContentTimeout = setTimeout(scrollContent, 1000);
  492. return;
  493. }
  494. }
  495. scrollContent();
  496. }
  497. };
  498. const INPUT_BLURRING = true;
  499. const startInputShims = async (config, platform) => {
  500. /**
  501. * If doc is undefined then we are in an SSR environment
  502. * where input shims do not apply.
  503. */
  504. if (doc === undefined) {
  505. return;
  506. }
  507. const isIOS = platform === 'ios';
  508. const isAndroid = platform === 'android';
  509. /**
  510. * Hide Caret and Input Blurring are needed on iOS.
  511. * Scroll Assist and Scroll Padding are needed on iOS and Android
  512. * with Chrome web browser (not Chrome webview).
  513. */
  514. const keyboardHeight = config.getNumber('keyboardHeight', 290);
  515. const scrollAssist = config.getBoolean('scrollAssist', true);
  516. const hideCaret = config.getBoolean('hideCaretOnScroll', isIOS);
  517. /**
  518. * The team is evaluating if inputBlurring is still needed. As a result
  519. * this feature is disabled by default as of Ionic 8.0. Developers are
  520. * able to re-enable it temporarily. The team may remove this utility
  521. * if it is determined that doing so would not bring any adverse side effects.
  522. * TODO FW-6014 remove input blurring utility (including implementation)
  523. */
  524. const inputBlurring = config.getBoolean('inputBlurring', false);
  525. const scrollPadding = config.getBoolean('scrollPadding', true);
  526. const inputs = Array.from(doc.querySelectorAll('ion-input, ion-textarea'));
  527. const hideCaretMap = new WeakMap();
  528. const scrollAssistMap = new WeakMap();
  529. /**
  530. * Grab the native keyboard resize configuration
  531. * and pass it to scroll assist. Scroll assist requires
  532. * that we adjust the input right before the input
  533. * is about to be focused. If we called `Keyboard.getResizeMode`
  534. * on focusin in scroll assist, we could potentially adjust the
  535. * input too late since this call is async.
  536. */
  537. const keyboardResizeMode = await Keyboard.getResizeMode();
  538. const registerInput = async (componentEl) => {
  539. await new Promise((resolve) => componentOnReady(componentEl, resolve));
  540. const inputRoot = componentEl.shadowRoot || componentEl;
  541. const inputEl = inputRoot.querySelector('input') || inputRoot.querySelector('textarea');
  542. const scrollEl = findClosestIonContent(componentEl);
  543. const footerEl = !scrollEl ? componentEl.closest('ion-footer') : null;
  544. if (!inputEl) {
  545. return;
  546. }
  547. if (!!scrollEl && hideCaret && !hideCaretMap.has(componentEl)) {
  548. const rmFn = enableHideCaretOnScroll(componentEl, inputEl, scrollEl);
  549. hideCaretMap.set(componentEl, rmFn);
  550. }
  551. /**
  552. * date/datetime-locale inputs on mobile devices show date picker
  553. * overlays instead of keyboards. As a result, scroll assist is
  554. * not needed. This also works around a bug in iOS <16 where
  555. * scroll assist causes the browser to lock up. See FW-1997.
  556. */
  557. const isDateInput = inputEl.type === 'date' || inputEl.type === 'datetime-local';
  558. if (!isDateInput &&
  559. (!!scrollEl || !!footerEl) &&
  560. scrollAssist &&
  561. !scrollAssistMap.has(componentEl)) {
  562. const rmFn = enableScrollAssist(componentEl, inputEl, scrollEl, footerEl, keyboardHeight, scrollPadding, keyboardResizeMode, isAndroid);
  563. scrollAssistMap.set(componentEl, rmFn);
  564. }
  565. };
  566. const unregisterInput = (componentEl) => {
  567. if (hideCaret) {
  568. const fn = hideCaretMap.get(componentEl);
  569. if (fn) {
  570. fn();
  571. }
  572. hideCaretMap.delete(componentEl);
  573. }
  574. if (scrollAssist) {
  575. const fn = scrollAssistMap.get(componentEl);
  576. if (fn) {
  577. fn();
  578. }
  579. scrollAssistMap.delete(componentEl);
  580. }
  581. };
  582. if (inputBlurring && INPUT_BLURRING) {
  583. enableInputBlurring();
  584. }
  585. // Input might be already loaded in the DOM before ion-device-hacks did.
  586. // At this point we need to look for all of the inputs not registered yet
  587. // and register them.
  588. for (const input of inputs) {
  589. registerInput(input);
  590. }
  591. doc.addEventListener('ionInputDidLoad', (ev) => {
  592. registerInput(ev.detail);
  593. });
  594. doc.addEventListener('ionInputDidUnload', (ev) => {
  595. unregisterInput(ev.detail);
  596. });
  597. };
  598. export { startInputShims };