Repository: ionic-team/ionic-framework
File: core/src/utils/input-shims/hacks/scroll-assist.ts (TypeScript)

Code before:
 * Use this function when you want to manually
 * focus an input but not have scroll assist run again.
 */
const setManualFocus = (el: HTMLElement) => {
  /**
   * If element is already focused then
   * a new focusin event will not be dispatched
   * to remove the SKIL_SCROLL_ASSIST attribute.
   */
  if (document.activeElement === el) {
    return;
  }

  /**
   * Optimization for scenarios where the currently focused element is a sibling
   * of the target element. In such cases, we avoid setting `SKIP_SCROLL_ASSIST`.
   *
   * This is crucial for accessibility: input elements can now contain focusable
   * siblings (e.g., clear buttons, slotted elements). If we didn't skip setting
   * the attribute here, screen readers would be unable to navigate to and interact
   * with these sibling elements.
   *
   * Without this check, we would need to call `ev.stopPropagation()` on the
   * 'focusin' event of each focusable sibling to prevent the scroll assist
   * listener from incorrectly moving focus back to the input. This approach
   * is less maintainable and more error-prone.
   */
  if (document.activeElement?.parentNode === el.parentNode) {
    return;
  }

  el.setAttribute(SKIP_SCROLL_ASSIST, 'true');
  el.focus();
};

const jsSetFocus = async (
  componentEl: HTMLElement,
  inputEl: HTMLInputElement | HTMLTextAreaElement,
  contentEl: HTMLElement | null,
  footerEl: HTMLIonFooterElement | null,
  keyboardHeight: number,
  enableScrollPadding: boolean,
  disableClonedInput = false,
  platformHeight = 0,
  waitForResize = true
) => {
  if (!contentEl && !footerEl) {
    return;
  }
  const scrollData = getScrollData(componentEl, (contentEl || footerEl)!, keyboardHeight, platformHeight);


Review comment (bug):
```suggestion
   * listener from incorrectly moving focus back to the input. That approach
   * would be less maintainable and more error-prone.
```

Technically I think the way it is now is fine, just a little confusing to me. I tried to clear up confusion, but feel free to disregard this suggestion

Code after:
 * Use this function when you want to manually
 * focus an input but not have scroll assist run again.
 */
const setManualFocus = (el: HTMLElement) => {
  /**
   * If element is already focused then
   * a new focusin event will not be dispatched
   * to remove the SKIL_SCROLL_ASSIST attribute.
   */
  if (document.activeElement === el) {
    return;
  }

  /**
   * Optimization for scenarios where the currently focused element is a sibling
   * of the target element. In such cases, we avoid setting `SKIP_SCROLL_ASSIST`.
   *
   * This is crucial for accessibility: input elements can now contain focusable
   * siblings (e.g., clear buttons, slotted elements). If we didn't skip setting
   * the attribute here, screen readers would be unable to navigate to and interact
   * with these sibling elements.
   *
   * Without this check, we would need to call `ev.stopPropagation()` on the
   * 'focusin' event of each focusable sibling to prevent the scroll assist
   * listener from incorrectly moving focus back to the input. That approach
   * would be less maintainable and more error-prone.
   */
  const inputId = el.getAttribute('id');
  const label = el.closest(`label[for="${inputId}"]`);
  const activeElLabel = document.activeElement?.closest(`label[for="${inputId}"]`);

  if (label !== null && label === activeElLabel) {
    // If the label is the same as the active element label, then
    // we don't need to set the `SKIP_SCROLL_ASSIST` and reset focus.
    return;
  }

  el.setAttribute(SKIP_SCROLL_ASSIST, 'true');
  el.focus();
};

const jsSetFocus = async (
  componentEl: HTMLElement,
  inputEl: HTMLInputElement | HTMLTextAreaElement,
  contentEl: HTMLElement | null,
  footerEl: HTMLIonFooterElement | null,
  keyboardHeight: number,
  enableScrollPadding: boolean,
  disableClonedInput = false,
  platformHeight = 0,
  waitForResize = true