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

[RFC] useFreezeScroll #2473

Open
dev-aly3n opened this issue Feb 11, 2024 · 0 comments · May be fixed by #2474
Open

[RFC] useFreezeScroll #2473

dev-aly3n opened this issue Feb 11, 2024 · 0 comments · May be fixed by #2474

Comments

@dev-aly3n
Copy link

It's very challenging to avoid layout shift when you remove the scrollbar from the window. Many UI libraries still face issues with this, experiencing layout shifts when they open a modal or freeze scroll in some way. After testing numerous approaches I found an approach that avoids layout shifts while removing the scrollbar and freezing the scroll.
The useFreezeScroll hook freezes the scroll and removes the scrollbar from the browser window when it is active, without causing any layout shifts. This is particularly useful, for example, when a modal is opened.

import { useFreezeScroll } from 'ahooks';
import React, { useState } from 'react';

export default () => {
  const [isActive, setIsActive] = useState(false);
  useFreezeScroll(isActive);

  return (
      <div style={{ height: '300px' }}>
        <div style={{ height: '5000px' }}>
          <div style={{ marginTop: '20px' }}>
            <p>Scroll should be frozen when enabled.</p>
            <p>Try scrolling to test the effect.</p>
          </div>
          <button onClick={() => setIsActive(!isActive)} style={{ position: 'sticky', top: '100px' }}>
            {isActive ? 'Disable Freeze Scroll' : 'Enable Freeze Scroll'}
          </button>
        </div>
      </div>
  );
};
@dev-aly3n dev-aly3n linked a pull request Feb 11, 2024 that will close this issue
18 tasks
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 a pull request may close this issue.

1 participant