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

Add Additional CSS Classes setting to Link UI #67560

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
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
31 changes: 20 additions & 11 deletions packages/block-editor/src/components/link-control/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,26 @@ const LinkControlSettings = ( { value, onChange = noop, settings } ) => {
} );
};

const theSettings = settings.map( ( setting ) => (
<CheckboxControl
__nextHasNoMarginBottom
className="block-editor-link-control__setting"
key={ setting.id }
label={ setting.title }
onChange={ handleSettingChange( setting ) }
checked={ value ? !! value[ setting.id ] : false }
help={ setting?.help }
/>
) );
const theSettings = settings.map( ( setting ) =>
setting.render ? (
<div
key={ setting.id }
className="block-editor-link-control__setting"
>
{ setting.render( setting, value, onChange ) }
</div>
) : (
<CheckboxControl
__nextHasNoMarginBottom
className="block-editor-link-control__setting"
key={ setting.id }
label={ setting.title }
onChange={ handleSettingChange( setting ) }
checked={ value ? !! value[ setting.id ] : false }
help={ setting?.help }
/>
)
);

return (
<fieldset className="block-editor-link-control__settings">
Expand Down
10 changes: 7 additions & 3 deletions packages/block-editor/src/components/link-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,15 @@ $block-editor-link-control-number-of-actions: 1;
.block-editor-link-control__setting {
margin-bottom: 0;
flex: 1;
padding: $grid-unit-10 0 $grid-unit-10 $grid-unit-30;
padding: $grid-unit-10 $grid-unit-30;

.components-base-control__field {
display: flex; // don't allow label to wrap under checkbox.
.components-base-control:not(.components-input-control) {
.components-base-control__field {
display: flex; // don't allow label to wrap under checkbox.
}
}

.components-base-control__field {
.components-checkbox-control__label {
color: $gray-900;
}
Expand Down
1 change: 1 addition & 0 deletions packages/format-library/src/link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export const link = {
_id: 'id',
target: 'target',
rel: 'rel',
class: 'class',
},
__unstablePasteRule( value, { html, plainText } ) {
const pastedText = ( html || plainText )
Expand Down
74 changes: 72 additions & 2 deletions packages/format-library/src/link/inline.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/**
* WordPress dependencies
*/
import { useMemo, createInterpolateElement } from '@wordpress/element';
import {
useState,
useMemo,
createInterpolateElement,
} from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import { Popover } from '@wordpress/components';
import {
Popover,
__experimentalInputControl as InputControl,
CheckboxControl,
} from '@wordpress/components';
import { prependHTTP } from '@wordpress/url';
import {
create,
Expand All @@ -30,12 +38,71 @@ import { useDispatch, useSelect } from '@wordpress/data';
import { createLinkFormat, isValidHref, getFormatBoundary } from './utils';
import { link as settings } from './index';

const TogglableSettingComponent = ( { setting, value, onChange } ) => {
const hasValue = value ? value?.cssClasses?.length > 0 : false;
const [ inputVisible, setInputVisible ] = useState( hasValue );

const handleSettingChange = ( newValue ) => {
onChange( {
...value,
[ setting.id ]: newValue,
} );
};

const handleCheckboxChange = () => {
if ( inputVisible ) {
if ( hasValue ) {
// Reset the value.
handleSettingChange( '' );
}
setInputVisible( false );
} else {
setInputVisible( true );
}
};

return (
<div className="block-editor-link-control__toggleable-setting">
<CheckboxControl
__nextHasNoMarginBottom
label={ setting.title }
onChange={ handleCheckboxChange }
checked={ inputVisible || hasValue }
help={ setting?.help }
/>
{ inputVisible && (
<InputControl
label={ setting.title }
value={ value?.cssClasses }
onChange={ handleSettingChange }
help={ __( 'Separate multiple classes with spaces.' ) }
__unstableInputWidth="100%"
__next40pxDefaultSize
/>
Comment on lines +74 to +81
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm thinking we could do with some validation and sanitization at this point. There might be some prior art for this.

) }
</div>
);
};

const LINK_SETTINGS = [
...LinkControl.DEFAULT_LINK_SETTINGS,
{
id: 'nofollow',
title: __( 'Mark as nofollow' ),
},
{
id: 'cssClasses',
title: __( 'Additional CSS class(es)' ),
render: ( setting, value, onChange ) => {
return (
<TogglableSettingComponent
setting={ setting }
value={ value }
onChange={ onChange }
/>
);
},
},
];

function InlineLinkUI( {
Expand Down Expand Up @@ -78,8 +145,10 @@ function InlineLinkUI( {
opensInNewTab: activeAttributes.target === '_blank',
nofollow: activeAttributes.rel?.includes( 'nofollow' ),
title: richTextText,
cssClasses: activeAttributes.class,
} ),
[
activeAttributes.class,
activeAttributes.id,
activeAttributes.rel,
activeAttributes.target,
Expand Down Expand Up @@ -116,6 +185,7 @@ function InlineLinkUI( {
: undefined,
opensInNewWindow: nextValue.opensInNewTab,
nofollow: nextValue.nofollow,
cssClasses: nextValue.cssClasses,
} );

const newText = nextValue.title || newUrl;
Expand Down
6 changes: 6 additions & 0 deletions packages/format-library/src/link/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@
color: $alert-red;
}
}

.block-editor-link-control__toggleable-setting {
display: flex;
flex-direction: column;
gap: $grid-unit-20 0;
}
8 changes: 8 additions & 0 deletions packages/format-library/src/link/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function isValidHref( href ) {
* @param {string} options.id The ID of the link.
* @param {boolean} options.opensInNewWindow Whether this link will open in a new window.
* @param {boolean} options.nofollow Whether this link is marked as no follow relationship.
* @param {string} options.cssClasses The CSS classes to apply to the link.
* @return {Object} The final format object.
*/
export function createLinkFormat( {
Expand All @@ -94,6 +95,7 @@ export function createLinkFormat( {
id,
opensInNewWindow,
nofollow,
cssClasses,
} ) {
const format = {
type: 'core/link',
Expand Down Expand Up @@ -122,6 +124,12 @@ export function createLinkFormat( {
: 'nofollow';
}

const trimmedCssClasses = cssClasses?.trim();

if ( trimmedCssClasses?.length ) {
format.attributes.class = trimmedCssClasses;
}

return format;
}

Expand Down
Loading