@testing-library/user-event
Version:
Fire events the same way the user does
26 lines (23 loc) • 949 B
JavaScript
import { eventWrapper } from '../misc/eventWrapper.js';
import { findClosest } from '../misc/findClosest.js';
import { getActiveElement } from './getActiveElement.js';
import { isFocusable } from './isFocusable.js';
import { updateSelectionOnFocus } from './selection.js';
/**
* Focus closest focusable element.
*/ function focus(element) {
const target = findClosest(element, isFocusable);
const activeElement = getActiveElement(element.ownerDocument);
if ((target !== null && target !== void 0 ? target : element.ownerDocument.body) === activeElement) {
return;
} else if (target) {
eventWrapper(()=>target.focus());
} else {
eventWrapper(()=>{
var ref;
return (ref = activeElement) === null || ref === void 0 ? void 0 : ref.blur();
});
}
updateSelectionOnFocus(target !== null && target !== void 0 ? target : element.ownerDocument.body);
}
export { focus };