feat(useId): add prefix parameter and adopt in TextInput/Textarea#7873
Draft
adierkens wants to merge 1 commit into
Draft
feat(useId): add prefix parameter and adopt in TextInput/Textarea#7873adierkens wants to merge 1 commit into
adierkens wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 78eefed The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
|
6f0709c to
cd5834b
Compare
…extarea
useId now accepts an optional second `prefix` argument that prepends a
human-readable label to the generated id. Existing behaviour is
preserved: bare `useId()` still returns a plain generated id, and
`useId(id)` still returns the explicit override.
useId() // ':r0:'
useId('my-id') // 'my-id'
useId(undefined, 'leading-visual') // 'leading-visual-:r0:'
useId('my-id', 'leading-visual') // 'my-id' (prefix ignored when id is provided)
TextInput's leading/trailing visual ids, loading id, and character-
count ids now use the prefix so the rendered `aria-describedby`
attributes are debuggable in the inspector. Textarea's character-count
ids do the same. Both files also drop the direct React `useId` import
in favour of the Primer wrapper for consistency with the rest of the
package.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
cd5834b to
78eefed
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #
Adds an optional second
prefixargument to Primer'suseIdhook so generated ids are self-documenting in DevTools and the rendered DOM. Adopts it inTextInputandTextareaat the internal-id call sites (leading-visual,trailing-visual,loading,character-count,character-count-message), and also switches those two components to importuseIdfrom Primer's wrapper instead of importing React'suseIddirectly (matching the rest of the package).Changelog
New
useIdsecond parameter:prefix?: string. Optional. Prepended to the generated id when no explicitidis provided. Ignored whenidis provided.Unit tests for the new prefix behaviour in
packages/react/src/hooks/__tests__/useId.test.tsx.Changed
packages/react/src/TextInput/TextInput.tsx— usesuseId(undefined, 'leading-visual'),useId(undefined, 'trailing-visual'),useId(undefined, 'loading'),useId(undefined, 'character-count'),useId(undefined, 'character-count-message')so thearia-describedbyattributes are debuggable in the inspector. Also drops the direct ReactuseIdimport in favour of Primer's wrapper.packages/react/src/Textarea/Textarea.tsx— samecharacter-count/character-count-messageprefixes. Same wrapper-import swap.Removed
Why the prefix matters
Without a prefix,
useId()returns IDs like:r0:,:r1:— opaque in DevTools and impossible to correlate with the corresponding DOM node. With a prefix, the inspector showsaria-describedby="leading-visual-:r0: character-count-:r1:", which is self-documenting. Particularly useful foraria-describedby/aria-labelledbydebugging.The wrapper now actually justifies its own existence — before this change, Primer's
useIdwas justidProp ?? useReactId(), which wasn't enough of a value-add to mandate using the wrapper at zero-arg call sites.Rollout strategy
Additive only. Bare
useId()anduseId(idProp)keep their existing behaviour byte-for-byte. The second parameter is opt-in.Testing & Reviewing
npx tsc -p packages/react/tsconfig.json --noEmitclean.npx prettier --checkandnpx eslinton changed files clean.useId.test.tsx— 5 tests pass covering: no-args, id-override, prefix-only, id-with-prefix (prefix ignored), stable-across-rerenders.TextInput/Textarea/TextInputWithTokenstest files show 16 snapshot failures — all pre-existing CSS-hash drift, unrelated. Verifiable by stashing and re-running onmain(same 16 failures appear).Worth a close look:
useId(id, prefix)follows the existing pattern of putting the override first. Could equally beuseId(prefix, id)oruseId({id, prefix})if you'd prefer a more discoverable shape. Current shape minimises churn at existing call sites.idreturns as-is andprefixis silently dropped. Documented in the JSDoc but worth confirming that's the right precedence.Merge checklist