Skip to content

Commit

Permalink
Fix focus loss issue
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Dec 5, 2024
1 parent 1754619 commit 045a3a9
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/dataviews/src/components/dataviews-view-config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ function FieldItem( {
onMoveUp?: () => void;
onMoveDown?: () => void;
} ) {
const focusVisibilityField = () => {
// 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-${ field.id } .dataviews-field-control__field-visibility-button`
);
if ( element instanceof HTMLElement ) {
element.focus();
}
}, 50 );
};

return (
<Item>
<HStack
Expand Down Expand Up @@ -291,7 +305,10 @@ function FieldItem( {
disabled={ ! field.enableHiding }
accessibleWhenDisabled
size="compact"
onClick={ onToggleVisibility }
onClick={ () => {
onToggleVisibility();
focusVisibilityField();
} }
icon={ isVisible ? unseen : seen }
label={
isVisible
Expand Down Expand Up @@ -346,17 +363,6 @@ function RegularFieldItem( {
)
: [ ...visibleFieldIds, field.id ],
} );
// 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-${ field.id } .dataviews-field-control__field-visibility-button`
);
if ( element instanceof HTMLElement ) {
element.focus();
}
}, 50 );
} }
onMoveUp={
index !== undefined
Expand Down

0 comments on commit 045a3a9

Please sign in to comment.