| 1 |
import { forwardRef, useEffect, useState } from 'react'; |
| 2 |
import styles from '@patternfly/react-styles/css/components/BackToTop/back-to-top'; |
| 3 |
import { css } from '@patternfly/react-styles'; |
| 4 |
import RhMicronsCaretUpIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-up-icon'; |
| 5 |
import { canUseDOM } from '../../helpers/util'; |
| 6 |
import { Button } from '../Button'; |
| 7 |
|
| 8 |
interface BackToTopProps extends React.DetailedHTMLProps<React.HTMLProps<HTMLDivElement>, HTMLDivElement> { |
| 9 |
/** Additional classes added to the back to top. */ |
| 10 |
className?: string; |
| 11 |
/** Title to appear in back to top button. */ |
| 12 |
title?: string; |
| 13 |
/** @hide Forwarded ref */ |
| 14 |
innerRef?: React.Ref<any>; |
| 15 |
/** Selector for the scrollable element to spy on. Not passing a selector defaults to spying on window scroll events. */ |
| 16 |
scrollableSelector?: string; |
| 17 |
/** Flag to always show back to top button, defaults to false. */ |
| 18 |
isAlwaysVisible?: boolean; |
| 19 |
} |
| 20 |
|
| 21 |
const BackToTopBase: React.FunctionComponent<BackToTopProps> = ({ |
| 22 |
className, |
| 23 |
title = 'Back to top', |
| 24 |
innerRef, |
| 25 |
scrollableSelector, |
| 26 |
isAlwaysVisible = false, |
| 27 |
...props |
| 28 |
}: BackToTopProps) => { |
| 29 |
const [visible, setVisible] = useState(isAlwaysVisible); |
| 30 |
useEffect(() => { |
| 31 |
setVisible(isAlwaysVisible); |
| 32 |
}, [isAlwaysVisible]); |
| 33 |
|
| 34 |
const [scrollElement, setScrollElement] = useState(null); |
| 35 |
|
| 36 |
const toggleVisible = () => { |
| 37 |
if (scrollElement) { |
| 38 |
const scrolled = scrollElement.scrollY ? scrollElement.scrollY : scrollElement.scrollTop; |
| 39 |
if (!isAlwaysVisible) { |
| 40 |
if (scrolled > 400) { |
| 41 |
setVisible(true); |
| 42 |
} else { |
| 43 |
setVisible(false); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
useEffect(() => { |
| 50 |
const hasScrollSpy = Boolean(scrollableSelector); |
| 51 |
if (hasScrollSpy) { |
| 52 |
const scrollEl = document.querySelector(scrollableSelector) as HTMLElement; |
| 53 |
if (!canUseDOM || !(scrollEl instanceof HTMLElement)) { |
| 54 |
return; |
| 55 |
} |
| 56 |
setScrollElement(scrollEl); |
| 57 |
scrollEl.addEventListener('scroll', toggleVisible); |
| 58 |
|
| 59 |
return () => { |
| 60 |
scrollEl.removeEventListener('scroll', toggleVisible); |
| 61 |
}; |
| 62 |
} else { |
| 63 |
if (!canUseDOM) { |
| 64 |
return; |
| 65 |
} |
| 66 |
const scrollEl = window; |
| 67 |
setScrollElement(scrollEl); |
| 68 |
scrollEl.addEventListener('scroll', toggleVisible); |
| 69 |
|
| 70 |
return () => { |
| 71 |
scrollEl.removeEventListener('scroll', toggleVisible); |
| 72 |
}; |
| 73 |
} |
| 74 |
}, [scrollableSelector, toggleVisible]); |
| 75 |
|
| 76 |
const handleClick = () => { |
| 77 |
scrollElement.scrollTo({ top: 0, behavior: 'smooth' }); |
| 78 |
}; |
| 79 |
|
| 80 |
return ( |
| 81 |
<div |
| 82 |
className={css(styles.backToTop, !visible && styles.modifiers.hidden, className)} |
| 83 |
ref={innerRef} |
| 84 |
onClick={handleClick} |
| 85 |
{...props} |
| 86 |
> |
| 87 |
<Button variant="primary" icon={<RhMicronsCaretUpIcon />} iconPosition="end"> |
| 88 |
{title} |
| 89 |
</Button> |
| 90 |
</div> |
| 91 |
); |
| 92 |
}; |
| 93 |
|
| 94 |
export const BackToTop = forwardRef((props: BackToTopProps, ref: React.Ref<any>) => ( |
| 95 |
<BackToTopBase innerRef={ref} {...props} /> |
| 96 |
)); |
| 97 |
BackToTop.displayName = 'BackToTop'; |