Skip to content

Feature/use-intersection-observer #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
โœจ feat: ๋งค๊ฐœ๋ณ€์ˆ˜ visibleOnce, initialView ์ถ”๊ฐ€
  • Loading branch information
foresec committed Jul 12, 2024
commit 8bc585096eb1aaaa02b2c09c82c47eb74c505753
10 changes: 4 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import useIntersectionObserver from './hooks/useIntersectionObserver';

function App() {
const [count, setCount] = useState(0);

const { setRef, isView, entry } = useIntersectionObserver({
// root: document.getElementById('root'),
// rootMargin: '20px',
// threshold:
visibleOnce: true,
});
console.log('ENTRY : ', isView);
console.log('ENTRY : ', entry);
console.log(isView, entry?.target);

return (
<>
<div>
<div ref={setRef}>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
Expand Down
23 changes: 14 additions & 9 deletions src/hooks/useIntersectionObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ interface IntersectionObserverOptions {
root?: Element | null;
rootMargin?: string;
threshold?: number | number[];
visibleOnce?: boolean;
initialView?: boolean;
}

interface IntersectionObserverResult {
Expand All @@ -12,27 +14,30 @@ interface IntersectionObserverResult {
entry?: IntersectionObserverEntry;
}

function useIntersectionObserver({
const useIntersectionObserver = ({
root,
rootMargin,
threshold,
}: IntersectionObserverOptions = {}): IntersectionObserverResult {
visibleOnce = false,
initialView = false,
}: IntersectionObserverOptions = {}): IntersectionObserverResult => {
const [ref, setRef] = useState<Element | null>(null);
const [entryState, setEntryState] = useState<{
isView: boolean;
entry?: IntersectionObserverEntry | null;
}>({ isView: false, entry: null });
}>({ isView: initialView, entry: null });

useEffect(() => {
if (!ref) return;

const handleIntersection: IntersectionObserverCallback = (entries) => {
entries.forEach((entry) => {
setEntryState((prev) => ({
...prev,
isView: entry.isIntersecting,
entry: entry,
}));
const isIntersecting = entry.isIntersecting;
setEntryState({ isView: isIntersecting, entry: entry });

if (visibleOnce && isIntersecting) {
observer.unobserve(ref);
}
});
};

Expand All @@ -54,6 +59,6 @@ function useIntersectionObserver({
isView: entryState.isView,
entry: entryState.entry,
} as IntersectionObserverResult;
}
};

export default useIntersectionObserver;