@entur/utils 1.0.0-beta.0 → 1.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/PolymorphicComponent.d.ts +1 -4
- package/dist/breakpoints.scss +1 -1
- package/dist/color-utils.scss +4 -2
- package/dist/getNodeText.d.ts +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/layers.scss +11 -0
- package/dist/mergeRefs.d.ts +1 -2
- package/dist/types/variants.d.ts +1 -0
- package/dist/useControllableProp.d.ts +6 -0
- package/dist/useOnClickOutside.d.ts +2 -2
- package/dist/useOnEscape.d.ts +2 -2
- package/dist/useRandomId.d.ts +11 -0
- package/dist/utils.cjs.js +180 -0
- package/dist/utils.cjs.js.map +1 -0
- package/dist/utils.esm.js +118 -127
- package/dist/utils.esm.js.map +1 -1
- package/package.json +34 -9
- package/dist/index.js +0 -8
- package/dist/utils.cjs.development.js +0 -202
- package/dist/utils.cjs.development.js.map +0 -1
- package/dist/utils.cjs.production.min.js +0 -2
- package/dist/utils.cjs.production.min.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* https://www.benmvp.com/blog/forwarding-refs-polymorphic-react-component-typescript/
|
|
3
|
-
*/
|
|
4
|
-
import React from 'react';
|
|
1
|
+
import { default as React } from 'react';
|
|
5
2
|
export type PropsOf<C extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>> = JSX.LibraryManagedAttributes<C, React.ComponentPropsWithoutRef<C>>;
|
|
6
3
|
type AsProp<C extends React.ElementType> = {
|
|
7
4
|
/**
|
package/dist/breakpoints.scss
CHANGED
package/dist/color-utils.scss
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
@use 'sass:color';
|
|
2
|
+
|
|
1
3
|
/// Slightly lighten a color
|
|
2
4
|
/// @access public
|
|
3
5
|
/// @param {Color} $color - color to tint
|
|
4
6
|
/// @param {Number} $percentage - percentage of `$color` in returned color
|
|
5
7
|
/// @return {Color}
|
|
6
8
|
@function tint($color, $percentage) {
|
|
7
|
-
@return mix(white, $color, $percentage);
|
|
9
|
+
@return color.mix(white, $color, $percentage);
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
/// Slightly darken a color
|
|
@@ -13,5 +15,5 @@
|
|
|
13
15
|
/// @param {Number} $percentage - percentage of `$color` in returned color
|
|
14
16
|
/// @return {Color}
|
|
15
17
|
@function shade($color, $percentage) {
|
|
16
|
-
@return mix(black, $color, $percentage);
|
|
18
|
+
@return color.mix(black, $color, $percentage);
|
|
17
19
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getNodeText: (node: React.ReactNode | string | number | string[] | number[]) => string;
|
package/dist/index.d.ts
CHANGED
|
@@ -9,3 +9,6 @@ export * from './useOnEscape';
|
|
|
9
9
|
export * from './useWindowDimensions';
|
|
10
10
|
export * from './ConditionalWrapper';
|
|
11
11
|
export * from './warnAboutMissingStyles';
|
|
12
|
+
export * from './types/variants';
|
|
13
|
+
export * from './getNodeText';
|
|
14
|
+
export * from './useControllableProp';
|
package/dist/layers.scss
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Top-level layer order (lowest to highest precedence)
|
|
2
|
+
@layer core, third-party, components, utilities, app;
|
|
3
|
+
|
|
4
|
+
// Foundation: resets, design tokens (CSS variables), and base element styles
|
|
5
|
+
@layer core.reset, core.tokens, core.base;
|
|
6
|
+
|
|
7
|
+
// Third-party: vendor CSS imports, then DS overrides to tame them
|
|
8
|
+
@layer third-party.imports, third-party.overrides;
|
|
9
|
+
|
|
10
|
+
// Components: primitives (buttons, icons), composites (datepicker, dropdown), then cross-component overrides
|
|
11
|
+
@layer components.primitives, components.composites, components.overrides;
|
package/dist/mergeRefs.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const mergeRefs: <T extends HTMLElement>(...refs: (import("react").MutableRefObject<T> | import("react").ForwardedRef<T> | undefined)[]) => (node: T) => void;
|
|
1
|
+
export declare const mergeRefs: <T extends HTMLElement>(...refs: (React.MutableRefObject<T> | React.ForwardedRef<T> | ((node: T | null) => void) | undefined)[]) => (node: T) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type VariantType = 'success' | 'negative' | 'warning' | 'information';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
export declare const useOnClickOutside: (refs: React.RefObject<
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
export declare const useOnClickOutside: (refs: (React.RefObject<HTMLElement> | React.MutableRefObject<any>)[], handler: () => void) => void;
|
package/dist/useOnEscape.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
export declare const useOnEscape: (ref: React.RefObject<
|
|
1
|
+
import { default as React } from 'react';
|
|
2
|
+
export declare const useOnEscape: (ref: React.RefObject<any> | React.MutableRefObject<any>, handler: () => void) => void;
|
package/dist/useRandomId.d.ts
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated Use `useId()` from React directly instead.
|
|
3
|
+
* Will be removed in a future major version.
|
|
4
|
+
*
|
|
5
|
+
* ```diff
|
|
6
|
+
* - import { useRandomId } from '@entur/utils';
|
|
7
|
+
* - const id = useRandomId('eds-my-component');
|
|
8
|
+
* + import { useId } from 'react';
|
|
9
|
+
* + const id = `eds-my-component${useId()}`;
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
1
12
|
export declare const useRandomId: (prefix?: string) => string;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const React = require("react");
|
|
4
|
+
const warning = require("tiny-warning");
|
|
5
|
+
function useDebounce(callBack, debounceTime) {
|
|
6
|
+
const timeoutRef = React.useRef();
|
|
7
|
+
React.useEffect(() => {
|
|
8
|
+
return () => {
|
|
9
|
+
if (timeoutRef.current) {
|
|
10
|
+
clearTimeout(timeoutRef.current);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
}, []);
|
|
14
|
+
const debouncedFunc = (...args) => {
|
|
15
|
+
if (timeoutRef.current) {
|
|
16
|
+
clearTimeout(timeoutRef.current);
|
|
17
|
+
}
|
|
18
|
+
timeoutRef.current = setTimeout(() => {
|
|
19
|
+
callBack(...args);
|
|
20
|
+
}, debounceTime);
|
|
21
|
+
};
|
|
22
|
+
return debouncedFunc;
|
|
23
|
+
}
|
|
24
|
+
const useRandomId = (prefix) => {
|
|
25
|
+
const id = React.useId();
|
|
26
|
+
return prefix ? `${prefix}${id}` : id;
|
|
27
|
+
};
|
|
28
|
+
function useOnMount(callback) {
|
|
29
|
+
const hasRun = React.useRef(false);
|
|
30
|
+
React.useEffect(() => {
|
|
31
|
+
if (!hasRun.current) {
|
|
32
|
+
hasRun.current = true;
|
|
33
|
+
callback();
|
|
34
|
+
}
|
|
35
|
+
}, [callback]);
|
|
36
|
+
}
|
|
37
|
+
const mergeRefs = (...refs) => {
|
|
38
|
+
return (node) => {
|
|
39
|
+
for (const ref of refs) {
|
|
40
|
+
if (typeof ref === "function") {
|
|
41
|
+
ref(node);
|
|
42
|
+
} else if (ref) ref.current = node;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
const useForceUpdate = () => {
|
|
47
|
+
const [_, setValue] = React.useState(0);
|
|
48
|
+
return () => setValue((value) => value + 1);
|
|
49
|
+
};
|
|
50
|
+
const useOnClickOutside = (refs, handler) => {
|
|
51
|
+
React.useEffect(() => {
|
|
52
|
+
const listener = (event) => {
|
|
53
|
+
if (refs.some((ref) => elementContainsEventTarget(ref.current, event))) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
handler();
|
|
57
|
+
};
|
|
58
|
+
document.addEventListener("mousedown", listener);
|
|
59
|
+
document.addEventListener("touchstart", listener);
|
|
60
|
+
return () => {
|
|
61
|
+
document.removeEventListener("mousedown", listener);
|
|
62
|
+
document.removeEventListener("touchstart", listener);
|
|
63
|
+
};
|
|
64
|
+
}, [refs, handler]);
|
|
65
|
+
};
|
|
66
|
+
const elementContainsEventTarget = (element, event) => {
|
|
67
|
+
if (!element) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
if (element.contains(event.target)) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
if (event.composed && event.composedPath) {
|
|
74
|
+
const contains = event.composedPath().find((target) => {
|
|
75
|
+
if (target === window) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return element.contains(target);
|
|
79
|
+
});
|
|
80
|
+
return contains;
|
|
81
|
+
}
|
|
82
|
+
return false;
|
|
83
|
+
};
|
|
84
|
+
const useOnEscape = (ref, handler) => {
|
|
85
|
+
React.useEffect(() => {
|
|
86
|
+
const runIfKeyIsEscape = (event) => {
|
|
87
|
+
if (event.key === "Escape") handler();
|
|
88
|
+
};
|
|
89
|
+
const currentRef = ref.current;
|
|
90
|
+
currentRef?.addEventListener("keydown", runIfKeyIsEscape);
|
|
91
|
+
return () => currentRef?.removeEventListener("keydown", runIfKeyIsEscape);
|
|
92
|
+
}, [ref, handler]);
|
|
93
|
+
};
|
|
94
|
+
const subscribe = (callback) => {
|
|
95
|
+
window.addEventListener("resize", callback);
|
|
96
|
+
return () => window.removeEventListener("resize", callback);
|
|
97
|
+
};
|
|
98
|
+
let cachedSnapshot = { width: void 0, height: void 0 };
|
|
99
|
+
const getSnapshot = () => {
|
|
100
|
+
const width = window.innerWidth;
|
|
101
|
+
const height = window.innerHeight;
|
|
102
|
+
if (cachedSnapshot.width !== width || cachedSnapshot.height !== height) {
|
|
103
|
+
cachedSnapshot = { width, height };
|
|
104
|
+
}
|
|
105
|
+
return cachedSnapshot;
|
|
106
|
+
};
|
|
107
|
+
const serverSnapshot = {
|
|
108
|
+
width: void 0,
|
|
109
|
+
height: void 0
|
|
110
|
+
};
|
|
111
|
+
const getServerSnapshot = () => serverSnapshot;
|
|
112
|
+
const useWindowDimensions = () => {
|
|
113
|
+
return React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
114
|
+
};
|
|
115
|
+
const ConditionalWrapper = ({
|
|
116
|
+
condition,
|
|
117
|
+
wrapper,
|
|
118
|
+
children
|
|
119
|
+
}) => condition ? wrapper(children) : children;
|
|
120
|
+
const packagesToCheck = /* @__PURE__ */ new Set();
|
|
121
|
+
let checkTimeoutId;
|
|
122
|
+
function checkAndWarn() {
|
|
123
|
+
const missingImports = Array.from(packagesToCheck).filter(
|
|
124
|
+
(namespace) => parseInt(
|
|
125
|
+
window.getComputedStyle(document.documentElement).getPropertyValue(`--eds-${namespace}`)
|
|
126
|
+
) !== 1
|
|
127
|
+
).sort();
|
|
128
|
+
const singleMissingImport = missingImports.length === 1;
|
|
129
|
+
warning(
|
|
130
|
+
missingImports.length === 0,
|
|
131
|
+
`You are missing ${singleMissingImport ? "a CSS import" : `${missingImports.length} CSS imports`}!
|
|
132
|
+
|
|
133
|
+
Please add the following CSS import${singleMissingImport ? "" : "s"} somewhere in your app:
|
|
134
|
+
|
|
135
|
+
${missingImports.map((namespace) => ` @import '@entur/${namespace}/dist/styles.css';`).join("\n")}
|
|
136
|
+
`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
function warnAboutMissingStyles(...namespaces) {
|
|
140
|
+
if (typeof window === "undefined" || typeof process !== "undefined" && process?.env?.TEST === "true" || typeof process !== "undefined" && process?.env?.NODE_ENV === "production") {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
window.clearTimeout(checkTimeoutId);
|
|
144
|
+
namespaces.forEach((namespace) => packagesToCheck.add(namespace));
|
|
145
|
+
checkTimeoutId = window.setTimeout(checkAndWarn, 1e3);
|
|
146
|
+
}
|
|
147
|
+
const getNodeText = (node) => {
|
|
148
|
+
if (node === null || node === void 0) return "";
|
|
149
|
+
if (["string", "number"].includes(typeof node)) return node.toString();
|
|
150
|
+
if (node instanceof Array) return node.map(getNodeText).join("").trim();
|
|
151
|
+
if (typeof node === "object")
|
|
152
|
+
return getNodeText(node.props?.children ?? "").trim();
|
|
153
|
+
return "unknown";
|
|
154
|
+
};
|
|
155
|
+
function useControllableProp({
|
|
156
|
+
prop,
|
|
157
|
+
updater = () => void 0,
|
|
158
|
+
defaultValue
|
|
159
|
+
}) {
|
|
160
|
+
const [internalState, setInternalState] = React.useState(defaultValue);
|
|
161
|
+
React.useEffect(() => {
|
|
162
|
+
if (prop !== void 0) {
|
|
163
|
+
setInternalState(prop);
|
|
164
|
+
}
|
|
165
|
+
}, [prop]);
|
|
166
|
+
return prop === void 0 ? [internalState, setInternalState] : [prop, updater];
|
|
167
|
+
}
|
|
168
|
+
exports.ConditionalWrapper = ConditionalWrapper;
|
|
169
|
+
exports.getNodeText = getNodeText;
|
|
170
|
+
exports.mergeRefs = mergeRefs;
|
|
171
|
+
exports.useControllableProp = useControllableProp;
|
|
172
|
+
exports.useDebounce = useDebounce;
|
|
173
|
+
exports.useForceUpdate = useForceUpdate;
|
|
174
|
+
exports.useOnClickOutside = useOnClickOutside;
|
|
175
|
+
exports.useOnEscape = useOnEscape;
|
|
176
|
+
exports.useOnMount = useOnMount;
|
|
177
|
+
exports.useRandomId = useRandomId;
|
|
178
|
+
exports.useWindowDimensions = useWindowDimensions;
|
|
179
|
+
exports.warnAboutMissingStyles = warnAboutMissingStyles;
|
|
180
|
+
//# sourceMappingURL=utils.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.cjs.js","sources":["../src/useDebounce.ts","../src/useRandomId.ts","../src/useOnMount.ts","../src/mergeRefs.ts","../src/useForceUpdate.ts","../src/useOnClickOutside.ts","../src/useOnEscape.ts","../src/useWindowDimensions.ts","../src/ConditionalWrapper.tsx","../src/warnAboutMissingStyles.ts","../src/getNodeText.ts","../src/useControllableProp.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\n\nexport function useDebounce<T extends (...args: any[]) => any>(\n callBack: T,\n debounceTime: number,\n) {\n const timeoutRef = useRef<ReturnType<typeof setTimeout>>();\n\n useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n };\n }, []);\n\n const debouncedFunc = (...args: any[]) => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n\n timeoutRef.current = setTimeout(() => {\n callBack(...args);\n }, debounceTime);\n };\n\n return debouncedFunc as T;\n}\n","import { useId } from 'react';\n\n/**\n * @deprecated Use `useId()` from React directly instead.\n * Will be removed in a future major version.\n *\n * ```diff\n * - import { useRandomId } from '@entur/utils';\n * - const id = useRandomId('eds-my-component');\n * + import { useId } from 'react';\n * + const id = `eds-my-component${useId()}`;\n * ```\n */\nexport const useRandomId = (prefix?: string): string => {\n const id = useId();\n return prefix ? `${prefix}${id}` : id;\n};\n","import React from 'react';\n\nexport function useOnMount(callback: () => void): void {\n const hasRun = React.useRef<boolean>(false);\n\n React.useEffect(() => {\n if (!hasRun.current) {\n hasRun.current = true;\n callback();\n }\n }, [callback]);\n}\n","export const mergeRefs = <T extends HTMLElement>(\n ...refs: (\n | React.MutableRefObject<T>\n | React.ForwardedRef<T>\n | ((node: T | null) => void)\n | undefined\n )[]\n) => {\n return (node: T) => {\n for (const ref of refs) {\n if (typeof ref === 'function') {\n ref(node);\n } else if (ref) ref.current = node;\n }\n };\n};\n","import { useState } from 'react';\n\nexport const useForceUpdate = () => {\n const [_, setValue] = useState(0);\n return () => setValue(value => value + 1);\n};\n","import React, { useEffect } from 'react';\n\nexport const useOnClickOutside = (\n refs: (React.RefObject<HTMLElement> | React.MutableRefObject<any>)[],\n handler: () => void,\n) => {\n useEffect(() => {\n const listener = (event: Event) => {\n // If the ref contains the clicked element, then the click is not outside\n if (refs.some(ref => elementContainsEventTarget(ref.current, event))) {\n return;\n }\n\n handler();\n };\n\n document.addEventListener('mousedown', listener);\n document.addEventListener('touchstart', listener);\n\n return () => {\n document.removeEventListener('mousedown', listener);\n document.removeEventListener('touchstart', listener);\n };\n }, [refs, handler]);\n};\n\nconst elementContainsEventTarget = (\n element: HTMLElement | null,\n event: Event,\n) => {\n if (!element) {\n return false;\n }\n\n if (element.contains(event.target as Node)) {\n return true;\n }\n\n // For elements inside a Shadow DOM we need to check the composedPath\n if (event.composed && event.composedPath) {\n const contains = event.composedPath().find(target => {\n if (target === window) {\n return false;\n }\n return element.contains(target as Node);\n });\n return contains;\n }\n\n return false;\n};\n","import React, { useEffect } from 'react';\n\nexport const useOnEscape = (\n ref: React.RefObject<any> | React.MutableRefObject<any>,\n handler: () => void,\n) => {\n useEffect(() => {\n const runIfKeyIsEscape = (event: KeyboardEvent) => {\n if (event.key === 'Escape') handler();\n };\n\n const currentRef = ref.current;\n currentRef?.addEventListener('keydown', runIfKeyIsEscape);\n\n return () => currentRef?.removeEventListener('keydown', runIfKeyIsEscape);\n }, [ref, handler]);\n};\n","import { useSyncExternalStore } from 'react';\n\ntype WindowDimensions = {\n width: number | undefined;\n height: number | undefined;\n};\n\nconst subscribe = (callback: () => void) => {\n window.addEventListener('resize', callback);\n return () => window.removeEventListener('resize', callback);\n};\n\nlet cachedSnapshot: WindowDimensions = { width: undefined, height: undefined };\n\nconst getSnapshot = (): WindowDimensions => {\n const width = window.innerWidth;\n const height = window.innerHeight;\n if (cachedSnapshot.width !== width || cachedSnapshot.height !== height) {\n cachedSnapshot = { width, height };\n }\n return cachedSnapshot;\n};\n\nconst serverSnapshot: WindowDimensions = {\n width: undefined,\n height: undefined,\n};\nconst getServerSnapshot = () => serverSnapshot;\n\nexport const useWindowDimensions = (): WindowDimensions => {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n};\n","type ConditionalWrapperType = {\n condition: boolean;\n wrapper: any;\n children: any;\n};\n\nexport const ConditionalWrapper = ({\n condition,\n wrapper,\n children,\n}: ConditionalWrapperType) => (condition ? wrapper(children) : children);\n","import warning from 'tiny-warning';\n\nconst packagesToCheck: Set<string> = new Set();\nlet checkTimeoutId: number;\n\nfunction checkAndWarn() {\n const missingImports = Array.from(packagesToCheck)\n .filter(\n namespace =>\n parseInt(\n window\n .getComputedStyle(document.documentElement)\n .getPropertyValue(`--eds-${namespace}`),\n ) !== 1,\n )\n .sort();\n\n // Finally, we warn about those pesky imports\n const singleMissingImport = missingImports.length === 1;\n warning(\n missingImports.length === 0,\n `You are missing ${\n singleMissingImport\n ? 'a CSS import'\n : `${missingImports.length} CSS imports`\n }!\n\nPlease add the following CSS import${\n singleMissingImport ? '' : 's'\n } somewhere in your app:\n\n${missingImports\n .map(namespace => `\\t@import '@entur/${namespace}/dist/styles.css';`)\n .join('\\n')}\n`,\n );\n}\n\n/** Warns the developer if they have forgotten to include styles */\nexport function warnAboutMissingStyles(...namespaces: string[]): void {\n // We skip this check in production, and when we build static sites\n if (\n typeof window === 'undefined' ||\n (typeof process !== 'undefined' && process?.env?.TEST === 'true') ||\n (typeof process !== 'undefined' && process?.env?.NODE_ENV === 'production')\n ) {\n return;\n }\n // First, let's clear earlier calls to setTimeout\n window.clearTimeout(checkTimeoutId);\n\n // Next, let's add all namespaces to the set of packages to check\n namespaces.forEach(namespace => packagesToCheck.add(namespace));\n\n // Finally. let's trigger a run of the checker.\n checkTimeoutId = window.setTimeout(checkAndWarn, 1000);\n}\n","// with inspiration from https://stackoverflow.com/questions/50428910/get-text-content-from-node-in-react\nexport const getNodeText = (\n node: React.ReactNode | string | number | string[] | number[],\n): string => {\n if (node === null || node === undefined) return '';\n if (['string', 'number'].includes(typeof node)) return node.toString();\n if (node instanceof Array) return node.map(getNodeText).join('').trim();\n if (typeof node === 'object')\n // @ts-expect-error props does exist for react nodes\n return getNodeText(node.props?.children ?? '').trim();\n return 'unknown';\n};\n","import { useEffect, useState } from 'react';\nexport type UseControllablePropType<T> = {\n prop?: T;\n updater?: (value?: T) => void;\n defaultValue: T;\n};\nexport function useControllableProp<T>({\n prop,\n updater = () => undefined,\n defaultValue,\n}: UseControllablePropType<T>): [T, (arg: T) => void] {\n const [internalState, setInternalState] = useState<T>(defaultValue);\n useEffect(() => {\n if (prop !== undefined) {\n setInternalState(prop);\n }\n }, [prop]);\n return prop === undefined\n ? [internalState, setInternalState]\n : [prop, updater];\n}\n"],"names":["useRef","useEffect","useId","useState","useSyncExternalStore"],"mappings":";;;;AAEO,SAAS,YACd,UACA,cACA;AACA,QAAM,aAAaA,MAAAA,OAAA;AAEnBC,QAAAA,UAAU,MAAM;AACd,WAAO,MAAM;AACX,UAAI,WAAW,SAAS;AACtB,qBAAa,WAAW,OAAO;AAAA,MACjC;AAAA,IACF;AAAA,EACF,GAAG,CAAA,CAAE;AAEL,QAAM,gBAAgB,IAAI,SAAgB;AACxC,QAAI,WAAW,SAAS;AACtB,mBAAa,WAAW,OAAO;AAAA,IACjC;AAEA,eAAW,UAAU,WAAW,MAAM;AACpC,eAAS,GAAG,IAAI;AAAA,IAClB,GAAG,YAAY;AAAA,EACjB;AAEA,SAAO;AACT;ACdO,MAAM,cAAc,CAAC,WAA4B;AACtD,QAAM,KAAKC,MAAAA,MAAA;AACX,SAAO,SAAS,GAAG,MAAM,GAAG,EAAE,KAAK;AACrC;ACdO,SAAS,WAAW,UAA4B;AACrD,QAAM,SAAS,MAAM,OAAgB,KAAK;AAE1C,QAAM,UAAU,MAAM;AACpB,QAAI,CAAC,OAAO,SAAS;AACnB,aAAO,UAAU;AACjB,eAAA;AAAA,IACF;AAAA,EACF,GAAG,CAAC,QAAQ,CAAC;AACf;ACXO,MAAM,YAAY,IACpB,SAMA;AACH,SAAO,CAAC,SAAY;AAClB,eAAW,OAAO,MAAM;AACtB,UAAI,OAAO,QAAQ,YAAY;AAC7B,YAAI,IAAI;AAAA,MACV,WAAW,IAAK,KAAI,UAAU;AAAA,IAChC;AAAA,EACF;AACF;ACbO,MAAM,iBAAiB,MAAM;AAClC,QAAM,CAAC,GAAG,QAAQ,IAAIC,MAAAA,SAAS,CAAC;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AGlC,aAAW,QAAQ,CAAA,cAAa,gBAAgB,IAAI,SAAS,CAAC;AAG9D,mBAAiB,OAAO,WAAW,cAAc,GAAI;AACvD;ACvDO,MAAM,cAAc,CACzB,SACW;AACX,MAAI,SAAS,QAAQ,SAAS,OAAW,QAAO;AAChD,MAAI,CAAC,UAAU,QAAQ,EAAE,SAAS,OAAO,IAAI,EAAG,QAAO,KAAK,SAAA;AAC5D,MAAI,gBAAgB,MAAO,QAAO,KAAK,IAAI,WAAW,EAAE,KAAK,EAAE,EAAE,KAAA;AACjE,MAAI,OAAO,SAAS;AAElB,WAAO,YAAY,KAAK,OAAO,YAAY,EAAE,EAAE,KAAA;AACjD,SAAO;AACT;ACLO,SAAS,oBAAuB;AAAA,EACrC;AAAA,EACA,UAAU,MAAM;AAAA,EAChB;AACF,GAAsD;AACpD,QAAM,CAAC,eAAe,gBAAgB,IAAID,MAAAA,SAAY,YAAY;AAClEF,QAAAA,UAAU,MAAM;AACd,QAAI,SAAS,QAAW;AACtB,uBAAiB,IAAI;AAAA,IACvB;AAAA,EACF,GAAG,CAAC,IAAI,CAAC;AACT,SAAO,SAAS,SACZ,CAAC,eAAe,gBAAgB,IAChC,CAAC,MAAM,OAAO;AACpB;;;;;;;;;;;;;"}
|
package/dist/utils.esm.js
CHANGED
|
@@ -1,189 +1,180 @@
|
|
|
1
|
-
import React, { useRef, useEffect, useState } from
|
|
2
|
-
import warning from
|
|
3
|
-
|
|
1
|
+
import React, { useRef, useEffect, useId, useState, useSyncExternalStore } from "react";
|
|
2
|
+
import warning from "tiny-warning";
|
|
4
3
|
function useDebounce(callBack, debounceTime) {
|
|
5
|
-
|
|
6
|
-
useEffect(
|
|
7
|
-
return
|
|
4
|
+
const timeoutRef = useRef();
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
return () => {
|
|
8
7
|
if (timeoutRef.current) {
|
|
9
8
|
clearTimeout(timeoutRef.current);
|
|
10
9
|
}
|
|
11
10
|
};
|
|
12
11
|
}, []);
|
|
13
|
-
|
|
14
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
15
|
-
args[_key] = arguments[_key];
|
|
16
|
-
}
|
|
12
|
+
const debouncedFunc = (...args) => {
|
|
17
13
|
if (timeoutRef.current) {
|
|
18
14
|
clearTimeout(timeoutRef.current);
|
|
19
15
|
}
|
|
20
|
-
timeoutRef.current = setTimeout(
|
|
21
|
-
callBack
|
|
16
|
+
timeoutRef.current = setTimeout(() => {
|
|
17
|
+
callBack(...args);
|
|
22
18
|
}, debounceTime);
|
|
23
19
|
};
|
|
24
20
|
return debouncedFunc;
|
|
25
21
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return prefix + "-" + ref.current;
|
|
22
|
+
const useRandomId = (prefix) => {
|
|
23
|
+
const id = useId();
|
|
24
|
+
return prefix ? `${prefix}${id}` : id;
|
|
30
25
|
};
|
|
31
|
-
|
|
32
26
|
function useOnMount(callback) {
|
|
33
|
-
|
|
34
|
-
React.useEffect(
|
|
27
|
+
const hasRun = React.useRef(false);
|
|
28
|
+
React.useEffect(() => {
|
|
35
29
|
if (!hasRun.current) {
|
|
36
30
|
hasRun.current = true;
|
|
37
31
|
callback();
|
|
38
32
|
}
|
|
39
33
|
}, [callback]);
|
|
40
34
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
return function (node) {
|
|
47
|
-
for (var _i = 0, _refs = refs; _i < _refs.length; _i++) {
|
|
48
|
-
var ref = _refs[_i];
|
|
49
|
-
if (typeof ref === 'function') {
|
|
35
|
+
const mergeRefs = (...refs) => {
|
|
36
|
+
return (node) => {
|
|
37
|
+
for (const ref of refs) {
|
|
38
|
+
if (typeof ref === "function") {
|
|
50
39
|
ref(node);
|
|
51
40
|
} else if (ref) ref.current = node;
|
|
52
41
|
}
|
|
53
42
|
};
|
|
54
43
|
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
setValue = _useState[1];
|
|
59
|
-
return function () {
|
|
60
|
-
return setValue(function (value) {
|
|
61
|
-
return value + 1;
|
|
62
|
-
});
|
|
63
|
-
};
|
|
44
|
+
const useForceUpdate = () => {
|
|
45
|
+
const [_, setValue] = useState(0);
|
|
46
|
+
return () => setValue((value) => value + 1);
|
|
64
47
|
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
// If the ref contains the clicked element, then the click is not outside
|
|
70
|
-
if (refs.some(function (ref) {
|
|
71
|
-
return elementContainsEventTarget(ref.current, event);
|
|
72
|
-
})) {
|
|
48
|
+
const useOnClickOutside = (refs, handler) => {
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
const listener = (event) => {
|
|
51
|
+
if (refs.some((ref) => elementContainsEventTarget(ref.current, event))) {
|
|
73
52
|
return;
|
|
74
53
|
}
|
|
75
54
|
handler();
|
|
76
55
|
};
|
|
77
|
-
document.addEventListener(
|
|
78
|
-
document.addEventListener(
|
|
79
|
-
return
|
|
80
|
-
document.removeEventListener(
|
|
81
|
-
document.removeEventListener(
|
|
56
|
+
document.addEventListener("mousedown", listener);
|
|
57
|
+
document.addEventListener("touchstart", listener);
|
|
58
|
+
return () => {
|
|
59
|
+
document.removeEventListener("mousedown", listener);
|
|
60
|
+
document.removeEventListener("touchstart", listener);
|
|
82
61
|
};
|
|
83
62
|
}, [refs, handler]);
|
|
84
63
|
};
|
|
85
|
-
|
|
64
|
+
const elementContainsEventTarget = (element, event) => {
|
|
86
65
|
if (!element) {
|
|
87
66
|
return false;
|
|
88
67
|
}
|
|
89
68
|
if (element.contains(event.target)) {
|
|
90
69
|
return true;
|
|
91
70
|
}
|
|
92
|
-
// For elements inside a Shadow DOM we need to check the composedPath
|
|
93
71
|
if (event.composed && event.composedPath) {
|
|
94
|
-
|
|
72
|
+
const contains = event.composedPath().find((target) => {
|
|
95
73
|
if (target === window) {
|
|
96
74
|
return false;
|
|
97
75
|
}
|
|
98
76
|
return element.contains(target);
|
|
99
77
|
});
|
|
100
|
-
return contains
|
|
78
|
+
return contains;
|
|
101
79
|
}
|
|
102
80
|
return false;
|
|
103
81
|
};
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (event.key === 'Escape') handler();
|
|
109
|
-
};
|
|
110
|
-
var currentRef = ref.current;
|
|
111
|
-
currentRef == null ? void 0 : currentRef.addEventListener('keydown', runIfKeyIsEscape);
|
|
112
|
-
return function () {
|
|
113
|
-
return currentRef == null ? void 0 : currentRef.removeEventListener('keydown', runIfKeyIsEscape);
|
|
82
|
+
const useOnEscape = (ref, handler) => {
|
|
83
|
+
useEffect(() => {
|
|
84
|
+
const runIfKeyIsEscape = (event) => {
|
|
85
|
+
if (event.key === "Escape") handler();
|
|
114
86
|
};
|
|
87
|
+
const currentRef = ref.current;
|
|
88
|
+
currentRef?.addEventListener("keydown", runIfKeyIsEscape);
|
|
89
|
+
return () => currentRef?.removeEventListener("keydown", runIfKeyIsEscape);
|
|
115
90
|
}, [ref, handler]);
|
|
116
91
|
};
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (typeof window === 'undefined') return {
|
|
121
|
-
width: undefined,
|
|
122
|
-
height: undefined
|
|
123
|
-
};
|
|
124
|
-
var _window = window,
|
|
125
|
-
width = _window.innerWidth,
|
|
126
|
-
height = _window.innerHeight;
|
|
127
|
-
return {
|
|
128
|
-
width: width,
|
|
129
|
-
height: height
|
|
130
|
-
};
|
|
92
|
+
const subscribe = (callback) => {
|
|
93
|
+
window.addEventListener("resize", callback);
|
|
94
|
+
return () => window.removeEventListener("resize", callback);
|
|
131
95
|
};
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (typeof window !== 'undefined') {
|
|
141
|
-
window.addEventListener('resize', handleResize);
|
|
142
|
-
return function () {
|
|
143
|
-
return window.removeEventListener('resize', handleResize);
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
}, []);
|
|
147
|
-
return windowDimensions;
|
|
96
|
+
let cachedSnapshot = { width: void 0, height: void 0 };
|
|
97
|
+
const getSnapshot = () => {
|
|
98
|
+
const width = window.innerWidth;
|
|
99
|
+
const height = window.innerHeight;
|
|
100
|
+
if (cachedSnapshot.width !== width || cachedSnapshot.height !== height) {
|
|
101
|
+
cachedSnapshot = { width, height };
|
|
102
|
+
}
|
|
103
|
+
return cachedSnapshot;
|
|
148
104
|
};
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
wrapper = _ref.wrapper,
|
|
153
|
-
children = _ref.children;
|
|
154
|
-
return condition ? wrapper(children) : React.createElement(React.Fragment, null, children);
|
|
105
|
+
const serverSnapshot = {
|
|
106
|
+
width: void 0,
|
|
107
|
+
height: void 0
|
|
155
108
|
};
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
109
|
+
const getServerSnapshot = () => serverSnapshot;
|
|
110
|
+
const useWindowDimensions = () => {
|
|
111
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
112
|
+
};
|
|
113
|
+
const ConditionalWrapper = ({
|
|
114
|
+
condition,
|
|
115
|
+
wrapper,
|
|
116
|
+
children
|
|
117
|
+
}) => condition ? wrapper(children) : children;
|
|
118
|
+
const packagesToCheck = /* @__PURE__ */ new Set();
|
|
119
|
+
let checkTimeoutId;
|
|
159
120
|
function checkAndWarn() {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
121
|
+
const missingImports = Array.from(packagesToCheck).filter(
|
|
122
|
+
(namespace) => parseInt(
|
|
123
|
+
window.getComputedStyle(document.documentElement).getPropertyValue(`--eds-${namespace}`)
|
|
124
|
+
) !== 1
|
|
125
|
+
).sort();
|
|
126
|
+
const singleMissingImport = missingImports.length === 1;
|
|
127
|
+
warning(
|
|
128
|
+
missingImports.length === 0,
|
|
129
|
+
`You are missing ${singleMissingImport ? "a CSS import" : `${missingImports.length} CSS imports`}!
|
|
130
|
+
|
|
131
|
+
Please add the following CSS import${singleMissingImport ? "" : "s"} somewhere in your app:
|
|
132
|
+
|
|
133
|
+
${missingImports.map((namespace) => ` @import '@entur/${namespace}/dist/styles.css';`).join("\n")}
|
|
134
|
+
`
|
|
135
|
+
);
|
|
168
136
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
// We skip this check in production, and when we build static sites
|
|
172
|
-
if (!(process.env.NODE_ENV !== "production") || typeof window === 'undefined') {
|
|
137
|
+
function warnAboutMissingStyles(...namespaces) {
|
|
138
|
+
if (typeof window === "undefined" || typeof process !== "undefined" && process?.env?.TEST === "true" || typeof process !== "undefined" && process?.env?.NODE_ENV === "production") {
|
|
173
139
|
return;
|
|
174
140
|
}
|
|
175
|
-
// First, let's clear earlier calls to setTimeout
|
|
176
141
|
window.clearTimeout(checkTimeoutId);
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
namespaces[_key] = arguments[_key];
|
|
180
|
-
}
|
|
181
|
-
namespaces.forEach(function (namespace) {
|
|
182
|
-
return packagesToCheck.add(namespace);
|
|
183
|
-
});
|
|
184
|
-
// Finally. let's trigger a run of the checker.
|
|
185
|
-
checkTimeoutId = window.setTimeout(checkAndWarn, 1000);
|
|
142
|
+
namespaces.forEach((namespace) => packagesToCheck.add(namespace));
|
|
143
|
+
checkTimeoutId = window.setTimeout(checkAndWarn, 1e3);
|
|
186
144
|
}
|
|
187
|
-
|
|
188
|
-
|
|
145
|
+
const getNodeText = (node) => {
|
|
146
|
+
if (node === null || node === void 0) return "";
|
|
147
|
+
if (["string", "number"].includes(typeof node)) return node.toString();
|
|
148
|
+
if (node instanceof Array) return node.map(getNodeText).join("").trim();
|
|
149
|
+
if (typeof node === "object")
|
|
150
|
+
return getNodeText(node.props?.children ?? "").trim();
|
|
151
|
+
return "unknown";
|
|
152
|
+
};
|
|
153
|
+
function useControllableProp({
|
|
154
|
+
prop,
|
|
155
|
+
updater = () => void 0,
|
|
156
|
+
defaultValue
|
|
157
|
+
}) {
|
|
158
|
+
const [internalState, setInternalState] = useState(defaultValue);
|
|
159
|
+
useEffect(() => {
|
|
160
|
+
if (prop !== void 0) {
|
|
161
|
+
setInternalState(prop);
|
|
162
|
+
}
|
|
163
|
+
}, [prop]);
|
|
164
|
+
return prop === void 0 ? [internalState, setInternalState] : [prop, updater];
|
|
165
|
+
}
|
|
166
|
+
export {
|
|
167
|
+
ConditionalWrapper,
|
|
168
|
+
getNodeText,
|
|
169
|
+
mergeRefs,
|
|
170
|
+
useControllableProp,
|
|
171
|
+
useDebounce,
|
|
172
|
+
useForceUpdate,
|
|
173
|
+
useOnClickOutside,
|
|
174
|
+
useOnEscape,
|
|
175
|
+
useOnMount,
|
|
176
|
+
useRandomId,
|
|
177
|
+
useWindowDimensions,
|
|
178
|
+
warnAboutMissingStyles
|
|
179
|
+
};
|
|
189
180
|
//# sourceMappingURL=utils.esm.js.map
|
package/dist/utils.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.esm.js","sources":["../src/useDebounce.ts","../src/useRandomId.ts","../src/useOnMount.ts","../src/mergeRefs.ts","../src/useForceUpdate.ts","../src/useOnClickOutside.ts","../src/useOnEscape.ts","../src/useWindowDimensions.ts","../src/ConditionalWrapper.tsx","../src/warnAboutMissingStyles.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\n\nexport function useDebounce<T extends (...args: any[]) => any>(\n callBack: T,\n debounceTime: number,\n) {\n const timeoutRef = useRef<ReturnType<typeof setTimeout>>();\n\n useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n };\n }, []);\n\n const debouncedFunc = (...args: any[]) => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n\n timeoutRef.current = setTimeout(() => {\n callBack(...args);\n }, debounceTime);\n };\n\n return debouncedFunc as T;\n}\n","import React from 'react';\n\nexport const useRandomId = (prefix?: string): string => {\n const ref = React.useRef(String(Math.random()).substring(2));\n return `${prefix}-${ref.current}`;\n};\n","import React from 'react';\n\nexport function useOnMount(callback: () => void): void {\n const hasRun = React.useRef<boolean>(false);\n\n React.useEffect(() => {\n if (!hasRun.current) {\n hasRun.current = true;\n callback();\n }\n }, [callback]);\n}\n","export const mergeRefs = <T extends HTMLElement>(\n ...refs: (React.MutableRefObject<T> | React.ForwardedRef<T> | undefined)[]\n) => {\n return (node: T) => {\n for (const ref of refs) {\n if (typeof ref === 'function') {\n ref(node);\n } else if (ref) ref.current = node;\n }\n };\n};\n","import { useState } from 'react';\n\nexport const useForceUpdate = () => {\n const [_, setValue] = useState(0);\n return () => setValue(value => value + 1);\n};\n","import React, { useEffect } from 'react';\n\nexport const useOnClickOutside = (\n refs: React.RefObject<HTMLDivElement>[],\n handler: () => void,\n) => {\n useEffect(() => {\n const listener = (event: Event) => {\n // If the ref contains the clicked element, then the click is not outside\n if (refs.some(ref => elementContainsEventTarget(ref.current, event))) {\n return;\n }\n\n handler();\n };\n\n document.addEventListener('mousedown', listener);\n document.addEventListener('touchstart', listener);\n\n return () => {\n document.removeEventListener('mousedown', listener);\n document.removeEventListener('touchstart', listener);\n };\n }, [refs, handler]);\n};\n\nconst elementContainsEventTarget = (\n element: HTMLElement | null,\n event: Event,\n) => {\n if (!element) {\n return false;\n }\n\n if (element.contains(event.target as Node)) {\n return true;\n }\n\n // For elements inside a Shadow DOM we need to check the composedPath\n if (event.composed && event.composedPath) {\n const contains = event.composedPath().find(target => {\n if (target === window) {\n return false;\n }\n return element.contains(target as Node);\n });\n return contains ? true : false;\n }\n\n return false;\n};\n","import React, { useEffect } from 'react';\n\nexport const useOnEscape = (\n ref: React.RefObject<HTMLElement>,\n handler: () => void,\n) => {\n useEffect(() => {\n const runIfKeyIsEscape = (event: KeyboardEvent) => {\n if (event.key === 'Escape') handler();\n };\n\n const currentRef = ref.current;\n currentRef?.addEventListener('keydown', runIfKeyIsEscape);\n\n return () => currentRef?.removeEventListener('keydown', runIfKeyIsEscape);\n }, [ref, handler]);\n};\n","// from https://stackoverflow.com/questions/36862334/get-viewport-window-height-in-reactjs\nimport { useState, useEffect } from 'react';\n\ntype WindowDimensions = {\n width: number | undefined;\n height: number | undefined;\n};\n\nconst getWindowDimensions = () => {\n if (typeof window === 'undefined')\n return { width: undefined, height: undefined };\n\n const { innerWidth: width, innerHeight: height } = window;\n return {\n width,\n height,\n };\n};\n\nexport const useWindowDimensions = (): WindowDimensions => {\n const [windowDimensions, setWindowDimensions] = useState(\n getWindowDimensions(),\n );\n\n useEffect(() => {\n function handleResize() {\n setWindowDimensions(getWindowDimensions());\n }\n\n if (typeof window !== 'undefined') {\n window.addEventListener('resize', handleResize);\n return () => window.removeEventListener('resize', handleResize);\n }\n }, []);\n\n return windowDimensions;\n};\n","import React from 'react';\n\ntype ConditionalWrapperType = {\n condition: boolean;\n wrapper: any;\n children: any;\n};\n\nexport const ConditionalWrapper = ({\n condition,\n wrapper,\n children,\n}: ConditionalWrapperType) => (condition ? wrapper(children) : <>{children}</>);\n","import warning from 'tiny-warning';\n\nconst packagesToCheck: Set<string> = new Set();\nlet checkTimeoutId: number;\n\nfunction checkAndWarn() {\n const missingImports = Array.from(packagesToCheck)\n .filter(\n namespace =>\n parseInt(\n window\n .getComputedStyle(document.documentElement)\n .getPropertyValue(`--eds-${namespace}`),\n ) !== 1,\n )\n .sort();\n\n // Finally, we warn about those pesky imports\n const singleMissingImport = missingImports.length === 1;\n warning(\n missingImports.length === 0,\n `You are missing ${\n singleMissingImport\n ? 'a CSS import'\n : `${missingImports.length} CSS imports`\n }!\n\nPlease add the following CSS import${\n singleMissingImport ? '' : 's'\n } somewhere in your app:\n\n${missingImports\n .map(namespace => `\\t@import '@entur/${namespace}/dist/styles.css';`)\n .join('\\n')}\n`,\n );\n}\n\n/** Warns the developer if they have forgotten to include styles */\nexport function warnAboutMissingStyles(...namespaces: string[]): void {\n // We skip this check in production, and when we build static sites\n if (!__DEV__ || typeof window === 'undefined') {\n return;\n }\n // First, let's clear earlier calls to setTimeout\n window.clearTimeout(checkTimeoutId);\n\n // Next, let's add all namespaces to the set of packages to check\n namespaces.forEach(namespace => packagesToCheck.add(namespace));\n\n // Finally. let's trigger a run of the checker.\n checkTimeoutId = window.setTimeout(checkAndWarn, 1000);\n}\n"],"names":["useDebounce","callBack","debounceTime","timeoutRef","useRef","useEffect","current","clearTimeout","debouncedFunc","args","setTimeout","useRandomId","prefix","ref","React","String","Math","random","substring","useOnMount","callback","hasRun","mergeRefs","refs","node","useForceUpdate","useState","setValue","value","useOnClickOutside","handler","listener","event","some","elementContainsEventTarget","document","addEventListener","removeEventListener","element","contains","target","composed","composedPath","find","window","useOnEscape","runIfKeyIsEscape","key","currentRef","getWindowDimensions","width","undefined","height","innerWidth","innerHeight","useWindowDimensions","windowDimensions","setWindowDimensions","handleResize","ConditionalWrapper","condition","wrapper","children","createElement","Fragment","packagesToCheck","Set","checkTimeoutId","checkAndWarn","missingImports","Array","from","filter","namespace","parseInt","getComputedStyle","documentElement","getPropertyValue","sort","singleMissingImport","length","warning","map","join","warnAboutMissingStyles","namespaces","forEach","add"],"mappings":";;;AAEgB,SAAAA,WAAW,CACzBC,QAAW,EACXC,YAAoB,EAAA;EAEpB,IAAMC,UAAU,GAAGC,MAAM,EAAiC,CAAA;AAE1DC,EAAAA,SAAS,CAAC,YAAK;AACb,IAAA,OAAO,YAAK;MACV,IAAIF,UAAU,CAACG,OAAO,EAAE;AACtBC,QAAAA,YAAY,CAACJ,UAAU,CAACG,OAAO,CAAC,CAAA;AACjC,OAAA;KACF,CAAA;GACF,EAAE,EAAE,CAAC,CAAA;AAEN,EAAA,IAAME,aAAa,GAAG,SAAhBA,aAAa,GAAsB;AAAA,IAAA,KAAA,IAAA,IAAA,GAAA,SAAA,CAAA,MAAA,EAAfC,IAAW,GAAA,IAAA,KAAA,CAAA,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":["../src/useDebounce.ts","../src/useRandomId.ts","../src/useOnMount.ts","../src/mergeRefs.ts","../src/useForceUpdate.ts","../src/useOnClickOutside.ts","../src/useOnEscape.ts","../src/useWindowDimensions.ts","../src/ConditionalWrapper.tsx","../src/warnAboutMissingStyles.ts","../src/getNodeText.ts","../src/useControllableProp.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\n\nexport function useDebounce<T extends (...args: any[]) => any>(\n callBack: T,\n debounceTime: number,\n) {\n const timeoutRef = useRef<ReturnType<typeof setTimeout>>();\n\n useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n };\n }, []);\n\n const debouncedFunc = (...args: any[]) => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n\n timeoutRef.current = setTimeout(() => {\n callBack(...args);\n }, debounceTime);\n };\n\n return debouncedFunc as T;\n}\n","import { useId } from 'react';\n\n/**\n * @deprecated Use `useId()` from React directly instead.\n * Will be removed in a future major version.\n *\n * ```diff\n * - import { useRandomId } from '@entur/utils';\n * - const id = useRandomId('eds-my-component');\n * + import { useId } from 'react';\n * + const id = `eds-my-component${useId()}`;\n * ```\n */\nexport const useRandomId = (prefix?: string): string => {\n const id = useId();\n return prefix ? `${prefix}${id}` : id;\n};\n","import React from 'react';\n\nexport function useOnMount(callback: () => void): void {\n const hasRun = React.useRef<boolean>(false);\n\n React.useEffect(() => {\n if (!hasRun.current) {\n hasRun.current = true;\n callback();\n }\n }, [callback]);\n}\n","export const mergeRefs = <T extends HTMLElement>(\n ...refs: (\n | React.MutableRefObject<T>\n | React.ForwardedRef<T>\n | ((node: T | null) => void)\n | undefined\n )[]\n) => {\n return (node: T) => {\n for (const ref of refs) {\n if (typeof ref === 'function') {\n ref(node);\n } else if (ref) ref.current = node;\n }\n };\n};\n","import { useState } from 'react';\n\nexport const useForceUpdate = () => {\n const [_, setValue] = useState(0);\n return () => setValue(value => value + 1);\n};\n","import React, { useEffect } from 'react';\n\nexport const useOnClickOutside = (\n refs: (React.RefObject<HTMLElement> | React.MutableRefObject<any>)[],\n handler: () => void,\n) => {\n useEffect(() => {\n const listener = (event: Event) => {\n // If the ref contains the clicked element, then the click is not outside\n if (refs.some(ref => elementContainsEventTarget(ref.current, event))) {\n return;\n }\n\n handler();\n };\n\n document.addEventListener('mousedown', listener);\n document.addEventListener('touchstart', listener);\n\n return () => {\n document.removeEventListener('mousedown', listener);\n document.removeEventListener('touchstart', listener);\n };\n }, [refs, handler]);\n};\n\nconst elementContainsEventTarget = (\n element: HTMLElement | null,\n event: Event,\n) => {\n if (!element) {\n return false;\n }\n\n if (element.contains(event.target as Node)) {\n return true;\n }\n\n // For elements inside a Shadow DOM we need to check the composedPath\n if (event.composed && event.composedPath) {\n const contains = event.composedPath().find(target => {\n if (target === window) {\n return false;\n }\n return element.contains(target as Node);\n });\n return contains;\n }\n\n return false;\n};\n","import React, { useEffect } from 'react';\n\nexport const useOnEscape = (\n ref: React.RefObject<any> | React.MutableRefObject<any>,\n handler: () => void,\n) => {\n useEffect(() => {\n const runIfKeyIsEscape = (event: KeyboardEvent) => {\n if (event.key === 'Escape') handler();\n };\n\n const currentRef = ref.current;\n currentRef?.addEventListener('keydown', runIfKeyIsEscape);\n\n return () => currentRef?.removeEventListener('keydown', runIfKeyIsEscape);\n }, [ref, handler]);\n};\n","import { useSyncExternalStore } from 'react';\n\ntype WindowDimensions = {\n width: number | undefined;\n height: number | undefined;\n};\n\nconst subscribe = (callback: () => void) => {\n window.addEventListener('resize', callback);\n return () => window.removeEventListener('resize', callback);\n};\n\nlet cachedSnapshot: WindowDimensions = { width: undefined, height: undefined };\n\nconst getSnapshot = (): WindowDimensions => {\n const width = window.innerWidth;\n const height = window.innerHeight;\n if (cachedSnapshot.width !== width || cachedSnapshot.height !== height) {\n cachedSnapshot = { width, height };\n }\n return cachedSnapshot;\n};\n\nconst serverSnapshot: WindowDimensions = {\n width: undefined,\n height: undefined,\n};\nconst getServerSnapshot = () => serverSnapshot;\n\nexport const useWindowDimensions = (): WindowDimensions => {\n return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n};\n","type ConditionalWrapperType = {\n condition: boolean;\n wrapper: any;\n children: any;\n};\n\nexport const ConditionalWrapper = ({\n condition,\n wrapper,\n children,\n}: ConditionalWrapperType) => (condition ? wrapper(children) : children);\n","import warning from 'tiny-warning';\n\nconst packagesToCheck: Set<string> = new Set();\nlet checkTimeoutId: number;\n\nfunction checkAndWarn() {\n const missingImports = Array.from(packagesToCheck)\n .filter(\n namespace =>\n parseInt(\n window\n .getComputedStyle(document.documentElement)\n .getPropertyValue(`--eds-${namespace}`),\n ) !== 1,\n )\n .sort();\n\n // Finally, we warn about those pesky imports\n const singleMissingImport = missingImports.length === 1;\n warning(\n missingImports.length === 0,\n `You are missing ${\n singleMissingImport\n ? 'a CSS import'\n : `${missingImports.length} CSS imports`\n }!\n\nPlease add the following CSS import${\n singleMissingImport ? '' : 's'\n } somewhere in your app:\n\n${missingImports\n .map(namespace => `\\t@import '@entur/${namespace}/dist/styles.css';`)\n .join('\\n')}\n`,\n );\n}\n\n/** Warns the developer if they have forgotten to include styles */\nexport function warnAboutMissingStyles(...namespaces: string[]): void {\n // We skip this check in production, and when we build static sites\n if (\n typeof window === 'undefined' ||\n (typeof process !== 'undefined' && process?.env?.TEST === 'true') ||\n (typeof process !== 'undefined' && process?.env?.NODE_ENV === 'production')\n ) {\n return;\n }\n // First, let's clear earlier calls to setTimeout\n window.clearTimeout(checkTimeoutId);\n\n // Next, let's add all namespaces to the set of packages to check\n namespaces.forEach(namespace => packagesToCheck.add(namespace));\n\n // Finally. let's trigger a run of the checker.\n checkTimeoutId = window.setTimeout(checkAndWarn, 1000);\n}\n","// with inspiration from https://stackoverflow.com/questions/50428910/get-text-content-from-node-in-react\nexport const getNodeText = (\n node: React.ReactNode | string | number | string[] | number[],\n): string => {\n if (node === null || node === undefined) return '';\n if (['string', 'number'].includes(typeof node)) return node.toString();\n if (node instanceof Array) return node.map(getNodeText).join('').trim();\n if (typeof node === 'object')\n // @ts-expect-error props does exist for react nodes\n return getNodeText(node.props?.children ?? '').trim();\n return 'unknown';\n};\n","import { useEffect, useState } from 'react';\nexport type UseControllablePropType<T> = {\n prop?: T;\n updater?: (value?: T) => void;\n defaultValue: T;\n};\nexport function useControllableProp<T>({\n prop,\n updater = () => undefined,\n defaultValue,\n}: UseControllablePropType<T>): [T, (arg: T) => void] {\n const [internalState, setInternalState] = useState<T>(defaultValue);\n useEffect(() => {\n if (prop !== undefined) {\n setInternalState(prop);\n }\n }, [prop]);\n return prop === undefined\n ? [internalState, setInternalState]\n : [prop, updater];\n}\n"],"names":[],"mappings":";;AAEO,SAAS,YACd,UACA,cACA;AACA,QAAM,aAAa,OAAA;AAEnB,YAAU,MAAM;AACd,WAAO,MAAM;AACX,UAAI,WAAW,SAAS;AACtB,qBAAa,WAAW,OAAO;AAAA,MACjC;AAAA,IACF;AAAA,EACF,GAAG,CAAA,CAAE;AAEL,QAAM,gBAAgB,IAAI,SAAgB;AACxC,QAAI,WAAW,SAAS;AACtB,mBAAa,WAAW,OAAO;AAAA,IACjC;AAEA,eAAW,UAAU,WAAW,MAAM;AACpC,eAAS,GAAG,IAAI;AAAA,IAClB,GAAG,YAAY;AAAA,EACjB;AAEA,SAAO;AACT;ACdO,MAAM,cAAc,CAAC,WAA4B;AACtD,QAAM,KAAK,MAAA;AACX,SAAO,SAAS,GAAG,MAAM,GAAG,EAAE,KAAK;AACrC;ACdO,SAAS,WAAW,UAA4B;AACrD,QAAM,SAAS,MAAM,OAAgB,KAAK;AAE1C,QAAM,UAAU,MAAM;AACpB,QAAI,CAAC,OAAO,SAAS;AACnB,aAAO,UAAU;AACjB,eAAA;AAAA,IACF;AAAA,EACF,GAAG,CAAC,QAAQ,CAAC;AACf;ACXO,MAAM,YAAY,IACpB,SAMA;AACH,SAAO,CAAC,SAAY;AAClB,eAAW,OAAO,MAAM;AACtB,UAAI,OAAO,QAAQ,YAAY;AAC7B,YAAI,IAA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O,YAAY,eAAe,SAAS,KAAK,SAAS,UACzD,OAAO,YAAY,eAAe,SAAS,KAAK,aAAa,cAC9D;AACA;AAAA,EACF;AAEA,SAAO,aAAa,cAAc;AAGlC,aAAW,QAAQ,CAAA,cAAa,gBAAgB,IAAI,SAAS,CAAC;AAG9D,mBAAiB,OAAO,WAAW,cAAc,GAAI;AACvD;ACvDO,MAAM,cAAc,CACzB,SACW;AACX,MAAI,SAAS,QAAQ,SAAS,OAAW,QAAO;AAChD,MAAI,CAAC,UAAU,QAAQ,EAAE,SAAS,OAAO,IAAI,EAAG,QAAO,KAAK,SAAA;AAC5D,MAAI,gBAAgB,MAAO,QAAO,KAAK,IAAI,WAAW,EAAE,KAAK,EAAE,EAAE,KAAA;AACjE,MAAI,OAAO,SAAS;AAElB,WAAO,YAAY,KAAK,OAAO,YAAY,EAAE,EAAE,KAAA;AACjD,SAAO;AACT;ACLO,SAAS,oBAAuB;AAAA,EACrC;AAAA,EACA,UAAU,MAAM;AAAA,EAChB;AACF,GAAsD;AACpD,QAAM,CAAC,eAAe,gBAAgB,IAAI,SAAY,YAAY;AAClE,YAAU,MAAM;AACd,QAAI,SAAS,QAAW;AACtB,uBAAiB,IAAI;AAAA,IACvB;AAAA,EACF,GAAG,CAAC,IAAI,CAAC;AACT,SAAO,SAAS,SACZ,CAAC,eAAe,gBAAgB,IAChC,CAAC,MAAM,OAAO;AACpB;"}
|
package/package.json
CHANGED
|
@@ -1,32 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@entur/utils",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-next.0",
|
|
4
4
|
"license": "EUPL-1.2",
|
|
5
|
-
"main": "dist/
|
|
5
|
+
"main": "dist/utils.cjs.js",
|
|
6
6
|
"module": "dist/utils.esm.js",
|
|
7
7
|
"typings": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/utils.esm.js",
|
|
12
|
+
"require": "./dist/utils.cjs.js",
|
|
13
|
+
"default": "./dist/utils.esm.js"
|
|
14
|
+
},
|
|
15
|
+
"./styles/layers": "./dist/layers.scss",
|
|
16
|
+
"./package.json": "./package.json",
|
|
17
|
+
"./dist/*": "./dist/*"
|
|
18
|
+
},
|
|
19
|
+
"sideEffects": [
|
|
20
|
+
"./dist/**/*.css"
|
|
21
|
+
],
|
|
8
22
|
"files": [
|
|
9
23
|
"dist"
|
|
10
24
|
],
|
|
11
25
|
"repository": {
|
|
12
26
|
"type": "git",
|
|
13
|
-
"url": "https://
|
|
27
|
+
"url": "https://github.com/entur/design-system.git",
|
|
14
28
|
"directory": "packages/utils"
|
|
15
29
|
},
|
|
16
30
|
"publishConfig": {
|
|
17
31
|
"access": "public"
|
|
18
32
|
},
|
|
19
33
|
"scripts": {
|
|
20
|
-
"start": "
|
|
21
|
-
"build": "
|
|
22
|
-
"
|
|
34
|
+
"start": "vite build --watch",
|
|
35
|
+
"build": "vite build && cp src/**/*.scss dist",
|
|
36
|
+
"test": "jest"
|
|
23
37
|
},
|
|
24
38
|
"peerDependencies": {
|
|
25
|
-
"react": ">=
|
|
26
|
-
"react-dom": ">=
|
|
39
|
+
"react": ">=18.0.0",
|
|
40
|
+
"react-dom": ">=18.0.0"
|
|
27
41
|
},
|
|
28
42
|
"dependencies": {
|
|
29
43
|
"tiny-warning": "^1.0.3"
|
|
30
44
|
},
|
|
31
|
-
"
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@testing-library/jest-dom": "^5.17.0",
|
|
47
|
+
"@testing-library/react": "^16.3.0",
|
|
48
|
+
"@testing-library/user-event": "14.6.1",
|
|
49
|
+
"jest": "^29.0.0",
|
|
50
|
+
"jest-environment-jsdom": "^29.0.0",
|
|
51
|
+
"ts-jest": "^29.0.0",
|
|
52
|
+
"typescript": "^5.9.2",
|
|
53
|
+
"vite": "^7.3.2",
|
|
54
|
+
"vite-plugin-dts": "^4.5.4"
|
|
55
|
+
},
|
|
56
|
+
"gitHead": "0f435bdeec98cc3cea1db7434b13b1f771a39a18"
|
|
32
57
|
}
|
package/dist/index.js
DELETED
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var React = require('react');
|
|
6
|
-
var warning = require('tiny-warning');
|
|
7
|
-
|
|
8
|
-
function useDebounce(callBack, debounceTime) {
|
|
9
|
-
var timeoutRef = React.useRef();
|
|
10
|
-
React.useEffect(function () {
|
|
11
|
-
return function () {
|
|
12
|
-
if (timeoutRef.current) {
|
|
13
|
-
clearTimeout(timeoutRef.current);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
}, []);
|
|
17
|
-
var debouncedFunc = function debouncedFunc() {
|
|
18
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
19
|
-
args[_key] = arguments[_key];
|
|
20
|
-
}
|
|
21
|
-
if (timeoutRef.current) {
|
|
22
|
-
clearTimeout(timeoutRef.current);
|
|
23
|
-
}
|
|
24
|
-
timeoutRef.current = setTimeout(function () {
|
|
25
|
-
callBack.apply(void 0, args);
|
|
26
|
-
}, debounceTime);
|
|
27
|
-
};
|
|
28
|
-
return debouncedFunc;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
var useRandomId = function useRandomId(prefix) {
|
|
32
|
-
var ref = React.useRef(String(Math.random()).substring(2));
|
|
33
|
-
return prefix + "-" + ref.current;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
function useOnMount(callback) {
|
|
37
|
-
var hasRun = React.useRef(false);
|
|
38
|
-
React.useEffect(function () {
|
|
39
|
-
if (!hasRun.current) {
|
|
40
|
-
hasRun.current = true;
|
|
41
|
-
callback();
|
|
42
|
-
}
|
|
43
|
-
}, [callback]);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
var mergeRefs = function mergeRefs() {
|
|
47
|
-
for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
48
|
-
refs[_key] = arguments[_key];
|
|
49
|
-
}
|
|
50
|
-
return function (node) {
|
|
51
|
-
for (var _i = 0, _refs = refs; _i < _refs.length; _i++) {
|
|
52
|
-
var ref = _refs[_i];
|
|
53
|
-
if (typeof ref === 'function') {
|
|
54
|
-
ref(node);
|
|
55
|
-
} else if (ref) ref.current = node;
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
var useForceUpdate = function useForceUpdate() {
|
|
61
|
-
var _useState = React.useState(0),
|
|
62
|
-
setValue = _useState[1];
|
|
63
|
-
return function () {
|
|
64
|
-
return setValue(function (value) {
|
|
65
|
-
return value + 1;
|
|
66
|
-
});
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
var useOnClickOutside = function useOnClickOutside(refs, handler) {
|
|
71
|
-
React.useEffect(function () {
|
|
72
|
-
var listener = function listener(event) {
|
|
73
|
-
// If the ref contains the clicked element, then the click is not outside
|
|
74
|
-
if (refs.some(function (ref) {
|
|
75
|
-
return elementContainsEventTarget(ref.current, event);
|
|
76
|
-
})) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
handler();
|
|
80
|
-
};
|
|
81
|
-
document.addEventListener('mousedown', listener);
|
|
82
|
-
document.addEventListener('touchstart', listener);
|
|
83
|
-
return function () {
|
|
84
|
-
document.removeEventListener('mousedown', listener);
|
|
85
|
-
document.removeEventListener('touchstart', listener);
|
|
86
|
-
};
|
|
87
|
-
}, [refs, handler]);
|
|
88
|
-
};
|
|
89
|
-
var elementContainsEventTarget = function elementContainsEventTarget(element, event) {
|
|
90
|
-
if (!element) {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
if (element.contains(event.target)) {
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
// For elements inside a Shadow DOM we need to check the composedPath
|
|
97
|
-
if (event.composed && event.composedPath) {
|
|
98
|
-
var contains = event.composedPath().find(function (target) {
|
|
99
|
-
if (target === window) {
|
|
100
|
-
return false;
|
|
101
|
-
}
|
|
102
|
-
return element.contains(target);
|
|
103
|
-
});
|
|
104
|
-
return contains ? true : false;
|
|
105
|
-
}
|
|
106
|
-
return false;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
var useOnEscape = function useOnEscape(ref, handler) {
|
|
110
|
-
React.useEffect(function () {
|
|
111
|
-
var runIfKeyIsEscape = function runIfKeyIsEscape(event) {
|
|
112
|
-
if (event.key === 'Escape') handler();
|
|
113
|
-
};
|
|
114
|
-
var currentRef = ref.current;
|
|
115
|
-
currentRef == null ? void 0 : currentRef.addEventListener('keydown', runIfKeyIsEscape);
|
|
116
|
-
return function () {
|
|
117
|
-
return currentRef == null ? void 0 : currentRef.removeEventListener('keydown', runIfKeyIsEscape);
|
|
118
|
-
};
|
|
119
|
-
}, [ref, handler]);
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
// from https://stackoverflow.com/questions/36862334/get-viewport-window-height-in-reactjs
|
|
123
|
-
var getWindowDimensions = function getWindowDimensions() {
|
|
124
|
-
if (typeof window === 'undefined') return {
|
|
125
|
-
width: undefined,
|
|
126
|
-
height: undefined
|
|
127
|
-
};
|
|
128
|
-
var _window = window,
|
|
129
|
-
width = _window.innerWidth,
|
|
130
|
-
height = _window.innerHeight;
|
|
131
|
-
return {
|
|
132
|
-
width: width,
|
|
133
|
-
height: height
|
|
134
|
-
};
|
|
135
|
-
};
|
|
136
|
-
var useWindowDimensions = function useWindowDimensions() {
|
|
137
|
-
var _useState = React.useState(getWindowDimensions()),
|
|
138
|
-
windowDimensions = _useState[0],
|
|
139
|
-
setWindowDimensions = _useState[1];
|
|
140
|
-
React.useEffect(function () {
|
|
141
|
-
function handleResize() {
|
|
142
|
-
setWindowDimensions(getWindowDimensions());
|
|
143
|
-
}
|
|
144
|
-
if (typeof window !== 'undefined') {
|
|
145
|
-
window.addEventListener('resize', handleResize);
|
|
146
|
-
return function () {
|
|
147
|
-
return window.removeEventListener('resize', handleResize);
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
}, []);
|
|
151
|
-
return windowDimensions;
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
var ConditionalWrapper = function ConditionalWrapper(_ref) {
|
|
155
|
-
var condition = _ref.condition,
|
|
156
|
-
wrapper = _ref.wrapper,
|
|
157
|
-
children = _ref.children;
|
|
158
|
-
return condition ? wrapper(children) : React.createElement(React.Fragment, null, children);
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
var packagesToCheck = /*#__PURE__*/new Set();
|
|
162
|
-
var checkTimeoutId;
|
|
163
|
-
function checkAndWarn() {
|
|
164
|
-
var missingImports = Array.from(packagesToCheck).filter(function (namespace) {
|
|
165
|
-
return parseInt(window.getComputedStyle(document.documentElement).getPropertyValue("--eds-" + namespace)) !== 1;
|
|
166
|
-
}).sort();
|
|
167
|
-
// Finally, we warn about those pesky imports
|
|
168
|
-
var singleMissingImport = missingImports.length === 1;
|
|
169
|
-
warning(missingImports.length === 0, "You are missing " + (singleMissingImport ? 'a CSS import' : missingImports.length + " CSS imports") + "!\n\nPlease add the following CSS import" + (singleMissingImport ? '' : 's') + " somewhere in your app:\n\n" + missingImports.map(function (namespace) {
|
|
170
|
-
return "\t@import '@entur/" + namespace + "/dist/styles.css';";
|
|
171
|
-
}).join('\n') + "\n") ;
|
|
172
|
-
}
|
|
173
|
-
/** Warns the developer if they have forgotten to include styles */
|
|
174
|
-
function warnAboutMissingStyles() {
|
|
175
|
-
// We skip this check in production, and when we build static sites
|
|
176
|
-
if (typeof window === 'undefined') {
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
// First, let's clear earlier calls to setTimeout
|
|
180
|
-
window.clearTimeout(checkTimeoutId);
|
|
181
|
-
// Next, let's add all namespaces to the set of packages to check
|
|
182
|
-
for (var _len = arguments.length, namespaces = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
183
|
-
namespaces[_key] = arguments[_key];
|
|
184
|
-
}
|
|
185
|
-
namespaces.forEach(function (namespace) {
|
|
186
|
-
return packagesToCheck.add(namespace);
|
|
187
|
-
});
|
|
188
|
-
// Finally. let's trigger a run of the checker.
|
|
189
|
-
checkTimeoutId = window.setTimeout(checkAndWarn, 1000);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
exports.ConditionalWrapper = ConditionalWrapper;
|
|
193
|
-
exports.mergeRefs = mergeRefs;
|
|
194
|
-
exports.useDebounce = useDebounce;
|
|
195
|
-
exports.useForceUpdate = useForceUpdate;
|
|
196
|
-
exports.useOnClickOutside = useOnClickOutside;
|
|
197
|
-
exports.useOnEscape = useOnEscape;
|
|
198
|
-
exports.useOnMount = useOnMount;
|
|
199
|
-
exports.useRandomId = useRandomId;
|
|
200
|
-
exports.useWindowDimensions = useWindowDimensions;
|
|
201
|
-
exports.warnAboutMissingStyles = warnAboutMissingStyles;
|
|
202
|
-
//# sourceMappingURL=utils.cjs.development.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs.development.js","sources":["../src/useDebounce.ts","../src/useRandomId.ts","../src/useOnMount.ts","../src/mergeRefs.ts","../src/useForceUpdate.ts","../src/useOnClickOutside.ts","../src/useOnEscape.ts","../src/useWindowDimensions.ts","../src/ConditionalWrapper.tsx","../src/warnAboutMissingStyles.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\n\nexport function useDebounce<T extends (...args: any[]) => any>(\n callBack: T,\n debounceTime: number,\n) {\n const timeoutRef = useRef<ReturnType<typeof setTimeout>>();\n\n useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n };\n }, []);\n\n const debouncedFunc = (...args: any[]) => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n\n timeoutRef.current = setTimeout(() => {\n callBack(...args);\n }, debounceTime);\n };\n\n return debouncedFunc as T;\n}\n","import React from 'react';\n\nexport const useRandomId = (prefix?: string): string => {\n const ref = React.useRef(String(Math.random()).substring(2));\n return `${prefix}-${ref.current}`;\n};\n","import React from 'react';\n\nexport function useOnMount(callback: () => void): void {\n const hasRun = React.useRef<boolean>(false);\n\n React.useEffect(() => {\n if (!hasRun.current) {\n hasRun.current = true;\n callback();\n }\n }, [callback]);\n}\n","export const mergeRefs = <T extends HTMLElement>(\n ...refs: (React.MutableRefObject<T> | React.ForwardedRef<T> | undefined)[]\n) => {\n return (node: T) => {\n for (const ref of refs) {\n if (typeof ref === 'function') {\n ref(node);\n } else if (ref) ref.current = node;\n }\n };\n};\n","import { useState } from 'react';\n\nexport const useForceUpdate = () => {\n const [_, setValue] = useState(0);\n return () => setValue(value => value + 1);\n};\n","import React, { useEffect } from 'react';\n\nexport const useOnClickOutside = (\n refs: React.RefObject<HTMLDivElement>[],\n handler: () => void,\n) => {\n useEffect(() => {\n const listener = (event: Event) => {\n // If the ref contains the clicked element, then the click is not outside\n if (refs.some(ref => elementContainsEventTarget(ref.current, event))) {\n return;\n }\n\n handler();\n };\n\n document.addEventListener('mousedown', listener);\n document.addEventListener('touchstart', listener);\n\n return () => {\n document.removeEventListener('mousedown', listener);\n document.removeEventListener('touchstart', listener);\n };\n }, [refs, handler]);\n};\n\nconst elementContainsEventTarget = (\n element: HTMLElement | null,\n event: Event,\n) => {\n if (!element) {\n return false;\n }\n\n if (element.contains(event.target as Node)) {\n return true;\n }\n\n // For elements inside a Shadow DOM we need to check the composedPath\n if (event.composed && event.composedPath) {\n const contains = event.composedPath().find(target => {\n if (target === window) {\n return false;\n }\n return element.contains(target as Node);\n });\n return contains ? true : false;\n }\n\n return false;\n};\n","import React, { useEffect } from 'react';\n\nexport const useOnEscape = (\n ref: React.RefObject<HTMLElement>,\n handler: () => void,\n) => {\n useEffect(() => {\n const runIfKeyIsEscape = (event: KeyboardEvent) => {\n if (event.key === 'Escape') handler();\n };\n\n const currentRef = ref.current;\n currentRef?.addEventListener('keydown', runIfKeyIsEscape);\n\n return () => currentRef?.removeEventListener('keydown', runIfKeyIsEscape);\n }, [ref, handler]);\n};\n","// from https://stackoverflow.com/questions/36862334/get-viewport-window-height-in-reactjs\nimport { useState, useEffect } from 'react';\n\ntype WindowDimensions = {\n width: number | undefined;\n height: number | undefined;\n};\n\nconst getWindowDimensions = () => {\n if (typeof window === 'undefined')\n return { width: undefined, height: undefined };\n\n const { innerWidth: width, innerHeight: height } = window;\n return {\n width,\n height,\n };\n};\n\nexport const useWindowDimensions = (): WindowDimensions => {\n const [windowDimensions, setWindowDimensions] = useState(\n getWindowDimensions(),\n );\n\n useEffect(() => {\n function handleResize() {\n setWindowDimensions(getWindowDimensions());\n }\n\n if (typeof window !== 'undefined') {\n window.addEventListener('resize', handleResize);\n return () => window.removeEventListener('resize', handleResize);\n }\n }, []);\n\n return windowDimensions;\n};\n","import React from 'react';\n\ntype ConditionalWrapperType = {\n condition: boolean;\n wrapper: any;\n children: any;\n};\n\nexport const ConditionalWrapper = ({\n condition,\n wrapper,\n children,\n}: ConditionalWrapperType) => (condition ? wrapper(children) : <>{children}</>);\n","import warning from 'tiny-warning';\n\nconst packagesToCheck: Set<string> = new Set();\nlet checkTimeoutId: number;\n\nfunction checkAndWarn() {\n const missingImports = Array.from(packagesToCheck)\n .filter(\n namespace =>\n parseInt(\n window\n .getComputedStyle(document.documentElement)\n .getPropertyValue(`--eds-${namespace}`),\n ) !== 1,\n )\n .sort();\n\n // Finally, we warn about those pesky imports\n const singleMissingImport = missingImports.length === 1;\n warning(\n missingImports.length === 0,\n `You are missing ${\n singleMissingImport\n ? 'a CSS import'\n : `${missingImports.length} CSS imports`\n }!\n\nPlease add the following CSS import${\n singleMissingImport ? '' : 's'\n } somewhere in your app:\n\n${missingImports\n .map(namespace => `\\t@import '@entur/${namespace}/dist/styles.css';`)\n .join('\\n')}\n`,\n );\n}\n\n/** Warns the developer if they have forgotten to include styles */\nexport function warnAboutMissingStyles(...namespaces: string[]): void {\n // We skip this check in production, and when we build static sites\n if (!__DEV__ || typeof window === 'undefined') {\n return;\n }\n // First, let's clear earlier calls to setTimeout\n window.clearTimeout(checkTimeoutId);\n\n // Next, let's add all namespaces to the set of packages to check\n namespaces.forEach(namespace => packagesToCheck.add(namespace));\n\n // Finally. let's trigger a run of the checker.\n checkTimeoutId = window.setTimeout(checkAndWarn, 1000);\n}\n"],"names":["useDebounce","callBack","debounceTime","timeoutRef","useRef","useEffect","current","clearTimeout","debouncedFunc","args","setTimeout","useRandomId","prefix","ref","React","String","Math","random","substring","useOnMount","callback","hasRun","mergeRefs","refs","node","useForceUpdate","useState","setValue","value","useOnClickOutside","handler","listener","event","some","elementContainsEventTarget","document","addEventListener","removeEventListener","element","contains","target","composed","composedPath","find","window","useOnEscape","runIfKeyIsEscape","key","currentRef","getWindowDimensions","width","undefined","height","innerWidth","innerHeight","useWindowDimensions","windowDimensions","setWindowDimensions","handleResize","ConditionalWrapper","condition","wrapper","children","createElement","Fragment","packagesToCheck","Set","checkTimeoutId","checkAndWarn","missingImports","Array","from","filter","namespace","parseInt","getComputedStyle","documentElement","getPropertyValue","sort","singleMissingImport","length","warning","map","join","warnAboutMissingStyles","namespaces","forEach","add"],"mappings":";;;;;;;AAEgB,SAAAA,WAAW,CACzBC,QAAW,EACXC,YAAoB,EAAA;EAEpB,IAAMC,UAAU,GAAGC,YAAM,EAAiC,CAAA;AAE1DC,EAAAA,eAAS,CAAC,YAAK;AACb,IAAA,OAAO,YAAK;MACV,IAAIF,UAAU,CAACG,OAAO,EAAE;AACtBC,QAAAA,YAAY,CAACJ,UAAU,CAACG,OAAO,CAAC,CAAA;AACjC,OAAA;KACF,CAAA;GACF,EAAE,EAAE,CAAC,CAAA;AAEN,EAAA,IAAME,aAAa,GAAG,SAAhBA,aAAa,GAAsB;AAAA,IAAA,KAAA,IAAA,IAAA,GAAA,SAAA,CAAA,MAAA,EAAfC,IAAW,GAAA,IAAA,KAAA,CAAA,IAAA,CAAA,EAAA,IAAA,G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OAAO,CACLZ,cAAc,CAACW,MAAM,KAAK,CAAC,EAAA,kBAAA,IAEzBD,mBAAmB,GACf,cAAc,GACXV,cAAc,CAACW,MAAM,GAAA,cAC9B,CAGED,GAAAA,0CAAAA,IAAAA,mBAAmB,GAAG,EAAE,GAAG,GAC7B,CAEFV,GAAAA,6BAAAA,GAAAA,cAAc,CACba,GAAG,CAAC,UAAAT,SAAS,EAAA;AAAA,IAAA,OAAA,oBAAA,GAAyBA,SAAS,GAAA,oBAAA,CAAA;AAAA,GAAoB,CAAC,CACpEU,IAAI,CAAC,IAAI,CAAC,GAEV,IAAA,CAAA,CAAA,CAAA;AACH,CAAA;AAEA;AACgB,SAAAC,sBAAsB,GAAwB;AAC5D;AACA,EAAA,IAAgB,OAAOxC,MAAM,KAAK,WAAW,EAAE;AAC7C,IAAA,OAAA;AACD,GAAA;AACD;AACAA,EAAAA,MAAM,CAACrC,YAAY,CAAC4D,cAAc,CAAC,CAAA;AAEnC;AAAA,EAAA,KAAA,IAAA,IAAA,GAAA,SAAA,CAAA,MAAA,EARwCkB,UAAoB,GAAA,IAAA,KAAA,CAAA,IAAA,CAAA,EAAA,IAAA,GAAA,CAAA,EAAA,IAAA,GAAA,IAAA,EAAA,IAAA,EAAA,EAAA;IAApBA,UAAoB,CAAA,IAAA,CAAA,GAAA,SAAA,CAAA,IAAA,CAAA,CAAA;AAAA,GAAA;AAS5DA,EAAAA,UAAU,CAACC,OAAO,CAAC,UAAAb,SAAS,EAAA;AAAA,IAAA,OAAIR,eAAe,CAACsB,GAAG,CAACd,SAAS,CAAC,CAAA;GAAC,CAAA,CAAA;AAE/D;EACAN,cAAc,GAAGvB,MAAM,CAAClC,UAAU,CAAC0D,YAAY,EAAE,IAAI,CAAC,CAAA;AACxD;;;;;;;;;;;;;"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");require("tiny-warning");var n=function(){if("undefined"==typeof window)return{width:void 0,height:void 0};var e=window;return{width:e.innerWidth,height:e.innerHeight}};exports.ConditionalWrapper=function(n){var t=n.children;return n.condition?(0,n.wrapper)(t):e.createElement(e.Fragment,null,t)},exports.mergeRefs=function(){for(var e=arguments.length,n=new Array(e),t=0;t<e;t++)n[t]=arguments[t];return function(e){for(var t=0,r=n;t<r.length;t++){var u=r[t];"function"==typeof u?u(e):u&&(u.current=e)}}},exports.useDebounce=function(n,t){var r=e.useRef();return e.useEffect((function(){return function(){r.current&&clearTimeout(r.current)}}),[]),function(){for(var e=arguments.length,u=new Array(e),o=0;o<e;o++)u[o]=arguments[o];r.current&&clearTimeout(r.current),r.current=setTimeout((function(){n.apply(void 0,u)}),t)}},exports.useForceUpdate=function(){var n=e.useState(0)[1];return function(){return n((function(e){return e+1}))}},exports.useOnClickOutside=function(n,t){e.useEffect((function(){var e=function(e){n.some((function(n){return function(e,n){return!(!e||!(e.contains(n.target)||n.composed&&n.composedPath&&n.composedPath().find((function(n){return n!==window&&e.contains(n)}))))}(n.current,e)}))||t()};return document.addEventListener("mousedown",e),document.addEventListener("touchstart",e),function(){document.removeEventListener("mousedown",e),document.removeEventListener("touchstart",e)}}),[n,t])},exports.useOnEscape=function(n,t){e.useEffect((function(){var e=function(e){"Escape"===e.key&&t()},r=n.current;return null==r||r.addEventListener("keydown",e),function(){return null==r?void 0:r.removeEventListener("keydown",e)}}),[n,t])},exports.useOnMount=function(n){var t=e.useRef(!1);e.useEffect((function(){t.current||(t.current=!0,n())}),[n])},exports.useRandomId=function(n){return n+"-"+e.useRef(String(Math.random()).substring(2)).current},exports.useWindowDimensions=function(){var t=e.useState(n()),r=t[0],u=t[1];return e.useEffect((function(){function e(){u(n())}if("undefined"!=typeof window)return window.addEventListener("resize",e),function(){return window.removeEventListener("resize",e)}}),[]),r},exports.warnAboutMissingStyles=function(){};
|
|
2
|
-
//# sourceMappingURL=utils.cjs.production.min.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs.production.min.js","sources":["../src/mergeRefs.ts","../src/useWindowDimensions.ts","../src/ConditionalWrapper.tsx","../src/useDebounce.ts","../src/useForceUpdate.ts","../src/useOnClickOutside.ts","../src/useOnEscape.ts","../src/useOnMount.ts","../src/useRandomId.ts","../src/warnAboutMissingStyles.ts"],"sourcesContent":["export const mergeRefs = <T extends HTMLElement>(\n ...refs: (React.MutableRefObject<T> | React.ForwardedRef<T> | undefined)[]\n) => {\n return (node: T) => {\n for (const ref of refs) {\n if (typeof ref === 'function') {\n ref(node);\n } else if (ref) ref.current = node;\n }\n };\n};\n","// from https://stackoverflow.com/questions/36862334/get-viewport-window-height-in-reactjs\nimport { useState, useEffect } from 'react';\n\ntype WindowDimensions = {\n width: number | undefined;\n height: number | undefined;\n};\n\nconst getWindowDimensions = () => {\n if (typeof window === 'undefined')\n return { width: undefined, height: undefined };\n\n const { innerWidth: width, innerHeight: height } = window;\n return {\n width,\n height,\n };\n};\n\nexport const useWindowDimensions = (): WindowDimensions => {\n const [windowDimensions, setWindowDimensions] = useState(\n getWindowDimensions(),\n );\n\n useEffect(() => {\n function handleResize() {\n setWindowDimensions(getWindowDimensions());\n }\n\n if (typeof window !== 'undefined') {\n window.addEventListener('resize', handleResize);\n return () => window.removeEventListener('resize', handleResize);\n }\n }, []);\n\n return windowDimensions;\n};\n","import React from 'react';\n\ntype ConditionalWrapperType = {\n condition: boolean;\n wrapper: any;\n children: any;\n};\n\nexport const ConditionalWrapper = ({\n condition,\n wrapper,\n children,\n}: ConditionalWrapperType) => (condition ? wrapper(children) : <>{children}</>);\n","import { useEffect, useRef } from 'react';\n\nexport function useDebounce<T extends (...args: any[]) => any>(\n callBack: T,\n debounceTime: number,\n) {\n const timeoutRef = useRef<ReturnType<typeof setTimeout>>();\n\n useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n };\n }, []);\n\n const debouncedFunc = (...args: any[]) => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n\n timeoutRef.current = setTimeout(() => {\n callBack(...args);\n }, debounceTime);\n };\n\n return debouncedFunc as T;\n}\n","import { useState } from 'react';\n\nexport const useForceUpdate = () => {\n const [_, setValue] = useState(0);\n return () => setValue(value => value + 1);\n};\n","import React, { useEffect } from 'react';\n\nexport const useOnClickOutside = (\n refs: React.RefObject<HTMLDivElement>[],\n handler: () => void,\n) => {\n useEffect(() => {\n const listener = (event: Event) => {\n // If the ref contains the clicked element, then the click is not outside\n if (refs.some(ref => elementContainsEventTarget(ref.current, event))) {\n return;\n }\n\n handler();\n };\n\n document.addEventListener('mousedown', listener);\n document.addEventListener('touchstart', listener);\n\n return () => {\n document.removeEventListener('mousedown', listener);\n document.removeEventListener('touchstart', listener);\n };\n }, [refs, handler]);\n};\n\nconst elementContainsEventTarget = (\n element: HTMLElement | null,\n event: Event,\n) => {\n if (!element) {\n return false;\n }\n\n if (element.contains(event.target as Node)) {\n return true;\n }\n\n // For elements inside a Shadow DOM we need to check the composedPath\n if (event.composed && event.composedPath) {\n const contains = event.composedPath().find(target => {\n if (target === window) {\n return false;\n }\n return element.contains(target as Node);\n });\n return contains ? true : false;\n }\n\n return false;\n};\n","import React, { useEffect } from 'react';\n\nexport const useOnEscape = (\n ref: React.RefObject<HTMLElement>,\n handler: () => void,\n) => {\n useEffect(() => {\n const runIfKeyIsEscape = (event: KeyboardEvent) => {\n if (event.key === 'Escape') handler();\n };\n\n const currentRef = ref.current;\n currentRef?.addEventListener('keydown', runIfKeyIsEscape);\n\n return () => currentRef?.removeEventListener('keydown', runIfKeyIsEscape);\n }, [ref, handler]);\n};\n","import React from 'react';\n\nexport function useOnMount(callback: () => void): void {\n const hasRun = React.useRef<boolean>(false);\n\n React.useEffect(() => {\n if (!hasRun.current) {\n hasRun.current = true;\n callback();\n }\n }, [callback]);\n}\n","import React from 'react';\n\nexport const useRandomId = (prefix?: string): string => {\n const ref = React.useRef(String(Math.random()).substring(2));\n return `${prefix}-${ref.current}`;\n};\n","import warning from 'tiny-warning';\n\nconst packagesToCheck: Set<string> = new Set();\nlet checkTimeoutId: number;\n\nfunction checkAndWarn() {\n const missingImports = Array.from(packagesToCheck)\n .filter(\n namespace =>\n parseInt(\n window\n .getComputedStyle(document.documentElement)\n .getPropertyValue(`--eds-${namespace}`),\n ) !== 1,\n )\n .sort();\n\n // Finally, we warn about those pesky imports\n const singleMissingImport = missingImports.length === 1;\n warning(\n missingImports.length === 0,\n `You are missing ${\n singleMissingImport\n ? 'a CSS import'\n : `${missingImports.length} CSS imports`\n }!\n\nPlease add the following CSS import${\n singleMissingImport ? '' : 's'\n } somewhere in your app:\n\n${missingImports\n .map(namespace => `\\t@import '@entur/${namespace}/dist/styles.css';`)\n .join('\\n')}\n`,\n );\n}\n\n/** Warns the developer if they have forgotten to include styles */\nexport function warnAboutMissingStyles(...namespaces: string[]): void {\n // We skip this check in production, and when we build static sites\n if (!__DEV__ || typeof window === 'undefined') {\n return;\n }\n // First, let's clear earlier calls to setTimeout\n window.clearTimeout(checkTimeoutId);\n\n // Next, let's add all namespaces to the set of packages to check\n namespaces.forEach(namespace => packagesToCheck.add(namespace));\n\n // Finally. let's trigger a run of the checker.\n checkTimeoutId = window.setTimeout(checkAndWarn, 1000);\n}\n"],"names":["mergeRefs","getWindowDimensions","window","width","undefined","height","_window","innerWidth","innerHeight","_ref","children","condition","wrapper","React","createElement","Fragment","_len","arguments","length","refs","Array","_key","node","_i","_refs","ref","current","callBack","debounceTime","timeoutRef","useRef","useEffect","clearTimeout","args","setTimeout","apply","useState","setValue","value","handler","listener","event","some","element","contains","target","composed","composedPath","find","elementContainsEventTarget","document","addEventListener","removeEventListener","runIfKeyIsEscape","key","currentRef","callback","hasRun","prefix","String","Math","random","substring","_useState","windowDimensions","setWindowDimensions","handleResize"],"mappings":"mHAAaA,ICQPC,EAAsB,WAC1B,GAAsB,oBAAXC,OACT,MAAO,CAAEC,WAAOC,EAAWC,YAAQD,GAErC,IAAAE,EAAmDJ,OACnD,MAAO,CACLC,QAFM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