Skip to content

Commit

Permalink
Fix focus loss when navigating the guide component (#40324)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Apr 15, 2022
1 parent e80748e commit 3492745
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 92 deletions.
26 changes: 0 additions & 26 deletions packages/components/src/guide/finish-button.js

This file was deleted.

25 changes: 12 additions & 13 deletions packages/components/src/guide/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState, useEffect, Children } from '@wordpress/element';
import { useState, useEffect, Children, useRef } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';
import { __ } from '@wordpress/i18n';
import { LEFT, RIGHT } from '@wordpress/keycodes';
import { focus } from '@wordpress/dom';

/**
* Internal dependencies
*/
import Modal from '../modal';
import Button from '../button';
import PageControl from './page-control';
import FinishButton from './finish-button';

export default function Guide( {
children,
Expand All @@ -27,6 +27,7 @@ export default function Guide( {
onFinish,
pages = [],
} ) {
const guideContainer = useRef();
const [ currentPage, setCurrentPage ] = useState( 0 );

useEffect( () => {
Expand All @@ -38,6 +39,12 @@ export default function Guide( {
}
}, [ children ] );

useEffect( () => {
// Each time we change the current page, start from the first element of the page.
// This also solves any focus loss that can happen.
focus.tabbable.find( guideContainer.current )?.[ 0 ]?.focus();
}, [ currentPage ] );

if ( Children.count( children ) ) {
pages = Children.map( children, ( child ) => ( { content: child } ) );
}
Expand Down Expand Up @@ -73,6 +80,7 @@ export default function Guide( {
goForward();
}
} }
ref={ guideContainer }
>
<div className="components-guide__container">
<div className="components-guide__page">
Expand All @@ -87,15 +95,6 @@ export default function Guide( {
) }

{ pages[ currentPage ].content }

{ ! canGoForward && (
<FinishButton
className="components-guide__inline-finish-button"
onClick={ onFinish }
>
{ finishButtonText || __( 'Finish' ) }
</FinishButton>
) }
</div>

<div className="components-guide__footer">
Expand All @@ -116,12 +115,12 @@ export default function Guide( {
</Button>
) }
{ ! canGoForward && (
<FinishButton
<Button
className="components-guide__finish-button"
onClick={ onFinish }
>
{ finishButtonText || __( 'Finish' ) }
</FinishButton>
</Button>
) }
</div>
</div>
Expand Down
4 changes: 0 additions & 4 deletions packages/components/src/guide/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,4 @@
&.components-guide__finish-button {
right: $grid-unit-40;
}

&.components-guide__inline-finish-button {
display: none;
}
}
49 changes: 0 additions & 49 deletions packages/components/src/guide/test/finish-button.js

This file was deleted.

0 comments on commit 3492745

Please sign in to comment.