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

Add autoRefreshCondition optional prop to useSnapCarousel #15

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

Conversation

suniahk
Copy link

@suniahk suniahk commented Aug 10, 2023

What does this add?

This adds a new prop, autoRefreshCondition ?: () => boolean = () => true;. This prop lets an implementation enable or disable uncontrolled refreshes, e.g. on scroll, on resize, etc.

Why is this important?

Allowing implementation control of auto-refresh enables more fine-grained control over the application's UI state. Additionally, the overhead required to add this functionality is minimal.

Example Use Case

The main reason I added this was to allow switching between a carousel view and an "expanded" view. This really only toggles a class that adds flex-wrap: wrap; to the carousel. I also want to be able to hide this functionality when the expanded view isn't required, and to do so, I rely on pages.length (when pages.length > 1, show the toggle). The downside here is that if I add wrapping to the carousel and the layout changes, pages.length automatically changes to 1 and I can no longer access the toggle from that point forward. Being able to disable uncontrolled refreshes will enable this use case.

@richardscarrott
Copy link
Owner

richardscarrott commented Aug 13, 2023

Thanks for the PR and for the detailed description, sounds like a reasonable change.

Is there a specific reason you need a function to disable the behaviour? Perhaps instead you could just pass in a boolean option like this:

const [view, setView] = useState<'carousel' | 'expanded'>('carousel');
const { ref } = useSnapCarousel({
  autoRefresh: view === 'carousel'
});

That way we can avoid attaching the various events if it's false? With a function, I'm a little worried it'll trip people up if they forget to maintain a stable identity with useCallback.

The only reason I can think a function would be necessary is if the condition depended on state outside of react, e.g. a mutable ref?

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