@entur/utils 0.12.2 → 0.12.4-beta.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Utils
2
2
 
3
- This package contains the different utils shared across the Entur Designsystem packages.
3
+ This package contains the different utils shared across the Entur Linje packages.
4
4
 
5
5
  > ## Think twice before adding stuff here
6
6
  >
package/dist/index.d.ts CHANGED
@@ -1,13 +1,82 @@
1
- export * from './PolymorphicComponent';
2
- export * from './useDebounce';
3
- export * from './useRandomId';
4
- export * from './useOnMount';
5
- export * from './mergeRefs';
6
- export * from './useForceUpdate';
7
- export * from './useOnClickOutside';
8
- export * from './useOnEscape';
9
- export * from './useWindowDimensions';
10
- export * from './ConditionalWrapper';
11
- export * from './warnAboutMissingStyles';
12
- export * from './types';
13
- export * from './getNodeText';
1
+ import { default as default_2 } from 'react';
2
+
3
+ declare type AsProp<C extends default_2.ElementType> = {
4
+ /**
5
+ * An override of the default HTML tag.
6
+ * Can also be another React component.
7
+ */
8
+ as?: C;
9
+ };
10
+
11
+ export declare const ConditionalWrapper: ({ condition, wrapper, children, }: ConditionalWrapperType) => any;
12
+
13
+ declare type ConditionalWrapperType = {
14
+ condition: boolean;
15
+ wrapper: any;
16
+ children: any;
17
+ };
18
+
19
+ /**
20
+ * Allows for extending a set of props (`ExtendedProps`) by an overriding set of props
21
+ * (`OverrideProps`), ensuring that any duplicates are overridden by the overriding
22
+ * set of props.
23
+ */
24
+ export declare type ExtendableProps<ExtendedProps = Record<string, unknown>, OverrideProps = Record<string, unknown>> = OverrideProps & Omit<ExtendedProps, keyof OverrideProps>;
25
+
26
+ export declare const getNodeText: (node: React.ReactNode | string | number | string[] | number[]) => string;
27
+
28
+ /**
29
+ * Allows for inheriting the props from the specified element type so that
30
+ * props like children, className & style work, as well as element-specific
31
+ * attributes like aria roles. The component (`C`) must be passed in.
32
+ */
33
+ export declare type InheritableElementProps<C extends default_2.ElementType, Props = Record<string, unknown>> = ExtendableProps<PropsOf<C>, Props>;
34
+
35
+ export declare const mergeRefs: <T extends HTMLElement>(...refs: (React.MutableRefObject<T> | React.ForwardedRef<T> | ((node: T | null) => void) | undefined)[]) => (node: T) => void;
36
+
37
+ /**
38
+ * A more sophisticated version of `InheritableElementProps` where
39
+ * the passed in `as` prop will determine which props can be included
40
+ */
41
+ export declare type PolymorphicComponentProps<C extends default_2.ElementType, Props = Record<string, unknown>> = InheritableElementProps<C, Props & AsProp<C>>;
42
+
43
+ /**
44
+ * A wrapper of `PolymorphicComponentProps` that also includes the `ref`
45
+ * prop for the polymorphic component
46
+ */
47
+ export declare type PolymorphicComponentPropsWithRef<C extends default_2.ElementType, Props = Record<string, unknown>> = PolymorphicComponentProps<C, Props> & {
48
+ ref?: PolymorphicRef<C>;
49
+ };
50
+
51
+ /**
52
+ * Utility type to extract the `ref` prop from a polymorphic component
53
+ */
54
+ export declare type PolymorphicRef<C extends default_2.ElementType> = default_2.ComponentPropsWithRef<C>['ref'];
55
+
56
+ export declare type PropsOf<C extends keyof JSX.IntrinsicElements | default_2.JSXElementConstructor<any>> = JSX.LibraryManagedAttributes<C, default_2.ComponentPropsWithoutRef<C>>;
57
+
58
+ export declare function useDebounce<T extends (...args: any[]) => any>(callBack: T, debounceTime: number): T;
59
+
60
+ export declare const useForceUpdate: () => () => void;
61
+
62
+ export declare const useOnClickOutside: (refs: (default_2.RefObject<HTMLElement> | default_2.MutableRefObject<any>)[], handler: () => void) => void;
63
+
64
+ export declare const useOnEscape: (ref: default_2.RefObject<any> | default_2.MutableRefObject<any>, handler: () => void) => void;
65
+
66
+ export declare function useOnMount(callback: () => void): void;
67
+
68
+ export declare const useRandomId: (prefix?: string) => string;
69
+
70
+ export declare const useWindowDimensions: () => WindowDimensions;
71
+
72
+ export declare type VariantType = 'success' | 'negative' | 'warning' | 'information';
73
+
74
+ /** Warns the developer if they have forgotten to include styles */
75
+ export declare function warnAboutMissingStyles(...namespaces: string[]): void;
76
+
77
+ declare type WindowDimensions = {
78
+ width: number | undefined;
79
+ height: number | undefined;
80
+ };
81
+
82
+ export { }
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const React = require("react");
4
+ const jsxRuntime = require("react/jsx-runtime");
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 ref = React.useRef(String(Math.random()).substring(2));
26
+ return `${prefix}-${ref.current}`;
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 ? true : false;
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 getWindowDimensions = () => {
95
+ if (typeof window === "undefined")
96
+ return { width: void 0, height: void 0 };
97
+ const { innerWidth: width, innerHeight: height } = window;
98
+ return {
99
+ width,
100
+ height
101
+ };
102
+ };
103
+ const useWindowDimensions = () => {
104
+ const [windowDimensions, setWindowDimensions] = React.useState(
105
+ getWindowDimensions()
106
+ );
107
+ React.useEffect(() => {
108
+ function handleResize() {
109
+ setWindowDimensions(getWindowDimensions());
110
+ }
111
+ if (typeof window !== "undefined") {
112
+ window.addEventListener("resize", handleResize);
113
+ return () => window.removeEventListener("resize", handleResize);
114
+ }
115
+ }, []);
116
+ return windowDimensions;
117
+ };
118
+ const ConditionalWrapper = ({
119
+ condition,
120
+ wrapper,
121
+ children
122
+ }) => condition ? wrapper(children) : /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
123
+ var isProduction = process.env.NODE_ENV === "production";
124
+ function warning(condition, message) {
125
+ if (!isProduction) {
126
+ if (condition) {
127
+ return;
128
+ }
129
+ var text = "Warning: " + message;
130
+ if (typeof console !== "undefined") {
131
+ console.warn(text);
132
+ }
133
+ try {
134
+ throw Error(text);
135
+ } catch (x) {
136
+ }
137
+ }
138
+ }
139
+ const packagesToCheck = /* @__PURE__ */ new Set();
140
+ let checkTimeoutId;
141
+ function checkAndWarn() {
142
+ const missingImports = Array.from(packagesToCheck).filter(
143
+ (namespace) => parseInt(
144
+ window.getComputedStyle(document.documentElement).getPropertyValue(`--eds-${namespace}`)
145
+ ) !== 1
146
+ ).sort();
147
+ const singleMissingImport = missingImports.length === 1;
148
+ warning(
149
+ missingImports.length === 0,
150
+ `You are missing ${singleMissingImport ? "a CSS import" : `${missingImports.length} CSS imports`}!
151
+
152
+ Please add the following CSS import${singleMissingImport ? "" : "s"} somewhere in your app:
153
+
154
+ ${missingImports.map((namespace) => ` @import '@entur/${namespace}/dist/styles.css';`).join("\n")}
155
+ `
156
+ );
157
+ }
158
+ function warnAboutMissingStyles(...namespaces) {
159
+ if (typeof window === "undefined" || typeof process !== "undefined" && process?.env?.TEST === "true" || typeof process !== "undefined" && process?.env?.NODE_ENV === "production") {
160
+ return;
161
+ }
162
+ window.clearTimeout(checkTimeoutId);
163
+ namespaces.forEach((namespace) => packagesToCheck.add(namespace));
164
+ checkTimeoutId = window.setTimeout(checkAndWarn, 1e3);
165
+ }
166
+ const getNodeText = (node) => {
167
+ if (node === null || node === void 0) return "";
168
+ if (["string", "number"].includes(typeof node)) return node.toString();
169
+ if (node instanceof Array) return node.map(getNodeText).join("").trim();
170
+ if (typeof node === "object")
171
+ return getNodeText(node.props?.children ?? "").trim();
172
+ return "unknown";
173
+ };
174
+ exports.ConditionalWrapper = ConditionalWrapper;
175
+ exports.getNodeText = getNodeText;
176
+ exports.mergeRefs = mergeRefs;
177
+ exports.useDebounce = useDebounce;
178
+ exports.useForceUpdate = useForceUpdate;
179
+ exports.useOnClickOutside = useOnClickOutside;
180
+ exports.useOnEscape = useOnEscape;
181
+ exports.useOnMount = useOnMount;
182
+ exports.useRandomId = useRandomId;
183
+ exports.useWindowDimensions = useWindowDimensions;
184
+ exports.warnAboutMissingStyles = warnAboutMissingStyles;
185
+ //# 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","../../../node_modules/tiny-warning/dist/tiny-warning.esm.js","../src/warnAboutMissingStyles.ts","../src/getNodeText.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: (\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 ? true : false;\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","// 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","var isProduction = process.env.NODE_ENV === 'production';\nfunction warning(condition, message) {\n if (!isProduction) {\n if (condition) {\n return;\n }\n\n var text = \"Warning: \" + message;\n\n if (typeof console !== 'undefined') {\n console.warn(text);\n }\n\n try {\n throw Error(text);\n } catch (x) {}\n }\n}\n\nexport default warning;\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"],"names":["useRef","useEffect","useState"],"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;ACzBO,MAAM,cAAc,CAAC,WAA4B;AACtD,QAAM,MAAM,MAAM,OAAO,OAAO,KAAK,QAAQ,EAAE,UAAU,CAAC,CAAC;AAC3D,SAAO,GAAG,MAAM,IAAI,IAAI,OAAO;AACjC;ACHO,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,QA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AA,IAC1B,mBACE,sBACI,iBACA,GAAG,eAAe,MAAM,cAC9B;AAAA;AAAA,qCAGE,sBAAsB,KAAK,GAC7B;AAAA;AAAA,EAEF,eACC,IAAI,CAAA,cAAa,oBAAqB,SAAS,oBAAoB,EACnE,KAAK,IAAI,CAAC;AAAA;AAAA,EAAA;AAGb;AAGO,SAAS,0BAA0B,YAA4B;AAEpE,MACE,OAAO,WAAW,eACjB,OAAO,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;;;;;;;;;;;;","x_google_ignoreList":[9]}
package/dist/utils.esm.js CHANGED
@@ -1,97 +1,75 @@
1
- import React, { useRef, useEffect, useState } from 'react';
2
- import warning from 'tiny-warning';
3
-
1
+ import React, { useRef, useEffect, useState } from "react";
2
+ import { jsx, Fragment } from "react/jsx-runtime";
4
3
  function useDebounce(callBack, debounceTime) {
5
- var timeoutRef = useRef();
6
- useEffect(function () {
7
- return function () {
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
- var debouncedFunc = function debouncedFunc() {
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(function () {
21
- callBack.apply(void 0, args);
16
+ timeoutRef.current = setTimeout(() => {
17
+ callBack(...args);
22
18
  }, debounceTime);
23
19
  };
24
20
  return debouncedFunc;
25
21
  }
26
-
27
- var useRandomId = function useRandomId(prefix) {
28
- var ref = React.useRef(String(Math.random()).substring(2));
29
- return prefix + "-" + ref.current;
22
+ const useRandomId = (prefix) => {
23
+ const ref = React.useRef(String(Math.random()).substring(2));
24
+ return `${prefix}-${ref.current}`;
30
25
  };
31
-
32
26
  function useOnMount(callback) {
33
- var hasRun = React.useRef(false);
34
- React.useEffect(function () {
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
- var mergeRefs = function mergeRefs() {
43
- for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
44
- refs[_key] = arguments[_key];
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
- var useForceUpdate = function useForceUpdate() {
57
- var _useState = useState(0),
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
- var useOnClickOutside = function useOnClickOutside(refs, handler) {
67
- useEffect(function () {
68
- var listener = function listener(event) {
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('mousedown', listener);
78
- document.addEventListener('touchstart', listener);
79
- return function () {
80
- document.removeEventListener('mousedown', listener);
81
- document.removeEventListener('touchstart', listener);
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
- var elementContainsEventTarget = function elementContainsEventTarget(element, event) {
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
- var contains = event.composedPath().find(function (target) {
72
+ const contains = event.composedPath().find((target) => {
95
73
  if (target === window) {
96
74
  return false;
97
75
  }
@@ -101,102 +79,107 @@ var elementContainsEventTarget = function elementContainsEventTarget(element, ev
101
79
  }
102
80
  return false;
103
81
  };
104
-
105
- var useOnEscape = function useOnEscape(ref, handler) {
106
- useEffect(function () {
107
- var runIfKeyIsEscape = function runIfKeyIsEscape(event) {
108
- if (event.key === 'Escape') handler();
109
- };
110
- var currentRef = ref.current;
111
- currentRef == null || 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
- // from https://stackoverflow.com/questions/36862334/get-viewport-window-height-in-reactjs
119
- var getWindowDimensions = function getWindowDimensions() {
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;
92
+ const getWindowDimensions = () => {
93
+ if (typeof window === "undefined")
94
+ return { width: void 0, height: void 0 };
95
+ const { innerWidth: width, innerHeight: height } = window;
127
96
  return {
128
- width: width,
129
- height: height
97
+ width,
98
+ height
130
99
  };
131
100
  };
132
- var useWindowDimensions = function useWindowDimensions() {
133
- var _useState = useState(getWindowDimensions()),
134
- windowDimensions = _useState[0],
135
- setWindowDimensions = _useState[1];
136
- useEffect(function () {
101
+ const useWindowDimensions = () => {
102
+ const [windowDimensions, setWindowDimensions] = useState(
103
+ getWindowDimensions()
104
+ );
105
+ useEffect(() => {
137
106
  function handleResize() {
138
107
  setWindowDimensions(getWindowDimensions());
139
108
  }
140
- if (typeof window !== 'undefined') {
141
- window.addEventListener('resize', handleResize);
142
- return function () {
143
- return window.removeEventListener('resize', handleResize);
144
- };
109
+ if (typeof window !== "undefined") {
110
+ window.addEventListener("resize", handleResize);
111
+ return () => window.removeEventListener("resize", handleResize);
145
112
  }
146
113
  }, []);
147
114
  return windowDimensions;
148
115
  };
116
+ const ConditionalWrapper = ({
117
+ condition,
118
+ wrapper,
119
+ children
120
+ }) => condition ? wrapper(children) : /* @__PURE__ */ jsx(Fragment, { children });
121
+ var isProduction = process.env.NODE_ENV === "production";
122
+ function warning(condition, message) {
123
+ if (!isProduction) {
124
+ if (condition) {
125
+ return;
126
+ }
127
+ var text = "Warning: " + message;
128
+ if (typeof console !== "undefined") {
129
+ console.warn(text);
130
+ }
131
+ try {
132
+ throw Error(text);
133
+ } catch (x) {
134
+ }
135
+ }
136
+ }
137
+ const packagesToCheck = /* @__PURE__ */ new Set();
138
+ let checkTimeoutId;
139
+ function checkAndWarn() {
140
+ const missingImports = Array.from(packagesToCheck).filter(
141
+ (namespace) => parseInt(
142
+ window.getComputedStyle(document.documentElement).getPropertyValue(`--eds-${namespace}`)
143
+ ) !== 1
144
+ ).sort();
145
+ const singleMissingImport = missingImports.length === 1;
146
+ warning(
147
+ missingImports.length === 0,
148
+ `You are missing ${singleMissingImport ? "a CSS import" : `${missingImports.length} CSS imports`}!
149
149
 
150
- var ConditionalWrapper = function ConditionalWrapper(_ref) {
151
- var condition = _ref.condition,
152
- wrapper = _ref.wrapper,
153
- children = _ref.children;
154
- return condition ? wrapper(children) : React.createElement(React.Fragment, null, children);
155
- };
150
+ Please add the following CSS import${singleMissingImport ? "" : "s"} somewhere in your app:
156
151
 
157
- var packagesToCheck = /*#__PURE__*/new Set();
158
- var checkTimeoutId;
159
- function checkAndWarn() {
160
- var missingImports = Array.from(packagesToCheck).filter(function (namespace) {
161
- return parseInt(window.getComputedStyle(document.documentElement).getPropertyValue("--eds-" + namespace)) !== 1;
162
- }).sort();
163
- // Finally, we warn about those pesky imports
164
- var singleMissingImport = missingImports.length === 1;
165
- process.env.NODE_ENV !== "production" ? 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) {
166
- return "\t@import '@entur/" + namespace + "/dist/styles.css';";
167
- }).join('\n') + "\n") : void 0;
152
+ ${missingImports.map((namespace) => ` @import '@entur/${namespace}/dist/styles.css';`).join("\n")}
153
+ `
154
+ );
168
155
  }
169
- /** Warns the developer if they have forgotten to include styles */
170
- function warnAboutMissingStyles() {
171
- var _process;
172
- // We skip this check in production, and when we build static sites
173
- if (!(process.env.NODE_ENV !== "production") || typeof window === 'undefined' || typeof process !== 'undefined' && ((_process = process) == null || (_process = _process.env) == null ? void 0 : _process.TEST) === 'true') {
156
+ function warnAboutMissingStyles(...namespaces) {
157
+ if (typeof window === "undefined" || typeof process !== "undefined" && process?.env?.TEST === "true" || typeof process !== "undefined" && process?.env?.NODE_ENV === "production") {
174
158
  return;
175
159
  }
176
- // First, let's clear earlier calls to setTimeout
177
160
  window.clearTimeout(checkTimeoutId);
178
- // Next, let's add all namespaces to the set of packages to check
179
- for (var _len = arguments.length, namespaces = new Array(_len), _key = 0; _key < _len; _key++) {
180
- namespaces[_key] = arguments[_key];
181
- }
182
- namespaces.forEach(function (namespace) {
183
- return packagesToCheck.add(namespace);
184
- });
185
- // Finally. let's trigger a run of the checker.
186
- checkTimeoutId = window.setTimeout(checkAndWarn, 1000);
161
+ namespaces.forEach((namespace) => packagesToCheck.add(namespace));
162
+ checkTimeoutId = window.setTimeout(checkAndWarn, 1e3);
187
163
  }
188
-
189
- // with inspiration from https://stackoverflow.com/questions/50428910/get-text-content-from-node-in-react
190
- var _getNodeText = function getNodeText(node) {
191
- var _node$props$children, _node$props;
192
- if (node === null || node === undefined) return '';
193
- if (['string', 'number'].includes(typeof node)) return node.toString();
194
- if (node instanceof Array) return node.map(_getNodeText).join('').trim();
195
- if (typeof node === 'object')
196
- // @ts-expect-error props does exist for react nodes
197
- return _getNodeText((_node$props$children = (_node$props = node.props) == null ? void 0 : _node$props.children) != null ? _node$props$children : '').trim();
198
- return 'unknown';
164
+ const getNodeText = (node) => {
165
+ if (node === null || node === void 0) return "";
166
+ if (["string", "number"].includes(typeof node)) return node.toString();
167
+ if (node instanceof Array) return node.map(getNodeText).join("").trim();
168
+ if (typeof node === "object")
169
+ return getNodeText(node.props?.children ?? "").trim();
170
+ return "unknown";
171
+ };
172
+ export {
173
+ ConditionalWrapper,
174
+ getNodeText,
175
+ mergeRefs,
176
+ useDebounce,
177
+ useForceUpdate,
178
+ useOnClickOutside,
179
+ useOnEscape,
180
+ useOnMount,
181
+ useRandomId,
182
+ useWindowDimensions,
183
+ warnAboutMissingStyles
199
184
  };
200
-
201
- export { ConditionalWrapper, _getNodeText as getNodeText, mergeRefs, useDebounce, useForceUpdate, useOnClickOutside, useOnEscape, useOnMount, useRandomId, useWindowDimensions, warnAboutMissingStyles };
202
185
  //# sourceMappingURL=utils.esm.js.map
@@ -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","../src/getNodeText.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: (\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 ? true : false;\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","// 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 (\n !__DEV__ ||\n typeof window === 'undefined' ||\n (typeof process !== 'undefined' && process?.env?.TEST === 'true')\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"],"names":["useDebounce","callBack","debounceTime","timeoutRef","useRef","useEffect","current","clearTimeout","debouncedFunc","_len","arguments","length","args","Array","_key","setTimeout","apply","useRandomId","prefix","ref","React","String","Math","random","substring","useOnMount","callback","hasRun","mergeRefs","refs","node","_i","_refs","useForceUpdate","_useState","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","_window","innerWidth","innerHeight","useWindowDimensions","windowDimensions","setWindowDimensions","handleResize","ConditionalWrapper","_ref","condition","wrapper","children","createElement","Fragment","packagesToCheck","Set","checkTimeoutId","checkAndWarn","missingImports","from","filter","namespace","parseInt","getComputedStyle","documentElement","getPropertyValue","sort","singleMissingImport","process","env","NODE_ENV","warning","map","join","warnAboutMissingStyles","_process","TEST","namespaces","forEach","add","getNodeText","_node$props$children","_node$props","includes","toString","trim","props"],"mappings":";;;AAEgB,SAAAA,WAAWA,CACzBC,QAAW,EACXC,YAAoB,EAAA;AAEpB,EAAA,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;AAClC,OAAA;KACD,CAAA;GACF,EAAE,EAAE,CAAC,CAAA;AAEN,EAAA,IAAME,aAAa,GAAG,SAAhBA,aAAaA,GAAsB;AAAA,IAAA,KAAA,IAAAC,IAAA,GAAAC,SAAA,CAAAC,MAAA,EAAfC,IAAW,GAAAC,IAAAA,KAAA,CAAAJ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ApBsF,IAAAA,UAAoB,CAAAtF,IAAA,CAAAJ,GAAAA,SAAA,CAAAI,IAAA,CAAA,CAAA;AAAA,GAAA;AAa5DsF,EAAAA,UAAU,CAACC,OAAO,CAAC,UAAAjB,SAAS,EAAA;AAAA,IAAA,OAAIP,eAAe,CAACyB,GAAG,CAAClB,SAAS,CAAC,CAAA;GAAC,CAAA,CAAA;AAE/D;EACAL,cAAc,GAAGzB,MAAM,CAACvC,UAAU,CAACiE,YAAY,EAAE,IAAI,CAAC,CAAA;AACxD;;ACxDA;AACO,IAAMuB,YAAW,GAAG,SAAdA,WAAWA,CACtBzE,IAA6D,EACnD;EAAA,IAAA0E,oBAAA,EAAAC,WAAA,CAAA;EACV,IAAI3E,IAAI,KAAK,IAAI,IAAIA,IAAI,KAAK+B,SAAS,EAAE,OAAO,EAAE,CAAA;AAClD,EAAA,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC6C,QAAQ,CAAC,OAAO5E,IAAI,CAAC,EAAE,OAAOA,IAAI,CAAC6E,QAAQ,EAAE,CAAA;AACtE,EAAA,IAAI7E,IAAI,YAAYjB,KAAK,EAAE,OAAOiB,IAAI,CAACiE,GAAG,CAACQ,YAAW,CAAC,CAACP,IAAI,CAAC,EAAE,CAAC,CAACY,IAAI,EAAE,CAAA;EACvE,IAAI,OAAO9E,IAAI,KAAK,QAAQ;AAC1B;IACA,OAAOyE,YAAW,EAAAC,oBAAA,GAAA,CAAAC,WAAA,GAAC3E,IAAI,CAAC+E,KAAK,KAAA,IAAA,GAAA,KAAA,CAAA,GAAVJ,WAAA,CAAY/B,QAAQ,YAAA8B,oBAAA,GAAI,EAAE,CAAC,CAACI,IAAI,EAAE,CAAA;AACvD,EAAA,OAAO,SAAS,CAAA;AAClB;;;;"}
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","../../../node_modules/tiny-warning/dist/tiny-warning.esm.js","../src/warnAboutMissingStyles.ts","../src/getNodeText.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: (\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 ? true : false;\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","// 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","var isProduction = process.env.NODE_ENV === 'production';\nfunction warning(condition, message) {\n if (!isProduction) {\n if (condition) {\n return;\n }\n\n var text = \"Warning: \" + message;\n\n if (typeof console !== 'undefined') {\n console.warn(text);\n }\n\n try {\n throw Error(text);\n } catch (x) {}\n }\n}\n\nexport default warning;\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"],"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;ACzBO,MAAM,cAAc,CAAC,WAA4B;AACtD,QAAM,MAAM,MAAM,OAAO,OAAO,KAAK,QAAQ,EAAE,UAAU,CAAC,CAAC;AAC3D,SAAO,GAAG,MAAM,IAAI,IAAI,OAAO;AACjC;ACHO,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,IAAI,SAAS,CAAC;AAChC,SAAO,MAAM,SAAS,CAAA,UAAS,QAAQ,CAAC;AAC1C;ACHO,MAAM,oBAAoB,CAC/B,MACA,YACG;AACH,YAAU,MAAM;AACd,UAAM,WAAW,CAAC,UAAiB;AAEjC,UAAI,K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AA0B,YAA4B;AAEpE,MACE,OAAO,WAAW,eACjB,OAAO,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;","x_google_ignoreList":[9]}
package/package.json CHANGED
@@ -1,10 +1,18 @@
1
1
  {
2
2
  "name": "@entur/utils",
3
- "version": "0.12.2",
3
+ "version": "0.12.4-beta.0",
4
4
  "license": "EUPL-1.2",
5
- "main": "dist/index.js",
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
+ },
14
+ "./dist/*": "./dist/*"
15
+ },
8
16
  "files": [
9
17
  "dist"
10
18
  ],
@@ -17,9 +25,10 @@
17
25
  "access": "public"
18
26
  },
19
27
  "scripts": {
20
- "start": "dts watch --noClean",
21
- "build": "dts build && cp src/*.scss dist",
22
- "lint": "dts lint src"
28
+ "start": "vite build --watch",
29
+ "build": "vite build && cp src/*.scss dist",
30
+ "test": "jest",
31
+ "lint": "eslint src"
23
32
  },
24
33
  "peerDependencies": {
25
34
  "react": ">=16.8.0",
@@ -29,7 +38,16 @@
29
38
  "tiny-warning": "^1.0.3"
30
39
  },
31
40
  "devDependencies": {
32
- "dts-cli": "2.0.5"
41
+ "@testing-library/jest-dom": "^5.17.0",
42
+ "@testing-library/react": "^10.4.9",
43
+ "@testing-library/user-event": "14.6.1",
44
+ "eslint": "^7.32.0",
45
+ "jest": "^29.0.0",
46
+ "jest-environment-jsdom": "^29.0.0",
47
+ "ts-jest": "^29.0.0",
48
+ "typescript": "^5.9.2",
49
+ "vite": "^7.1.3",
50
+ "vite-plugin-dts": "^4.5.4"
33
51
  },
34
- "gitHead": "835e1335b390f48b80379ae6c473ac89041a392a"
52
+ "gitHead": "414181fa2084722438238256a9aa32e304896b9d"
35
53
  }
@@ -1,7 +0,0 @@
1
- type ConditionalWrapperType = {
2
- condition: boolean;
3
- wrapper: any;
4
- children: any;
5
- };
6
- export declare const ConditionalWrapper: ({ condition, wrapper, children, }: ConditionalWrapperType) => any;
7
- export {};
@@ -1,41 +0,0 @@
1
- /** These polymorphic types are derrived from Ben Ilegbodu's article available at the following link:
2
- * https://www.benmvp.com/blog/forwarding-refs-polymorphic-react-component-typescript/
3
- */
4
- import React from 'react';
5
- export type PropsOf<C extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>> = JSX.LibraryManagedAttributes<C, React.ComponentPropsWithoutRef<C>>;
6
- type AsProp<C extends React.ElementType> = {
7
- /**
8
- * An override of the default HTML tag.
9
- * Can also be another React component.
10
- */
11
- as?: C;
12
- };
13
- /**
14
- * Allows for extending a set of props (`ExtendedProps`) by an overriding set of props
15
- * (`OverrideProps`), ensuring that any duplicates are overridden by the overriding
16
- * set of props.
17
- */
18
- export type ExtendableProps<ExtendedProps = Record<string, unknown>, OverrideProps = Record<string, unknown>> = OverrideProps & Omit<ExtendedProps, keyof OverrideProps>;
19
- /**
20
- * Allows for inheriting the props from the specified element type so that
21
- * props like children, className & style work, as well as element-specific
22
- * attributes like aria roles. The component (`C`) must be passed in.
23
- */
24
- export type InheritableElementProps<C extends React.ElementType, Props = Record<string, unknown>> = ExtendableProps<PropsOf<C>, Props>;
25
- /**
26
- * A more sophisticated version of `InheritableElementProps` where
27
- * the passed in `as` prop will determine which props can be included
28
- */
29
- export type PolymorphicComponentProps<C extends React.ElementType, Props = Record<string, unknown>> = InheritableElementProps<C, Props & AsProp<C>>;
30
- /**
31
- * Utility type to extract the `ref` prop from a polymorphic component
32
- */
33
- export type PolymorphicRef<C extends React.ElementType> = React.ComponentPropsWithRef<C>['ref'];
34
- /**
35
- * A wrapper of `PolymorphicComponentProps` that also includes the `ref`
36
- * prop for the polymorphic component
37
- */
38
- export type PolymorphicComponentPropsWithRef<C extends React.ElementType, Props = Record<string, unknown>> = PolymorphicComponentProps<C, Props> & {
39
- ref?: PolymorphicRef<C>;
40
- };
41
- export {};
@@ -1 +0,0 @@
1
- export declare const getNodeText: (node: React.ReactNode | string | number | string[] | number[]) => string;
package/dist/index.js DELETED
@@ -1,8 +0,0 @@
1
-
2
- 'use strict'
3
-
4
- if (process.env.NODE_ENV === 'production') {
5
- module.exports = require('./utils.cjs.production.min.js')
6
- } else {
7
- module.exports = require('./utils.cjs.development.js')
8
- }
@@ -1 +0,0 @@
1
- export declare const mergeRefs: <T extends HTMLElement>(...refs: (React.MutableRefObject<T> | React.ForwardedRef<T> | ((node: T | null) => void) | undefined)[]) => (node: T) => void;
@@ -1 +0,0 @@
1
- export * from './variants';
@@ -1 +0,0 @@
1
- export type VariantType = 'success' | 'negative' | 'warning' | 'information';
@@ -1 +0,0 @@
1
- export declare function useDebounce<T extends (...args: any[]) => any>(callBack: T, debounceTime: number): T;
@@ -1 +0,0 @@
1
- export declare const useForceUpdate: () => () => void;
@@ -1,2 +0,0 @@
1
- import React from 'react';
2
- export declare const useOnClickOutside: (refs: (React.RefObject<HTMLElement> | React.MutableRefObject<any>)[], handler: () => void) => void;
@@ -1,2 +0,0 @@
1
- import React from 'react';
2
- export declare const useOnEscape: (ref: React.RefObject<any> | React.MutableRefObject<any>, handler: () => void) => void;
@@ -1 +0,0 @@
1
- export declare function useOnMount(callback: () => void): void;
@@ -1 +0,0 @@
1
- export declare const useRandomId: (prefix?: string) => string;
@@ -1,6 +0,0 @@
1
- type WindowDimensions = {
2
- width: number | undefined;
3
- height: number | undefined;
4
- };
5
- export declare const useWindowDimensions: () => WindowDimensions;
6
- export {};
@@ -1,216 +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 || 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
- var _process;
176
- // We skip this check in production, and when we build static sites
177
- if (typeof window === 'undefined' || typeof process !== 'undefined' && ((_process = process) == null || (_process = _process.env) == null ? void 0 : _process.TEST) === 'true') {
178
- return;
179
- }
180
- // First, let's clear earlier calls to setTimeout
181
- window.clearTimeout(checkTimeoutId);
182
- // Next, let's add all namespaces to the set of packages to check
183
- for (var _len = arguments.length, namespaces = new Array(_len), _key = 0; _key < _len; _key++) {
184
- namespaces[_key] = arguments[_key];
185
- }
186
- namespaces.forEach(function (namespace) {
187
- return packagesToCheck.add(namespace);
188
- });
189
- // Finally. let's trigger a run of the checker.
190
- checkTimeoutId = window.setTimeout(checkAndWarn, 1000);
191
- }
192
-
193
- // with inspiration from https://stackoverflow.com/questions/50428910/get-text-content-from-node-in-react
194
- var _getNodeText = function getNodeText(node) {
195
- var _node$props$children, _node$props;
196
- if (node === null || node === undefined) return '';
197
- if (['string', 'number'].includes(typeof node)) return node.toString();
198
- if (node instanceof Array) return node.map(_getNodeText).join('').trim();
199
- if (typeof node === 'object')
200
- // @ts-expect-error props does exist for react nodes
201
- return _getNodeText((_node$props$children = (_node$props = node.props) == null ? void 0 : _node$props.children) != null ? _node$props$children : '').trim();
202
- return 'unknown';
203
- };
204
-
205
- exports.ConditionalWrapper = ConditionalWrapper;
206
- exports.getNodeText = _getNodeText;
207
- exports.mergeRefs = mergeRefs;
208
- exports.useDebounce = useDebounce;
209
- exports.useForceUpdate = useForceUpdate;
210
- exports.useOnClickOutside = useOnClickOutside;
211
- exports.useOnEscape = useOnEscape;
212
- exports.useOnMount = useOnMount;
213
- exports.useRandomId = useRandomId;
214
- exports.useWindowDimensions = useWindowDimensions;
215
- exports.warnAboutMissingStyles = warnAboutMissingStyles;
216
- //# 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","../src/getNodeText.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: (\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 ? true : false;\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","// 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 (\n !__DEV__ ||\n typeof window === 'undefined' ||\n (typeof process !== 'undefined' && process?.env?.TEST === 'true')\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"],"names":["useDebounce","callBack","debounceTime","timeoutRef","useRef","useEffect","current","clearTimeout","debouncedFunc","_len","arguments","length","args","Array","_key","setTimeout","apply","useRandomId","prefix","ref","React","String","Math","random","substring","useOnMount","callback","hasRun","mergeRefs","refs","node","_i","_refs","useForceUpdate","_useState","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","_window","innerWidth","innerHeight","useWindowDimensions","windowDimensions","setWindowDimensions","handleResize","ConditionalWrapper","_ref","condition","wrapper","children","createElement","Fragment","packagesToCheck","Set","checkTimeoutId","checkAndWarn","missingImports","from","filter","namespace","parseInt","getComputedStyle","documentElement","getPropertyValue","sort","singleMissingImport","process","warning","map","join","warnAboutMissingStyles","_process","env","TEST","namespaces","forEach","add","getNodeText","_node$props$children","_node$props","includes","toString","trim","props"],"mappings":";;;;;;;AAEgB,SAAAA,WAAWA,CACzBC,QAAW,EACXC,YAAoB,EAAA;AAEpB,EAAA,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;AAClC,OAAA;KACD,CAAA;GACF,EAAE,EAAE,CAAC,CAAA;AAEN,EAAA,IAAME,aAAa,GAAG,SAAhBA,aAAaA,GAAsB;AAAA,IAAA,KAAA,IAAAC,IAAA,GAAAC,SAAA,CAAAC,MAAA,EAAfC,IAAW,GAAAC,IAAAA,KAAA,CAAAJ,IAAA,GAAAK,IAAA,GAAA,CAAA,EAAAA,IAAA,GAAAL,IAAA,EAAAK,IAAA,EAAA,EAAA;AAAXF,MAAAA,IAAW,CAAAE,IAAA,CAAAJ,GAAAA,SAAA,CAAAI,IAAA,CAAA,CAAA;AAAA,KAAA;IACnC,IAAIX,UAAU,CAACG,OAAO,EAAE;AACtBC,MAAAA,YAAY,CAACJ,UAAU,CAACG,OAAO,CAAC,CAAA;AAClC,KAAA;AAEAH,IAAAA,UAAU,CAACG,OAAO,GAAGS,UAAU,CAAC,YAAK;AACnCd,MAAAA,QAAQ,CAAAe,KAAA,CAAIJ,KAAAA,CAAAA,EAAAA,IAAI,CAAC,CAAA;KAClB,EAAEV,YAAY,CAAC,CAAA;GACjB,CAAA;AAED,EAAA,OAAOM,aAAkB,CAAA;AAC3B;;ICzBaS,WAAW,GAAG,SAAdA,WAAWA,CAAIC,MAAe,EAAY;AACrD,EAAA,IAAMC,GAAG,GAAGC,KAAK,CAAChB,MAAM,CAACiB,MAAM,CAACC,IAAI,CAACC,MAAM,EAAE,CAAC,CAACC,SAAS,CAAC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AAE,CAAA;EACvE,IAAI,OAAO7E,IAAI,KAAK,QAAQ;AAC1B;IACA,OAAOwE,YAAW,EAAAC,oBAAA,GAAA,CAAAC,WAAA,GAAC1E,IAAI,CAAC8E,KAAK,KAAA,IAAA,GAAA,KAAA,CAAA,GAAVJ,WAAA,CAAY9B,QAAQ,YAAA6B,oBAAA,GAAI,EAAE,CAAC,CAACI,IAAI,EAAE,CAAA;AACvD,EAAA,OAAO,SAAS,CAAA;AAClB;;;;;;;;;;;;;;"}
@@ -1,2 +0,0 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var n=require("react");require("tiny-warning");var e=function(){if("undefined"==typeof window)return{width:void 0,height:void 0};var n=window;return{width:n.innerWidth,height:n.innerHeight}},t=function(n){var e,r;return null==n?"":["string","number"].includes(typeof n)?n.toString():n instanceof Array?n.map(t).join("").trim():"object"==typeof n?t(null!=(e=null==(r=n.props)?void 0:r.children)?e:"").trim():"unknown"};exports.ConditionalWrapper=function(e){var t=e.children;return e.condition?(0,e.wrapper)(t):n.createElement(n.Fragment,null,t)},exports.getNodeText=t,exports.mergeRefs=function(){for(var n=arguments.length,e=new Array(n),t=0;t<n;t++)e[t]=arguments[t];return function(n){for(var t=0,r=e;t<r.length;t++){var o=r[t];"function"==typeof o?o(n):o&&(o.current=n)}}},exports.useDebounce=function(e,t){var r=n.useRef();return n.useEffect((function(){return function(){r.current&&clearTimeout(r.current)}}),[]),function(){for(var n=arguments.length,o=new Array(n),u=0;u<n;u++)o[u]=arguments[u];r.current&&clearTimeout(r.current),r.current=setTimeout((function(){e.apply(void 0,o)}),t)}},exports.useForceUpdate=function(){var e=n.useState(0)[1];return function(){return e((function(n){return n+1}))}},exports.useOnClickOutside=function(e,t){n.useEffect((function(){var n=function(n){e.some((function(e){return function(n,e){return!(!n||!(n.contains(e.target)||e.composed&&e.composedPath&&e.composedPath().find((function(e){return e!==window&&n.contains(e)}))))}(e.current,n)}))||t()};return document.addEventListener("mousedown",n),document.addEventListener("touchstart",n),function(){document.removeEventListener("mousedown",n),document.removeEventListener("touchstart",n)}}),[e,t])},exports.useOnEscape=function(e,t){n.useEffect((function(){var n=function(n){"Escape"===n.key&&t()},r=e.current;return null==r||r.addEventListener("keydown",n),function(){return null==r?void 0:r.removeEventListener("keydown",n)}}),[e,t])},exports.useOnMount=function(e){var t=n.useRef(!1);n.useEffect((function(){t.current||(t.current=!0,e())}),[e])},exports.useRandomId=function(e){return e+"-"+n.useRef(String(Math.random()).substring(2)).current},exports.useWindowDimensions=function(){var t=n.useState(e()),r=t[0],o=t[1];return n.useEffect((function(){function n(){o(e())}if("undefined"!=typeof window)return window.addEventListener("resize",n),function(){return window.removeEventListener("resize",n)}}),[]),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/useWindowDimensions.ts","../src/getNodeText.ts","../src/ConditionalWrapper.tsx","../src/mergeRefs.ts","../src/useDebounce.ts","../src/useForceUpdate.ts","../src/useOnClickOutside.ts","../src/useOnEscape.ts","../src/useOnMount.ts","../src/useRandomId.ts","../src/warnAboutMissingStyles.ts"],"sourcesContent":["// 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","// 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 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","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 { 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<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 ? true : false;\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 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 (\n !__DEV__ ||\n typeof window === 'undefined' ||\n (typeof process !== 'undefined' && process?.env?.TEST === 'true')\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"],"names":["getWindowDimensions","window","width","undefined","height","_window","innerWidth","innerHeight","getNodeText","node","_node$props$children","_node$props","includes","toString","Array","map","join","trim","props","children","_ref","condition","wrapper","React","createElement","Fragment","_len","arguments","length","refs","_key","_i","_refs","ref","current","callBack","debounceTime","timeoutRef","useRef","useEffect","clearTimeout","args","setTimeout","apply","setValue","useState","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":"uHAQMA,EAAsB,WAC1B,GAAsB,oBAAXC,OACT,MAAO,CAAEC,WAAOC,EAAWC,YAAQD,GAErC,IAAAE,EAAmDJ,OACnD,MAAO,CACLC,MAFuBG,EAAjBC,WAGNF,OAH4CC,EAAnBE,YAK7B,EChBaC,EAAc,SACzBC,GACU,IAAAC,EAAAC,EACV,OAAIF,QAA4C,GAC5C,CAAC,SAAU,UAAUG,gBAAgBH,GAAcA,EAAKI,WACxDJ,aAAgBK,MAAcL,EAAKM,IAAIP,GAAaQ,KAAK,IAAIC,OAC7C,iBAATR,EAEFD,SAAWE,EAAW,OAAXC,EAACF,EAAKS,YAAK,EAAVP,EA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eAAeC,MAAK,SAAAH,GACzC,OAAIA,IAAWpD,QAGRkD,EAAQC,SAASC,EAC1B,KAKJ,CAzC2BI,CAA2BxB,EAAIC,QAASe,EAAM,KAInEF,KAMF,OAHAW,SAASC,iBAAiB,YAAaX,GACvCU,SAASC,iBAAiB,aAAcX,GAEjC,WACLU,SAASE,oBAAoB,YAAaZ,GAC1CU,SAASE,oBAAoB,aAAcZ,GAE/C,GAAG,CAACnB,EAAMkB,GACZ,sBCtB2B,SACzBd,EACAc,GAEAR,EAAAA,WAAU,WACR,IAAMsB,EAAmB,SAACZ,GACN,WAAdA,EAAMa,KAAkBf,KAGxBgB,EAAa9B,EAAIC,QAGvB,OAFU,MAAV6B,GAAAA,EAAYJ,iBAAiB,UAAWE,GAEjC,WAAA,aAAME,SAAAA,EAAYH,oBAAoB,UAAWC,EAAiB,CAC3E,GAAG,CAAC5B,EAAKc,GACX,qBCdM,SAAqBiB,GACzB,IAAMC,EAAS1C,EAAMe,QAAgB,GAErCf,EAAMgB,WAAU,WACT0B,EAAO/B,UACV+B,EAAO/B,SAAU,EACjB8B,IAEJ,GAAG,CAACA,GACN,sBCT2B,SAACE,GAE1B,OAAUA,EAAM,IADJ3C,EAAMe,OAAO6B,OAAOC,KAAKC,UAAUC,UAAU,IACjCpC,OAC1B,8BTcmC,WACjC,IAAAqC,EAAgD1B,WAC9C7C,KADKwE,EAAgBD,EAAA,GAAEE,EAAmBF,EAAA,GAe5C,OAXAhC,EAAAA,WAAU,WACR,SAASmC,IACPD,EAAoBzE,IACtB,CAEA,GAAsB,oBAAXC,OAET,OADAA,OAAO0D,iBAAiB,SAAUe,GAC3B,WAAA,OAAMzE,OAAO2D,oBAAoB,SAAUc,EAAa,CAElE,GAAE,IAEIF,CACT,iCUGgB,WAiBhB"}
@@ -1,2 +0,0 @@
1
- /** Warns the developer if they have forgotten to include styles */
2
- export declare function warnAboutMissingStyles(...namespaces: string[]): void;