@base-ui/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
30 lines • 600 B
JavaScript
import { NOOP } from '@base-ui/utils/empty';
let rafId = 0;
export function enqueueFocus(el, options = {}) {
const {
preventScroll = false,
sync = false,
shouldFocus
} = options;
cancelAnimationFrame(rafId);
function exec() {
if (shouldFocus && !shouldFocus()) {
return;
}
el?.focus({
preventScroll
});
}
if (sync) {
exec();
return NOOP;
}
const currentRafId = requestAnimationFrame(exec);
rafId = currentRafId;
return () => {
if (rafId === currentRafId) {
cancelAnimationFrame(currentRafId);
rafId = 0;
}
};
}