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

Conversation

foresec
Copy link
Collaborator

@foresec foresec commented Jul 12, 2024

๐Ÿ‘พ Pull Request

  • ์ž‘์—…๋ช…: use-intersection-observer
  • ์ƒํƒœ: ์‹ ๊ทœ

1๏ธโƒฃ Spec

  • IntersectionObserver API๋ฅผ ์ด์šฉํ•˜์—ฌ ์š”์†Œ์˜ ๊ฐ€์‹œ์„ฑ์„ ๊ฐ์ง€ํ•˜๋Š” ์ปค์Šคํ…€ ํ›…
  • ๊ธฐ๋ณธ์ ์ธ Intersection Observer API์˜ ์˜ต์…˜๊ฐ’(root, rootMargin, threshold)๊ณผ ๋ถ€๊ฐ€์ ์ธ ์˜ต์…˜๊ฐ’ ๋ฐ ์‹คํ–‰ํ•  ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ๋ฐ›์Œ
  • ๊ฐ์ง€ํ•  ์š”์†Œ์˜ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•˜๋Š” ํ•จ์ˆ˜, ํ˜„์žฌ ๊ฐ์ง€ ์—ฌ๋ถ€, ๊ต์ฐจ์ƒํƒœ์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๊ด€๋ จ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜

2๏ธโƒฃ ๋ณ€๊ฒฝ ์‚ฌํ•ญ

  • ๋ณ€๊ฒฝ ์‚ฌํ•ญ ์—†์Œ

3๏ธโƒฃ ์˜ˆ์‹œ ์ฝ”๋“œ

1. ๊ธฐ๋ณธ

  const { intersectionRef, isView, entry } = useIntersectionObserver();
 <div ref={intersectionRef}></div>
  • intersectionRef : ํ›…์—์„œ ์‚ฌ์šฉํ•  ์š”์†Œ์˜ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋””์ŠคํŒจ์น˜ ํ•จ์ˆ˜. ๊ฐ์ง€ํ•˜๊ณ ์ž ํ•˜๋Š” ์š”์†Œ์— ์„ค์ •
  • isView : ์š”์†Œ๊ฐ€ ํ˜„์žฌ ๋ทฐํฌํŠธ ๋‚ด์— ๋ณด์ด๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ƒํƒœ ๊ฐ’
  • entry : Intersection Observer API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ์˜ ๊ต์ฐจ ์ƒํƒœ์™€ ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด

2. IntersectionObserver API ๊ธฐ๋ณธ ์˜ต์…˜

  const { intersectionRef, isView } = useIntersectionObserver({
    root: document.querySelector('#container') 
    rootMargin: '10px 20px 30px 40px' 
    threshold: 0.5,
  });
  • root : ๋ทฐํฌํŠธ ๋Œ€์‹  ์‚ฌ์šฉํ•  ์š”์†Œ ๊ฐ์ฒด ์ง€์ •. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ธŒ๋ผ์šฐ์ €์˜ Viewport๊ฐ€ ์‚ฌ์šฉ๋จ(null)
  • rootMargin : Root์˜ ๋ฒ”์œ„๋ฅผ ํ™•์žฅํ•˜๊ฑฐ๋‚˜ ์ถ•์†Œ์‹œํ‚ด
  • threshold : observer๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์œ„ํ•œ ์ตœ์†Œํ•œ์˜ ํƒ€์ผ“์˜ ๊ฐ€์‹œ์„ฑ ๋น„์œจ

3. ๋ถ€๊ฐ€์˜ต์…˜

  const { intersectionRef, isView } = useIntersectionObserver({
    initialView: true,
    visibleOnce: true,
  });
  • initialView : ์ดˆ๊ธฐ ๊ฐ์ง€๊ฐ’
  • visibleOnce : ์ฒ˜์Œ ํ•œ๋ฒˆ๋งŒ ๊ฐ์ง€

4. callback ํ•จ์ˆ˜

  const { intersectionRef, isView, entry } = useIntersectionObserver({
    onChange: (isView) => {
      if (isView) {
        console.log('visible');
      } else {
        console.log('x');
      }
    },
    onEnter() {
      console.log('onEnter');
    },
    onLeave() {
      console.log('onLeave');
    },
  });
  • onChange : ํƒ€๊ฒŸ ์—˜๋ฆฌ๋จผํŠธ์˜ ๊ฐ€์‹œ์„ฑ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ํ˜ธ์ถœํ•  ์ฝœ๋ฐฑ ํ•จ์ˆ˜
  • onEnter : ํƒ€๊ฒŸ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚  ๋•Œ ํ˜ธ์ถœํ•  ์ฝœ๋ฐฑ ํ•จ์ˆ˜
  • onLeave : ํƒ€๊ฒŸ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ํ™”๋ฉด์—์„œ ์‚ฌ๋ผ์งˆ ๋•Œ ํ˜ธ์ถœํ•  ์ฝœ๋ฐฑ ํ•จ์ˆ˜

4๏ธโƒฃ ๊ด€๋ จ ๋ฌธ์„œ (์„ ํƒ ์‚ฌํ•ญ)

@foresec foresec changed the title Feature/use intersection observer Feature/use-intersection-observer Jul 12, 2024
@foresec foresec self-assigned this Jul 12, 2024
@foresec foresec closed this Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant