focus method

Future<Online> focus(
  1. AbstractSelector selector, {
  2. bool show = true,
})

Implementation

Future<Online> focus(AbstractSelector selector, {bool show = true}) async {
  if (show) Show.focusing(selector.selector);
  var item = await (await page).waitForSelector(selector.selector);
  if (item != null) {
    await item.evaluate('''(element) => {
    element.scrollIntoView({behavior: "auto", block: "center", inline: "center"});
  }''');
    await item.hover();
    await item.focus();
    // just in case if the element focusable is child of this element, we should bubble up the focus
    await item.evaluate('''(element) => {
    // bubble down the focus to the first focusable child
    var focusable = element.querySelector('input,button,textarea,select');
    if (focusable) {
      focusable.focus();
    }
    else {
      //find the first focusable parent and focus on it
      var parent = element.parentElement;
      while (parent) {
        var focusable = parent.querySelector('input,button,textarea,select');
        if (focusable) {
          focusable.focus();
          break;
        }
        parent = parent.parentElement;
      }
    }
  }''');
  }
  return this;
}