import { BaseControl, useBaseControlProps, VisuallyHidden } from '@wordpress/components';
import { useI18n } from '@wordpress/react-i18n';
import clsx from 'clsx';
import { useId } from 'react';
import { IconBad, IconGood, IconNeutral } from './icons';
import type {
ExperienceValue,
ExperienceOption,
ExperienceControlOptionProps,
ExperienceControlBaseProps,
ExperienceControlProps,
} from './types';
import './style.scss';
const ExperienceControlOption = ( {
className,
icon,
ariaLabel,
...restProps
}: ExperienceControlOptionProps ) => (
);
const ExperienceControlBase = ( {
children,
className,
hideLabelFromVision,
label,
...restProps
}: ExperienceControlBaseProps ) => {
const { baseControlProps, controlProps } = useBaseControlProps( restProps );
return (
);
};
/**
* A flexible component for collecting user experience feedback through a simple three-state rating system.
* The component provides an accessible way to gather user sentiment with visual and interactive feedback.
* @example
* Usage:
* ```jsx
* import { ExperienceControl } from '@automattic/components';
* function MyComponent() {
* const [ experience, setExperience ] = useState( 'good' );
*
* return (
*
* );
* }
* ```
* @description
* - The component is fully accessible with proper ARIA labels and keyboard navigation
* - Each option (good, neutral, bad) is represented by an icon and can be selected via click or keyboard
* - The component provides visual feedback for the selected option
*/
export function ExperienceControl( {
label,
onChange,
value,
help,
name,
}: ExperienceControlProps ) {
const { __ } = useI18n();
const nameId = useId();
const options: ExperienceOption[] = [
{
value: 'good',
icon: ,
ariaLabel: __( 'Good' ),
},
{
value: 'neutral',
icon: ,
ariaLabel: __( 'Neutral' ),
},
{
value: 'bad',
icon: ,
ariaLabel: __( 'Bad' ),
},
];
return (
{ options.map( ( option ) => (
) =>
onChange( event.target.value as ExperienceValue )
}
value={ option.value }
name={ name ?? `experience-control-${ nameId }` }
ariaLabel={ option.ariaLabel }
icon={ option.icon }
/>
) ) }
);
}
export default ExperienceControl;