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

feat(hooks):add block body scroll hook #3405

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hooks/useBlockBodyScroll/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {} from './useBlockBodyScroll';
23 changes: 23 additions & 0 deletions src/hooks/useBlockBodyScroll/useBlockBodyScroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useLayoutEffect } from 'react';

const getBodyElement = () => window.document.body;
const getScrollBarWidth = () => {
return window.innerWidth - document.body.offsetWidth;
};

export const useBlockBodyScroll = () => {
useLayoutEffect(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Привет, поигрался с примером, давай добавим в useBlockBodyScroll флаг вкл/выкл. Просто не всегда нужно заблокировать скрол у боди при монтировании компонента, иногда это нужно сделать по флагу.

const body = getBodyElement();
const defaultOverflow = body.style.overflow;
const defaultPaddingRight = body.style.paddingRight;
const scrollBarWidth = getScrollBarWidth();

body.style.overflow = 'hidden';
body.style.paddingRight = `${scrollBarWidth}px`;

return () => {
body.style.overflow = defaultOverflow;
body.style.paddingRight = defaultPaddingRight;
};
}, []);
};