react-use/src/misc/util.ts
xobotyi a087deb48e
fix: proper definition for isBrowser and isNavigator states.
should fix: #1777

Also introduce jest config for ssr tests (it fails hard now).
2021-01-31 10:33:48 +03:00

23 lines
787 B
TypeScript

export const noop = () => {};
export function on<T extends Window | Document | HTMLElement | EventTarget>(
obj: T | null,
...args: Parameters<T['addEventListener']> | [string, Function | null, ...any]
): void {
if (obj && obj.addEventListener) {
obj.addEventListener(...(args as Parameters<HTMLElement['addEventListener']>));
}
}
export function off<T extends Window | Document | HTMLElement | EventTarget>(
obj: T | null,
...args: Parameters<T['removeEventListener']> | [string, Function | null, ...any]
): void {
if (obj && obj.removeEventListener) {
obj.removeEventListener(...(args as Parameters<HTMLElement['removeEventListener']>));
}
}
export const isBrowser = typeof window !== 'undefined';
export const isNavigator = typeof navigator !== 'undefined';