@entur/utils 0.13.1 → 0.13.2
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/dist/index.d.ts +1 -0
- package/dist/useControllableProp.d.ts +6 -0
- package/dist/utils.cjs.js +14 -0
- package/dist/utils.cjs.js.map +1 -1
- package/dist/utils.esm.js +14 -0
- package/dist/utils.esm.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/utils.cjs.js
CHANGED
|
@@ -156,9 +156,23 @@ const getNodeText = (node) => {
|
|
|
156
156
|
return getNodeText(node.props?.children ?? "").trim();
|
|
157
157
|
return "unknown";
|
|
158
158
|
};
|
|
159
|
+
function useControllableProp({
|
|
160
|
+
prop,
|
|
161
|
+
updater = () => void 0,
|
|
162
|
+
defaultValue
|
|
163
|
+
}) {
|
|
164
|
+
const [internalState, setInternalState] = React.useState(defaultValue);
|
|
165
|
+
React.useEffect(() => {
|
|
166
|
+
if (prop !== void 0) {
|
|
167
|
+
setInternalState(prop);
|
|
168
|
+
}
|
|
169
|
+
}, [prop]);
|
|
170
|
+
return prop === void 0 ? [internalState, setInternalState] : [prop, updater];
|
|
171
|
+
}
|
|
159
172
|
exports.ConditionalWrapper = ConditionalWrapper;
|
|
160
173
|
exports.getNodeText = getNodeText;
|
|
161
174
|
exports.mergeRefs = mergeRefs;
|
|
175
|
+
exports.useControllableProp = useControllableProp;
|
|
162
176
|
exports.useDebounce = useDebounce;
|
|
163
177
|
exports.useForceUpdate = useForceUpdate;
|
|
164
178
|
exports.useOnClickOutside = useOnClickOutside;
|
package/dist/utils.cjs.js.map
CHANGED
|
@@ -1 +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"],"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 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,GA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AA;AACjE,MAAI,OAAO,SAAS;AAElB,WAAO,YAAY,KAAK,OAAO,YAAY,EAAE,EAAE,KAAA;AACjD,SAAO;AACT;;;;;;;;;;;;"}
|
|
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 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 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 { useState, useEffect } 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","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,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;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;ACLO,SAAS,oBAAuB;AAAA,EACrC;AAAA,EACA,UAAU,MAAM;AAAA,EAChB;AACF,GAAsD;AACpD,QAAM,CAAC,eAAe,gBAAgB,IAAIC,MAAAA,SAAY,YAAY;AAClED,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
|
@@ -154,10 +154,24 @@ const getNodeText = (node) => {
|
|
|
154
154
|
return getNodeText(node.props?.children ?? "").trim();
|
|
155
155
|
return "unknown";
|
|
156
156
|
};
|
|
157
|
+
function useControllableProp({
|
|
158
|
+
prop,
|
|
159
|
+
updater = () => void 0,
|
|
160
|
+
defaultValue
|
|
161
|
+
}) {
|
|
162
|
+
const [internalState, setInternalState] = useState(defaultValue);
|
|
163
|
+
useEffect(() => {
|
|
164
|
+
if (prop !== void 0) {
|
|
165
|
+
setInternalState(prop);
|
|
166
|
+
}
|
|
167
|
+
}, [prop]);
|
|
168
|
+
return prop === void 0 ? [internalState, setInternalState] : [prop, updater];
|
|
169
|
+
}
|
|
157
170
|
export {
|
|
158
171
|
ConditionalWrapper,
|
|
159
172
|
getNodeText,
|
|
160
173
|
mergeRefs,
|
|
174
|
+
useControllableProp,
|
|
161
175
|
useDebounce,
|
|
162
176
|
useForceUpdate,
|
|
163
177
|
useOnClickOutside,
|
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","../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 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;A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A,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;"}
|
|
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","../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 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 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 { useState, useEffect } 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,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A;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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@entur/utils",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.2",
|
|
4
4
|
"license": "EUPL-1.2",
|
|
5
5
|
"main": "dist/utils.cjs.js",
|
|
6
6
|
"module": "dist/utils.esm.js",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"vite": "^7.1.3",
|
|
56
56
|
"vite-plugin-dts": "^4.5.4"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "9ac8be0952badd4043470e47e50992369ada2c11"
|
|
59
59
|
}
|