perf(Announce): skip getComputedStyle when there is no text content to announce#7546
Conversation
🦋 Changeset detectedLatest commit: ac6635a 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 |
|
👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Or, apply the |
There was a problem hiding this comment.
Pull request overview
This PR optimizes the Announce component's performance by reordering logic to check for empty text content before calling getComputedStyle, avoiding expensive forced style recalculations when there's nothing to announce. This is particularly beneficial for components like TreeView that render with initially empty live region content.
Changes:
- Reordered announce logic to check text content before visibility styles, eliminating unnecessary
getComputedStylecalls on empty content - Updated TreeView test to get live region after first announcement instead of before, reflecting the lazy creation behavior
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/react/src/live-region/Announce.tsx | Reordered logic to check text content before getComputedStyle, avoiding forced reflows for empty announcements |
| packages/react/src/TreeView/TreeView.test.tsx | Updated test to get live region after first announcement, reflecting lazy element creation |
| .changeset/announce-skip-empty-reflow.md | Added changeset documenting the performance improvement |
siddharthkp
left a comment
There was a problem hiding this comment.
Surprised this makes that much of a difference!
|
@siddharthkp I know, right? |
|
👋 Hi from github/github-ui! Your integration PR is ready: https://github.com/github/github-ui/pull/13894 |
Closes #
Announcecallswindow.getComputedStyle(element)to check if the element is visible before announcing. This forces synchronous style recalculation for the entire DOM. WhenAnnouncerenders with initially empty content (e.g. TreeView'sAriaStatuslive region, which starts withariaLiveMessage = ''), this reflow is completely wasted.The fix: check
getTextContent(element)first. If empty, bail out before the expensivegetComputedStylecall.Benchmarks (TreeView StressTest, 1000 items, 14K DOM nodes)
Announce.tsxforced reflowChangelog
New
N/A
Changed
Announcechecks for empty text content before callinggetComputedStyle, avoiding a forced synchronous style recalculation on mount when there's nothing to announceRemoved
N/A
Rollout strategy
Testing & Reviewing
live-regionelement would be created on initial empty-content mount. The element is now lazily created on the first real announcement instead.Announce.tsxforced reflow appears.Merge checklist