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

feat: add useScrollLock #104

Merged
merged 7 commits into from
Aug 4, 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
5 changes: 5 additions & 0 deletions .changeset/forty-tables-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@raddix/use-scroll-lock': major
---

Added useScrollLock hook.
5 changes: 5 additions & 0 deletions .changeset/six-students-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@raddix/use-clipboard': minor
---

Remove @raddix/use-timeout of peerDependecies
6 changes: 6 additions & 0 deletions .changeset/tasty-boats-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@raddix/use-scroll-position': minor
'@raddix/use-window-size': minor
---

Remove @raddix/use-event-listener of peerDependencies
26 changes: 12 additions & 14 deletions docs/_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@
"title": "useMediaQuery",
"path": "/hooks/use-media-query"
},
{
"title": "useScrollLock",
"path": "/hooks/use-scroll-lock"
},
{
"title": "useScrollPosition",
"path": "/hooks/use-scroll-position"
},
{
"title": "useScrollSpy",
"path": "/hooks/use-scroll-spy"
},
{
"title": "useWindowSize",
"path": "/hooks/use-window-size"
Expand Down Expand Up @@ -64,6 +72,10 @@
"title": "useCounter",
"path": "/hooks/use-counter"
},
{
"title": "useCountDown",
"path": "/hooks/use-count-down"
},
{
"title": "useDebounce",
"path": "/hooks/use-debounce"
Expand Down Expand Up @@ -103,20 +115,6 @@
"path": "/hooks/use-isomorphic-effect"
}
]
},
{
"title": "Utilities",
"heading": true,
"children": [
{
"title": "useCountDown",
"path": "/hooks/use-count-down"
},
{
"title": "useScrollSpy",
"path": "/hooks/use-scroll-spy"
}
]
}
]
}
56 changes: 56 additions & 0 deletions docs/en/use-scroll-lock.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: useScrollLock
description: Disables scrolling in the document body.
---

## Features

- Removes the scroll bar from the document, while preserving the width of the page.
- Works on any desktop or mobile browser.

## Installation

Install the custom hook from your command line.

<Snippet pkg text='@raddix/use-scroll-lock' />

## Usage

Once the component using the `useScrollLock` hook is mounted, scrolling is disabled
in the document body. When the component is unmounted, the hook returns a cleanup function
that restores the original overflow style.

```jsx
import { useState } from 'react';
import { useScrollLock } from '@raddix/use-scroll-lock';

function Modal({ handleClose }) {
useScrollLock();

return (
<dialog>
<button onClick={handleClose}>Close</button>
<h2>Modal</h2>
</dialog>
)
}

export default function App() {
const [isOpen, setIsOpen] = useState(false);

return (
<>
{isOpen && <Modal handleClose={() => setIsOpen(false)} />}
<button className="primary" onClick={() => setIsOpen(true)}>
Open Modal
</button>
</>
);
}
```

## API

### Parameters

The `useScrollLock` hook does not accept any parameters.
56 changes: 56 additions & 0 deletions docs/es/use-scroll-lock.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
title: useScrollLock
description: Deshabilita el desplazamiento en el cuerpo del documento.
---

## CaracterΓ­sticas

- Elimina la barra de desplazamiento del documento, conservando el ancho de la pΓ‘gina.
- Funciona en cualquier navegador de escritorio o mΓ³vil.

## InstalaciΓ³n

Instala el custom hook desde su linea de comando.

<Snippet pkg text='@raddix/use-scroll-lock' />

## Uso

Una vez que el componente que utiliza el hook `useScrollLock` se monta, se deshabilita el desplazamiento
en el cuerpo del documento. Cuando se desmonta el componente el hook devuelve una funciΓ³n de limpieza
que restaura el estilo de desbordamiento original.

```jsx
import { useState } from 'react';
import { useScrollLock } from '@raddix/use-scroll-lock';

function Modal({ handleClose }) {
useScrollLock();

return (
<dialog>
<button onClick={handleClose}>Close</button>
<h2>Modal</h2>
</dialog>
)
}

export default function App() {
const [isOpen, setIsOpen] = useState(false);

return (
<>
{isOpen && <Modal handleClose={() => setIsOpen(false)} />}
<button className="primary" onClick={() => setIsOpen(true)}>
Open Modal
</button>
</>
);
}
```

## API

### ParΓ‘metros

El hook `useScrollLock` no acepta ningun parΓ‘metro.
3 changes: 1 addition & 2 deletions packages/hooks/use-clipboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
],
"peerDependencies": {
"react": ">=16.8.0",
"react-dom": ">=16.8.0",
"@raddix/use-timeout": "workspace:*"
"react-dom": ">=16.8.0"
},
"clean-package": "../../../clean-package.config.json",
"tsup": {
Expand Down
13 changes: 9 additions & 4 deletions packages/hooks/use-clipboard/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useTimeout } from '@raddix/use-timeout';
import { useState } from 'react';
import { useEffect, useRef, useState } from 'react';

export type UseClipboard = (options?: {
timeout?: number;
Expand All @@ -10,7 +9,7 @@ export type UseClipboard = (options?: {
export const useClipboard: UseClipboard = (options = {}) => {
const { timeout = 2000, onError, onSuccess } = options;
const [isCopied, setIsCopied] = useState(false);
const { run } = useTimeout(() => setIsCopied(false), timeout, false);
const idTimeout = useRef<NodeJS.Timeout | null>(null);

const copy = (data: string) => {
if ('clipboard' in navigator) {
Expand All @@ -19,13 +18,19 @@ export const useClipboard: UseClipboard = (options = {}) => {
.then(() => {
setIsCopied(true);
onSuccess?.();
run();
idTimeout.current = setTimeout(() => setIsCopied(false), timeout);
})
.catch(err => onError?.(err));
} else {
onError?.(new Error('navigator.clipboard is not supported'));
}
};

useEffect(() => {
return () => {
if (idTimeout.current) clearTimeout(idTimeout.current);
};
}, []);

return [isCopied, copy];
};
5 changes: 5 additions & 0 deletions packages/hooks/use-scroll-lock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# useScrollLock

A hook that locks and unlocks scroll.

Please refer to the [documentation](https://raddix.dev/hooks/use-scroll-lock) for more information.
46 changes: 46 additions & 0 deletions packages/hooks/use-scroll-lock/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@raddix/use-scroll-lock",
"description": "A hook that locks and unlocks scroll.",
"version": "0.1.0",
"license": "MIT",
"main": "src/index.ts",
"author": "Moises Machuca Valverde <[email protected]> (https://www.moisesmachuca.com)",
"homepage": "https://raddix.dev",
"repository": {
"type": "git",
"url": "https://github.com/gdvu/raddix.git"
},
"keywords": [
"react-hook",
"react-scroll-lock-hook",
"react-use-scroll-lock",
"use-scroll-lock",
"use-scroll-lock-hook",
"hook-scroll-lock"
],
"sideEffects": false,
"scripts": {
"lint": "eslint \"{src,tests}/*.{ts,tsx,css}\"",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"build": "tsup src --dts",
"prepack": "clean-package",
"postpack": "clean-package restore"
},
"files": [
"dist",
"README.md"
],
"peerDependencies": {
"react": ">=16.8.0",
"react-dom": ">=16.8.0"
},
"clean-package": "../../../clean-package.config.json",
"tsup": {
"clean": true,
"target": "es2019",
"format": [
"cjs",
"esm"
]
}
}
36 changes: 36 additions & 0 deletions packages/hooks/use-scroll-lock/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useLayoutEffect, useRef } from 'react';

interface OriginalStyle {
overflow: string;
paddingRight: string;
}

export const useScrollLock = (): void => {
const originalStyle = useRef<OriginalStyle | null>(null);

const preventTouch = (e: Event) => {
e.preventDefault();
return false;
};

useLayoutEffect(() => {
const scrollbarWidth = window.innerWidth - document.body.scrollWidth;
const paddingRight = window.getComputedStyle(document.body).paddingRight;
const overflow = window.getComputedStyle(document.body).overflow;
const right = scrollbarWidth + parseInt(paddingRight, 10);

originalStyle.current = { overflow, paddingRight };
document.body.style.paddingRight = `${right}px`;
document.body.style.overflow = 'hidden';
document.addEventListener('touchmove', preventTouch, { passive: false });

return () => {
if (originalStyle.current) {
document.body.style.overflow = originalStyle.current.overflow;
document.body.style.paddingRight = originalStyle.current.paddingRight;
}

document.removeEventListener('touchmove', preventTouch);
};
}, []);
};
44 changes: 44 additions & 0 deletions packages/hooks/use-scroll-lock/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-disable @typescript-eslint/unbound-method */
import { renderHook } from '@testing-library/react';
import { useScrollLock } from '../src';

describe('useScrollLock test:', () => {
beforeEach(() => {
document.body.style.overflow = 'auto';
document.body.style.paddingRight = '0px';

jest.spyOn(document, 'addEventListener');
jest.spyOn(document, 'removeEventListener');
});

afterEach(() => {
jest.restoreAllMocks();
});

it('should block the scroll and add the touchmove event', () => {
const { unmount } = renderHook(() => useScrollLock());
const touchMoveEvent = new Event('touchmove');
const scrollbarWidth = window.innerWidth - document.body.scrollWidth;
touchMoveEvent.preventDefault = jest.fn();
document.dispatchEvent(touchMoveEvent);

expect(touchMoveEvent.preventDefault).toHaveBeenCalled();
expect(document.body.style.overflow).toBe('hidden');
expect(document.body.style.paddingRight).toBe(`${scrollbarWidth}px`);

expect(document.addEventListener).toHaveBeenCalledWith(
'touchmove',
expect.any(Function),
{ passive: false }
);

unmount();
expect(document.body.style.overflow).toBe('auto');
expect(document.body.style.paddingRight).toBe('0px');

expect(document.removeEventListener).toHaveBeenCalledWith(
'touchmove',
expect.any(Function)
);
});
});
3 changes: 1 addition & 2 deletions packages/hooks/use-scroll-position/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
],
"peerDependencies": {
"react": ">=16.8.0",
"react-dom": ">=16.8.0",
"@raddix/use-event-listener": "workspace:*"
"react-dom": ">=16.8.0"
},
"clean-package": "../../../clean-package.config.json",
"tsup": {
Expand Down
Loading
Loading