mirror of
https://github.com/Choices-js/Choices.git
synced 2024-06-19 22:25:09 +02:00
guard browser globals on server
This commit is contained in:
parent
515a294ab3
commit
533fda6d4a
|
@ -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;
|
||||
|
||||
|
|
|
@ -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 => {
|
||||
|
|
Loading…
Reference in a new issue