Skip to content

Commit

Permalink
fix focus loose and unable to close panel
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jan 7, 2025
1 parent 24bbb1d commit 722ac27
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/dataviews/src/components/dataviews-view-config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,25 @@ function PreviewOptions( {
onMenuOpenChange: ( isOpen: boolean ) => void;
activeOption?: string;
} ) {
const focusPreviewOptionsField = ( id: string ) => {
// Focus the visibility button to avoid focus loss.
// Our code is safe against the component being unmounted, so we don't need to worry about cleaning the timeout.
// eslint-disable-next-line @wordpress/react-no-unsafe-timeout
setTimeout( () => {
const element = document.querySelector(
`.dataviews-field-control__field-${ id } .dataviews-field-control__field-preview-options-button`
);
if ( element instanceof HTMLElement ) {
element.focus();
}
}, 50 );
};
return (
<Menu onOpenChange={ onMenuOpenChange }>
<Menu.TriggerButton
render={
<Button
className="dataviews-field-control__field-preview-options-button"
size="compact"
icon={ moreVertical }
label={ __( 'Preview' ) }
Expand All @@ -286,6 +300,7 @@ function PreviewOptions( {
checked={ id === activeOption }
onChange={ () => {
onChangePreviewOption?.( id );
focusPreviewOptionsField( id );
} }
>
<Menu.ItemLabel>{ label }</Menu.ItemLabel>
Expand Down

0 comments on commit 722ac27

Please sign in to comment.