guard browser globals on server

This commit is contained in:
Adam Mockor 2020-04-24 22:28:54 +02:00
parent 515a294ab3
commit 533fda6d4a
2 changed files with 16 additions and 0 deletions

View file

@ -40,6 +40,7 @@ import {
generateId,
existsInArray,
diff,
canUseDom,
} from './lib/utils';
import {
Options,
@ -54,6 +55,7 @@ import { defaultState } from './reducers';
/** @see {@link http://browserhacks.com/#hack-acea075d0ac6954f275a70023906050c} */
const IS_IE11 =
canUseDom &&
'-ms-scroll-limit' in document.documentElement.style &&
'-ms-ime-align' in document.documentElement.style;

View file

@ -1,5 +1,11 @@
import { EventMap, Choice } from '../interfaces';
export const canUseDom = !!(
typeof window !== 'undefined' &&
window.document &&
window.document.createElement
);
/* eslint-disable @typescript-eslint/no-explicit-any */
export const getRandomNumber = (min: number, max: number): number =>
@ -97,6 +103,14 @@ export const sanitise = <T>(value: T | string): T | string => {
};
export const strToEl = ((): ((str: string) => Element) => {
// do not run this in non-browser environment
if (!canUseDom) {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-empty-function
return (): void => {};
}
const tmpEl = document.createElement('div');
return (str): Element => {