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

[core][Drawer] Refactor getScrollbarSize usages #43828

Merged
merged 4 commits into from
Sep 24, 2024
Merged
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
8 changes: 4 additions & 4 deletions packages/mui-base/src/unstable_useModal/ModalManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ describe('ModalManager', () => {
modalManager.add(modal, container1);
modalManager.mount(modal, {});
expect(container1.style.overflow).to.equal('hidden');
expect(container1.style.paddingRight).to.equal(`${20 + getScrollbarSize(document)}px`);
expect(fixedNode.style.paddingRight).to.equal(`${14 + getScrollbarSize(document)}px`);
expect(container1.style.paddingRight).to.equal(`${20 + getScrollbarSize(window)}px`);
expect(fixedNode.style.paddingRight).to.equal(`${14 + getScrollbarSize(window)}px`);
modalManager.remove(modal);
expect(container1.style.overflow).to.equal('');
expect(container1.style.paddingRight).to.equal('20px');
Expand Down Expand Up @@ -171,8 +171,8 @@ describe('ModalManager', () => {
modalManager.add(modal, container1);
modalManager.mount(modal, {});
expect(container1.style.overflow).to.equal('hidden');
expect(container1.style.paddingRight).to.equal(`${20 + getScrollbarSize(document)}px`);
expect(fixedNode.style.paddingRight).to.equal(`${getScrollbarSize(document)}px`);
expect(container1.style.paddingRight).to.equal(`${20 + getScrollbarSize(window)}px`);
expect(fixedNode.style.paddingRight).to.equal(`${getScrollbarSize(window)}px`);
modalManager.remove(modal);
expect(container1.style.overflow).to.equal('');
expect(container1.style.paddingRight).to.equal('20px');
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-base/src/unstable_useModal/ModalManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function handleContainer(containerInfo: Container, props: ManagedModalProps) {
if (!props.disableScrollLock) {
if (isOverflowing(container)) {
// Compute the size before applying overflow hidden to avoid any scroll jumps.
const scrollbarSize = getScrollbarSize(ownerDocument(container));
const scrollbarSize = getScrollbarSize(ownerWindow(container));

restoreStyle.push({
value: container.style.paddingRight,
Expand Down
3 changes: 2 additions & 1 deletion packages/mui-material/src/MenuList/MenuList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import List from '../List';
import getScrollbarSize from '../utils/getScrollbarSize';
import useForkRef from '../utils/useForkRef';
import useEnhancedEffect from '../utils/useEnhancedEffect';
import { ownerWindow } from '../utils';

function nextItem(list, item, disableListWrap) {
if (list === item) {
Expand Down Expand Up @@ -130,7 +131,7 @@ const MenuList = React.forwardRef(function MenuList(props, ref) {
// of the menu.
const noExplicitWidth = !listRef.current.style.width;
if (containerElement.clientHeight < listRef.current.clientHeight && noExplicitWidth) {
const scrollbarSize = `${getScrollbarSize(ownerDocument(containerElement))}px`;
const scrollbarSize = `${getScrollbarSize(ownerWindow(containerElement))}px`;
listRef.current.style[direction === 'rtl' ? 'paddingLeft' : 'paddingRight'] =
scrollbarSize;
listRef.current.style.width = `calc(100% + ${scrollbarSize})`;
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/MenuList/MenuList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('<MenuList />', () => {
});

describe('actions: adjustStyleForScrollbar', () => {
const expectedPadding = `${getScrollbarSize(document)}px`;
const expectedPadding = `${getScrollbarSize(window)}px`;

it('should not adjust style when container element height is greater', () => {
const menuListActionsRef = React.createRef();
Expand Down
8 changes: 4 additions & 4 deletions packages/mui-material/src/Modal/ModalManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ describe('ModalManager', () => {
modalManager.add(modal, container1);
modalManager.mount(modal, {});
expect(container1.style.overflow).to.equal('hidden');
expect(container1.style.paddingRight).to.equal(`${20 + getScrollbarSize(document)}px`);
expect(fixedNode.style.paddingRight).to.equal(`${14 + getScrollbarSize(document)}px`);
expect(container1.style.paddingRight).to.equal(`${20 + getScrollbarSize(window)}px`);
expect(fixedNode.style.paddingRight).to.equal(`${14 + getScrollbarSize(window)}px`);
modalManager.remove(modal);
expect(container1.style.overflow).to.equal('');
expect(container1.style.paddingRight).to.equal('20px');
Expand Down Expand Up @@ -171,8 +171,8 @@ describe('ModalManager', () => {
modalManager.add(modal, container1);
modalManager.mount(modal, {});
expect(container1.style.overflow).to.equal('hidden');
expect(container1.style.paddingRight).to.equal(`${20 + getScrollbarSize(document)}px`);
expect(fixedNode.style.paddingRight).to.equal(`${getScrollbarSize(document)}px`);
expect(container1.style.paddingRight).to.equal(`${20 + getScrollbarSize(window)}px`);
expect(fixedNode.style.paddingRight).to.equal(`${getScrollbarSize(window)}px`);
modalManager.remove(modal);
expect(container1.style.overflow).to.equal('');
expect(container1.style.paddingRight).to.equal('20px');
Expand Down
2 changes: 1 addition & 1 deletion packages/mui-material/src/Modal/ModalManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function handleContainer(containerInfo: Container, props: ManagedModalProps) {
if (!props.disableScrollLock) {
if (isOverflowing(container)) {
// Compute the size before applying overflow hidden to avoid any scroll jumps.
const scrollbarSize = getScrollbarSize(ownerDocument(container));
const scrollbarSize = getScrollbarSize(ownerWindow(container));

restoreStyle.push({
value: container.style.paddingRight,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ describe('getScrollbarSize', () => {
document.head.appendChild(styleElement);
divElement.style.height = '2000px';
document.body.appendChild(divElement);
expect(getScrollbarSize(document)).to.equal(5);
expect(getScrollbarSize(window)).to.equal(5);
});
});
6 changes: 3 additions & 3 deletions packages/mui-utils/src/getScrollbarSize/getScrollbarSize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// A change of the browser zoom change the scrollbar size.
// Credit https://github.com/twbs/bootstrap/blob/488fd8afc535ca3a6ad4dc581f5e89217b6a36ac/js/src/util/scrollbar.js#L14-L18
export default function getScrollbarSize(doc: Document): number {
export default function getScrollbarSize(win: Window = window): number {
// https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth#usage_notes
const documentWidth = doc.documentElement.clientWidth;
return Math.abs(doc.defaultView!.innerWidth - documentWidth);
const documentWidth = win.document.documentElement.clientWidth;
return win.innerWidth - documentWidth;
}
Loading