Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions packages/components/src/navigable-container/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ForwardedRef } from 'react';
/**
* WordPress dependencies
*/
import { forwardRef, useRef, useEffect, useCallback } from '@wordpress/element';
import { forwardRef, useRef, useEffect } from '@wordpress/element';
import { useMergeRefs } from '@wordpress/compose';
import { focus } from '@wordpress/dom';

Expand All @@ -29,6 +29,25 @@ function cycleValue( value: number, total: number, offset: number ) {
return nextValue;
}

function getFocusableContext(
container: HTMLElement | null,
target: Element,
tabbableOnly: boolean
) {
if ( ! container ) {
return null;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also drop the | null type and the !container guard, since the only place where getFocusableContext is called already checks for !container


const finder = tabbableOnly ? focus.tabbable : focus.focusable;
const focusables = finder.find( container );

const index = focusables.indexOf( target as HTMLElement );
if ( index > -1 && target ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that target is required and non-nullable, the && target guard is superfluous and can be removed

return { index, target, focusables };
}
return null;
}

function UnforwardedNavigableContainer(
{
children,
Expand All @@ -44,33 +63,6 @@ function UnforwardedNavigableContainer(
) {
const containerRef = useRef< HTMLDivElement | null >( null );

const getFocusableIndex = useCallback(
( focusables: Element[], target: Element ) => {
return focusables.indexOf( target );
},
[]
);

const getFocusableContext = useCallback(
( target: Element ) => {
if ( ! containerRef.current ) {
return null;
}

const finder = onlyBrowserTabstops
? focus.tabbable
: focus.focusable;
const focusables = finder.find( containerRef.current );

const index = getFocusableIndex( focusables, target );
if ( index > -1 && target ) {
return { index, target, focusables };
}
return null;
},
[ onlyBrowserTabstops, getFocusableIndex ]
);

useEffect( () => {
const container = containerRef.current;
if ( ! container ) {
Expand Down Expand Up @@ -114,7 +106,11 @@ function UnforwardedNavigableContainer(
return;
}

const context = getFocusableContext( activeElement );
const context = getFocusableContext(
container,
activeElement,
!! onlyBrowserTabstops
);
if ( ! context ) {
return;
}
Expand Down Expand Up @@ -151,7 +147,7 @@ function UnforwardedNavigableContainer(
stopNavigationEvents,
cycle,
onNavigate,
getFocusableContext,
onlyBrowserTabstops,
] );

const mergedRef = useMergeRefs( [ containerRef, ref ] );
Expand Down
Loading