mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-16 01:01:38 +00:00
update useInView hook
This commit is contained in:
@ -21,8 +21,7 @@ const RUACodeSandbox = ({ url }: Props) => {
|
|||||||
const currentTheme = theme === 'system' ? systemTheme : theme ?? 'light';
|
const currentTheme = theme === 'system' ? systemTheme : theme ?? 'light';
|
||||||
|
|
||||||
const { ref, inView } = useInView();
|
const { ref, inView } = useInView();
|
||||||
const sandUrl = new URL(url);
|
const embed = new URL(url).pathname.split('/')[2];
|
||||||
const embed = sandUrl.pathname.split('/')[2];
|
|
||||||
const [src, setSrc] = useState('');
|
const [src, setSrc] = useState('');
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
inView &&
|
inView &&
|
||||||
|
@ -1,23 +1,26 @@
|
|||||||
import { useEffect, useRef, useState } from 'react';
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
const useInView = () => {
|
const useInView = () => {
|
||||||
const ref = useRef(null);
|
const ref = useRef(null);
|
||||||
const [inView, setInView] = useState(false);
|
const [inView, setInView] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
const observeCallback: IntersectionObserverCallback = useCallback(
|
||||||
const observer = new IntersectionObserver((entries, observer) => {
|
(entries, observer) => {
|
||||||
entries.forEach((entry) => {
|
entries.forEach((entry) => {
|
||||||
if (entry.isIntersecting) {
|
if (!entry.isIntersecting) return;
|
||||||
if (ref.current) {
|
if (!ref.current) return;
|
||||||
setInView(true);
|
setInView(true);
|
||||||
observer.unobserve(ref.current);
|
observer.unobserve(ref.current);
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
},
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const observer = new IntersectionObserver(observeCallback);
|
||||||
|
|
||||||
ref.current && observer.observe(ref.current);
|
ref.current && observer.observe(ref.current);
|
||||||
}, []);
|
}, [observeCallback]);
|
||||||
|
|
||||||
return { ref, inView };
|
return { ref, inView };
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user