|
1 | 1 | import { Button, type BackTopProps } from 'antd'; |
2 | 2 | import { ListEnd } from 'lucide-react'; |
3 | | -import { |
4 | | - MouseEventHandler, |
5 | | - memo, |
6 | | - useEffect, |
7 | | - useMemo, |
8 | | - useRef, |
9 | | - useState, |
10 | | - type CSSProperties, |
11 | | -} from 'react'; |
| 3 | +import { MouseEventHandler, useEffect, useMemo, useRef, useState, type CSSProperties } from 'react'; |
12 | 4 |
|
13 | 5 | import Icon from '@/Icon'; |
14 | 6 | import { useStyles } from './style'; |
15 | 7 |
|
16 | 8 | export interface BackBottomProps { |
17 | 9 | className?: string; |
| 10 | + /** |
| 11 | + * @description |
| 12 | + * 点击的回调 |
| 13 | + */ |
18 | 14 | onClick?: BackTopProps['onClick']; |
19 | 15 | style?: CSSProperties; |
20 | 16 | target: React.RefObject<HTMLDivElement>; |
21 | 17 | text?: string; |
22 | 18 | visibilityHeight?: BackTopProps['visibilityHeight']; |
| 19 | + /** |
| 20 | + * @description 自定义渲染 dom |
| 21 | + * @param defaultDom |
| 22 | + * @param scrollToBottom |
| 23 | + * @param BackBottomConfig |
| 24 | + * @returns React.ReactNode |
| 25 | + */ |
| 26 | + render?: ( |
| 27 | + defaultDom: React.ReactNode, |
| 28 | + scrollToBottom: MouseEventHandler<HTMLDivElement>, |
| 29 | + BackBottomConfig: BackBottomProps, |
| 30 | + ) => React.ReactNode; |
| 31 | + /** |
| 32 | + * @description |
| 33 | + * 是否一直显示 |
| 34 | + */ |
| 35 | + alwaysShow?: boolean; |
23 | 36 | } |
24 | 37 |
|
25 | | -const BackBottom = memo<BackBottomProps>( |
26 | | - ({ visibilityHeight = 240, target, onClick, style, className, text }) => { |
27 | | - const [visible, setVisible] = useState<boolean>(false); |
28 | | - const { styles, cx } = useStyles(visible); |
29 | | - const ref = useRef<HTMLAnchorElement | HTMLButtonElement>(null); |
| 38 | +const BackBottom = (props: BackBottomProps) => { |
| 39 | + const { |
| 40 | + visibilityHeight = 240, |
| 41 | + target, |
| 42 | + onClick, |
| 43 | + style, |
| 44 | + className, |
| 45 | + text, |
| 46 | + render, |
| 47 | + alwaysShow = false, |
| 48 | + } = props || {}; |
| 49 | + const [visible, setVisible] = useState<boolean>(alwaysShow); |
| 50 | + const { styles, cx } = useStyles(visible); |
| 51 | + const ref = useRef<HTMLAnchorElement | HTMLButtonElement>(null); |
30 | 52 |
|
31 | | - const [isWindowAvailable, setIsWindowAvailable] = useState(false); |
| 53 | + const [isWindowAvailable, setIsWindowAvailable] = useState(false); |
32 | 54 |
|
33 | | - useEffect(() => { |
34 | | - // 检查window对象是否已经可用 |
35 | | - if (typeof window !== 'undefined') { |
36 | | - setIsWindowAvailable(true); |
37 | | - } |
38 | | - }, []); |
| 55 | + useEffect(() => { |
| 56 | + // 检查window对象是否已经可用 |
| 57 | + if (typeof window !== 'undefined') { |
| 58 | + setIsWindowAvailable(true); |
| 59 | + } |
| 60 | + }, []); |
39 | 61 |
|
40 | | - const current = useMemo(() => { |
41 | | - if (target.current && target.current.scrollHeight > target.current.clientHeight) { |
42 | | - return target.current; |
43 | | - } |
44 | | - return document.body; |
45 | | - }, [isWindowAvailable]); |
| 62 | + const current = useMemo(() => { |
| 63 | + if (target.current && target.current.scrollHeight > target.current.clientHeight) { |
| 64 | + return target.current; |
| 65 | + } |
| 66 | + return document.body; |
| 67 | + }, [isWindowAvailable]); |
46 | 68 |
|
47 | | - const scrollHeight = current?.scrollHeight || 0; |
48 | | - const clientHeight = current?.clientHeight || 0; |
49 | | - const [scroll, setScroll] = useState({ top: 0, left: 0 }); |
| 69 | + const scrollHeight = current?.scrollHeight || 0; |
| 70 | + const clientHeight = current?.clientHeight || 0; |
| 71 | + const [scroll, setScroll] = useState({ top: 0, left: 0 }); |
50 | 72 |
|
51 | | - const timeRef = useRef<number | null>(null); |
| 73 | + const timeRef = useRef<number | null>(null); |
52 | 74 |
|
53 | | - useEffect(() => { |
54 | | - if (typeof window === 'undefined') return; |
55 | | - if (typeof current === 'undefined') return; |
56 | | - const scroll = () => { |
57 | | - timeRef.current = window.setTimeout(() => { |
| 75 | + useEffect(() => { |
| 76 | + if (typeof window === 'undefined') return; |
| 77 | + if (typeof current === 'undefined') return; |
| 78 | + const scroll = () => { |
| 79 | + timeRef.current = window.setTimeout(() => { |
| 80 | + if (!alwaysShow) { |
58 | 81 | setVisible(current?.scrollTop + clientHeight + visibilityHeight < scrollHeight); |
59 | | - setScroll({ |
60 | | - top: current?.scrollTop, |
61 | | - left: current?.scrollLeft, |
62 | | - }); |
63 | | - }, 60); |
64 | | - }; |
65 | | - current?.addEventListener?.('scroll', scroll, { |
66 | | - passive: true, |
67 | | - }); |
68 | | - return () => { |
69 | | - if (timeRef.current) { |
70 | | - clearTimeout(timeRef.current); |
71 | 82 | } |
72 | | - current?.removeEventListener?.('scroll', scroll); |
73 | | - }; |
74 | | - }, [current]); |
75 | | - |
76 | | - useEffect(() => { |
77 | | - if (scroll?.top) { |
78 | | - setVisible(scroll?.top + clientHeight + visibilityHeight < scrollHeight); |
| 83 | + setScroll({ |
| 84 | + top: current?.scrollTop, |
| 85 | + left: current?.scrollLeft, |
| 86 | + }); |
| 87 | + }, 60); |
| 88 | + }; |
| 89 | + current?.addEventListener?.('scroll', scroll, { |
| 90 | + passive: true, |
| 91 | + }); |
| 92 | + return () => { |
| 93 | + if (timeRef.current) { |
| 94 | + clearTimeout(timeRef.current); |
79 | 95 | } |
80 | | - }, [scrollHeight, scroll, visibilityHeight, current]); |
| 96 | + current?.removeEventListener?.('scroll', scroll); |
| 97 | + }; |
| 98 | + }, [current]); |
81 | 99 |
|
82 | | - const scrollToBottom: MouseEventHandler<HTMLDivElement> = (e) => { |
| 100 | + useEffect(() => { |
| 101 | + if (scroll?.top && !alwaysShow) { |
| 102 | + setVisible(scroll?.top + clientHeight + visibilityHeight < scrollHeight); |
| 103 | + } |
| 104 | + }, [scrollHeight, scroll, visibilityHeight, current]); |
| 105 | + |
| 106 | + const scrollToBottom: MouseEventHandler<HTMLDivElement> = (e) => { |
| 107 | + (target as any)?.current?.scrollTo({ behavior: 'smooth', left: 0, top: scrollHeight }); |
| 108 | + onClick?.(e); |
| 109 | + }; |
| 110 | + |
| 111 | + /** |
| 112 | + * @description |
| 113 | + * 为了解决在使用了 ProChatProvider 的情况下,BackBottom 无法正常工作的问题 |
| 114 | + */ |
| 115 | + useEffect(() => { |
| 116 | + setTimeout(() => { |
83 | 117 | (target as any)?.current?.scrollTo({ behavior: 'smooth', left: 0, top: scrollHeight }); |
84 | | - onClick?.(e); |
85 | | - }; |
| 118 | + }, 16); |
| 119 | + }, []); |
| 120 | + |
| 121 | + const defauleDom = ( |
| 122 | + <Button |
| 123 | + className={cx(styles, className)} |
| 124 | + icon={<Icon icon={ListEnd} />} |
| 125 | + onClick={scrollToBottom} |
| 126 | + ref={ref} |
| 127 | + size={'small'} |
| 128 | + style={{ bottom: 18, position: 'absolute', right: 16, ...style }} |
| 129 | + > |
| 130 | + {text || 'Back to bottom'} |
| 131 | + </Button> |
| 132 | + ); |
| 133 | + |
| 134 | + if (render) return render(defauleDom, scrollToBottom, props); |
86 | 135 |
|
87 | | - /** |
88 | | - * @description |
89 | | - * 为了解决在使用了 ProChatProvider 的情况下,BackBottom 无法正常工作的问题 |
90 | | - */ |
91 | | - useEffect(() => { |
92 | | - setTimeout(() => { |
93 | | - (target as any)?.current?.scrollTo({ behavior: 'smooth', left: 0, top: scrollHeight }); |
94 | | - }, 16); |
95 | | - }, []); |
96 | | - |
97 | | - return ( |
98 | | - <Button |
99 | | - className={cx(styles, className)} |
100 | | - icon={<Icon icon={ListEnd} />} |
101 | | - onClick={scrollToBottom} |
102 | | - ref={ref} |
103 | | - size={'small'} |
104 | | - style={{ bottom: 16, position: 'absolute', right: 16, ...style }} |
105 | | - > |
106 | | - {text || 'Back to bottom'} |
107 | | - </Button> |
108 | | - ); |
109 | | - }, |
110 | | -); |
| 136 | + return defauleDom; |
| 137 | +}; |
111 | 138 |
|
112 | 139 | export default BackBottom; |
0 commit comments