UNPKG

@wordpress/block-editor

Version:
8 lines (7 loc) 12.4 kB
{ "version": 3, "sources": ["../../../src/components/writing-flow/use-tab-nav.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { focus, isFormElement } from '@wordpress/dom';\nimport { TAB } from '@wordpress/keycodes';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { useRefEffect, useMergeRefs } from '@wordpress/compose';\nimport { useRef } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { store as blockEditorStore } from '../../store';\nimport { isInSameBlock, isInsideRootBlock } from '../../utils/dom';\nimport { unlock } from '../../lock-unlock';\n\nexport default function useTabNav() {\n\tconst containerRef = /** @type {typeof useRef<HTMLElement>} */ ( useRef )();\n\tconst focusCaptureBeforeRef = useRef();\n\tconst focusCaptureAfterRef = useRef();\n\n\tconst {\n\t\thasMultiSelection,\n\t\tgetSelectedBlockClientId,\n\t\tgetBlockCount,\n\t\tgetBlockOrder,\n\t\tgetLastFocus,\n\t\tgetSectionRootClientId,\n\t\tisZoomOut,\n\t} = unlock( useSelect( blockEditorStore ) );\n\tconst { setLastFocus } = unlock( useDispatch( blockEditorStore ) );\n\n\t// Reference that holds the a flag for enabling or disabling\n\t// capturing on the focus capture elements.\n\tconst noCaptureRef = useRef();\n\n\tfunction onFocusCapture( event ) {\n\t\tconst canvasElement =\n\t\t\tcontainerRef.current.ownerDocument === event.target.ownerDocument\n\t\t\t\t? containerRef.current\n\t\t\t\t: containerRef.current.ownerDocument.defaultView.frameElement;\n\n\t\t// Do not capture incoming focus if set by us in WritingFlow.\n\t\tif ( noCaptureRef.current ) {\n\t\t\tnoCaptureRef.current = null;\n\t\t} else if ( hasMultiSelection() ) {\n\t\t\tcontainerRef.current.focus();\n\t\t} else if ( getSelectedBlockClientId() ) {\n\t\t\tif ( getLastFocus()?.current ) {\n\t\t\t\tgetLastFocus().current.focus();\n\t\t\t} else {\n\t\t\t\t// Handles when the last focus has not been set yet, or has been cleared by new blocks being added via the inserter.\n\t\t\t\tcontainerRef.current\n\t\t\t\t\t.querySelector(\n\t\t\t\t\t\t`[data-block=\"${ getSelectedBlockClientId() }\"]`\n\t\t\t\t\t)\n\t\t\t\t\t.focus();\n\t\t\t}\n\t\t}\n\t\t// In \"compose\" mode without a selected ID, we want to place focus on the section root when tabbing to the canvas.\n\t\telse if ( isZoomOut() ) {\n\t\t\tconst sectionRootClientId = getSectionRootClientId();\n\t\t\tconst sectionBlocks = getBlockOrder( sectionRootClientId );\n\n\t\t\t// If we have section within the section root, focus the first one.\n\t\t\tif ( sectionBlocks.length ) {\n\t\t\t\tcontainerRef.current\n\t\t\t\t\t.querySelector( `[data-block=\"${ sectionBlocks[ 0 ] }\"]` )\n\t\t\t\t\t.focus();\n\t\t\t}\n\t\t\t// If we don't have any section blocks, focus the section root.\n\t\t\telse if ( sectionRootClientId ) {\n\t\t\t\tcontainerRef.current\n\t\t\t\t\t.querySelector( `[data-block=\"${ sectionRootClientId }\"]` )\n\t\t\t\t\t.focus();\n\t\t\t} else {\n\t\t\t\t// If we don't have any section root, focus the canvas.\n\t\t\t\tcanvasElement.focus();\n\t\t\t}\n\t\t} else {\n\t\t\tconst isBefore =\n\t\t\t\t// eslint-disable-next-line no-bitwise\n\t\t\t\tevent.target.compareDocumentPosition( canvasElement ) &\n\t\t\t\tevent.target.DOCUMENT_POSITION_FOLLOWING;\n\t\t\tconst tabbables = focus.tabbable.find( containerRef.current );\n\t\t\tif ( tabbables.length ) {\n\t\t\t\tconst next = isBefore\n\t\t\t\t\t? tabbables[ 0 ]\n\t\t\t\t\t: tabbables[ tabbables.length - 1 ];\n\t\t\t\tnext.focus();\n\t\t\t}\n\t\t}\n\t}\n\n\tconst before = (\n\t\t<div\n\t\t\tref={ focusCaptureBeforeRef }\n\t\t\ttabIndex=\"0\"\n\t\t\tonFocus={ onFocusCapture }\n\t\t/>\n\t);\n\n\tconst after = (\n\t\t<div\n\t\t\tref={ focusCaptureAfterRef }\n\t\t\ttabIndex=\"0\"\n\t\t\tonFocus={ onFocusCapture }\n\t\t/>\n\t);\n\n\tconst ref = useRefEffect( ( node ) => {\n\t\tfunction onKeyDown( event ) {\n\t\t\tif ( event.defaultPrevented ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// In Edit mode, Tab should focus the first tabbable element after\n\t\t\t// the content, which is normally the sidebar (with block controls)\n\t\t\t// and Shift+Tab should focus the first tabbable element before the\n\t\t\t// content, which is normally the block toolbar.\n\t\t\t// Arrow keys can be used, and Tab and arrow keys can be used in\n\t\t\t// Navigation mode (press Esc), to navigate through blocks.\n\t\t\tif ( event.keyCode !== TAB ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (\n\t\t\t\t// Bails in case the focus capture elements aren\u2019t present. They\n\t\t\t\t// may be omitted to avoid silent tab stops in preview mode.\n\t\t\t\t// See: https://github.com/WordPress/gutenberg/pull/59317\n\t\t\t\t! focusCaptureAfterRef.current ||\n\t\t\t\t! focusCaptureBeforeRef.current\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst { target, shiftKey: isShift } = event;\n\t\t\tconst direction = isShift ? 'findPrevious' : 'findNext';\n\t\t\tconst nextTabbable = focus.tabbable[ direction ]( target );\n\n\t\t\t// We want to constrain the tabbing to the block and its child blocks.\n\t\t\t// If the preceding form element is within a different block,\n\t\t\t// such as two sibling image blocks in the placeholder state,\n\t\t\t// we want shift + tab from the first form element to move to the image\n\t\t\t// block toolbar and not the previous image block's form element.\n\t\t\tconst currentBlock = target.closest( '[data-block]' );\n\t\t\tconst isElementPartOfSelectedBlock =\n\t\t\t\tcurrentBlock &&\n\t\t\t\tnextTabbable &&\n\t\t\t\t( isInSameBlock( currentBlock, nextTabbable ) ||\n\t\t\t\t\tisInsideRootBlock( currentBlock, nextTabbable ) );\n\n\t\t\t// Allow tabbing from the block wrapper to a form element,\n\t\t\t// and between form elements rendered in a block and its child blocks,\n\t\t\t// such as inside a placeholder. Form elements are generally\n\t\t\t// meant to be UI rather than part of the content. Ideally\n\t\t\t// these are not rendered in the content and perhaps in the\n\t\t\t// future they can be rendered in an iframe or shadow DOM.\n\t\t\tif (\n\t\t\t\tisFormElement( nextTabbable ) &&\n\t\t\t\tisElementPartOfSelectedBlock\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst next = isShift ? focusCaptureBeforeRef : focusCaptureAfterRef;\n\n\t\t\t// Disable focus capturing on the focus capture element, so it\n\t\t\t// doesn't refocus this block and so it allows default behaviour\n\t\t\t// (moving focus to the next tabbable element).\n\t\t\tnoCaptureRef.current = true;\n\n\t\t\t// Focusing the focus capture element, which is located above and\n\t\t\t// below the editor, should not scroll the page all the way up or\n\t\t\t// down.\n\t\t\tnext.current.focus( { preventScroll: true } );\n\t\t}\n\n\t\tfunction onFocusOut( event ) {\n\t\t\tsetLastFocus( { ...getLastFocus(), current: event.target } );\n\n\t\t\tconst { ownerDocument } = node;\n\n\t\t\t// If focus disappears due to there being no blocks, move focus to\n\t\t\t// the writing flow wrapper.\n\t\t\tif (\n\t\t\t\t! event.relatedTarget &&\n\t\t\t\tevent.target.hasAttribute( 'data-block' ) &&\n\t\t\t\townerDocument.activeElement === ownerDocument.body &&\n\t\t\t\tgetBlockCount() === 0\n\t\t\t) {\n\t\t\t\tnode.focus();\n\t\t\t}\n\t\t}\n\n\t\t// When tabbing back to an element in block list, this event handler prevents scrolling if the\n\t\t// focus capture divs (before/after) are outside of the viewport. (For example shift+tab back to a paragraph\n\t\t// when focus is on a sidebar element. This prevents the scrollable writing area from jumping either to the\n\t\t// top or bottom of the document.\n\t\t//\n\t\t// Note that it isn't possible to disable scrolling in the onFocus event. We need to intercept this\n\t\t// earlier in the keypress handler, and call focus( { preventScroll: true } ) instead.\n\t\t// https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/focus#parameters\n\t\tfunction preventScrollOnTab( event ) {\n\t\t\tif ( event.keyCode !== TAB ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( event.target?.getAttribute( 'role' ) === 'region' ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( containerRef.current === event.target ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst isShift = event.shiftKey;\n\t\t\tconst direction = isShift ? 'findPrevious' : 'findNext';\n\t\t\tconst target = focus.tabbable[ direction ]( event.target );\n\t\t\t// Only do something when the next tabbable is a focus capture div (before/after)\n\t\t\tif (\n\t\t\t\ttarget === focusCaptureBeforeRef.current ||\n\t\t\t\ttarget === focusCaptureAfterRef.current\n\t\t\t) {\n\t\t\t\tevent.preventDefault();\n\t\t\t\ttarget.focus( { preventScroll: true } );\n\t\t\t}\n\t\t}\n\n\t\tconst { ownerDocument } = node;\n\t\tconst { defaultView } = ownerDocument;\n\t\tdefaultView.addEventListener( 'keydown', preventScrollOnTab );\n\t\tnode.addEventListener( 'keydown', onKeyDown );\n\t\tnode.addEventListener( 'focusout', onFocusOut );\n\t\treturn () => {\n\t\t\tdefaultView.removeEventListener( 'keydown', preventScrollOnTab );\n\t\t\tnode.removeEventListener( 'keydown', onKeyDown );\n\t\t\tnode.removeEventListener( 'focusout', onFocusOut );\n\t\t};\n\t}, [] );\n\n\tconst mergedRefs = useMergeRefs( [ containerRef, ref ] );\n\n\treturn [ before, mergedRefs, after ];\n}\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,iBAAqC;AACrC,sBAAoB;AACpB,kBAAuC;AACvC,qBAA2C;AAC3C,qBAAuB;AAKvB,mBAA0C;AAC1C,IAAAA,cAAiD;AACjD,yBAAuB;AAiFrB;AA/Ea,SAAR,YAA6B;AACnC,QAAM;AAAA;AAAA,QAA2D,uBAAS;AAAA;AAC1E,QAAM,4BAAwB,uBAAO;AACrC,QAAM,2BAAuB,uBAAO;AAEpC,QAAM;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,Q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