From 2844a2bd71dcf1eb17a53992c10129b7496332e0 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 31 Oct 2023 17:41:43 +0100 Subject: feat(components): add an Overlay component * add useScrollbarWidth hook * add useScrollLock hook * add a new component to lock scroll with an overlay (it can be useful especially on small screens to prevent background contents to be scrolled) --- src/utils/hooks/use-scrollbar-width/index.ts | 1 + .../use-scrollbar-width.test.ts | 12 +++++++ .../use-scrollbar-width/use-scrollbar-width.ts | 42 ++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/utils/hooks/use-scrollbar-width/index.ts create mode 100644 src/utils/hooks/use-scrollbar-width/use-scrollbar-width.test.ts create mode 100644 src/utils/hooks/use-scrollbar-width/use-scrollbar-width.ts (limited to 'src/utils/hooks/use-scrollbar-width') diff --git a/src/utils/hooks/use-scrollbar-width/index.ts b/src/utils/hooks/use-scrollbar-width/index.ts new file mode 100644 index 0000000..932c9bd --- /dev/null +++ b/src/utils/hooks/use-scrollbar-width/index.ts @@ -0,0 +1 @@ +export * from './use-scrollbar-width'; diff --git a/src/utils/hooks/use-scrollbar-width/use-scrollbar-width.test.ts b/src/utils/hooks/use-scrollbar-width/use-scrollbar-width.test.ts new file mode 100644 index 0000000..04148be --- /dev/null +++ b/src/utils/hooks/use-scrollbar-width/use-scrollbar-width.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from '@jest/globals'; +import { renderHook } from '@testing-library/react'; +import { useScrollBarWidth } from './use-scrollbar-width'; + +describe('useScrollbarWidth', () => { + it('returns the scrollbar width', () => { + const { result } = renderHook(() => useScrollBarWidth()); + + // JSdom always return 0 for measurements. + expect(result.current).toBe(0); + }); +}); diff --git a/src/utils/hooks/use-scrollbar-width/use-scrollbar-width.ts b/src/utils/hooks/use-scrollbar-width/use-scrollbar-width.ts new file mode 100644 index 0000000..19bfebc --- /dev/null +++ b/src/utils/hooks/use-scrollbar-width/use-scrollbar-width.ts @@ -0,0 +1,42 @@ +import { useCallback, useEffect, useState } from 'react'; + +/** + * Retrieve the scrollbar width of the window. + * + * @returns {number} The scrollbar width. + */ +export const getScrollbarWidth = (): number => { + const defaultWidth = 15; + + if (typeof window === 'undefined') return defaultWidth; + + return window.document.body.clientWidth + ? window.innerWidth - window.document.body.clientWidth + : 0; +}; + +/** + * React hook to retrieve the current scrollbar width of the window. + * + * @returns {number} The scrollbar width. + */ +export const useScrollBarWidth = (): number => { + const [scrollbarWidth, setScrollbarWidth] = useState(0); + + const updateScrollbarWidth = useCallback(() => { + setScrollbarWidth(getScrollbarWidth()); + }, []); + + useEffect(() => { + updateScrollbarWidth(); + window.addEventListener('resize', updateScrollbarWidth); + window.addEventListener('orientationchange', updateScrollbarWidth); + + return () => { + window.removeEventListener('resize', updateScrollbarWidth); + window.removeEventListener('orientationchange', updateScrollbarWidth); + }; + }, [updateScrollbarWidth]); + + return scrollbarWidth; +}; -- cgit v1.2.3