UNPKG

@kubit-ui-web/react-components

Version:

Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience

68 lines 3.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BackToTopControlled = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const stylesName_1 = require("../../constants/stylesName/stylesName"); const useManageState_1 = require("../../hooks/useManageState/useManageState"); const useStyles_1 = require("../../hooks/useStyles/useStyles"); const errorBoundary_1 = require("../../provider/errorBoundary/errorBoundary"); const fallbackComponent_1 = require("../../provider/errorBoundary/fallbackComponent"); const backToTopStandAlone_1 = require("./backToTopStandAlone"); const state_1 = require("./types/state"); const BackToTopControlledComponent = react_1.default.forwardRef(({ variant, ctv, stopElement, visibilityScrollOffset = 1, bottomPosition = 40, ...props }, ref) => { const styles = (0, useStyles_1.useStyles)(stylesName_1.STYLES_NAME.BACK_TO_TOP, variant, ctv); const innerRef = react_1.default.useRef(null); react_1.default.useImperativeHandle(ref, () => { return innerRef?.current; }, []); const { state, setRef } = (0, useManageState_1.useManageState)({ states: Object.values(state_1.BackToTopStateType), ref: innerRef, }); const handleScrollListener = react_1.default.useCallback(() => { const stop = stopElement?.current ?? document.querySelector('footer'); const button = innerRef?.current; if (!button) { return; } // set bottom position to 0 to calc the distance between the posible footer button.style.bottom = '0px'; let newBottomPosition = bottomPosition; // update button bottom position when stop is present if (stop) { const buttonBottom = button.getBoundingClientRect().bottom; const stopTop = stop.getBoundingClientRect().top; const distance = buttonBottom - stopTop; if (distance > 0) { newBottomPosition += distance; } } // update the distance and set the visibility button.style.bottom = `${newBottomPosition}px`; const display = window.scrollY >= visibilityScrollOffset; button.style.display = display ? 'flex' : 'none'; }, [bottomPosition, stopElement, visibilityScrollOffset]); react_1.default.useEffect(() => { handleScrollListener(); window.addEventListener('scroll', handleScrollListener); return () => { window.removeEventListener('scroll', handleScrollListener); }; }, [handleScrollListener]); return ((0, jsx_runtime_1.jsx)(backToTopStandAlone_1.BackToTopStandAlone, { ...props, ref: setRef, state: state, styles: styles })); }); BackToTopControlledComponent.displayName = 'BackToTopControlledComponent'; const BackToTopBoundary = (props, ref) => ((0, jsx_runtime_1.jsx)(errorBoundary_1.ErrorBoundary, { fallBackComponent: (0, jsx_runtime_1.jsx)(fallbackComponent_1.FallbackComponent, { children: (0, jsx_runtime_1.jsx)(backToTopStandAlone_1.BackToTopStandAlone, { ...props, ref: ref }) }), children: (0, jsx_runtime_1.jsx)(BackToTopControlledComponent, { ...props, ref: ref }) })); /** * @description * BackToTop component is a component that shows a button to scroll to the top of the page. * @param {React.PropsWithChildren<IBackToTopControlled<V>>} props * @returns {JSX.Element} */ const BackToTopControlled = react_1.default.forwardRef(BackToTopBoundary); exports.BackToTopControlled = BackToTopControlled; //# sourceMappingURL=backToTopControlled.js.map