-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #312 from SMAKSS/develop
[Release] Allow specifying a custom scrollable element
- Loading branch information
Showing
11 changed files
with
822 additions
and
827 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,49 @@ | ||
import tsParser from '@typescript-eslint/parser'; | ||
import tsPlugin from '@typescript-eslint/eslint-plugin'; | ||
import reactHooksPlugin from 'eslint-plugin-react-hooks'; | ||
|
||
export default [ | ||
{ | ||
files: ['**/*.{js,jsx,ts,tsx}'], | ||
ignores: [ | ||
'dist/**', | ||
'prettier.config.cjs', | ||
'lint-staged.config.cjs', | ||
'commitlint.config.cjs' | ||
], | ||
languageOptions: { | ||
parser: tsParser, | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
modules: true | ||
}, | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
tsconfigRootDir: '.', | ||
project: ['./tsconfig.json'] | ||
} | ||
}, | ||
plugins: { | ||
'@typescript-eslint': tsPlugin, | ||
'react-hooks': reactHooksPlugin | ||
}, | ||
rules: { | ||
'linebreak-style': 'off', | ||
'@typescript-eslint/no-explicit-any': 'warn', | ||
'@typescript-eslint/ban-types': [ | ||
'error', | ||
{ | ||
extendDefaults: true, | ||
types: { | ||
'{}': false | ||
} | ||
} | ||
], | ||
'react-hooks/rules-of-hooks': 'error', | ||
'react-hooks/exhaustive-deps': 'warn', | ||
'object-shorthand': 'error', | ||
'no-console': ['warn', { allow: ['warn', 'error', 'info'] }] | ||
} | ||
} | ||
]; |
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 |
---|---|---|
@@ -1,200 +1,4 @@ | ||
import { useState, useEffect, useCallback, useRef } from 'react'; | ||
|
||
/** Enumeration for axis values */ | ||
export enum Axis { | ||
/** | ||
* The x-axis represents the horizontal direction. | ||
*/ | ||
X = 'x', | ||
/** | ||
* The y-axis represents the vertical direction. | ||
*/ | ||
Y = 'y' | ||
} | ||
|
||
/** Enumeration for direction values */ | ||
export enum Direction { | ||
/** | ||
* The up direction represents the scroll direction moving towards the top. | ||
*/ | ||
Up = 'up', | ||
/** | ||
* The down direction represents the scroll direction moving towards the bottom. | ||
*/ | ||
Down = 'down', | ||
/** | ||
* The left direction represents the scroll direction moving towards the left. | ||
*/ | ||
Left = 'left', | ||
/** | ||
* The right direction represents the scroll direction moving towards the right. | ||
*/ | ||
Right = 'right', | ||
/** | ||
* The still direction represents the scroll direction when the user is not scrolling. | ||
*/ | ||
Still = 'still' | ||
} | ||
|
||
type ScrollPosition = { | ||
/** | ||
* The top position represents the distance from the top edge of the page. | ||
*/ | ||
top: number; | ||
/** | ||
* The bottom position represents the distance from the bottom edge of the page. | ||
*/ | ||
bottom: number; | ||
/** | ||
* The left position represents the distance from the left edge of the page. | ||
*/ | ||
left: number; | ||
/** | ||
* The right position represents the distance from the right edge of the page. | ||
*/ | ||
right: number; | ||
}; | ||
|
||
/** Type declaration for the returned scroll information */ | ||
type ScrollInfo = { | ||
/** | ||
* The scrollDir represents the current scroll direction. | ||
*/ | ||
scrollDir: Direction; | ||
/** | ||
* The scrollPosition represents the current scroll position. | ||
*/ | ||
scrollPosition: ScrollPosition; | ||
}; | ||
|
||
/** Type declaration for scroll properties */ | ||
type ScrollProps = { | ||
/** | ||
* The thr represents the threshold value for scroll detection. | ||
*/ | ||
thr?: number; | ||
/** | ||
* The axis represents the scroll axis (x or y). | ||
*/ | ||
axis?: Axis; | ||
/** | ||
* The scrollUp represents the scroll direction when moving up. | ||
*/ | ||
scrollUp?: Direction; | ||
/** | ||
* The scrollDown represents the scroll direction when moving down. | ||
*/ | ||
scrollDown?: Direction; | ||
/** | ||
* The still represents the scroll direction when the user is not scrolling. | ||
*/ | ||
still?: Direction; | ||
}; | ||
|
||
/** | ||
* useDetectScroll hook. | ||
* | ||
* This hook provides a mechanism to detect the scroll direction and position. | ||
* It will return the scroll direction as a string (up, down, left, right, or still) based on user scrolling, | ||
* as well as the scroll position from the top, bottom, left, and right edges of the page. | ||
* | ||
* @example | ||
* | ||
* import useDetectScroll, { Axis, Direction } from '@smakss/react-scroll-direction'; | ||
* | ||
* function App() { | ||
* const { scrollDir, scrollPosition } = useDetectScroll({ | ||
* thr: 100, | ||
* axis: Axis.Y, | ||
* scrollUp: Direction.Up, | ||
* scrollDown: Direction.Down, | ||
* still: Direction.Still | ||
* }); | ||
* | ||
* return ( | ||
* <div> | ||
* <p>Current scroll direction: {scrollDir}</p> | ||
* <p>Scroll position - Top: {scrollPosition.top}, Bottom: {scrollPosition.bottom}, | ||
* Left: {scrollPosition.left}, Right: {scrollPosition.right}</p> | ||
* </div> | ||
* ); | ||
* } | ||
* | ||
* @param {ScrollProps} props - The properties related to scrolling. | ||
* @returns {ScrollInfo} - The current direction and position of scrolling. | ||
*/ | ||
function useDetectScroll(props: ScrollProps = {}): ScrollInfo { | ||
const { | ||
thr = 0, | ||
axis = Axis.Y, | ||
scrollUp = axis === Axis.Y ? Direction.Up : Direction.Left, | ||
scrollDown = axis === Axis.Y ? Direction.Down : Direction.Right, | ||
still = Direction.Still | ||
} = props; | ||
|
||
const [scrollDir, setScrollDir] = useState<Direction>(still); | ||
const [scrollPosition, setScrollPosition] = useState<ScrollPosition>({ | ||
top: 0, | ||
bottom: 0, | ||
left: 0, | ||
right: 0 | ||
}); | ||
|
||
const threshold = Math.max(0, thr); | ||
const ticking = useRef(false); | ||
const lastScroll = useRef(0); | ||
|
||
/** Function to update scroll direction */ | ||
const updateScrollDir = useCallback(() => { | ||
const scroll = axis === Axis.Y ? window.scrollY : window.scrollX; | ||
|
||
if (Math.abs(scroll - lastScroll.current) >= threshold) { | ||
setScrollDir(scroll > lastScroll.current ? scrollDown : scrollUp); | ||
lastScroll.current = Math.max(0, scroll); | ||
} | ||
ticking.current = false; | ||
}, [axis, threshold, scrollDown, scrollUp]); | ||
|
||
useEffect(() => { | ||
/** Function to update scroll position */ | ||
const updateScrollPosition = () => { | ||
const top = window.scrollY; | ||
const left = window.scrollX; | ||
const bottom = | ||
document.documentElement.scrollHeight - window.innerHeight - top; | ||
const right = | ||
document.documentElement.scrollWidth - window.innerWidth - left; | ||
|
||
setScrollPosition({ top, bottom, left, right }); | ||
}; | ||
|
||
/** Call the update function when the component mounts */ | ||
updateScrollPosition(); | ||
|
||
window.addEventListener('scroll', updateScrollPosition); | ||
|
||
return () => { | ||
window.removeEventListener('scroll', updateScrollPosition); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
lastScroll.current = axis === Axis.Y ? window.scrollY : window.scrollX; | ||
|
||
/** Function to handle onScroll event */ | ||
const onScroll = () => { | ||
if (!ticking.current) { | ||
window.requestAnimationFrame(updateScrollDir); | ||
ticking.current = true; | ||
} | ||
}; | ||
|
||
window.addEventListener('scroll', onScroll); | ||
|
||
return () => window.removeEventListener('scroll', onScroll); | ||
}, [axis, updateScrollDir]); | ||
|
||
return { scrollDir, scrollPosition }; | ||
} | ||
import useDetectScroll from './useDetectScroll'; | ||
|
||
export default useDetectScroll; | ||
export { Axis, Direction } from './types'; |
Oops, something went wrong.