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

Merged
merged 9 commits into from
Mar 12, 2025

Conversation

vipierozan99
Copy link
Contributor

@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 84e48bd

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
Contributor 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
Contributor 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.

@ZeeshanTamboli ZeeshanTamboli changed the title [useScrollTrigger] Do nothing if target is null [material-ui][useScrollTrigger] Do nothing if target is null Mar 8, 2025
@ZeeshanTamboli ZeeshanTamboli added v6.x enhancement This is not a bug, nor a new feature needs cherry-pick The PR should be cherry-picked to master after merge labels Mar 12, 2025
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.

Looks good!

@ZeeshanTamboli ZeeshanTamboli merged commit 27f0fa9 into mui:master Mar 12, 2025
22 checks passed
Copy link

Cherry-pick PRs will be created targeting branches: v6.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is not a bug, nor a new feature hook: useScrollTrigger needs cherry-pick The PR should be cherry-picked to master after merge package: material-ui Specific to @mui/material v6.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants