Add a character counter to the status composer
React · React · intermediate · modification
Adds a character counter to the status composer on the team home page. `StatusComposer.jsx` counts the text in grapheme clusters with `Intl.Segmenter`, shows how many of the 280 characters are left (or how many over), switches the counter to a warning look at 20 left and a danger look over the limit, and disables Post over the limit. The textarea points at the counter with `aria-describedby`, and a visually hidden `role="status"` region announces entering the warning range and going over the limit. `StatusComposer.css` adds the counter styles, and `StatusComposer.test.jsx` gains tests for the counter, the looks, Post and the announcements.
Team workspace web app: React 18.3 function components, Vite, Vitest 2 with jsdom and @testing-library/react 16. The Vitest config runs with `globals: true`, so Testing Library cleans up after each test; the project does not use jest-dom matchers. `StatusComposer` is rendered once on the team home page as `<StatusComposer onPost={postStatus} />`; `onPost(text)` is synchronous: it queues the post, and the parent shows any send error itself, so the composer clears right after calling it (unchanged). The status API (not in this PR) also enforces the 280-character limit; the counter is there so people see the limit before they hit it. Supported browsers are the latest two versions of Chrome, Edge, Firefox and Safari, which all ship `Intl.Segmenter`, and so does Node 22, which runs the tests. The global stylesheet has a `.visually-hidden` utility class (kept in the accessibility tree, off screen) and the theme tokens used here. Statuses are short; a pasted draft is at most a few thousand characters.
Requirements
- A counter under the textarea always shows how much of the 280-character limit is left: `N characters left` (`1 character left` for one, `0 characters left` at the limit), and once the text is over the limit, `N characters over the limit` (`1 character over the limit` for one). It updates on every change.
- A character is a user-perceived character: one extended grapheme cluster as `Intl.Segmenter` with `granularity: 'grapheme'` splits the text. An emoji built from several code points (a ZWJ family, a flag, a skin-tone modifier) or a letter with a combining accent counts as one. Spaces, tabs and line breaks count like any other character.
- The counter has three looks: normal (`--fg-muted`) while more than 20 characters are left; warning (`--attention-fg`) from 20 left down to 0 left, both included; danger (`--danger-fg`, bold, `font-weight: 700`) when over the limit. Exactly 280 characters is within the limit.
- Post is disabled when the text is empty or whitespace only (whitespace as `String.prototype.trim` defines it, as today) or over the limit. Over the limit, the textarea is also marked `aria-invalid="true"`. Typing or pasting past the limit is allowed, nothing is cut off, so a long draft can be edited down.
- Post sends the text exactly as typed, not trimmed, and clears the box, as today.
- The textarea is described by the counter (`aria-describedby`), so a screen reader reads the count when it gets focus. Announcements go through a `role="status"` live region that is always rendered: it announces the counter text once when the text enters the warning range from the normal range, and once when it goes over the limit (from either range). No other change announces anything (not each keystroke inside a range, not leaving a range); leaving a range, or posting, empties the region, so the next entry is announced again.
- The component's tests cover the new behaviour.
Files touched
- src/components/StatusComposer.jsx
- src/components/StatusComposer.css
- src/components/StatusComposer.test.jsx