Skip to content
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

feat: enhance IsInViewport and useIntersectionObserver #202

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

max-got
Copy link

@max-got max-got commented Jan 5, 2025

…with observer control methods and once option.

  • Added methods to IsInViewport class: stop(), pause(), resume(), and isActive to manage the intersection observer.
  • Updated useIntersectionObserver to include an optional 'once' parameter that stops observing after the first intersection.

As i way using IsInViewport and useIntersectionObserver i often found the need to unobserve on intersecting. Especially for classic fade in animations and so on. Furthermore i was confused that i cant handle the IsInViewport utility correctly. Therefore i added these two enhancements.

Just lmk if that is not desired or if anything is not correctly implemented. I have opted for once naming scheme. Alternatives would be: triggerOnce, unobserveOnEnter, ...

Example:

<script lang="ts">
	import { IsInViewport } from "runed";
	let targetNode = $state<HTMLElement>()!;
	const inViewport = new IsInViewport(() => targetNode, {
		once: true
	});
	
	inViewPort.stop();
	inViewPort.isActive;
	//...
</script>
<div bind:this={targetNode} class="transition" class="transition-opacity {hasBeenSeen ? 'opacity-100' : 'opacity-0'}">
	I'll fade in once when first visible, then stop observing
</div>

…control methods

- Added methods to IsInViewport class: stop(), pause(), resume(), and isActive to manage the intersection observer.
- Updated useIntersectionObserver to include an optional 'once' parameter that stops observing after the first intersection.
Copy link

changeset-bot bot commented Jan 5, 2025

⚠️ No Changeset found

Latest commit: 36db215

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

- Updated the callback function to handle intersection entries more efficiently.
- Ensured that the callback is invoked with individual entries
/**
* Stop observing the element.
*/
stop() {
Copy link
Member

Choose a reason for hiding this comment

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

We'll want to bind these methods to the constructor so they can be used like so:

<button onclick={observer.stop}></button>

Similar to the others we have this way!

Copy link
Contributor

github-actions bot commented Jan 20, 2025

built with Refined Cloudflare Pages Action

⚡ Cloudflare Pages Deployment

Name Status Preview Last Commit
runed ✅ Ready (View Log) Visit Preview 36db215

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.

2 participants