Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix focus loss when navigating the guide component #40324

Merged
merged 2 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

AFAIK, this button was always hidden and useless.

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.