@chance/react-utils
Advanced tools
+3
-3
| { | ||
| "name": "@chance/react-utils", | ||
| "version": "0.0.7", | ||
| "version": "0.0.8", | ||
| "main": "./dist/react-utils.js", | ||
@@ -21,4 +21,4 @@ "module": "./dist/react-utils.mjs", | ||
| "dependencies": { | ||
| "@chance/dom": "workspace:*", | ||
| "@chance/utils": "workspace:*", | ||
| "@chance/dom": "^0.0.8", | ||
| "@chance/utils": "^0.0.8", | ||
| "tiny-warning": "^1.0.3" | ||
@@ -25,0 +25,0 @@ }, |
@@ -12,9 +12,9 @@ import { cloneElement, isValidElement } from "react"; | ||
| export function cloneValidElement<Props>( | ||
| element: React.ReactElement<Props> | React.ReactNode, | ||
| props?: Partial<Props> & React.Attributes, | ||
| ...children: React.ReactNode[] | ||
| element: React.ReactElement<Props> | React.ReactNode, | ||
| props?: Partial<Props> & React.Attributes, | ||
| ...children: React.ReactNode[] | ||
| ): React.ReactElement<Props> | React.ReactNode { | ||
| return isValidElement(element) | ||
| ? cloneElement(element, props, ...children) | ||
| : element; | ||
| return isValidElement(element) | ||
| ? cloneElement(element, props, ...children) | ||
| : element; | ||
| } |
@@ -8,7 +8,7 @@ import { useRef } from "react"; | ||
| export function useConstant<ValueType>(fn: () => ValueType): ValueType { | ||
| const ref = useRef<{ v: ValueType }>(); | ||
| if (!ref.current) { | ||
| ref.current = { v: fn() }; | ||
| } | ||
| return ref.current.v; | ||
| const ref = useRef<{ v: ValueType }>(); | ||
| if (!ref.current) { | ||
| ref.current = { v: fn() }; | ||
| } | ||
| return ref.current.v; | ||
| } |
+25
-25
@@ -11,32 +11,32 @@ import { useRef, useEffect } from "react"; | ||
| export function useFocusChange( | ||
| handleChange: ( | ||
| activeElement: Element | null, | ||
| previousActiveElement: Element | null, | ||
| event?: FocusEvent | ||
| ) => void = console.log, | ||
| when: "focus" | "blur" = "focus", | ||
| ownerDocument: Document = document | ||
| handleChange: ( | ||
| activeElement: Element | null, | ||
| previousActiveElement: Element | null, | ||
| event?: FocusEvent | ||
| ) => void = console.log, | ||
| when: "focus" | "blur" = "focus", | ||
| ownerDocument: Document = document | ||
| ) { | ||
| let lastActiveElement = useRef(ownerDocument.activeElement); | ||
| let lastActiveElement = useRef(ownerDocument.activeElement); | ||
| useEffect(() => { | ||
| lastActiveElement.current = ownerDocument.activeElement; | ||
| useEffect(() => { | ||
| lastActiveElement.current = ownerDocument.activeElement; | ||
| function onChange(event: FocusEvent) { | ||
| if (lastActiveElement.current !== ownerDocument.activeElement) { | ||
| handleChange( | ||
| ownerDocument.activeElement, | ||
| lastActiveElement.current, | ||
| event | ||
| ); | ||
| lastActiveElement.current = ownerDocument.activeElement; | ||
| } | ||
| } | ||
| function onChange(event: FocusEvent) { | ||
| if (lastActiveElement.current !== ownerDocument.activeElement) { | ||
| handleChange( | ||
| ownerDocument.activeElement, | ||
| lastActiveElement.current, | ||
| event | ||
| ); | ||
| lastActiveElement.current = ownerDocument.activeElement; | ||
| } | ||
| } | ||
| ownerDocument.addEventListener(when, onChange, true); | ||
| ownerDocument.addEventListener(when, onChange, true); | ||
| return () => { | ||
| ownerDocument.removeEventListener(when, onChange); | ||
| }; | ||
| }, [when, handleChange, ownerDocument]); | ||
| return () => { | ||
| ownerDocument.removeEventListener(when, onChange); | ||
| }; | ||
| }, [when, handleChange, ownerDocument]); | ||
| } |
@@ -7,6 +7,6 @@ import { useState, useCallback } from "react"; | ||
| export function useForceUpdate() { | ||
| let [, dispatch] = useState<{}>(Object.create(null)); | ||
| return useCallback(() => { | ||
| dispatch(Object.create(null)); | ||
| }, []); | ||
| let [, dispatch] = useState<{}>(Object.create(null)); | ||
| return useCallback(() => { | ||
| dispatch(Object.create(null)); | ||
| }, []); | ||
| } |
@@ -5,11 +5,11 @@ import { useRef } from "react"; | ||
| export function useLazyRef<F extends (...args: any[]) => any>( | ||
| fn: F | ||
| fn: F | ||
| ): React.MutableRefObject<ReturnType<F>> { | ||
| let isSet = useRef(false); | ||
| let ref = useRef<any>(); | ||
| if (!isSet.current) { | ||
| isSet.current = true; | ||
| ref.current = fn(); | ||
| } | ||
| return ref; | ||
| let isSet = useRef(false); | ||
| let ref = useRef<any>(); | ||
| if (!isSet.current) { | ||
| isSet.current = true; | ||
| ref.current = fn(); | ||
| } | ||
| return ref; | ||
| } |
@@ -9,7 +9,7 @@ import { useRef, useEffect } from "react"; | ||
| export function usePrevious<ValueType = any>(value: ValueType) { | ||
| const ref = useRef<ValueType | null>(null); | ||
| useEffect(() => { | ||
| ref.current = value; | ||
| }, [value]); | ||
| return ref.current; | ||
| const ref = useRef<ValueType | null>(null); | ||
| useEffect(() => { | ||
| ref.current = value; | ||
| }, [value]); | ||
| return ref.current; | ||
| } |
@@ -12,20 +12,20 @@ /* eslint-disable react-hooks/rules-of-hooks */ | ||
| function createStableCallbackHook<T extends (...args: any[]) => any>( | ||
| useEffectHook: ( | ||
| effect: React.EffectCallback, | ||
| deps?: React.DependencyList | undefined | ||
| ) => void, | ||
| callback: T | null | undefined | ||
| useEffectHook: ( | ||
| effect: React.EffectCallback, | ||
| deps?: React.DependencyList | undefined | ||
| ) => void, | ||
| callback: T | null | undefined | ||
| ): T { | ||
| let callbackRef = useRef(callback); | ||
| useEffectHook(() => { | ||
| callbackRef.current = callback; | ||
| }); | ||
| let callbackRef = useRef(callback); | ||
| useEffectHook(() => { | ||
| callbackRef.current = callback; | ||
| }); | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| return useCallback( | ||
| ((...args) => { | ||
| callbackRef.current && callbackRef.current(...args); | ||
| }) as T, | ||
| [] | ||
| ); | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| return useCallback( | ||
| ((...args) => { | ||
| callbackRef.current && callbackRef.current(...args); | ||
| }) as T, | ||
| [] | ||
| ); | ||
| } | ||
@@ -39,5 +39,5 @@ | ||
| export function useStableCallback<T extends (...args: any[]) => any>( | ||
| callback: T | null | undefined | ||
| callback: T | null | undefined | ||
| ): T { | ||
| return createStableCallbackHook(useEffect, callback); | ||
| return createStableCallbackHook(useEffect, callback); | ||
| } | ||
@@ -55,5 +55,5 @@ | ||
| export function useStableLayoutCallback<T extends (...args: any[]) => any>( | ||
| callback: T | null | undefined | ||
| callback: T | null | undefined | ||
| ): T { | ||
| return createStableCallbackHook(useLayoutEffect, callback); | ||
| return createStableCallbackHook(useLayoutEffect, callback); | ||
| } |
+11
-11
@@ -11,14 +11,14 @@ import { useRef, useEffect } from "react"; | ||
| export function useUpdateEffect( | ||
| effect: React.EffectCallback, | ||
| deps?: React.DependencyList | ||
| effect: React.EffectCallback, | ||
| deps?: React.DependencyList | ||
| ) { | ||
| const mounted = useRef(false); | ||
| useEffect(() => { | ||
| if (mounted.current) { | ||
| effect(); | ||
| } else { | ||
| mounted.current = true; | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, deps); | ||
| const mounted = useRef(false); | ||
| useEffect(() => { | ||
| if (mounted.current) { | ||
| effect(); | ||
| } else { | ||
| mounted.current = true; | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, deps); | ||
| } |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
22129
-0.69%+ Added
+ Added
Updated
Updated