|
| 1 | +import type { DTransitionState } from '../_transition'; |
| 2 | +import type { DFabButtonProps } from './FabButton'; |
| 3 | +import type { DElementSelector } from '@react-devui/hooks/useElement'; |
| 4 | + |
| 5 | +import React, { useRef, useState } from 'react'; |
| 6 | + |
| 7 | +import { useElement, useEvent, useIsomorphicLayoutEffect, useResize } from '@react-devui/hooks'; |
| 8 | +import { VerticalAlignTopOutlined } from '@react-devui/icons'; |
| 9 | +import { checkNodeExist, scrollTo } from '@react-devui/utils'; |
| 10 | + |
| 11 | +import { useComponentConfig, useLayout } from '../../hooks'; |
| 12 | +import { registerComponentMate, TTANSITION_DURING_BASE } from '../../utils'; |
| 13 | +import { DTransition } from '../_transition'; |
| 14 | +import { DFabButton } from './FabButton'; |
| 15 | + |
| 16 | +export interface DFabBacktopProps extends DFabButtonProps { |
| 17 | + dPage?: DElementSelector; |
| 18 | + dDistance?: number; |
| 19 | + dScrollBehavior?: 'instant' | 'smooth'; |
| 20 | +} |
| 21 | + |
| 22 | +const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DFab.Backtop' as const }); |
| 23 | +function FabBacktop(props: DFabBacktopProps, ref: React.ForwardedRef<HTMLButtonElement>): JSX.Element | null { |
| 24 | + const { |
| 25 | + children, |
| 26 | + dPage, |
| 27 | + dDistance = 400, |
| 28 | + dScrollBehavior = 'instant', |
| 29 | + |
| 30 | + ...restProps |
| 31 | + } = useComponentConfig(COMPONENT_NAME, props); |
| 32 | + |
| 33 | + //#region Context |
| 34 | + const { dScrollEl, dResizeEl } = useLayout(); |
| 35 | + //#endregion |
| 36 | + |
| 37 | + const dataRef = useRef<{ |
| 38 | + clearTid?: () => void; |
| 39 | + }>({}); |
| 40 | + |
| 41 | + const pageEl = useElement(dPage ?? dScrollEl); |
| 42 | + const resizeEl = useElement(dResizeEl); |
| 43 | + |
| 44 | + const [visible, setVisible] = useState(false); |
| 45 | + |
| 46 | + const transitionStyles: Partial<Record<DTransitionState, React.CSSProperties>> = { |
| 47 | + enter: { opacity: 0 }, |
| 48 | + entering: { |
| 49 | + transition: ['opacity'].map((attr) => `${attr} ${TTANSITION_DURING_BASE}ms linear`).join(', '), |
| 50 | + }, |
| 51 | + leaving: { |
| 52 | + opacity: 0, |
| 53 | + transition: ['opacity'].map((attr) => `${attr} ${TTANSITION_DURING_BASE}ms linear`).join(', '), |
| 54 | + }, |
| 55 | + leaved: { display: 'none' }, |
| 56 | + }; |
| 57 | + |
| 58 | + const updateBackTop = () => { |
| 59 | + if (!pageEl) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + setVisible(pageEl.scrollTop >= dDistance); |
| 64 | + }; |
| 65 | + useIsomorphicLayoutEffect(() => { |
| 66 | + updateBackTop(); |
| 67 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 68 | + }, []); |
| 69 | + |
| 70 | + useResize(resizeEl, updateBackTop); |
| 71 | + useEvent(pageEl, 'scroll', updateBackTop, { passive: true }); |
| 72 | + |
| 73 | + return ( |
| 74 | + <DTransition dIn={visible} dDuring={TTANSITION_DURING_BASE}> |
| 75 | + {(state) => ( |
| 76 | + <DFabButton |
| 77 | + {...restProps} |
| 78 | + ref={ref} |
| 79 | + style={{ |
| 80 | + ...restProps.style, |
| 81 | + ...transitionStyles[state], |
| 82 | + }} |
| 83 | + onClick={(e) => { |
| 84 | + restProps.onClick?.(e); |
| 85 | + |
| 86 | + if (pageEl) { |
| 87 | + dataRef.current.clearTid?.(); |
| 88 | + dataRef.current.clearTid = scrollTo(pageEl, { |
| 89 | + top: 0, |
| 90 | + behavior: dScrollBehavior, |
| 91 | + }); |
| 92 | + } |
| 93 | + }} |
| 94 | + > |
| 95 | + {checkNodeExist(children) ? children : <VerticalAlignTopOutlined />} |
| 96 | + </DFabButton> |
| 97 | + )} |
| 98 | + </DTransition> |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +export const DFabBacktop = React.forwardRef(FabBacktop); |
0 commit comments