Skip to content

Commit

Permalink
Guide: Place focus on the guide's container instead of its first tabb…
Browse files Browse the repository at this point in the history
…able (#52300)

* Guide: Place focus on the guide's container instead of its first tabbable

* Update CHANGELOG
  • Loading branch information
noisysocks authored Jul 5, 2023
1 parent 7d7617a commit 58ba018
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- `Modal`: Add small top padding to the content so that avoid cutting off the visible outline when hovering items ([#51829](https://github.com/WordPress/gutenberg/pull/51829)).
- `DropdownMenu`: fix icon style when dashicon is used ([#43574](https://github.com/WordPress/gutenberg/pull/43574)).
- `UnitControl`: Fix crash when certain units are used ([#52211](https://github.com/WordPress/gutenberg/pull/52211)).
- `Guide`: Place focus on the guide's container instead of its first tabbable ([#52300](https://github.com/WordPress/gutenberg/pull/52300)).

## 25.2.0 (2023-06-23)

Expand Down
23 changes: 10 additions & 13 deletions packages/components/src/guide/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import classnames from 'classnames';
import { useState, useEffect, Children, useRef } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';
import { __ } from '@wordpress/i18n';
import { focus } from '@wordpress/dom';

/**
* Internal dependencies
Expand Down Expand Up @@ -59,9 +58,17 @@ function Guide( {
onFinish,
pages = [],
}: GuideProps ) {
const guideContainer = useRef< HTMLDivElement >( null );
const ref = useRef< HTMLDivElement >( null );
const [ currentPage, setCurrentPage ] = useState( 0 );

useEffect( () => {
// Place focus at the top of the guide on mount and when the page changes.
const frame = ref.current?.querySelector( '.components-guide' );
if ( frame instanceof HTMLElement ) {
frame.focus();
}
}, [ currentPage ] );

useEffect( () => {
if ( Children.count( children ) ) {
deprecated( 'Passing children to <Guide>', {
Expand All @@ -71,16 +78,6 @@ 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.
if ( guideContainer.current ) {
(
focus.tabbable.find( guideContainer.current ) as HTMLElement[]
)[ 0 ]?.focus();
}
}, [ currentPage ] );

if ( Children.count( children ) ) {
pages =
Children.map( children, ( child ) => ( {
Expand Down Expand Up @@ -124,7 +121,7 @@ function Guide( {
event.preventDefault();
}
} }
ref={ guideContainer }
ref={ ref }
>
<div className="components-guide__container">
<div className="components-guide__page">
Expand Down

1 comment on commit 58ba018

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 58ba018.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5461045194
📝 Reported issues:

Please sign in to comment.