UNPKG

@awsui/components-react

Version:

AWS UI is a collection of [React](https://reactjs.org/) components that help create intuitive, responsive, and accessible user experiences for web applications. It is developed by Amazon Web Services (AWS). This work is available under the terms of the [A

47 lines (46 loc) 1.71 kB
import { useEffect, useRef } from 'react'; export function useScrollSync(refs, disabled) { if (disabled === void 0) { disabled = false; } var activePanelIndex = useRef(-1); useEffect(function () { if (disabled) { return; } var listeners = []; var onScrollEvent = function (element, index) { handler(element, index); }; refs.forEach(function (ref, index) { var element = ref.current; if (element) { var listener = onScrollEvent.bind(null, element, index); element.addEventListener('scroll', listener); listeners.push({ element: element, listener: listener }); } }); return function () { listeners.forEach(function (_a) { var element = _a.element, listener = _a.listener; element.removeEventListener('scroll', listener); }); }; }, []); var handler = function (activePanel, index) { if (activePanelIndex.current === -1 || activePanelIndex.current === index) { requestAnimationFrame(function () { activePanelIndex.current = index; if (activePanel) { refs.forEach(function (ref) { var panel = ref.current; if (panel && panel !== activePanel) { panel.scrollLeft = activePanel.scrollLeft; } }); } requestAnimationFrame(function () { activePanelIndex.current = -1; }); }); } }; }