-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# `useLockBodyScroll` | ||
|
||
React side-effect hook that locks scrolling on the body element. Useful for modal and other overlay components. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import {useLockBodyScroll, useToggle} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const [locked, toggleLocked] = useToggle(false) | ||
|
||
useLockBodyScroll(locked); | ||
|
||
return ( | ||
<div> | ||
<button onClick={() => toggleLocked()}> | ||
{locked ? 'Unlock' : 'Lock'} | ||
</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
useToggle(enabled?: boolean = true); | ||
``` | ||
|
||
- `enabled` — Hook will lock scrolling on the body element if `true`, defaults to `true` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import * as React from 'react'; | ||
import {useLockBodyScroll, useToggle} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [locked, toggleLocked] = useToggle(false) | ||
useLockBodyScroll(locked); | ||
|
||
return ( | ||
<div style={{height: '200vh'}}> | ||
<button onClick={() => toggleLocked()} style={{position: 'fixed', left: 0}}> | ||
{locked ? 'Unlock' : 'Lock'} | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
const AnotherComponent = () => { | ||
const [locked, toggleLocked] = useToggle(false) | ||
useLockBodyScroll(locked); | ||
|
||
return ( | ||
<button onClick={() => toggleLocked()} style={{position: 'fixed', left: 0, top: 40}}> | ||
{locked ? 'Unlock' : 'Lock'} | ||
</button> | ||
); | ||
}; | ||
|
||
storiesOf('Side effects|useLockBodyScroll', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useLockBodyScroll.md')} />) | ||
.add('Demo', () => <Demo/>) | ||
.add('Two hooks on page', () => | ||
<> | ||
<AnotherComponent /> | ||
<Demo /> | ||
</> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {useEffect} from 'react'; | ||
|
||
let counter = 0; | ||
let originalOverflow: string | null = null; | ||
|
||
const lock = () => { | ||
originalOverflow = window.getComputedStyle(document.body).overflow; | ||
document.body.style.overflow = 'hidden'; | ||
}; | ||
|
||
const unlock = () => { | ||
document.body.style.overflow = originalOverflow; | ||
originalOverflow = null; | ||
}; | ||
|
||
const increment = () => { | ||
counter++; | ||
if (counter === 1) lock(); | ||
}; | ||
|
||
const decrement = () => { | ||
counter--; | ||
if (counter === 0) unlock(); | ||
}; | ||
|
||
const useLockBodyScroll = (enabled: boolean = true) => { | ||
useEffect(() => enabled ? (increment(), decrement) : undefined, [enabled]); | ||
} | ||
|
||
export default useLockBodyScroll; |