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

LinkControl: Add suggestions-only mode #23787

Closed
Show file tree
Hide file tree
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
45 changes: 26 additions & 19 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,18 @@ function LinkControl( {
showInitialSuggestions,
forceIsEditingLink,
createSuggestion,
onlySuggestions = false,
inputValue: propInputValue = '',
} ) {
const cancelableOnCreate = useRef();
const cancelableCreateSuggestion = useRef();

const wrapperNode = useRef();
const instanceId = useInstanceId( LinkControl );
const [ inputValue, setInputValue ] = useState(
const [ internalInputValue, setInternalInputValue ] = useState(
( value && value.url ) || ''
);
const currentInputValue = propInputValue || internalInputValue;
const [ isEditingLink, setIsEditingLink ] = useState(
forceIsEditingLink !== undefined
? forceIsEditingLink
Expand Down Expand Up @@ -249,7 +252,7 @@ function LinkControl( {
* @param {string} val Current value returned by the search.
*/
const onInputChange = ( val = '' ) => {
setInputValue( val );
setInternalInputValue( val );
};

const handleDirectEntry = noDirectEntry
Expand Down Expand Up @@ -283,10 +286,10 @@ function LinkControl( {

const handleEntitySearch = async ( val, args ) => {
let results = await Promise.all( [
handleDirectEntry( val ),
fetchSearchSuggestions( val, {
...( args.isInitialSuggestions ? { perPage: 3 } : {} ),
} ),
handleDirectEntry( val ),
] );

const couldBeURL = ! val.includes( ' ' );
Expand Down Expand Up @@ -460,7 +463,7 @@ function LinkControl( {
: sprintf(
/* translators: %s: search term. */
__( 'Search results for "%s"' ),
inputValue
currentInputValue
);

// VisuallyHidden rightly doesn't accept custom classNames
Expand All @@ -479,7 +482,7 @@ function LinkControl( {

return (
<div className="block-editor-link-control__search-results-wrapper">
{ searchResultsLabel }
{ ! onlySuggestions && searchResultsLabel }
<div
{ ...suggestionsListProps }
className={ resultsListClasses }
Expand All @@ -492,7 +495,7 @@ function LinkControl( {
) {
return (
<LinkControlSearchCreate
searchTerm={ inputValue }
searchTerm={ currentInputValue }
onClick={ async () => {
await handleOnCreate(
suggestion.title
Expand Down Expand Up @@ -534,7 +537,8 @@ function LinkControl( {
isURL={ directLinkEntryTypes.includes(
suggestion.type.toLowerCase()
) }
searchTerm={ inputValue }
searchTerm={ currentInputValue }
onlySuggestions={ onlySuggestions }
/>
);
} ) }
Expand All @@ -543,12 +547,12 @@ function LinkControl( {
);
};

const className = classnames( 'block-editor-link-control', {
'is-lite': onlySuggestions,
} );

return (
<div
tabIndex={ -1 }
ref={ wrapperNode }
className="block-editor-link-control"
>
<div tabIndex={ -1 } ref={ wrapperNode } className={ className }>
{ isResolvingLink && (
<div className="block-editor-link-control__loading">
<Spinner /> { __( 'Creating' ) }…
Expand All @@ -558,11 +562,11 @@ function LinkControl( {
{ ( isEditingLink || ! value ) && ! isResolvingLink && (
<LinkControlSearchInput
placeholder={ searchInputPlaceholder }
value={ inputValue }
value={ currentInputValue }
onChange={ onInputChange }
onSelect={ async ( suggestion ) => {
if ( CREATE_TYPE === suggestion.type ) {
await handleOnCreate( inputValue );
await handleOnCreate( currentInputValue );
} else if (
! noDirectEntry ||
Object.keys( suggestion ).length > 1
Expand All @@ -577,6 +581,7 @@ function LinkControl( {
fetchSuggestions={ getSearchHandler }
showInitialSuggestions={ showInitialSuggestions }
errorMessage={ errorMessage }
onlySuggestions={ onlySuggestions }
/>
) }

Expand Down Expand Up @@ -617,11 +622,13 @@ function LinkControl( {
</div>
</Fragment>
) }
<LinkControlSettingsDrawer
value={ value }
settings={ settings }
onChange={ onChange }
/>
{ ! onlySuggestions && (
<LinkControlSettingsDrawer
value={ value }
settings={ settings }
onChange={ onChange }
/>
) }
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';
import { Button, Icon } from '@wordpress/components';
import { createInterpolateElement } from '@wordpress/element';

export const LinkControlSearchCreate = ( {
searchTerm,
Expand All @@ -33,19 +32,16 @@ export const LinkControlSearchCreate = ( {
>
<Icon
className="block-editor-link-control__search-item-icon"
icon="insert"
size={ 18 }
icon="plus"
/>

<span className="block-editor-link-control__search-item-header">
<span className="block-editor-link-control__search-item-title">
{ createInterpolateElement(
sprintf(
/* translators: %s: search term. */
__( 'New page: <mark>%s</mark>' ),
searchTerm
),
{ mark: <mark /> }
) }
<mark>{ searchTerm }</mark>
</span>
<span className="block-editor-link-control__search-item-info">
{ __( 'Create a new page' ) }
</span>
</span>
</Button>
Expand Down
20 changes: 12 additions & 8 deletions packages/block-editor/src/components/link-control/search-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const LinkControlSearchInput = ( {
renderSuggestions,
fetchSuggestions,
showInitialSuggestions,
onlySuggestions,
errorMessage,
} ) => {
const [ selectedSuggestion, setSelectedSuggestion ] = useState();
Expand Down Expand Up @@ -54,19 +55,22 @@ const LinkControlSearchInput = ( {
placeholder={ placeholder ?? __( 'Search or type url' ) }
__experimentalRenderSuggestions={ renderSuggestions }
__experimentalFetchLinkSuggestions={ fetchSuggestions }
__experimentalOnlySuggestions={ onlySuggestions }
__experimentalHandleURLSuggestions={ true }
__experimentalShowInitialSuggestions={
showInitialSuggestions
}
/>
<div className="block-editor-link-control__search-actions">
<Button
type="submit"
label={ __( 'Submit' ) }
icon={ keyboardReturn }
className="block-editor-link-control__search-submit"
/>
</div>
{ ! onlySuggestions && (
<div className="block-editor-link-control__search-actions">
<Button
type="submit"
label={ __( 'Submit' ) }
icon={ keyboardReturn }
className="block-editor-link-control__search-submit"
/>
</div>
) }
</div>

{ errorMessage && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import classnames from 'classnames';
import { safeDecodeURI, filterURLForDisplay } from '@wordpress/url';
import { __ } from '@wordpress/i18n';
import { Button, TextHighlight } from '@wordpress/components';
import { Icon, globe } from '@wordpress/icons';

export const LinkControlSearchItem = ( {
itemProps,
Expand All @@ -29,12 +28,6 @@ export const LinkControlSearchItem = ( {
'is-entity': ! isURL,
} ) }
>
{ isURL && (
<Icon
className="block-editor-link-control__search-item-icon"
icon={ globe }
/>
) }
<span className="block-editor-link-control__search-item-header">
<span className="block-editor-link-control__search-item-title">
<TextHighlight
Expand All @@ -51,7 +44,7 @@ export const LinkControlSearchItem = ( {
safeDecodeURI( suggestion.url )
) ||
'' ) }
{ isURL && __( 'Press ENTER to add this link' ) }
{ isURL && __( 'Add a link to this URL' ) }
</span>
</span>
{ suggestion.type && (
Expand Down
55 changes: 55 additions & 0 deletions packages/block-editor/src/components/link-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ $block-editor-link-control-number-of-actions: 1;
}
}


.block-editor-link-control__loading {
margin: $grid-unit-20; // when only loading control is shown it requires it's own spacing.
display: flex;
Expand Down Expand Up @@ -279,7 +280,61 @@ $block-editor-link-control-number-of-actions: 1;
}
}


.block-editor-link-control__search-item-action {
margin-left: auto; // push to far right hand side
flex-shrink: 0;
}

.block-editor-link-control.is-lite {
.block-editor-link-control__search-input {
// Specificity override.
&.block-editor-link-control__search-input input[type="text"] {
width: calc(100% - #{$grid-unit-05*2});
margin: $grid-unit-05;
}
}

.block-editor-link-control__search-actions {
top: $grid-unit-05 + 1px + ( ( 40px - $button-size ) / 2 );
right: $grid-unit-05 + 1px + min($grid-unit-10, ( 40px - $button-size ) / 2);
}

.block-editor-link-control__search-input .components-spinner {
&.components-spinner {
top: $grid-unit-05 + 1px + ( ( 40px - $spinner-size ) / 2 );
right: $grid-unit-05 + 1px + ( $button-size * $block-editor-link-control-number-of-actions ) + ( ( $button-size - $spinner-size ) / 2 );
}
}

.block-editor-link-control__settings {
padding: $grid-unit-20 15px;
}

.block-editor-link-control__search-results-wrapper {
margin-top: 1px;

&::before,
&::after {
display: none;
}
}

.block-editor-link-control__search-results {
padding: $grid-unit-05;
}

.block-editor-link-control__search-item {
padding: 6px 10px;
border-radius: 2px;
}

.components-button + .block-editor-link-control__search-create {
margin-top: $block-selected-child-margin*2;
padding: inherit;

&::before {
top: -#{$block-selected-child-margin};
}
}
}
Loading