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

[useScrollTrigger] Do nothing if target is null #45441

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

vipierozan99
Copy link

@vipierozan99 vipierozan99 commented Feb 28, 2025

Currently if options.target is undefined it tried to default to the window.

In my case I have another component that may or may not be mounted at a given time, so the ref to it might be null.

Right now I do:

const isScrolled = useScrollTrigger({
  target: containerRef.current ?? undefined,
  threshold: 1,
});

but that will default to the window, and if its not present it will throw an error. Which is not what I want exactly.

This change just disables the hook if null is explicitly passed.

@vipierozan99 vipierozan99 force-pushed the use-scroll-trigger-nullable branch 2 times, most recently from 35cab5a to de8d14d Compare February 28, 2025 13:14
@mui-bot
Copy link

mui-bot commented Mar 3, 2025

Netlify deploy preview

https://deploy-preview-45441--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 2c7418b

Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

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

@vipierozan99 Can you fix the failing CI? And in what case is window not present?

@vipierozan99 vipierozan99 force-pushed the use-scroll-trigger-nullable branch from 2c7418b to c3e61da Compare March 3, 2025 16:48
@vipierozan99 vipierozan99 force-pushed the use-scroll-trigger-nullable branch from c3e61da to 680496f Compare March 3, 2025 16:49
@vipierozan99
Copy link
Author

vipierozan99 commented Mar 3, 2025

@vipierozan99 Can you fix the failing CI? And in what case is window not present?

whoops sorry, first time contributing here 😅, this should fix it. Thanks for the quick response!

w.r.t. the window not being present this line seems to imply its possible, maybe in some testing environments?

const defaultTarget = typeof window !== 'undefined' ? window : null;

My use case is different tho, I want the target to be an element that might not be mounted yet so I want the hook to do nothing in the meantime, does that make sense?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants