Skip to content

Commit ea2b873

Browse files
authored
fix: a11y word limit announcement bug (#21191)
1 parent 359192d commit ea2b873

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

packages/react/src/components/TextArea/TextArea.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,13 @@ const TextArea = frFn((props, forwardRef) => {
397397
(counterMode === 'character' || counterMode === 'word') ? (
398398
<Text
399399
as="div"
400-
className={counterClasses}>{`${textCount}/${maxCount}`}</Text>
400+
className={counterClasses}
401+
aria-hidden="true">{`${textCount}/${maxCount}`}</Text>
401402
) : null;
402403

404+
const counterDescriptionId =
405+
enableCounter && maxCount ? `${id}-counter-desc` : undefined;
406+
403407
const helperId = !helperText
404408
? undefined
405409
: `text-area-helper-text-${textAreaInstanceId}`;
@@ -451,8 +455,13 @@ const TextArea = frFn((props, forwardRef) => {
451455
let ariaDescribedBy;
452456
if (invalid) {
453457
ariaDescribedBy = errorId;
454-
} else if (!invalid && !warn && !isFluid && helperText) {
455-
ariaDescribedBy = helperId;
458+
} else if (warn && !isFluid) {
459+
ariaDescribedBy = warnId;
460+
} else {
461+
const ids: string[] = [];
462+
if (!isFluid && helperText && helperId) ids.push(helperId);
463+
if (counterDescriptionId) ids.push(counterDescriptionId);
464+
ariaDescribedBy = ids.length > 0 ? ids.join(' ') : undefined;
456465
}
457466

458467
if (enableCounter) {
@@ -527,6 +536,15 @@ const TextArea = frFn((props, forwardRef) => {
527536
{label}
528537
{counter}
529538
</div>
539+
{enableCounter && maxCount && (
540+
<span
541+
id={counterDescriptionId}
542+
className={`${prefix}--visually-hidden`}>
543+
{counterMode === 'word'
544+
? `Word limit ${maxCount}`
545+
: `Character limit ${maxCount}`}
546+
</span>
547+
)}
530548
<div
531549
ref={wrapperRef}
532550
className={textAreaWrapperClasses}

0 commit comments

Comments
 (0)