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

Do not control Nav Link item focus when focus within sidebar control #68044

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
30 changes: 23 additions & 7 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ function getMissingText( type ) {
* packages/block-library/src/navigation-submenu/edit.js
* Consider reuseing this components for both blocks.
*/
function Controls( { attributes, setAttributes, setIsLabelFieldFocused } ) {
function Controls( { attributes, setAttributes, setIsFocused } ) {
const { label, url, description, title, rel } = attributes;

const focusProps = {
onFocus: () => setIsFocused( true ),
onBlur: () => setIsFocused( false ),
};

return (
<PanelBody title={ __( 'Settings' ) }>
<TextControl
Expand All @@ -171,8 +177,7 @@ function Controls( { attributes, setAttributes, setIsLabelFieldFocused } ) {
} }
label={ __( 'Text' ) }
autoComplete="off"
onFocus={ () => setIsLabelFieldFocused( true ) }
onBlur={ () => setIsLabelFieldFocused( false ) }
{ ...focusProps }
/>
<TextControl
__nextHasNoMarginBottom
Expand All @@ -187,6 +192,7 @@ function Controls( { attributes, setAttributes, setIsLabelFieldFocused } ) {
} }
label={ __( 'Link' ) }
autoComplete="off"
{ ...focusProps }
/>
<TextareaControl
__nextHasNoMarginBottom
Expand All @@ -198,6 +204,7 @@ function Controls( { attributes, setAttributes, setIsLabelFieldFocused } ) {
help={ __(
'The description will be displayed in the menu if the current theme supports it.'
) }
{ ...focusProps }
/>
<TextControl
__nextHasNoMarginBottom
Expand All @@ -211,6 +218,7 @@ function Controls( { attributes, setAttributes, setIsLabelFieldFocused } ) {
help={ __(
'Additional information to help clarify the purpose of the link.'
) }
{ ...focusProps }
/>
<TextControl
__nextHasNoMarginBottom
Expand All @@ -224,6 +232,7 @@ function Controls( { attributes, setAttributes, setIsLabelFieldFocused } ) {
help={ __(
'The relationship of the linked URL as space-separated link types.'
) }
{ ...focusProps }
/>
</PanelBody>
);
Expand Down Expand Up @@ -266,7 +275,8 @@ export default function NavigationLinkEdit( {

// Change the label using inspector causes rich text to change focus on firefox.
// This is a workaround to keep the focus on the label field when label filed is focused we don't render the rich text.
const [ isLabelFieldFocused, setIsLabelFieldFocused ] = useState( false );
const [ isControlFieldFocused, setIsControlFieldFocused ] =
useState( false );

const {
isAtMaxNesting,
Expand Down Expand Up @@ -484,7 +494,7 @@ export default function NavigationLinkEdit( {
<Controls
attributes={ attributes }
setAttributes={ setAttributes }
setIsLabelFieldFocused={ setIsLabelFieldFocused }
setIsFocused={ setIsControlFieldFocused }
/>
</InspectorControls>
<div { ...blockProps }>
Expand All @@ -501,7 +511,7 @@ export default function NavigationLinkEdit( {
<>
{ ! isInvalid &&
! isDraft &&
! isLabelFieldFocused && (
! isControlFieldFocused && (
Comment on lines -504 to +514
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ellatrix Is it expected that we need to conditionally render RichText like this to avoid it "stealing" focus?

<>
<RichText
ref={ ref }
Expand Down Expand Up @@ -537,7 +547,7 @@ export default function NavigationLinkEdit( {
) }
{ ( isInvalid ||
isDraft ||
isLabelFieldFocused ) && (
isControlFieldFocused ) && (
<div className="wp-block-navigation-link__placeholder-text wp-block-navigation-link__label">
<Tooltip text={ tooltipText }>
<span
Expand Down Expand Up @@ -592,6 +602,12 @@ export default function NavigationLinkEdit( {
}

setIsLinkOpen( false );

// If a control is focused allow it to retain focus.
if ( isControlFieldFocused ) {
return;
}

if ( openedBy ) {
openedBy.focus();
setOpenedBy( null );
Expand Down
Loading