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

fix(useScroll): trigger once onMounted to get correct inital arrivedStates values #3384

Merged
merged 2 commits into from Nov 9, 2023

Conversation

nicooprat
Copy link
Contributor

@nicooprat nicooprat commented Sep 8, 2023

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.
⚠️ Slowing down new functions

Warning: Slowing down new functions

As the VueUse audience continues to grow, we have been inundated with an overwhelming number of feature requests and pull requests. As a result, maintaining the project has become increasingly challenging and has stretched our capacity to its limits. As such, in the near future, we may need to slow down our acceptance of new features and prioritize the stability and quality of existing functions. Please note that new features for VueUse may not be accepted at this time. If you have any new ideas, we suggest that you first incorporate them into your own codebase, iterate on them to suit your needs, and assess their generalizability. If you strongly believe that your ideas are beneficial to the community, you may submit a pull request along with your use cases, and we would be happy to review and discuss them. Thank you for your understanding.


Description

Fix #2875

useScroll have incorrect initial values for arrivedStates (bottom & right are false by default) and are not computed if the child is smaller than its parent (can't scroll at all anyway).

Running the demo, it's easy to reproduce: just decrease the child dimensions here

<div ref="el" class="w-300px h-300px m-auto overflow-scroll bg-gray-500/5 rounded">
<div class="w-500px h-400px relative">

Before:

image

After:

image

Additional context

Following antfu proposition, I just triggered once setArrivedState when the component is first mounted.

Edit: we might also want to watch for element resize for responsive purposes, but I'm not sure how would be the best way to use useResizeObserver here.


🤖 Generated by Copilot at 2838f20

Improved useScroll function to initialize state and handle errors on mount, and to update state based on element's scroll properties.

🤖 Generated by Copilot at 2838f20

  • Import tryOnMounted function from @vueuse/shared to execute callbacks on mount (link)
  • Call setArrivedState function with resolved element argument to initialize scroll state on mount (link)

@antfu antfu enabled auto-merge November 9, 2023 17:21
@antfu antfu disabled auto-merge November 9, 2023 17:21
@antfu antfu changed the title Fix useScroll trigger once onMounted to get correct inital arrivedStates values fix(useScroll): trigger once onMounted to get correct inital arrivedStates values Nov 9, 2023
@antfu antfu enabled auto-merge November 9, 2023 17:21
@antfu antfu disabled auto-merge November 9, 2023 18:12
@antfu antfu merged commit 86bd8e8 into vueuse:main Nov 9, 2023
4 checks passed
@steklopod
Copy link
Contributor

steklopod commented Nov 16, 2023

@antfu this breaks what was working in version 1.5.0:

const { arrivedState } = useScroll(scrollBox)
const { bottom } = toRefs(arrivedState) // <-- ❌ This line produces an error in version 1.6.0 [ but not in 1.5.0 ]
Снимок экрана 2023-11-16 в 16 27 26 Снимок экрана 2023-11-16 в 16 27 34

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.

useScroll updating on container resize
3 participants