Skip to content

feat: implement the useOnInView hook - #738

Merged
thebuilder merged 9 commits into
mainfrom
feat/use-on-inview
Oct 28, 2025
Merged

feat: implement the useOnInView hook#738
thebuilder merged 9 commits into
mainfrom
feat/use-on-inview

Conversation

@thebuilder

@thebuilderthebuilder commented Oct 27, 2025

Copy link
Copy Markdown
Owner

Based on the great work in #718 by @jantimon - This is an attempt to implement just the useOnInView hook, while maintaining the fallback functionality, and legacy React version support.

✨ New

  • useOnInView hook — a no-re-render alternative to useInView that delivers (inView, entry) to your callback while returning a ref you can attach to any element. Designed for tracking, analytics, and other side effect heavy workloads where state updates are unnecessary.
  • IntersectionChangeEffect / IntersectionEffectOptions types — exported helper types that describe the new hook’s callback and options surface.
  • Storybook playground + documentation — new story, README section, and JSDoc example demonstrating how to use useOnInView.

✨ Improvements

  • useInView, useOnInView, and <InView> now ignore the browser’s initial inView === false emission so handlers only fire once a real visibility change occurs, while still reporting all subsequent enter/leave transitions (including threshold arrays).
  • Observer cleanup logic across the hooks/components was tightened to ensure skip toggles and fallback scenarios re-attach correctly without losing previous state.

🧪 Testing

  • Added a dedicated Vitest suite for useOnInView, covering thresholds, triggerOnce, skip toggling, merged refs, and multiple observers on the same node.

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz CodeflowRun & review this pull request in StackBlitz Codeflow.

@vercel

vercelBot commented Oct 27, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

ProjectDeploymentPreviewCommentsUpdated (UTC)
react-intersection-observerReadyReadyPreviewCommentOct 28, 2025 2:29pm

@pkg-pr-new

pkg-pr-newBot commented Oct 27, 2025

Copy link
Copy Markdown
npm i https://pkg.pr.new/thebuilder/react-intersection-observer@738

commit: 8cd8e42

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements the useOnInView hook, a new alternative to useInView that provides a callback-based API for monitoring element visibility without triggering re-renders. This is useful for performance-critical use cases like tracking and logging.

Key changes:

  • Added useOnInView hook with callback-based API and optional cleanup function
  • Introduced trigger option to specify whether callbacks fire on enter or leave
  • Comprehensive test coverage for various scenarios including threshold tracking, cleanup behavior, and observer destruction

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
FileDescription
src/useOnInView.tsxCore implementation of the new hook with ref callback pattern and observer lifecycle management
src/index.tsxExported new hook and added related TypeScript types for the API
src/__tests__/useOnInView.test.tsxComplete test suite covering callback execution, cleanup, thresholds, and edge cases
storybook/stories/useOnInView.story.tsxInteractive Storybook examples demonstrating the hook's features
README.mdDocumentation for the new hook with usage examples and options table

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment threadsrc/__tests__/useOnInView.test.tsx Outdated
@thebuilder
thebuilder merged commit efe59c8 into mainOct 28, 2025
15 checks passed
@thebuilder
thebuilder deleted the feat/use-on-inview branch October 28, 2025 14:49
Sign up for freeto 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.

2 participants

@thebuilder