Skip to content

Commit c6a70d3

Browse files
committed
main 🧊 v0.2.15
1 parent 7a244f3 commit c6a70d3

File tree

5 files changed

+80
-20
lines changed

5 files changed

+80
-20
lines changed

‎packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"prompts": "^2.4.2",
4949
"tsconfig-paths": "^4.2.0",
5050
"yargs": "^18.0.0",
51-
"zod": "^3.25.67"
51+
"zod": "^3.25.74"
5252
},
5353
"devDependencies": {
5454
"@types/prompts": "^2.4.9",

‎packages/core/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@siberiacancode/reactuse",
3-
"version": "0.2.14",
3+
"version": "0.2.15",
44
"description": "The ultimate collection of react hooks",
55
"author": {
66
"name": "SIBERIA CAN CODE 🧊",
@@ -51,9 +51,9 @@
5151
"lint-staged": "lint-staged"
5252
},
5353
"peerDependencies": {
54-
"@types/react": "^17 || ^18 || ^19",
55-
"react": "^17 || ^18 || ^19",
56-
"react-dom": "^17 || ^18 || ^19"
54+
"@types/react": "^18 || ^19",
55+
"react": "^18 || ^19",
56+
"react-dom": "^18 || ^19"
5757
},
5858
"peerDependenciesMeta": {
5959
"@types/react": {

‎packages/core/src/bundle/hooks/useIntersectionObserver/useIntersectionObserver.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { useRefState } from '../useRefState/useRefState';
1212
* @param {HookTarget} target The target element to detect intersection
1313
* @param {boolean} [options.enabled=true] The IntersectionObserver options
1414
* @param {((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined} [options.onChange] The callback to execute when intersection is detected
15-
* @param {HookTarget} [options.root] The root element to observe
15+
* @param {HookTarget} [options.root=document] The root element to observe
1616
* @returns {UseIntersectionObserverReturn} An object containing the state
1717
*
1818
* @example
@@ -22,20 +22,43 @@ import { useRefState } from '../useRefState/useRefState';
2222
* @template Target The target element
2323
* @param {boolean} [options.enabled=true] The IntersectionObserver options
2424
* @param {((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined} [options.onChange] The callback to execute when intersection is detected
25-
* @param {HookTarget} [options.root] The root element to observe
25+
* @param {HookTarget} [options.root=document] The root element to observe
2626
* @returns {UseIntersectionObserverReturn & { ref: StateRef<Target> }} A React ref to attach to the target element
2727
*
2828
* @example
2929
* const { entry, inView } = useIntersectionObserver(ref);
30+
*
31+
* @overload
32+
* @template Target The target element
33+
* @param {(entry: IntersectionObserverEntry) => void} callback The callback to execute when intersection is detected
34+
* @returns {UseIntersectionObserverReturn & { ref: StateRef<Target> }} A React ref to attach to the target element
35+
*
36+
* @example
37+
* const { ref, entry, inView } = useIntersectionObserver(() => console.log('callback'));
38+
*
39+
* @overload
40+
* @param {(entry: IntersectionObserverEntry) => void} callback The callback to execute when intersection is detected
41+
* @param {HookTarget} target The target element to detect intersection
42+
* @returns {UseIntersectionObserverReturn} An object containing the state
43+
*
44+
* @example
45+
* const { entry, inView } = useIntersectionObserver(() => console.log('callback'), ref);
3046
*/
3147
export const useIntersectionObserver = (...params) => {
3248
const target = isTarget(params[0]) ? params[0] : undefined;
33-
const options = target ? params[1] : params[0];
49+
const options = target
50+
? typeof params[1] === 'object'
51+
? params[1]
52+
: { onChange: params[1] }
53+
: typeof params[0] === 'object'
54+
? params[0]
55+
: { onChange: params[0] };
56+
const callback = options?.onChange;
3457
const enabled = options?.enabled ?? true;
3558
const [entry, setEntry] = useState();
3659
const internalRef = useRefState();
37-
const internalOnChangeRef = useRef(options?.onChange);
38-
internalOnChangeRef.current = options?.onChange;
60+
const internalOnChangeRef = useRef(callback);
61+
internalOnChangeRef.current = callback;
3962
useEffect(() => {
4063
if (!enabled || (!target && !internalRef.state)) return;
4164
const element = target ? getElement(target) : internalRef.current;

‎packages/core/src/hooks/useIntersectionObserver/useIntersectionObserver.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ export interface UseIntersectionObserver {
2828
): UseIntersectionObserverReturn & { ref: StateRef<Target> };
2929

3030
(target: HookTarget, options?: UseIntersectionObserverOptions): UseIntersectionObserverReturn;
31+
32+
<Target extends Element>(
33+
callback: (entry: IntersectionObserverEntry) => void,
34+
target?: never
35+
): UseIntersectionObserverReturn & { ref: StateRef<Target> };
36+
37+
(
38+
callback: (entry: IntersectionObserverEntry) => void,
39+
target: HookTarget
40+
): UseIntersectionObserverReturn;
3141
}
3242

3343
/**
@@ -41,7 +51,7 @@ export interface UseIntersectionObserver {
4151
* @param {HookTarget} target The target element to detect intersection
4252
* @param {boolean} [options.enabled=true] The IntersectionObserver options
4353
* @param {((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined} [options.onChange] The callback to execute when intersection is detected
44-
* @param {HookTarget} [options.root] The root element to observe
54+
* @param {HookTarget} [options.root=document] The root element to observe
4555
* @returns {UseIntersectionObserverReturn} An object containing the state
4656
*
4757
* @example
@@ -51,22 +61,49 @@ export interface UseIntersectionObserver {
5161
* @template Target The target element
5262
* @param {boolean} [options.enabled=true] The IntersectionObserver options
5363
* @param {((entries: IntersectionObserverEntry[], observer: IntersectionObserver) => void) | undefined} [options.onChange] The callback to execute when intersection is detected
54-
* @param {HookTarget} [options.root] The root element to observe
64+
* @param {HookTarget} [options.root=document] The root element to observe
5565
* @returns {UseIntersectionObserverReturn & { ref: StateRef<Target> }} A React ref to attach to the target element
5666
*
5767
* @example
5868
* const { entry, inView } = useIntersectionObserver(ref);
69+
*
70+
* @overload
71+
* @template Target The target element
72+
* @param {(entry: IntersectionObserverEntry) => void} callback The callback to execute when intersection is detected
73+
* @returns {UseIntersectionObserverReturn & { ref: StateRef<Target> }} A React ref to attach to the target element
74+
*
75+
* @example
76+
* const { ref, entry, inView } = useIntersectionObserver(() => console.log('callback'));
77+
*
78+
* @overload
79+
* @param {(entry: IntersectionObserverEntry) => void} callback The callback to execute when intersection is detected
80+
* @param {HookTarget} target The target element to detect intersection
81+
* @returns {UseIntersectionObserverReturn} An object containing the state
82+
*
83+
* @example
84+
* const { entry, inView } = useIntersectionObserver(() => console.log('callback'), ref);
5985
*/
6086
export const useIntersectionObserver = ((...params: any[]) => {
6187
const target = (isTarget(params[0]) ? params[0] : undefined) as HookTarget | undefined;
62-
const options = (target ? params[1] : params[0]) as UseIntersectionObserverOptions | undefined;
88+
89+
const options = (
90+
target
91+
? typeof params[1] === 'object'
92+
? params[1]
93+
: { onChange: params[1] }
94+
: typeof params[0] === 'object'
95+
? params[0]
96+
: { onChange: params[0] }
97+
) as UseIntersectionObserverOptions | undefined;
98+
99+
const callback = options?.onChange;
63100
const enabled = options?.enabled ?? true;
64101

65102
const [entry, setEntry] = useState<IntersectionObserverEntry>();
66103

67104
const internalRef = useRefState<Element>();
68-
const internalOnChangeRef = useRef<UseIntersectionObserverOptions['onChange']>(options?.onChange);
69-
internalOnChangeRef.current = options?.onChange;
105+
const internalOnChangeRef = useRef<UseIntersectionObserverOptions['onChange']>(callback);
106+
internalOnChangeRef.current = callback;
70107

71108
useEffect(() => {
72109
if (!enabled || (!target && !internalRef.state)) return;

‎pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)