Skip to content

Commit

Permalink
Refactor BackButton component: replace span with Button for improved …
Browse files Browse the repository at this point in the history
…accessibility and keyboard support
  • Loading branch information
agrullon95 committed Jan 6, 2025
1 parent 5c24c13 commit 8d11415
Showing 1 changed file with 10 additions and 28 deletions.
38 changes: 10 additions & 28 deletions packages/help-center/src/components/back-button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button } from '@wordpress/components';
import { Icon, chevronLeft } from '@wordpress/icons';
import { useI18n } from '@wordpress/react-i18n';
import { useEffect, useRef } from 'react';
import { useNavigate, useSearchParams, useLocation } from 'react-router-dom';

import './back-button.scss';
Expand All @@ -9,7 +9,6 @@ export const BackButton = () => {
const navigate = useNavigate();
const [ searchParams ] = useSearchParams();
const { pathname } = useLocation();
const backButtonRef = useRef< HTMLElement >( null );
const { __ } = useI18n();

function handleClick() {
Expand All @@ -22,37 +21,20 @@ export const BackButton = () => {
}
}

useEffect( () => {
const nodeRef = backButtonRef;
const onBackButtonKeyDown = ( e: KeyboardEvent ) => {
if ( e.key === 'Enter' ) {
handleClick();
}
};

if ( nodeRef.current ) {
nodeRef.current?.addEventListener( 'keydown', onBackButtonKeyDown );
// Fixes accessibility for back button
nodeRef.current?.removeAttribute( 'aria-hidden' );
}
return () => {
nodeRef.current?.removeEventListener( 'keydown', onBackButtonKeyDown );
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [] );

return (
<span className="back-button__help-center">
<Icon
aria-label={ __( 'Go Back', __i18n_text_domain__ ) }
role="button"
ref={ backButtonRef }
<Button
tabIndex={ 0 }
data-testid="back-button-icon"
onClick={ handleClick }
icon={ chevronLeft }
size={ 18 }
/>
className="back-button__help-center"
>
<Icon
aria-label={ __( 'Go Back', __i18n_text_domain__ ) }
icon={ chevronLeft }
size={ 18 }
/>
</Button>
</span>
);
};

0 comments on commit 8d11415

Please sign in to comment.