UNPKG

@wordpress/block-editor

Version:
31 lines (30 loc) 1.2 kB
// packages/block-editor/src/components/list-view/use-list-view-scroll-into-view.js import { getScrollContainer } from "@wordpress/dom"; import { useLayoutEffect } from "@wordpress/element"; function useListViewScrollIntoView({ isSelected, selectedClientIds, rowItemRef }) { const isSingleSelection = selectedClientIds.length === 1; useLayoutEffect(() => { if (!isSelected || !isSingleSelection || !rowItemRef.current) { return; } const scrollContainer = getScrollContainer(rowItemRef.current); const { ownerDocument } = rowItemRef.current; const windowScroll = scrollContainer === ownerDocument.body || scrollContainer === ownerDocument.documentElement; if (windowScroll || !scrollContainer) { return; } const rowRect = rowItemRef.current.getBoundingClientRect(); const scrollContainerRect = scrollContainer.getBoundingClientRect(); if (rowRect.top < scrollContainerRect.top || rowRect.bottom > scrollContainerRect.bottom) { rowItemRef.current.scrollIntoView(); } }, [isSelected, isSingleSelection, rowItemRef]); } export { useListViewScrollIntoView as default }; //# sourceMappingURL=use-list-view-scroll-into-view.js.map