mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-15 16:51:37 +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 { ref, inView } = useInView();
|
||||
const sandUrl = new URL(url);
|
||||
const embed = sandUrl.pathname.split('/')[2];
|
||||
const embed = new URL(url).pathname.split('/')[2];
|
||||
const [src, setSrc] = useState('');
|
||||
useEffect(() => {
|
||||
inView &&
|
||||
|
@ -1,23 +1,26 @@
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
|
||||
const useInView = () => {
|
||||
const ref = useRef(null);
|
||||
const [inView, setInView] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const observer = new IntersectionObserver((entries, observer) => {
|
||||
const observeCallback: IntersectionObserverCallback = useCallback(
|
||||
(entries, observer) => {
|
||||
entries.forEach((entry) => {
|
||||
if (entry.isIntersecting) {
|
||||
if (ref.current) {
|
||||
setInView(true);
|
||||
observer.unobserve(ref.current);
|
||||
}
|
||||
}
|
||||
if (!entry.isIntersecting) return;
|
||||
if (!ref.current) return;
|
||||
setInView(true);
|
||||
observer.unobserve(ref.current);
|
||||
});
|
||||
});
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const observer = new IntersectionObserver(observeCallback);
|
||||
|
||||
ref.current && observer.observe(ref.current);
|
||||
}, []);
|
||||
}, [observeCallback]);
|
||||
|
||||
return { ref, inView };
|
||||
};
|
||||
|
Reference in New Issue
Block a user