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 Dialog background scroll #3907

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions .changeset/three-years-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@salt-ds/core": minor
---

Added `disableScroll` prop to FloatingComponent.
27 changes: 27 additions & 0 deletions packages/core/src/__tests__/__e2e__/utils/useFloatingUI.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ const TestComponent = ({
contentId = "test-1-content",
focusManager,
open = true,
disableScroll = false,
}: {
id?: string;
contentId?: string;
focusManager?: boolean;
open?: boolean;
disableScroll?: boolean;
}) => {
const { Component: FloatingComponent } = useFloatingComponent();
const { context } = useFloatingUI({
Expand All @@ -26,6 +28,7 @@ const TestComponent = ({
<FloatingComponent
open={Boolean(open)}
focusManagerProps={focusManager ? { context } : undefined}
disableScroll={disableScroll}
>
<div id={contentId} />
</FloatingComponent>
Expand Down Expand Up @@ -84,4 +87,28 @@ describe("Use useFloatingComponent", () => {
);
});
});
describe("without disableScroll", () => {
it("the document body should not have hidden overflow", () => {
mount(
<SaltProvider>
<TestComponent disableScroll={false} />
</SaltProvider>,
);

cy.document().its("documentElement.style.overflow").should("equal", "");
});
});
describe("with disableScroll", () => {
it("the document body should have hidden overflow", () => {
mount(
<SaltProvider>
<TestComponent disableScroll={true} />
</SaltProvider>,
);

cy.document()
.its("documentElement.style.overflow")
.should("equal", "hidden");
});
});
});
1 change: 1 addition & 0 deletions packages/core/src/dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const Dialog = forwardRef<HTMLDivElement, DialogProps>(
ref={floatingRef}
width={elements.floating?.offsetWidth}
height={elements.floating?.offsetHeight}
disableScroll={true}
focusManagerProps={{
context: context,
initialFocus,
Expand Down
20 changes: 20 additions & 0 deletions packages/core/src/utils/useFloatingUI/useFloatingUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
createContext,
forwardRef,
useContext,
useEffect,
useMemo,
} from "react";
import { SaltProvider, SaltProviderNext, useTheme } from "../../salt-provider";
Expand All @@ -45,6 +46,10 @@ export interface FloatingComponentProps
width?: number;
height?: number;
position?: Strategy;
/**
* Disables scrolling the document body while the floating component is open.
*/
disableScroll?: boolean;
}

const DefaultFloatingComponent = forwardRef<
Expand All @@ -59,6 +64,7 @@ const DefaultFloatingComponent = forwardRef<
width,
height,
focusManagerProps,
disableScroll,
...rest
} = props;
const style = {
Expand All @@ -71,6 +77,20 @@ const DefaultFloatingComponent = forwardRef<

const ChosenSaltProvider = themeNext ? SaltProviderNext : SaltProvider;

useEffect(() => {
if (!disableScroll) {
return;
}
if (open) {
document.documentElement.style.overflow = "hidden";
joshwooding marked this conversation as resolved.
Show resolved Hide resolved
} else {
document.documentElement.style.removeProperty("overflow");
}
return () => {
document.documentElement.style.removeProperty("overflow");
};
}, [disableScroll, open]);

if (focusManagerProps && open) {
return (
<FloatingPortal>
Expand Down
Loading