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 to Link UI #67560

Open
wants to merge 4 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
24 changes: 23 additions & 1 deletion packages/format-library/src/link/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import { 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,
} from '@wordpress/components';
import { prependHTTP } from '@wordpress/url';
import {
create,
Expand Down Expand Up @@ -36,6 +39,22 @@ const LINK_SETTINGS = [
id: 'nofollow',
title: __( 'Mark as nofollow' ),
},
{
id: 'cssClasses',
title: __( 'Additional CSS class(es)' ),
render: ( setting, value, onChange ) => (
<InputControl
label={ setting.title }
value={ value?.cssClasses }
onChange={ ( cssClasses ) =>
onChange( { ...value, cssClasses } )
}
help={ __( 'Separate multiple classes with spaces.' ) }
__unstableInputWidth="100%"
__next40pxDefaultSize
/>
),
},
];

function InlineLinkUI( {
Expand Down Expand Up @@ -78,8 +97,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 +137,7 @@ function InlineLinkUI( {
: undefined,
opensInNewWindow: nextValue.opensInNewTab,
nofollow: nextValue.nofollow,
cssClasses: nextValue.cssClasses,
} );

const newText = nextValue.title || newUrl;
Expand Down
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