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

Fix : Duplicative link control label and placeholder #66329

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ const PREFERENCE_KEY = 'linkControlSettingsDrawer';
* @param {WPLinkControlProps} props Component props.
*/
function LinkControl( {
searchInputPlaceholder,
searchInputLabel = null,
searchInputPlaceholder = null,
value,
settings = DEFAULT_LINK_SETTINGS,
onChange = noop,
Expand Down Expand Up @@ -390,6 +391,7 @@ function LinkControl( {
<LinkControlSearchInput
currentLink={ value }
className="block-editor-link-control__field block-editor-link-control__search-input"
label={ searchInputLabel }
placeholder={ searchInputPlaceholder }
value={ currentUrlInputValue }
withCreateSuggestion={ withCreateSuggestion }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const LinkControlSearchInput = forwardRef(
children,
currentLink = {},
className = null,
label = null,
placeholder = null,
withCreateSuggestion = false,
onCreateSuggestion = noop,
Expand Down Expand Up @@ -113,18 +114,16 @@ const LinkControlSearchInput = forwardRef(
}
};

const inputLabel = placeholder ?? __( 'Search or type URL' );

return (
<div className="block-editor-link-control__search-input-container">
<URLInput
disableSuggestions={ currentLink?.url === value }
label={ inputLabel }
label={ label ?? __( 'Search or type URL' ) }
hideLabelFromVision={ hideLabelFromVision }
className={ className }
value={ value }
onChange={ onInputChange }
placeholder={ inputLabel }
Vrishabhsk marked this conversation as resolved.
Show resolved Hide resolved
placeholder={ placeholder }
Copy link
Contributor

Choose a reason for hiding this comment

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

From the Why section of the PR that introduced the duplicative change, the rationale is that if there is no visible label then the placeholder text must match the label. It doesn't look like <InputBase /> handles this for us, so we could do it here:

  1. Set a const for the inputLabel above this block: const inputLabel = label ?? __( 'Search or type URL' )
  2. Apply the label: label={ inputLabel }
  3. Check for hideLabelFromVision before applying the placeholder: placeholder={ hideLabelFromVision ? inputLabel : placeholder }

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 We don't want to end up in a situation where we have the label and placeholder showing the same thing. That's the principle bug we have to address in this PR. Visual reference:

Image

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup - hopefully the sample code I added will:

  • Not have the placeholder and label match unless the consumer passes strings that match.
  • Force the placeholder to match the label only if hideLabelFromVision is true and we would want the placeholder and label to match.

__experimentalRenderSuggestions={
showSuggestions ? handleRenderSuggestions : null
}
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/specs/editor/blocks/links.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ test.describe( 'Links', () => {
// Change the URL.
// getByPlaceholder required in order to handle Link Control component
// managing focus onto other inputs within the control.
await page.getByPlaceholder( 'Search or type URL' ).fill( '' );
await page.getByLabel( 'Search or type URL' ).fill( '' );
await page.keyboard.type( '/handbook' );

// Submit the link.
Expand Down Expand Up @@ -349,7 +349,7 @@ test.describe( 'Links', () => {
// Change the URL.
// getByPlaceholder required in order to handle Link Control component
// managing focus onto other inputs within the control.
await page.getByPlaceholder( 'Search or type URL' ).fill( '' );
await page.getByLabel( 'Search or type URL' ).fill( '' );
await page.keyboard.type( '/handbook' );

// Submit the link.
Expand Down Expand Up @@ -679,7 +679,7 @@ test.describe( 'Links', () => {
// Change the URL.
// Note: getByPlaceholder required in order to handle Link Control component
// managing focus onto other inputs within the control.
await linkPopover.getByPlaceholder( 'Search or type URL' ).fill( '' );
await linkPopover.getByLabel( 'Search or type URL' ).fill( '' );
await page.keyboard.type( 'wordpress.org' );

// Save the link.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ test.describe( 'Registered sources', () => {
.getByRole( 'button', { name: 'Edit link', exact: true } )
.click();
await page
.getByPlaceholder( 'Search or type URL' )
.getByLabel( 'Search or type URL' )
.fill( '#url-custom-field-modified' );
await pageUtils.pressKeys( 'Enter' );

Expand Down Expand Up @@ -693,9 +693,7 @@ test.describe( 'Registered sources', () => {
await page
.getByRole( 'button', { name: 'Edit link', exact: true } )
.click();
await page
.getByPlaceholder( 'Search or type URL' )
.fill( testingImgSrc );
await page.getByLabel( 'Search or type URL' ).fill( testingImgSrc );
await pageUtils.pressKeys( 'Enter' );

// Check that the image url attribute didn't change and still uses the placeholder.
Expand Down
Loading