UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 2.93 kB
{ "version": 3, "sources": ["../../../src/components/block-popover/use-popover-scroll.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useRefEffect } from '@wordpress/compose';\nimport { getScrollContainer } from '@wordpress/dom';\n\nconst scrollContainerCache = new WeakMap();\n\n/**\n * Allow scrolling \"through\" popovers over the canvas. This is only called for\n * as long as the pointer is over a popover. Do not use React events because it\n * will bubble through portals.\n *\n * @param {Object} contentRef\n */\nfunction usePopoverScroll( contentRef ) {\n\tconst effect = useRefEffect(\n\t\t( node ) => {\n\t\t\tfunction onWheel( event ) {\n\t\t\t\tconst { deltaX, deltaY, target } = event;\n\t\t\t\tconst contentEl = contentRef.current;\n\t\t\t\tlet scrollContainer = scrollContainerCache.get( contentEl );\n\t\t\t\tif ( ! scrollContainer ) {\n\t\t\t\t\tscrollContainer = getScrollContainer( contentEl );\n\t\t\t\t\tscrollContainerCache.set( contentEl, scrollContainer );\n\t\t\t\t}\n\t\t\t\t// Finds a scrollable ancestor of the event\u2019s target. It's not cached because the\n\t\t\t\t// it may not remain scrollable due to popover position changes. The cache is also\n\t\t\t\t// less likely to be utilized because the target may be different every event.\n\t\t\t\tconst eventScrollContainer = getScrollContainer( target );\n\t\t\t\t// Scrolls \u201Cthrough\u201D the popover only if another contained scrollable area isn\u2019t\n\t\t\t\t// in front of it. This is to avoid scrolling both containers simultaneously.\n\t\t\t\tif ( ! node.contains( eventScrollContainer ) ) {\n\t\t\t\t\tscrollContainer.scrollBy( deltaX, deltaY );\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Tell the browser that we do not call event.preventDefault\n\t\t\t// See https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#improving_scrolling_performance_with_passive_listeners\n\t\t\tconst options = { passive: true };\n\t\t\tnode.addEventListener( 'wheel', onWheel, options );\n\t\t\treturn () => {\n\t\t\t\tnode.removeEventListener( 'wheel', onWheel, options );\n\t\t\t};\n\t\t},\n\t\t[ contentRef ]\n\t);\n\treturn contentRef ? effect : null;\n}\n\nexport default usePopoverScroll;\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAA6B;AAC7B,iBAAmC;AAEnC,IAAM,uBAAuB,oBAAI,QAAQ;AASzC,SAAS,iBAAkB,YAAa;AACvC,QAAM,aAAS;AAAA,IACd,CAAE,SAAU;AACX,eAAS,QAAS,OAAQ;AACzB,cAAM,EAAE,QAAQ,QAAQ,OAAO,IAAI;AACnC,cAAM,YAAY,WAAW;AAC7B,YAAI,kBAAkB,qBAAqB,IAAK,SAAU;AAC1D,YAAK,CAAE,iBAAkB;AACxB,gCAAkB,+BAAoB,SAAU;AAChD,+BAAqB,IAAK,WAAW,eAAgB;AAAA,QACtD;AAIA,cAAM,2BAAuB,+BAAoB,MAAO;AAGxD,YAAK,CAAE,KAAK,SAAU,oBAAqB,GAAI;AAC9C,0BAAgB,SAAU,QAAQ,MAAO;AAAA,QAC1C;AAAA,MACD;AAGA,YAAM,UAAU,EAAE,SAAS,KAAK;AAChC,WAAK,iBAAkB,SAAS,SAAS,OAAQ;AACjD,aAAO,MAAM;AACZ,aAAK,oBAAqB,SAAS,SAAS,OAAQ;AAAA,MACrD;AAAA,IACD;AAAA,IACA,CAAE,UAAW;AAAA,EACd;AACA,SAAO,aAAa,SAAS;AAC9B;AAEA,IAAO,6BAAQ;", "names": [] }