|
| 1 | +import { getWindowScrollTop } from '@primeuix/utils'; |
| 2 | +import * as React from 'react'; |
| 3 | + |
| 4 | +/** |
| 5 | + * The options for the `useScrollTop` hook. |
| 6 | + */ |
| 7 | +export interface UseScrollTopOptions { |
| 8 | + /** |
| 9 | + * Reference to the target element. |
| 10 | + * @default window |
| 11 | + */ |
| 12 | + target?: Window | Element | null; |
| 13 | + /** |
| 14 | + * Defines the threshold value of the vertical scroll position of the target to toggle the visibility. |
| 15 | + * @default 400 |
| 16 | + */ |
| 17 | + threshold?: number; |
| 18 | + /** |
| 19 | + * Defines the scrolling behaviour, 'smooth' adds an animation and 'auto' scrolls with a jump. |
| 20 | + * @default smooth |
| 21 | + */ |
| 22 | + behavior?: ScrollBehavior; |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * The exposes for the `useScrollTop` hook. |
| 27 | + */ |
| 28 | +export interface UseScrollTopExposes { |
| 29 | + /** |
| 30 | + * Current visible state as a boolean. |
| 31 | + * @default false |
| 32 | + */ |
| 33 | + visible: boolean; |
| 34 | + /** |
| 35 | + * Scrolls the target element to the top. |
| 36 | + */ |
| 37 | + scrollToTop: () => void; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * useScrollTop hook is used to enter input in a certain format such as numeric, date, currency, email and phone. |
| 42 | + * |
| 43 | + * @param {UseScrollTopOptions} options - The options for the scroll top behavior. |
| 44 | + * @returns {UseScrollTopExposes} - The exposed methods for the scroll top behavior. |
| 45 | + * |
| 46 | + * @example |
| 47 | + * ```tsx |
| 48 | + * const { scrollToTop, visible } = useScrollTop({ |
| 49 | + * target: elementRef.current, |
| 50 | + * threshold: 400, |
| 51 | + * behavior: 'smooth' |
| 52 | + * }); |
| 53 | + * |
| 54 | + * return ( |
| 55 | + * <div ref={elementRef}> |
| 56 | + * {visible && ( |
| 57 | + * <Button onClick={scrollToTop}> |
| 58 | + * <i className="pi pi-arrow-up" /> |
| 59 | + * </Button> |
| 60 | + * )} |
| 61 | + * </div> |
| 62 | + * ); |
| 63 | + */ |
| 64 | +export function useScrollTop(options?: UseScrollTopOptions): UseScrollTopExposes { |
| 65 | + const { target = typeof window !== 'undefined' ? window : null, threshold = 400, behavior = 'smooth' } = options || {}; |
| 66 | + const [visible, setVisible] = React.useState(false); |
| 67 | + |
| 68 | + const scrollToTop = () => { |
| 69 | + target?.scroll({ |
| 70 | + top: 0, |
| 71 | + behavior: behavior |
| 72 | + }); |
| 73 | + }; |
| 74 | + |
| 75 | + React.useEffect(() => { |
| 76 | + if (!target) return; |
| 77 | + |
| 78 | + const checkVisibility = (scrollY: number) => { |
| 79 | + if (scrollY > threshold) setVisible(true); |
| 80 | + else setVisible(false); |
| 81 | + }; |
| 82 | + |
| 83 | + const onScroll = () => { |
| 84 | + const scrollY = target === window ? getWindowScrollTop() : (target as Element).scrollTop; |
| 85 | + |
| 86 | + checkVisibility(scrollY); |
| 87 | + }; |
| 88 | + |
| 89 | + target.addEventListener('scroll', onScroll); |
| 90 | + |
| 91 | + return () => { |
| 92 | + target.removeEventListener('scroll', onScroll); |
| 93 | + }; |
| 94 | + }, [target, threshold]); |
| 95 | + |
| 96 | + return { |
| 97 | + //state |
| 98 | + visible, |
| 99 | + //methods |
| 100 | + scrollToTop |
| 101 | + }; |
| 102 | +} |
0 commit comments