Skip to content

Commit

Permalink
chore(utils): Export enableScrollLock and disableScrollLock utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mlaursen committed Jan 30, 2022
1 parent b696b72 commit 6a95734
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
28 changes: 14 additions & 14 deletions packages/utils/src/wia-aria/__tests__/useScrollLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { renderHook } from "@testing-library/react-hooks";

import {
useScrollLock,
disable,
enable,
disableScrollLock,
enableScrollLock,
DATA_RMD_NOSCROLL,
} from "../useScrollLock";

Expand All @@ -16,21 +16,21 @@ describe("useScrollLock", () => {
it("should set the correct styles for an HTMLElement", () => {
const div = document.createElement("div");

enable(div);
enableScrollLock(div);
expect(div.style.overflow).toBe("hidden");
});

it("should apply the data-rmd-noscroll attribute to the element", () => {
const div = document.createElement("div");
enable(div);
enableScrollLock(div);
expect(div.getAttribute(DATA_RMD_NOSCROLL)).toBe("");

enable(document.body);
enableScrollLock(document.body);
expect(document.body.getAttribute(DATA_RMD_NOSCROLL)).toBe("");
});

it("should set the correct styles for the body element", () => {
enable(document.body);
enableScrollLock(document.body);
expect(document.body.style.overflow).toBe("hidden");
expect(document.body.getAttribute(DATA_RMD_NOSCROLL)).toBe("");
});
Expand All @@ -40,26 +40,26 @@ describe("useScrollLock", () => {
it("should not do anything if the element does not have data-rmd-noscroll", () => {
const div = document.createElement("div");
div.style.overflow = "auto";
disable(div);
disableScrollLock(div);
expect(div.style.overflow).toBe("auto");

document.body.style.overflow = "auto";
disable(document.body);
disableScrollLock(document.body);
expect(document.body.style.overflow).toBe("auto");
});

it("should reset all the styles for a div element", () => {
const div = document.createElement("div");
enable(div);
disable(div);
enableScrollLock(div);
disableScrollLock(div);

expect(div.style.overflow).toBe("");
expect(div.getAttribute(DATA_RMD_NOSCROLL)).toBeNull();
});

it("should reset all the styles for the body element", () => {
enable(document.body);
disable(document.body);
enableScrollLock(document.body);
disableScrollLock(document.body);

expect(document.body.style.overflow).toBe("");
expect(document.body.getAttribute(DATA_RMD_NOSCROLL)).toBeNull();
Expand All @@ -73,7 +73,7 @@ describe("useScrollLock", () => {
});

afterEach(() => {
disable(document.body);
disableScrollLock(document.body);
});

it("should apply the correct styles when enabled", () => {
Expand All @@ -92,7 +92,7 @@ describe("useScrollLock", () => {
renderHook(() => useScrollLock(true));
expect(document.body.getAttribute(DATA_RMD_NOSCROLL)).toBe("");

disable(document.body);
disableScrollLock(document.body);
renderHook(() => useScrollLock(true, null));
expect(document.body.getAttribute(DATA_RMD_NOSCROLL)).toBe("");
});
Expand Down
10 changes: 6 additions & 4 deletions packages/utils/src/wia-aria/useScrollLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ export const DATA_RMD_NOSCROLL = "data-rmd-noscroll";
* @param element - Either the `<body>` tag or an element within the page to
* disable scroll for.
* @internal
* @remarks \@since 5.0.0 Renamed from `enable` to `enableScrollLock`.
*/
export function enable(element: HTMLElement): void {
export function enableScrollLock(element: HTMLElement): void {
element.style.overflow = "hidden";
element.setAttribute(DATA_RMD_NOSCROLL, "");
}
Expand All @@ -32,8 +33,9 @@ export function enable(element: HTMLElement): void {
* @param element - Either the `<body>` tag or an element within the page to
* disable scroll locking for.
* @internal
* @remarks \@since 5.0.0 Renamed from `disable` to `disableScrollLock`.
*/
export function disable(element: HTMLElement): void {
export function disableScrollLock(element: HTMLElement): void {
if (element.getAttribute(DATA_RMD_NOSCROLL) === null) {
return;
}
Expand Down Expand Up @@ -75,9 +77,9 @@ export function useScrollLock(
return;
}

enable(element);
enableScrollLock(element);
return () => {
disable(element as HTMLElement);
disableScrollLock(element as HTMLElement);
};
}, [enabled, selectorOrElement]);
}

0 comments on commit 6a95734

Please sign in to comment.