How to use the @wordpress/dom.focus.tabbable function in @wordpress/dom

To help you get started, we’ve selected a few @wordpress/dom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github front / gutenberg-js / src / js / gutenberg-overrides / packages / editor / build-module / components / block-list / block.js View on Github external
focusTabbable () {
    const { initialPosition } = this.props;

    // Focus is captured by the wrapper node, so while focus transition
    // should only consider tabbables within editable display, since it
    // may be the wrapper itself or a side control which triggered the
    // focus event, don't unnecessary transition to an inner tabbable.
    if (this.wrapperNode.contains(document.activeElement)) {
      return;
    }

    // Find all tabbables within node.
    const textInputs = focus.tabbable.find(this.node).filter(isTextField);

    // If reversed (e.g. merge via backspace), use the last in the set of
    // tabbables.
    const isReverse = -1 === initialPosition;
    const target = (isReverse ? last : first)(textInputs);

    if (! target) {
      this.wrapperNode.focus();
      return;
    }

    target.focus();

    // In reverse case, need to explicitly place caret position.
    if (isReverse) {
      placeCaretAtHorizontalEdge(target, true);
github WordPress / gutenberg / packages / components / src / popover / index.js View on Github external
const focusTimeout = setTimeout( () => {
			if ( ! focusOnMount || ! contentRef.current ) {
				return;
			}

			if ( focusOnMount === 'firstElement' ) {
			// Find first tabbable node within content and shift focus, falling
			// back to the popover panel itself.
				const firstTabbable = focus.tabbable.find( contentRef.current )[ 0 ];
				if ( firstTabbable ) {
					firstTabbable.focus();
				} else {
					contentRef.current.focus();
				}

				return;
			}

			if ( focusOnMount === 'container' ) {
			// Focus the popover panel itself so items in the popover are easily
			// accessed via keyboard navigation.
				contentRef.current.focus();
			}
		}, 0 );
github WordPress / gutenberg / packages / block-editor / src / components / block-list / block.js View on Github external
// Focus is captured by the wrapper node, so while focus transition
		// should only consider tabbables within editable display, since it
		// may be the wrapper itself or a side control which triggered the
		// focus event, don't unnecessary transition to an inner tabbable.
		if ( wrapper.current.contains( document.activeElement ) ) {
			return;
		}

		if ( isNavigationMode ) {
			breadcrumb.current.focus();
			return;
		}

		// Find all tabbables within node.
		const textInputs = focus.tabbable
			.find( blockNodeRef.current )
			.filter( isTextField )
			// Exclude inner blocks
			.filter( ( node ) => ! ignoreInnerBlocks || isInsideRootBlock( blockNodeRef.current, node ) );

		// If reversed (e.g. merge via backspace), use the last in the set of
		// tabbables.
		const isReverse = -1 === initialPosition;
		const target = ( isReverse ? last : first )( textInputs );

		if ( ! target ) {
			wrapper.current.focus();
			return;
		}

		placeCaretAtHorizontalEdge( target, isReverse );
github WordPress / gutenberg / packages / block-editor / src / components / block-list / block.js View on Github external
const focusTabbable = ( ignoreInnerBlocks ) => {
		// Focus is captured by the wrapper node, so while focus transition
		// should only consider tabbables within editable display, since it
		// may be the wrapper itself or a side control which triggered the
		// focus event, don't unnecessary transition to an inner tabbable.
		if ( wrapper.current.contains( document.activeElement ) ) {
			return;
		}

		if ( isNavigationMode ) {
			breadcrumb.current.focus();
			return;
		}

		// Find all tabbables within node.
		const textInputs = focus.tabbable
			.find( blockNodeRef.current )
			.filter( isTextField )
			// Exclude inner blocks
			.filter( ( node ) => ! ignoreInnerBlocks || isInsideRootBlock( blockNodeRef.current, node ) );

		// If reversed (e.g. merge via backspace), use the last in the set of
		// tabbables.
		const isReverse = -1 === initialPosition;
		const target = ( isReverse ? last : first )( textInputs );

		if ( ! target ) {
			wrapper.current.focus();
			return;
		}

		placeCaretAtHorizontalEdge( target, isReverse );
github easydigitaldownloads / easy-digital-downloads / assets / js / admin / orders / order-overview / views / base.js View on Github external
handleTabBehavior( e ) {
		const { keyCode, shiftKey, target } = e;

		// 9 = TAB
		if ( 9 !== keyCode ) {
			return;
		}

		const tabbables = focus.tabbable.find( this.el );

		if ( ! tabbables.length ) {
			return;
		}

		const firstTabbable = tabbables[ 0 ];
		const lastTabbable = tabbables[ tabbables.length - 1 ];
		let toFocus;

		if ( shiftKey && target === firstTabbable ) {
			toFocus = lastTabbable;
		} else if ( ! shiftKey && target === lastTabbable ) {
			toFocus = firstTabbable;
		} else if ( shiftKey ) {
			toFocus = focus.tabbable.findPrevious( target );
		} else {
github WordPress / gutenberg / packages / block-editor / src / components / writing-flow / index.js View on Github external
* Browser constants
 */

const { getSelection, getComputedStyle } = window;

/**
 * Given an element, returns true if the element is a tabbable text field, or
 * false otherwise.
 *
 * @param {Element} element Element to test.
 *
 * @return {boolean} Whether element is a tabbable text field.
 */
const isTabbableTextField = overEvery( [
	isTextField,
	focus.tabbable.isTabbableIndex,
] );

/**
 * Returns true if the element should consider edge navigation upon a keyboard
 * event of the given directional key code, or false otherwise.
 *
 * @param {Element} element     HTML element to test.
 * @param {number}  keyCode     KeyboardEvent keyCode to test.
 * @param {boolean} hasModifier Whether a modifier is pressed.
 *
 * @return {boolean} Whether element should consider edge navigation.
 */
export function isNavigationCandidate( element, keyCode, hasModifier ) {
	const isVertical = ( keyCode === UP || keyCode === DOWN );

	// Currently, all elements support unmodified vertical navigation.
github WordPress / gutenberg / packages / block-editor / src / components / navigable-toolbar / index.js View on Github external
focusToolbar() {
		const tabbables = focus.tabbable.find( this.toolbar.current );
		if ( tabbables.length ) {
			tabbables[ 0 ].focus();
		}
	}
github easydigitaldownloads / easy-digital-downloads / assets / js / admin / orders / order-overview / views / base.js View on Github external
if ( ! tabbables.length ) {
			return;
		}

		const firstTabbable = tabbables[ 0 ];
		const lastTabbable = tabbables[ tabbables.length - 1 ];
		let toFocus;

		if ( shiftKey && target === firstTabbable ) {
			toFocus = lastTabbable;
		} else if ( ! shiftKey && target === lastTabbable ) {
			toFocus = firstTabbable;
		} else if ( shiftKey ) {
			toFocus = focus.tabbable.findPrevious( target );
		} else {
			toFocus = focus.tabbable.findNext( target );
		}

		if ( 'undefined' !== typeof toFocus ) {
			this.focusedEl = toFocus;
			this.focusedElCartetPos = toFocus.value.length;
		} else {
			this.focusedEl = null;
			this.focusedElCartetPos = 0;
		}
	},
github WordPress / gutenberg / packages / editor / src / components / block-toolbar / index.js View on Github external
focusContainer() {
		const tabbables = focus.tabbable.find( this.container.current );
		if ( tabbables.length ) {
			tabbables[ 0 ].focus();
		}
	}
github WordPress / gutenberg / packages / components / src / modal / frame.js View on Github external
focusFirstTabbable() {
		const tabbables = focus.tabbable.find( this.containerRef.current );
		if ( tabbables.length ) {
			tabbables[ 0 ].focus();
		}
	}