{
  "version": 3,
  "sources": ["../../../src/hooks/use-fixed-window-list/index.ts"],
  "sourcesContent": ["import { useState, useLayoutEffect, useRef } from '@wordpress/element';\nimport { getScrollContainer } from '@wordpress/dom';\nimport { PAGEUP, PAGEDOWN, HOME, END } from '@wordpress/keycodes';\nimport useEvent from '../use-event';\n\nconst DEFAULT_INIT_WINDOW_SIZE = 30;\n\ninterface FixedWindowList {\n\t/** Items visible in the viewport, as of the last rendered window */\n\tvisibleItems: number;\n\t/** Start index of the window */\n\tstart: number;\n\t/** End index of the window */\n\tend: number;\n\t/** Returns true if item is in the window */\n\titemInView: ( index: number ) => boolean;\n}\n\ninterface FixedWindowListOptions {\n\t/** Renders windowOverscan number of items before and after the calculated visible window. */\n\twindowOverscan?: number;\n\t/** When false avoids calculating the window size */\n\tuseWindowing?: boolean;\n\t/** Initial window size to use on first render before we can calculate the window size. */\n\tinitWindowSize?: number;\n\t/** Used to recalculate the window size when the expanded state of a list changes. */\n\texpandedState?: any;\n}\n\n/**\n *\n * @param elementRef Used to find the closest scroll container that contains element.\n * @param itemHeight Fixed item height in pixels\n * @param totalItems Total items in list\n * @param [options]  Options object\n * @return Array with the fixed window list and setter\n */\nexport default function useFixedWindowList(\n\telementRef: React.RefObject< HTMLElement >,\n\titemHeight: number,\n\ttotalItems: number,\n\toptions?: FixedWindowListOptions\n): [\n\tFixedWindowList,\n\tReact.Dispatch< React.SetStateAction< FixedWindowList > >,\n] {\n\tconst {\n\t\twindowOverscan,\n\t\tuseWindowing = true,\n\t\tinitWindowSize = DEFAULT_INIT_WINDOW_SIZE,\n\t\texpandedState,\n\t} = options ?? {};\n\n\tconst [ fixedListWindow, setFixedListWindow ] = useState< FixedWindowList >(\n\t\t{\n\t\t\tvisibleItems: initWindowSize,\n\t\t\tstart: 0,\n\t\t\tend: initWindowSize,\n\t\t\titemInView: ( index: number ) => {\n\t\t\t\treturn index >= 0 && index <= initWindowSize;\n\t\t\t},\n\t\t}\n\t);\n\n\t// Kept out of state so that measuring the list does not, on its own, force a\n\t// re-render. Only the rendered window does that.\n\tconst visibleItemsRef = useRef( initWindowSize );\n\n\t// The measuring effect re-runs whenever the list changes, so the effect\n\t// running can't on its own tell the very first measurement from later ones.\n\tconst isFirstMeasurementRef = useRef( true );\n\n\t// Stable identity, so the listeners below never have to be re-attached, and\n\t// reads the latest props on every call.\n\tconst measureWindow = useEvent( ( initRender?: boolean ) => {\n\t\tconst scrollContainer = getScrollContainer( elementRef.current );\n\t\tif ( ! scrollContainer ) {\n\t\t\treturn;\n\t\t}\n\t\tconst visibleItems = Math.ceil(\n\t\t\tscrollContainer.clientHeight / itemHeight\n\t\t);\n\t\tvisibleItemsRef.current = visibleItems;\n\t\tconst isFirstMeasurement = isFirstMeasurementRef.current;\n\t\tisFirstMeasurementRef.current = false;\n\t\t// Aim to keep opening list view fast, afterward we can optimize for scrolling.\n\t\tconst overscan = initRender\n\t\t\t? visibleItems\n\t\t\t: windowOverscan ?? visibleItems;\n\t\tconst firstViewableIndex = Math.floor(\n\t\t\tscrollContainer.scrollTop / itemHeight\n\t\t);\n\t\tconst start = Math.max( 0, firstViewableIndex - overscan );\n\t\tconst end = Math.min(\n\t\t\ttotalItems - 1,\n\t\t\tfirstViewableIndex + visibleItems + overscan\n\t\t);\n\t\tsetFixedListWindow( ( lastWindow ) => {\n\t\t\t// Rendering the window is the expensive part, and for the items in\n\t\t\t// `lastWindow` it is already paid for. When the next window has\n\t\t\t// nothing to add, keep the current one: a window that only shrinks\n\t\t\t// drops nodes that are known to be needed again and forces another\n\t\t\t// style recalculation, with nothing new to show for it.\n\t\t\tif ( lastWindow.start <= start && lastWindow.end >= end ) {\n\t\t\t\treturn lastWindow;\n\t\t\t}\n\t\t\t// The first window is rendered before the list can be measured, so\n\t\t\t// it is sized by `initWindowSize` rather than by the viewport, and\n\t\t\t// the measured window is normally wider than it by the overscan.\n\t\t\t// Nothing has painted yet though, so there is no scrolling for the\n\t\t\t// overscan to absorb: covering the visible items is enough, and\n\t\t\t// keeping that window saves a render pass while the list opens.\n\t\t\tif (\n\t\t\t\tisFirstMeasurement &&\n\t\t\t\tlastWindow.start <= firstViewableIndex &&\n\t\t\t\tlastWindow.end >= firstViewableIndex + visibleItems\n\t\t\t) {\n\t\t\t\treturn lastWindow;\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tvisibleItems,\n\t\t\t\tstart,\n\t\t\t\tend,\n\t\t\t\titemInView: ( index: number ) => {\n\t\t\t\t\treturn start <= index && index <= end;\n\t\t\t\t},\n\t\t\t};\n\t\t} );\n\t} );\n\n\tconst handleKeyDown = useEvent(\n\t\t( event: KeyboardEvent, scrollContainer: Element ) => {\n\t\t\tswitch ( event.keyCode ) {\n\t\t\t\tcase HOME: {\n\t\t\t\t\treturn scrollContainer.scrollTo( { top: 0 } );\n\t\t\t\t}\n\t\t\t\tcase END: {\n\t\t\t\t\treturn scrollContainer.scrollTo( {\n\t\t\t\t\t\ttop: totalItems * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\tcase PAGEUP: {\n\t\t\t\t\treturn scrollContainer.scrollTo( {\n\t\t\t\t\t\ttop:\n\t\t\t\t\t\t\tscrollContainer.scrollTop -\n\t\t\t\t\t\t\tvisibleItemsRef.current * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\tcase PAGEDOWN: {\n\t\t\t\t\treturn scrollContainer.scrollTo( {\n\t\t\t\t\t\ttop:\n\t\t\t\t\t\t\tscrollContainer.scrollTop +\n\t\t\t\t\t\t\tvisibleItemsRef.current * itemHeight,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t);\n\n\t// Measure whenever something that the window is derived from changes.\n\tuseLayoutEffect( () => {\n\t\tif ( ! useWindowing ) {\n\t\t\treturn;\n\t\t}\n\t\tmeasureWindow( true );\n\t}, [\n\t\tuseWindowing,\n\t\tmeasureWindow,\n\t\titemHeight,\n\t\ttotalItems,\n\t\twindowOverscan,\n\t\texpandedState,\n\t] );\n\n\t// Only ever attaches and detaches listeners. The deps are the things that\n\t// can change which element the scroll container is, or whether there is one\n\t// at all: a list that is too short to overflow has no scroll container to\n\t// attach to, and it grows into one by gaining items or by being expanded.\n\tuseLayoutEffect( () => {\n\t\tif ( ! useWindowing ) {\n\t\t\treturn;\n\t\t}\n\t\tconst scrollContainer = getScrollContainer( elementRef.current );\n\t\tif ( ! scrollContainer ) {\n\t\t\treturn;\n\t\t}\n\t\tconst { defaultView } = scrollContainer.ownerDocument;\n\t\t// `scroll` and `resize` already fire at about the rendering rate, so\n\t\t// there is nothing to gain from debouncing them.\n\t\tconst onMeasure = () => measureWindow();\n\t\tconst onKeyDown = ( event: KeyboardEvent ) =>\n\t\t\thandleKeyDown( event, scrollContainer );\n\n\t\tscrollContainer.addEventListener( 'scroll', onMeasure );\n\t\tdefaultView?.addEventListener( 'resize', onMeasure );\n\t\tdefaultView?.addEventListener( 'keydown', onKeyDown );\n\n\t\treturn () => {\n\t\t\tscrollContainer.removeEventListener( 'scroll', onMeasure );\n\t\t\tdefaultView?.removeEventListener( 'resize', onMeasure );\n\t\t\tdefaultView?.removeEventListener( 'keydown', onKeyDown );\n\t\t};\n\t}, [\n\t\tuseWindowing,\n\t\telementRef,\n\t\titemHeight,\n\t\ttotalItems,\n\t\texpandedState,\n\t\tmeasureWindow,\n\t\thandleKeyDown,\n\t] );\n\n\treturn [ fixedListWindow, setFixedListWindow ];\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAkD;AAClD,iBAAmC;AACnC,sBAA4C;AAC5C,uBAAqB;AAErB,IAAM,2BAA2B;AAgClB,SAAR,mBACN,YACA,YACA,YACA,SAIC;AACD,QAAM;AAAA,IACL;AAAA,IACA,eAAe;AAAA,IACf,iBAAiB;AAAA,IACjB;AAAA,EACD,IAAI,WAAW,CAAC;AAEhB,QAAM,CAAE,iBAAiB,kBAAmB,QAAI;AAAA,IAC/C;AA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