Skip to content

Commit

Permalink
fix useRef
Browse files Browse the repository at this point in the history
  • Loading branch information
sirineJ committed Dec 20, 2024
1 parent 3201f0f commit fa113ee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
27 changes: 4 additions & 23 deletions packages/circuit-ui/hooks/useScrollLock/useScrollLock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import { beforeEach, describe, expect, it, vi } from 'vitest';

import { act, renderHook } from '../../util/test-utils.js';
import { renderHook } from '../../util/test-utils.js';

import { useScrollLock } from './useScrollLock.js';

Expand All @@ -30,12 +30,11 @@ describe('useScrollLock', () => {
beforeEach(() => {
document.body.style.position = '';
document.body.style.top = '';
document.documentElement.style.setProperty('--scroll-y', '');
window.scrollY = 1;
});

it('locks the scroll when `isLocked` is true', () => {
document.documentElement.style.setProperty('--scroll-y', '100px');

window.scrollY = 100;
const { rerender } = renderHook(({ isLocked }) => useScrollLock(isLocked), {
initialProps: { isLocked: false },
});
Expand All @@ -47,7 +46,7 @@ describe('useScrollLock', () => {
});

it('unlocks the scroll when `isLocked` is false', () => {
document.body.style.top = '-100px';
window.scrollY = 100;

const { rerender } = renderHook(({ isLocked }) => useScrollLock(isLocked), {
initialProps: { isLocked: true },
Expand All @@ -59,22 +58,4 @@ describe('useScrollLock', () => {
expect(document.body.style.top).toBe('');
expect(window.scrollTo).toHaveBeenCalledWith(0, 100);
});

it('updates `--scroll-y` on scroll', () => {
global.requestAnimationFrame = vi
.fn()
.mockImplementation((callback: () => void) => callback());

renderHook(() => useScrollLock(false));

act(() => {
window.scrollY = 200;
const scrollEvent = new Event('scroll');
window.dispatchEvent(scrollEvent);
});

expect(document.documentElement.style.getPropertyValue('--scroll-y')).toBe(
'200px',
);
});
});
26 changes: 3 additions & 23 deletions packages/circuit-ui/hooks/useScrollLock/useScrollLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,12 @@
import { useEffect, useRef } from 'react';

export const useScrollLock = (isLocked: boolean): void => {
const busy = useRef(false);

useEffect(() => {
function setScrollProperty() {
if (!busy.current) {
requestAnimationFrame(() => {
document.documentElement.style.setProperty(
'--scroll-y',
`${window.scrollY}px`,
);
busy.current = false;
});
busy.current = true;
}
}
window.addEventListener('scroll', setScrollProperty);

return () => {
window.removeEventListener('scroll', setScrollProperty);
};
}, []);
const scrollValue = useRef<string>();

useEffect(() => {
if (isLocked) {
const scrollY =
document.documentElement.style.getPropertyValue('--scroll-y');
scrollValue.current = `${window.scrollY}px`;
const scrollY = scrollValue.current;
const { body } = document;
const bodyWidth = body.offsetWidth;
// when position is set to fixed, the body element is taken out of
Expand Down

0 comments on commit fa113ee

Please sign in to comment.