@wordpress/block-editor
Version:
8 lines (7 loc) • 2.44 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/components/list-view/use-list-view-scroll-into-view.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { getScrollContainer } from '@wordpress/dom';\nimport { useLayoutEffect } from '@wordpress/element';\n\nexport default function useListViewScrollIntoView( {\n\tisSelected,\n\tselectedClientIds,\n\trowItemRef,\n} ) {\n\tconst isSingleSelection = selectedClientIds.length === 1;\n\n\tuseLayoutEffect( () => {\n\t\t// Skip scrolling into view if this particular block isn't selected,\n\t\t// or if more than one block is selected overall. This is to avoid\n\t\t// scrolling the view in a multi selection where the user has intentionally\n\t\t// selected multiple blocks within the list view, but the initially\n\t\t// selected block may be out of view.\n\t\tif ( ! isSelected || ! isSingleSelection || ! rowItemRef.current ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst scrollContainer = getScrollContainer( rowItemRef.current );\n\t\tconst { ownerDocument } = rowItemRef.current;\n\n\t\tconst windowScroll =\n\t\t\tscrollContainer === ownerDocument.body ||\n\t\t\tscrollContainer === ownerDocument.documentElement;\n\n\t\t// If the there is no scroll container, of if the scroll container is the window,\n\t\t// do not scroll into view, as the block is already in view.\n\t\tif ( windowScroll || ! scrollContainer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst rowRect = rowItemRef.current.getBoundingClientRect();\n\t\tconst scrollContainerRect = scrollContainer.getBoundingClientRect();\n\n\t\t// If the selected block is not currently visible, scroll to it.\n\t\tif (\n\t\t\trowRect.top < scrollContainerRect.top ||\n\t\t\trowRect.bottom > scrollContainerRect.bottom\n\t\t) {\n\t\t\trowItemRef.current.scrollIntoView();\n\t\t}\n\t}, [ isSelected, isSingleSelection, rowItemRef ] );\n}\n"],
"mappings": ";AAGA,SAAS,0BAA0B;AACnC,SAAS,uBAAuB;AAEjB,SAAR,0BAA4C;AAAA,EAClD;AAAA,EACA;AAAA,EACA;AACD,GAAI;AACH,QAAM,oBAAoB,kBAAkB,WAAW;AAEvD,kBAAiB,MAAM;AAMtB,QAAK,CAAE,cAAc,CAAE,qBAAqB,CAAE,WAAW,SAAU;AAClE;AAAA,IACD;AAEA,UAAM,kBAAkB,mBAAoB,WAAW,OAAQ;AAC/D,UAAM,EAAE,cAAc,IAAI,WAAW;AAErC,UAAM,eACL,oBAAoB,cAAc,QAClC,oBAAoB,cAAc;AAInC,QAAK,gBAAgB,CAAE,iBAAkB;AACxC;AAAA,IACD;AAEA,UAAM,UAAU,WAAW,QAAQ,sBAAsB;AACzD,UAAM,sBAAsB,gBAAgB,sBAAsB;AAGlE,QACC,QAAQ,MAAM,oBAAoB,OAClC,QAAQ,SAAS,oBAAoB,QACpC;AACD,iBAAW,QAAQ,eAAe;AAAA,IACnC;AAAA,EACD,GAAG,CAAE,YAAY,mBAAmB,UAAW,CAAE;AAClD;",
"names": []
}