Skip to content

Commit

Permalink
Merge pull request #2121 from daostack/bugfix/CW-2106-body-scroll-width
Browse files Browse the repository at this point in the history
Right pane slightly moves right upon clicking the breadcrumb #2106
  • Loading branch information
andreymikhadyuk authored Sep 26, 2023
2 parents 9b5cc3d + 89bc0d5 commit 0baefe6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
font-display: swap;
}

:root {
--page-extra-pr: 0px;
}

body {
--safe-area-inset-bottom: 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}

.desktopRightPane {
right: var(--sb-h-indent);
right: calc(var(--page-extra-pr) + var(--sb-h-indent));
width: var(--chat-w, 22rem);
border-right: 0.0625rem solid $c-light-gray;
}
Expand Down
26 changes: 19 additions & 7 deletions src/shared/hooks/useLockedBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ interface Return {
unlockBodyScroll: () => void;
}

const PAGE_EXTRA_PR_NAME = "--page-extra-pr";

const useLockedBody = (initialLocked = false, rootId = "root"): Return => {
const [locked, setLocked] = useState(initialLocked);

Expand All @@ -24,25 +26,35 @@ const useLockedBody = (initialLocked = false, rootId = "root"): Return => {

// Save initial body style
const originalOverflow = document.body.style.overflow;
const originalPaddingRight = document.body.style.paddingRight;
const originalPaddingRight = parseInt(
window.getComputedStyle(document.body).getPropertyValue("padding-right"),
10,
);

// Get the scrollBar width
const scrollBarWidth =
window.innerWidth - document.documentElement.clientWidth;

// Lock body scroll
document.body.style.overflow = "hidden";

// Get the scrollBar width
const root = document.getElementById(rootId); // or root
const scrollBarWidth = root ? root.offsetWidth - root.scrollWidth : 0;

// Avoid width reflow
if (scrollBarWidth) {
document.body.style.paddingRight = `${scrollBarWidth}px`;
document.body.style.paddingRight = `${
originalPaddingRight + scrollBarWidth
}px`;
document.documentElement.style.setProperty(
PAGE_EXTRA_PR_NAME,
`${scrollBarWidth}px`,
);
}

return () => {
document.body.style.overflow = originalOverflow;

if (scrollBarWidth) {
document.body.style.paddingRight = originalPaddingRight;
document.body.style.paddingRight = `${originalPaddingRight}px`;
document.documentElement.style.setProperty(PAGE_EXTRA_PR_NAME, "0px");
}
};
}, [locked]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.container {
position: fixed;
top: 0;
right: var(--sb-h-indent);
right: calc(var(--page-extra-pr) + var(--sb-h-indent));
left: var(--sb-h-indent);
z-index: 3;
height: var(--header-h);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
.header {
position: fixed;
top: var(--header-h);
right: var(--sb-h-indent);
right: calc(var(--page-extra-pr) + var(--sb-h-indent));
left: var(--main-pl);
z-index: 2;
height: var(--page-content-header);
Expand Down
13 changes: 12 additions & 1 deletion src/shared/ui-kit/ContextMenu/ContextMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {
forwardRef,
useEffect,
useImperativeHandle,
useRef,
useState,
Expand All @@ -20,6 +21,7 @@ import {
FloatingFocusManager,
FloatingOverlay,
} from "@floating-ui/react";
import { useLockedBody } from "@/shared/hooks";
import { ContextMenuItem as Item } from "@/shared/interfaces";
import { ContextMenuItem } from "./components";
import styles from "./ContextMenu.module.scss";
Expand All @@ -41,6 +43,7 @@ export const ContextMenu = forwardRef<ContextMenuRef, ContextMenuProps>(
const [isOpen, setIsOpen] = useState(false);
const listItemsRef = useRef<(HTMLElement | null)[]>([]);
const listContentRef = useRef(menuItems.map((item) => item.text));
const { lockBodyScroll, unlockBodyScroll } = useLockedBody();

const handleOpenChange = (open: boolean) => {
setIsOpen(open);
Expand Down Expand Up @@ -113,10 +116,18 @@ export const ContextMenu = forwardRef<ContextMenuRef, ContextMenuProps>(
[refs],
);

useEffect(() => {
if (isOpen) {
lockBodyScroll();
} else {
unlockBodyScroll();
}
}, [isOpen]);

return (
<FloatingPortal>
{isOpen && (
<FloatingOverlay className={styles.overlay} lockScroll>
<FloatingOverlay className={styles.overlay}>
<FloatingFocusManager
context={context}
initialFocus={refs.floating}
Expand Down

0 comments on commit 0baefe6

Please sign in to comment.