|
| 1 | +/** |
| 2 | + * Shared character counting functionality for text inputs with character limits. |
| 3 | + * Handles real-time character count updates, validation, and aria-live announcements. |
| 4 | + */ |
| 5 | +export class CharacterCounter { |
| 6 | + private SCREEN_READER_DELAY: number = 500 |
| 7 | + private announceTimeout: number | null = null |
| 8 | + private isInitialLoad: boolean = true |
| 9 | + |
| 10 | + constructor( |
| 11 | + private inputElement: HTMLInputElement | HTMLTextAreaElement, |
| 12 | + private characterLimitElement: HTMLElement, |
| 13 | + private characterLimitSrElement: HTMLElement, |
| 14 | + ) {} |
| 15 | + |
| 16 | + /** |
| 17 | + * Initialize character counting by setting up event listener and initial count |
| 18 | + */ |
| 19 | + initialize(signal?: AbortSignal): void { |
| 20 | + this.inputElement.addEventListener('keyup', () => this.updateCharacterCount(), signal ? {signal} : undefined) // Keyup used over input for better screen reader support |
| 21 | + this.inputElement.addEventListener( |
| 22 | + 'paste', |
| 23 | + () => setTimeout(() => this.updateCharacterCount(), 50), // Gives the pasted content time to register |
| 24 | + signal ? {signal} : undefined, |
| 25 | + ) |
| 26 | + this.updateCharacterCount() |
| 27 | + this.isInitialLoad = false |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Clean up any pending timeouts |
| 32 | + */ |
| 33 | + cleanup(): void { |
| 34 | + if (this.announceTimeout) { |
| 35 | + clearTimeout(this.announceTimeout) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Pluralizes a word based on the count |
| 41 | + */ |
| 42 | + private pluralize(count: number, string: string): string { |
| 43 | + return count === 1 ? string : `${string}s` |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Update the character count display and validation state |
| 48 | + */ |
| 49 | + private updateCharacterCount(): void { |
| 50 | + if (!this.characterLimitElement) return |
| 51 | + |
| 52 | + const maxLengthAttr = this.characterLimitElement.getAttribute('data-max-length') |
| 53 | + if (!maxLengthAttr) return |
| 54 | + |
| 55 | + const maxLength = parseInt(maxLengthAttr, 10) |
| 56 | + const currentLength = this.inputElement.value.length |
| 57 | + const charactersRemaining = maxLength - currentLength |
| 58 | + let message = '' |
| 59 | + |
| 60 | + if (charactersRemaining >= 0) { |
| 61 | + const characterText = this.pluralize(charactersRemaining, 'character') |
| 62 | + message = `${charactersRemaining} ${characterText} remaining` |
| 63 | + const textSpan = this.characterLimitElement.querySelector('.FormControl-caption-text') |
| 64 | + if (textSpan) { |
| 65 | + textSpan.textContent = message |
| 66 | + } |
| 67 | + this.clearError() |
| 68 | + } else { |
| 69 | + const charactersOver = -charactersRemaining |
| 70 | + const characterText = this.pluralize(charactersOver, 'character') |
| 71 | + message = `${charactersOver} ${characterText} over` |
| 72 | + const textSpan = this.characterLimitElement.querySelector('.FormControl-caption-text') |
| 73 | + if (textSpan) { |
| 74 | + textSpan.textContent = message |
| 75 | + } |
| 76 | + this.setError() |
| 77 | + } |
| 78 | + |
| 79 | + // We don't want this announced on initial load |
| 80 | + if (!this.isInitialLoad) { |
| 81 | + this.announceToScreenReader(message) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Announce character count to screen readers with debouncing |
| 87 | + */ |
| 88 | + private announceToScreenReader(message: string): void { |
| 89 | + if (this.announceTimeout) { |
| 90 | + clearTimeout(this.announceTimeout) |
| 91 | + } |
| 92 | + |
| 93 | + this.announceTimeout = window.setTimeout(() => { |
| 94 | + if (this.characterLimitSrElement) { |
| 95 | + this.characterLimitSrElement.textContent = message |
| 96 | + } |
| 97 | + }, this.SCREEN_READER_DELAY) |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Set error when character limit is exceeded |
| 102 | + */ |
| 103 | + private setError(): void { |
| 104 | + this.inputElement.setAttribute('invalid', 'true') |
| 105 | + this.inputElement.setAttribute('aria-invalid', 'true') |
| 106 | + this.characterLimitElement.classList.add('fgColor-danger') |
| 107 | + |
| 108 | + // Show danger icon |
| 109 | + const icon = this.characterLimitElement.querySelector('.FormControl-caption-icon') |
| 110 | + if (icon) { |
| 111 | + icon.removeAttribute('hidden') |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Clear error when back under character limit |
| 117 | + */ |
| 118 | + private clearError(): void { |
| 119 | + this.inputElement.removeAttribute('invalid') |
| 120 | + this.inputElement.removeAttribute('aria-invalid') |
| 121 | + this.characterLimitElement.classList.remove('fgColor-danger') |
| 122 | + |
| 123 | + // Hide danger icon |
| 124 | + const icon = this.characterLimitElement.querySelector('.FormControl-caption-icon') |
| 125 | + if (icon) { |
| 126 | + icon.setAttribute('hidden', '') |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments