File size: 3,416 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
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 ) => (
<label className={ clsx( 'a8c-experience-control__option', className ) }>
<VisuallyHidden>
<input type="radio" aria-label={ ariaLabel } { ...restProps } />
</VisuallyHidden>
<div className="a8c-experience-control__option-icon">{ icon }</div>
</label>
);
const ExperienceControlBase = ( {
children,
className,
hideLabelFromVision,
label,
...restProps
}: ExperienceControlBaseProps ) => {
const { baseControlProps, controlProps } = useBaseControlProps( restProps );
return (
<BaseControl
__nextHasNoMarginBottom
className={ clsx( 'a8c-experience-control', className ) }
{ ...baseControlProps }
>
<fieldset { ...controlProps } className="a8c-experience-control__fieldset">
{ hideLabelFromVision ? (
<VisuallyHidden as="legend">{ label }</VisuallyHidden>
) : (
<BaseControl.VisualLabel as="legend">{ label }</BaseControl.VisualLabel>
) }
{ children }
</fieldset>
</BaseControl>
);
};
/**
* 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 (
* <ExperienceControl
* label="What was your experience like?"
* onChange={ setExperience }
* value={ experience }
* />
* );
* }
* ```
* @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: <IconGood />,
ariaLabel: __( 'Good' ),
},
{
value: 'neutral',
icon: <IconNeutral />,
ariaLabel: __( 'Neutral' ),
},
{
value: 'bad',
icon: <IconBad />,
ariaLabel: __( 'Bad' ),
},
];
return (
<ExperienceControlBase label={ label } help={ help }>
{ options.map( ( option ) => (
<ExperienceControlOption
key={ option.value }
className={ `is-${ option.value }` }
checked={ value === option.value }
onChange={ ( event: React.ChangeEvent< HTMLInputElement > ) =>
onChange( event.target.value as ExperienceValue )
}
value={ option.value }
name={ name ?? `experience-control-${ nameId }` }
ariaLabel={ option.ariaLabel }
icon={ option.icon }
/>
) ) }
</ExperienceControlBase>
);
}
export default ExperienceControl;
|