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

[material-ui][useScrollTrigger] Do nothing if target is null #45441

Open
wants to merge 7 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 from 87cb731 to 35cab5a Compare February 28, 2025 13:13
@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 9cb31e2

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?

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Mar 5, 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?

Can you only call the hook when the target element exists? Instead of modifying useScrollTrigger, you could prevent calling the hook when target is null.

Edit: I know hook shouldn't be called conditionally, but maybe you can avoid rendering that component where the hook is called. Basically in your parent component:

if (!containerRef.current) return <ComponentWithoutHookInside />;

else return <ComponentWithHookInside  />

@vipierozan99
Copy link
Author

Edit: I know hook shouldn't be called conditionally, but maybe you can avoid rendering that component where the hook is called. Basically in your parent component:

well, yes, I could do that, but I don't think it's a nice DX, now the parent component has to know if a child is using that hook or not. I'm using it to animate a bunch of components on the page depending on the scroll of another element on the page.

Right now I'm patching MUI using pnpm patch, but I thought its a nice change to contribute to MUI :)

It's also not a breaking change because you have to explicitly pass null to get that behaviour.

Comment on lines +30 to +31
setTrigger(false);
return undefined;
Copy link
Member

Choose a reason for hiding this comment

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

Would this work?

Suggested change
setTrigger(false);
return undefined;
return setTrigger(false);

@ZeeshanTamboli ZeeshanTamboli changed the title [useScrollTrigger] Do nothing if target is null [material-ui][useScrollTrigger] Do nothing if target is null Mar 8, 2025
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