Addon A11y: Prevent setting highlights for old results #32178
Merged
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 #32135
What I did
Prevent setting highlights for old a11y results. When switching stories, the a11y results from the previous story would apply highlights to the new story before the a11y results of the new story were ready, causing incorrect highlights to briefly appear. This is resolved by preventing a11y highlights from being set until the report is either running or ready (i.e. not
initial).Before:
Note how the outline buttons are already highlighted before the others.
Screen.Recording.2025-08-01.at.16.47.46.mov
After:
Screen.Recording.2025-08-01.at.16.45.45.mov
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
Have two stories, one of which renders a component in multiple variants, and the other renders one identical variant and one whole new variant. When switching between the stories, the identical variant should yield identical classnames. Now enable a11y highlights (e.g. passing) and switch between the stories. All highlights should clear before new highlights are added. Previously the identical component would "inherit" the highlight from the other story due to the identical classname.
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal,ci:mergedorci:dailyGH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli-storybook/src/sandbox-templates.tsMake sure this PR contains one of the labels below:
Available labels
bug: Internal changes that fixes incorrect behavior.maintenance: User-facing maintenance tasks.dependencies: Upgrading (sometimes downgrading) dependencies.build: Internal-facing build tooling & test updates. Will not show up in release changelog.cleanup: Minor cleanup style change. Will not show up in release changelog.documentation: Documentation only changes. Will not show up in release changelog.feature request: Introducing a new feature.BREAKING CHANGE: Changes that break compatibility in some way with current major version.other: Changes that don't fit in the above categories.🦋 Canary release
This pull request has been released as version
0.0.0-pr-32178-sha-9b4c95c0. Try it out in a new sandbox by runningnpx [email protected] sandboxor in an existing project withnpx [email protected] upgrade.More information
0.0.0-pr-32178-sha-9b4c95c0clear-a11y-highlights-on-changed-story9b4c95c01755169311)To request a new release of this pull request, mention the
@storybookjs/coreteam.core team members can create a new canary release here or locally with
gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=32178Greptile Summary
This PR fixes a UX issue in the Accessibility addon where highlights from the previous story would briefly flash on a new story before the new story's accessibility analysis completed. The change modifies the
A11yContextProvidercomponent inA11yContext.tsxto add a guard that prevents setting highlights when the addon status is'initial'.The implementation adds an
isInitialboolean flag that checks if the current status is'initial', and updates the highlightinguseEffectto return early when!highlighted || isInitial. This ensures highlights are only applied when the addon has progressed to either'running'or'ready'status, preventing stale highlights from appearing during story transitions.Additionally, the PR adds defensive checks around
HIGHLIGHTevent emissions to only emit events when there are actual selectors to highlight, avoiding unnecessary empty highlight events. TheuseEffectdependency array is updated to include theisInitialflag to ensure proper re-evaluation when the status changes.This change integrates well with the existing accessibility addon architecture, which already manages different status states (
'initial','running','ready') throughout the accessibility testing lifecycle. The fix addresses the timing issue without disrupting the core functionality of the accessibility analysis and reporting system.Confidence score: 4/5
• This is a focused bug fix that addresses a specific UX issue with minimal risk of breaking existing functionality
• The implementation follows existing patterns in the codebase and adds appropriate safeguards
• The change requires manual testing to verify the fix works as intended, as the issue involves timing and visual behavior during story transitions