{"version":3,"file":"container.mjs","names":[],"sources":["../../src/navigable-container/container.tsx"],"sourcesContent":["import type { ForwardedRef } from 'react';\nimport { forwardRef, useRef, useEffect } from '@wordpress/element';\nimport { useMergeRefs } from '@wordpress/compose';\nimport { focus } from '@wordpress/dom';\nimport type { NavigableContainerProps } from './types';\n\nconst noop = () => {};\nconst MENU_ITEM_ROLES = [ 'menuitem', 'menuitemradio', 'menuitemcheckbox' ];\n\nfunction cycleValue( value: number, total: number, offset: number ) {\n\tconst nextValue = value + offset;\n\tif ( nextValue < 0 ) {\n\t\treturn total + nextValue;\n\t} else if ( nextValue >= total ) {\n\t\treturn nextValue - total;\n\t}\n\n\treturn nextValue;\n}\n\nfunction getFocusableContext(\n\tcontainer: HTMLElement,\n\ttarget: Element,\n\ttabbableOnly: boolean\n) {\n\tconst finder = tabbableOnly ? focus.tabbable : focus.focusable;\n\tconst focusables = finder.find( container );\n\n\tconst index = focusables.indexOf( target as HTMLElement );\n\tif ( index > -1 || ( ! tabbableOnly && target === container ) ) {\n\t\treturn { index, target, focusables };\n\t}\n\treturn null;\n}\n\nfunction UnforwardedNavigableContainer(\n\t{\n\t\tchildren,\n\t\tstopNavigationEvents,\n\t\teventToOffset,\n\t\tonNavigate = noop,\n\t\tonKeyDown,\n\t\tcycle = true,\n\t\tonlyBrowserTabstops,\n\t\t...restProps\n\t}: NavigableContainerProps,\n\tref: ForwardedRef< HTMLDivElement >\n) {\n\tconst containerRef = useRef< HTMLDivElement | null >( null );\n\n\tuseEffect( () => {\n\t\tconst container = containerRef.current;\n\t\tif ( ! container ) {\n\t\t\treturn;\n\t\t}\n\n\t\tfunction handleKeyDown( event: KeyboardEvent ) {\n\t\t\tif ( onKeyDown ) {\n\t\t\t\tonKeyDown( event );\n\t\t\t}\n\n\t\t\tconst offset = eventToOffset( event );\n\n\t\t\t// eventToOffset returns undefined if the event is not handled by the component.\n\t\t\tif ( offset !== undefined && stopNavigationEvents ) {\n\t\t\t\t// Prevents arrow key handlers bound to the document directly interfering.\n\t\t\t\tevent.stopImmediatePropagation();\n\n\t\t\t\t// When navigating a collection of items, prevent scroll containers\n\t\t\t\t// from scrolling. The preventDefault also prevents Voiceover from\n\t\t\t\t// 'handling' the event, as voiceover will try to use arrow keys\n\t\t\t\t// for highlighting text.\n\t\t\t\tconst targetRole = (\n\t\t\t\t\tevent.target as HTMLDivElement | null\n\t\t\t\t )?.getAttribute( 'role' );\n\t\t\t\tconst targetHasMenuItemRole =\n\t\t\t\t\t!! targetRole && MENU_ITEM_ROLES.includes( targetRole );\n\n\t\t\t\tif (\n\t\t\t\t\ttargetHasMenuItemRole ||\n\t\t\t\t\t( ! onlyBrowserTabstops && event.target === container )\n\t\t\t\t) {\n\t\t\t\t\tevent.preventDefault();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif ( ! offset ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst activeElement = ( event.target as HTMLElement | null )\n\t\t\t\t?.ownerDocument?.activeElement;\n\t\t\tif ( ! activeElement ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst context = getFocusableContext(\n\t\t\t\tcontainer!,\n\t\t\t\tactiveElement,\n\t\t\t\t!! onlyBrowserTabstops\n\t\t\t);\n\t\t\tif ( ! context ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst { index, focusables } = context;\n\t\t\tlet nextIndex = index + offset;\n\t\t\tif ( index === -1 ) {\n\t\t\t\tnextIndex = offset > 0 ? 0 : focusables.length - 1;\n\t\t\t} else if ( cycle ) {\n\t\t\t\tnextIndex = cycleValue( index, focusables.length, offset );\n\t\t\t}\n\n\t\t\tif ( nextIndex >= 0 && nextIndex < focusables.length ) {\n\t\t\t\tfocusables[ nextIndex ].focus();\n\t\t\t\tonNavigate( nextIndex, focusables[ nextIndex ] as HTMLElement );\n\n\t\t\t\t// `preventDefault()` on tab to avoid having the browser move the focus\n\t\t\t\t// after this component has already moved it.\n\t\t\t\tif ( event.code === 'Tab' ) {\n\t\t\t\t\tevent.preventDefault();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// We use DOM event listeners instead of React event listeners\n\t\t// because we want to catch events from the underlying DOM tree.\n\t\t// The React Tree can be different from the DOM tree when using\n\t\t// portals. Block Toolbars for instance are rendered in a separate\n\t\t// React Trees.\n\t\tcontainer.addEventListener( 'keydown', handleKeyDown );\n\t\treturn () => {\n\t\t\tcontainer.removeEventListener( 'keydown', handleKeyDown );\n\t\t};\n\t}, [\n\t\tonKeyDown,\n\t\teventToOffset,\n\t\tstopNavigationEvents,\n\t\tcycle,\n\t\tonNavigate,\n\t\tonlyBrowserTabstops,\n\t] );\n\n\tconst mergedRef = useMergeRefs( [ containerRef, ref ] );\n\n\treturn (\n\t\t<div ref={ mergedRef } { ...restProps }>\n\t\t\t{ children }\n\t\t</div>\n\t);\n}\n\nconst NavigableContainer = forwardRef( UnforwardedNavigableContainer );\nNavigableContainer.displayName = 'NavigableContainer';\n\nexport default NavigableContainer;\n"],"mappings":";;;;;AAMA,MAAM,aAAa,CAAC;AACpB,MAAM,kBAAkB;CAAE;CAAY;CAAiB;AAAmB;AAE1E,SAAS,WAAY,OAAe,OAAe,QAAiB;CACnE,MAAM,YAAY,QAAQ;CAC1B,IAAK,YAAY,GAChB,OAAO,QAAQ;MACT,IAAK,aAAa,OACxB,OAAO,YAAY;CAGpB,OAAO;AACR;AAEA,SAAS,oBACR,WACA,QACA,cACC;CAED,MAAM,cADS,eAAe,MAAM,WAAW,MAAM,UAAA,CAC3B,KAAM,SAAU;CAE1C,MAAM,QAAQ,WAAW,QAAS,MAAsB;CACxD,IAAK,QAAQ,MAAQ,CAAE,gBAAgB,WAAW,WACjD,OAAO;EAAE;EAAO;EAAQ;CAAW;CAEpC,OAAO;AACR;AAEA,SAAS,8BACR,EACC,UACA,sBACA,eACA,aAAa,MACb,WACA,QAAQ,MACR,qBACA,GAAG,aAEJ,KACC;CACD,MAAM,eAAe,OAAiC,IAAK;CAE3D,gBAAiB;EAChB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