import { useState } from '@wordpress/element'; import { useInstanceId } from '@wordpress/compose'; import { __ } from '@wordpress/i18n'; import { __experimentalInputControl as WCInputControl, CheckboxControl as WCCheckboxControl, } from '@wordpress/components'; import { Stack, VisuallyHidden } from '@wordpress/ui'; /** * Props for the Link UI's "Additional CSS class(es)" setting. */ export interface CSSClassesSettingProps { setting: { id: string; title?: string }; value?: { cssClasses?: string }; onChange: ( newValue: { cssClasses?: string } ) => void; } /** * CSSClassesSettingComponent * * Presents a toggleable text input for editing link CSS classes. The input * is shown when the toggle is enabled or when there is already a value. When * toggled off and a value exists, it resets the value to an empty string. * * @param props - Component props. * @param props.setting - Setting configuration object. * @param props.value - Current link value object. * @param props.onChange - Callback when value changes. */ const CSSClassesSettingComponent = ( { setting, value, onChange, }: CSSClassesSettingProps ) => { const hasValue = !! value?.cssClasses?.length; const [ isSettingActive, setIsSettingActive ] = useState( hasValue ); const instanceId = useInstanceId( CSSClassesSettingComponent ); const controlledRegionId = `css-classes-setting-${ instanceId }`; // Sanitize user input: replace commas with spaces, collapse repeated spaces, and trim const handleSettingChange = ( newValue: string | undefined ) => { const sanitizedValue = typeof newValue === 'string' ? newValue.replace( /,/g, ' ' ).replace( /\s+/g, ' ' ).trim() : newValue; onChange( { ...value, [ setting.id ]: sanitizedValue, } ); }; const handleCheckboxChange = () => { if ( isSettingActive ) { if ( hasValue ) { // Reset the value when hiding the input and a value exists. handleSettingChange( '' ); } setIsSettingActive( false ); } else { setIsSettingActive( true ); } }; return (
); }; export default CSSClassesSettingComponent;