-
-
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.
feat: useScrollbarWidth hook; (#825)
* feat: useScrollbarWidth hook; * chore: bump @xobotyi/scrollbar-width to 1.5.0 and add it to dependencies. * fix: remove @xobotyi/scrollbar-width from dev-deps.
- Loading branch information
Showing
8 changed files
with
117 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,25 @@ | ||
# `useScrollbarWidth` | ||
|
||
Hook that will return current browser's scrollbar width. | ||
In case hook been called before DOM ready, it will return `undefined` and will cause re-render on first available RAF. | ||
> **_NOTE:_** it does not work (return 0) for mobile devices, because their scrollbar width can not be determined. | ||
## Usage | ||
|
||
```jsx | ||
const Demo = () => { | ||
const sbw = useScrollbarWidth(); | ||
|
||
return ( | ||
<div> | ||
{sbw === undefined ? `DOM is not ready yet, SBW detection delayed` : `Browser's scrollbar width is ${sbw}px`} | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```typescript | ||
const sbw: number | undefined = useScrollbarWidth(); | ||
``` |
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,18 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import { useScrollbarWidth } from '..'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const sbw = useScrollbarWidth(); | ||
|
||
return ( | ||
<div> | ||
{sbw === undefined ? `DOM is not ready yet, SBW detection delayed` : `Browser's scrollbar width is ${sbw}px`} | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('Sensors/useScrollbarWidth', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useScroll.md')} />) | ||
.add('Demo', () => <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,21 @@ | ||
import { scrollbarWidth } from '@xobotyi/scrollbar-width'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
export function useScrollbarWidth(): number | undefined { | ||
const [sbw, setSbw] = useState(scrollbarWidth()); | ||
|
||
// this needed to ensure the scrollbar width in case hook called before the DOM is ready | ||
useEffect(() => { | ||
if (typeof sbw !== 'undefined') { | ||
return; | ||
} | ||
|
||
const raf = requestAnimationFrame(() => { | ||
setSbw(scrollbarWidth()); | ||
}); | ||
|
||
return () => cancelAnimationFrame(raf); | ||
}, []); | ||
|
||
return sbw; | ||
} |
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,45 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { scrollbarWidth } from '@xobotyi/scrollbar-width'; | ||
import { useScrollbarWidth } from '../src'; | ||
import { replaceRaf } from 'raf-stub'; | ||
|
||
declare var requestAnimationFrame: { | ||
add: (cb: Function) => number; | ||
remove: (id: number) => void; | ||
flush: (duration?: number) => void; | ||
reset: () => void; | ||
step: (steps?: number, duration?: number) => void; | ||
}; | ||
|
||
describe('useScrollbarWidth', () => { | ||
beforeAll(() => { | ||
replaceRaf(); | ||
}); | ||
|
||
afterEach(() => { | ||
requestAnimationFrame.reset(); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(useScrollbarWidth).toBeDefined(); | ||
}); | ||
|
||
it('should return value of scrollbarWidth result', () => { | ||
scrollbarWidth.__cache = 21; | ||
const { result } = renderHook(() => useScrollbarWidth()); | ||
|
||
expect(result.current).toBe(21); | ||
}); | ||
|
||
it('should re-call scrollbar width in RAF in case `scrollbarWidth()` returned undefined', () => { | ||
scrollbarWidth.__cache = undefined; | ||
const { result } = renderHook(() => useScrollbarWidth()); | ||
expect(result.current).toBe(undefined); | ||
scrollbarWidth.__cache = 34; | ||
act(() => { | ||
requestAnimationFrame.step(); | ||
}); | ||
|
||
expect(result.current).toBe(34); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -3094,6 +3094,11 @@ | |
"@webassemblyjs/wast-parser" "1.8.5" | ||
"@xtuc/long" "4.2.2" | ||
|
||
"@xobotyi/[email protected]": | ||
version "1.5.0" | ||
resolved "https://registry.yarnpkg.com/@xobotyi/scrollbar-width/-/scrollbar-width-1.5.0.tgz#488210bff634548040dc22a72f62722a85b134e1" | ||
integrity sha512-BK+HR1D00F2xh7n4+5en8/dMkG13uvIXLmEbsjtc1702b7+VwXkvlBDKoRPJMbkRN5hD7VqWa3nS9fNT8JG3CA== | ||
|
||
"@xtuc/ieee754@^1.2.0": | ||
version "1.2.0" | ||
resolved "https://registry.yarnpkg.com/@xtuc/ieee754/-/ieee754-1.2.0.tgz#eef014a3145ae477a1cbc00cd1e552336dceb790" | ||
|