UNPKG

@platform/react

Version:

React refs and helpers.

32 lines (31 loc) 894 B
import { Subject } from 'rxjs'; import { share } from 'rxjs/operators'; import { is } from '@platform/util.is'; const _focus$ = new Subject(); export const focus$ = _focus$.pipe(share()); (() => { if (!is.browser) { return; } let last = document.activeElement; const hasChanged = () => { const current = document.activeElement; const result = last !== current; last = current; return result; }; const onEvent = (type) => { return () => { const e = { type, from: last || undefined, to: document.activeElement || undefined, }; if (hasChanged()) { _focus$.next(e); } }; }; window.addEventListener('focus', onEvent('FOCUS'), true); window.addEventListener('blur', onEvent('BLUR'), true); })();