@mediamonks/react-kit 1.0.1 → 1.0.3
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.
|
@@ -2,7 +2,9 @@ import gsap from 'gsap';
|
|
|
2
2
|
import Flip from 'gsap/Flip';
|
|
3
3
|
import { useEffect, useRef } from 'react';
|
|
4
4
|
import { unref } from '../../../utils/unref/unref.js';
|
|
5
|
-
|
|
5
|
+
if (typeof window !== 'undefined') {
|
|
6
|
+
gsap.registerPlugin(Flip);
|
|
7
|
+
}
|
|
6
8
|
export function useFlip(ref, flipStateVariables = {}) {
|
|
7
9
|
const flipStateRef = useRef();
|
|
8
10
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useRef } from 'react';
|
|
1
|
+
import { useRef, useCallback } from 'react';
|
|
2
2
|
import { useMount } from '../../lifecycle/hooks/useMount/useMount.js';
|
|
3
3
|
import { unref } from '../../utils/unref/unref.js';
|
|
4
4
|
import { useResizeObserver } from '../useResizeObserver/useResizeObserver.js';
|
|
@@ -8,9 +8,10 @@ import { useResizeObserver } from '../useResizeObserver/useResizeObserver.js';
|
|
|
8
8
|
*/
|
|
9
9
|
export function useContentRect(target) {
|
|
10
10
|
const contentRectRef = useRef(null);
|
|
11
|
-
|
|
11
|
+
const onResize = useCallback((entries) => {
|
|
12
12
|
contentRectRef.current = entries.at(0)?.contentRect ?? null;
|
|
13
|
-
});
|
|
13
|
+
}, []);
|
|
14
|
+
useResizeObserver(target, onResize);
|
|
14
15
|
useMount(() => {
|
|
15
16
|
contentRectRef.current = unref(target)?.getBoundingClientRect() ?? null;
|
|
16
17
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useRef, useState } from 'react';
|
|
1
|
+
import { useCallback, useRef, useState } from 'react';
|
|
2
2
|
import { useMount } from '../../index.js';
|
|
3
3
|
import { unref } from '../../utils/unref/unref.js';
|
|
4
4
|
import { useResizeObserver } from '../useResizeObserver/useResizeObserver.js';
|
|
@@ -9,12 +9,13 @@ import { useResizeObserver } from '../useResizeObserver/useResizeObserver.js';
|
|
|
9
9
|
export function useContentRectState(target) {
|
|
10
10
|
const [contentRect, setContentRect] = useState(null);
|
|
11
11
|
const rafRef = useRef(0);
|
|
12
|
-
|
|
12
|
+
const onResize = useCallback((entries) => {
|
|
13
13
|
cancelAnimationFrame(rafRef.current);
|
|
14
14
|
rafRef.current = requestAnimationFrame(() => {
|
|
15
15
|
setContentRect(entries.at(0)?.contentRect ?? null);
|
|
16
16
|
});
|
|
17
|
-
});
|
|
17
|
+
}, []);
|
|
18
|
+
useResizeObserver(target, onResize);
|
|
18
19
|
useMount(() => {
|
|
19
20
|
setContentRect(unref(target)?.getBoundingClientRect() ?? null);
|
|
20
21
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
2
|
import { unref } from '../../utils/unref/unref.js';
|
|
3
|
+
import { useRefValue } from '../useRefValue/useRefValue.js';
|
|
3
4
|
/**
|
|
4
5
|
* This hook allows you to add a ResizeObserver for an element and remove it
|
|
5
6
|
* when the component unmounts.
|
|
@@ -9,16 +10,19 @@ import { unref } from '../../utils/unref/unref.js';
|
|
|
9
10
|
* @param options - The ResizeObserverOptions for the observed element
|
|
10
11
|
*/
|
|
11
12
|
export function useResizeObserver(target, callback, options) {
|
|
13
|
+
const callbackRef = useRefValue(callback);
|
|
12
14
|
useEffect(() => {
|
|
13
15
|
const element = unref(target);
|
|
14
16
|
if (element === null || callback === undefined) {
|
|
15
17
|
return;
|
|
16
18
|
}
|
|
17
|
-
const resizeObserver = new ResizeObserver(
|
|
19
|
+
const resizeObserver = new ResizeObserver((..._arguments) => {
|
|
20
|
+
callbackRef.current?.(..._arguments);
|
|
21
|
+
});
|
|
18
22
|
resizeObserver.observe(element, options);
|
|
19
23
|
return () => {
|
|
20
24
|
resizeObserver.disconnect();
|
|
21
25
|
};
|
|
22
26
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
23
|
-
}, [target,
|
|
27
|
+
}, [target, ...Object.values(options ?? {})]);
|
|
24
28
|
}
|