Skip to content

Commit

Permalink
Implement Toggleable control for CSS classes
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Dec 17, 2024
1 parent 78d2e70 commit ba65cee
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 13 deletions.
74 changes: 61 additions & 13 deletions packages/format-library/src/link/inline.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
/**
* 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,
__experimentalInputControl as InputControl,
CheckboxControl,
} from '@wordpress/components';
import { prependHTTP } from '@wordpress/url';
import {
Expand All @@ -33,6 +38,52 @@ 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
/>
) }
</div>
);
};

const LINK_SETTINGS = [
...LinkControl.DEFAULT_LINK_SETTINGS,
{
Expand All @@ -42,18 +93,15 @@ const LINK_SETTINGS = [
{
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
/>
),
render: ( setting, value, onChange ) => {
return (
<TogglableSettingComponent
setting={ setting }
value={ value }
onChange={ onChange }
/>
);
},
},
];

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;
}

0 comments on commit ba65cee

Please sign in to comment.