Refactor: Extract shared PseudoStateGrid component in pseudo-states stories#34334
Conversation
…cate code in pseudo-states stories Co-authored-by: valentinpalkovic <5889929+valentinpalkovic@users.noreply.github.com> Agent-Logs-Url: https://github.com/storybookjs/storybook/sessions/d64da8e9-5f2a-402f-92c2-bc4185d77af0
PseudoStateGrid component in pseudo-states stories
|
View your CI Pipeline Execution ↗ for commit a413a7f
☁️ Nx Cloud last updated this comment at |
|
Caution Review failedPull request was closed or merged during review No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughA new reusable Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~20 minutes ✨ Finishing Touches📝 Generate docstrings
Warning Review ran into problems🔥 ProblemsTimed out fetching pipeline failures after 30000ms Comment |
The 8-cell pseudo-state combination grid (Normal, Hover, Focus, Active, and their combinations) was copy-pasted verbatim across 6 story files in
addon-pseudo-states, totalling ~200 lines of near-identical JSX and creating a maintenance hazard.Changes
PseudoStateGrid.tsx— shared component accepting arender: (label: string) => ReactNodeprop that owns the grid layout and all pseudo-state class combinationsAllstory in all 6 files to delegate toPseudoStateGrid:Button.stories.tsxCSSAtRules.stories.tsxCustomElement.stories.tsxCustomElementNested.stories.tsxShadowRoot.stories.tsxShadowRootWithPart.stories.tsxUsage
Adding a new pseudo-state or changing the grid layout now requires a single edit. Existing
DynamicStylesstories that callAll.render!directly continue to work unchanged.Original prompt
This section details on the original issue you should resolve
<issue_title>Duplicate Code: Repeated pseudo-state grid render in 6 story files</issue_title>
<issue_description>Analysis of commit 6798300
Assignee:
@copilotSummary
The
Allstory export with an 8-cell pseudo-state grid (Normal, Hover, Focus, Active, Hover+Focus, Hover+Active, Focus+Active, Hover+Focus+Active) is copy-pasted nearly verbatim across 6 story files in theaddon-pseudo-statespackage. Each instance is 28–40 lines of near-identical JSX, differing only in the child component used.Duplication Details
Pattern: Pseudo-state combination grid in
Allstory exportSeverity: Medium
Occurrences: 6 instances
Locations:
code/addons/pseudo-states/src/stories/Button.stories.tsx(lines 19–49)code/addons/pseudo-states/src/stories/CSSAtRules.stories.tsx(lines 22–52)code/addons/pseudo-states/src/stories/CustomElement.stories.tsx(lines 21–59)code/addons/pseudo-states/src/stories/CustomElementNested.stories.tsx(lines 21–59)code/addons/pseudo-states/src/stories/ShadowRoot.stories.tsx(lines 17–47)code/addons/pseudo-states/src/stories/ShadowRootWithPart.stories.tsx(lines 17–47)Code Sample (representative, from
Button.stories.tsx):Additionally, the
Default,Hover,Focus, andActivestory exports with identicalparameters: { pseudo: { (state): true } }shapes are repeated in all 6 files (~8–12 lines each).Impact Analysis
focus-within) requires updating all 6 files individually. Any UI layout change to the grid must be replicated 6 times.Button.stories.tsxusesargsin the render,CustomElementdoesn't). A fix in one file is unlikely to be applied to the others.Refactoring Recommendations
Extract a shared
PseudoStateGridcomponentcode/addons/pseudo-states/src/stories/PseudoStateGrid.tsxrender: (label: string) => ReactNodeprop for the child componentAllrenders with(PseudoStateGrid render={(label) =) (Button {...args}){label}(/Button)} />Extract shared base story exports
Default,Hover,Focus,Activeonce in a shared file and re-export or spread into each story fileImplementation Checklist
PseudoStateGridshared componentAnalysis Metadata
Comments on the Issue (you are @Copi...
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.