-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Storybook 7 viewport menu not working #22829
Conversation
code/addons/viewport/src/Tool.tsx
Outdated
if (typeof styles === 'function') { | ||
if (prevStyles) { | ||
result = styles(prevStyles); | ||
} | ||
} else { | ||
result = styles; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But what now if styles is a function and prevStyles is not yet defined. Then result
will also be always undefined right? I think that maybe the right fix would be to rollback what we had before, and change the type to be:
export type Styles = ViewportStyles | ((previous: ViewportStyles | null) => ViewportStyles) | null;
So that the user has to explicitly handle previous being still null
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to rollback what we had before
You mean reverting back to before fe02579, right?
And yes you're right the nested if if (prevStyles)
was only there because of the type Styles
.
And what about the type for prevStyles
? The actual value for prevStyles
argument comes from const ref = useRef<ViewportStyles>();
which allows it to be undefined
, so you either have to give it the initial value or accept undefined
for prevStyles
arg of getStyles
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean reverting back to before fe02579, right?
Yes!
And yes, widening previous
in the type of Styles
to undefined
instead of null
sounds also good to me indeed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey um one more thing about the return type. Since the ref only takes ViewportStyles | undefined
I adjusted the return value to be ViewportStyles | undefined
rather than ViewportStyles | null
. I pushed a revised commit so could you check if everything's alright?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Closes #22718
What I did
Modified
getStyles
function incode/addons/viewport/src/Tool.tsx
so that it works correctly even whenprevStyles
isundefined
.I did a little bit of digging, and it seems like the bug was introduced by fe02579. The main problem introduced by this commit is that
getStyles
now always returnsundefined
ifprevStyles
isundefined
which is initialized as such and never gets updated, so subsequent calls togetStyles
inViewportTool
component's body always yieldundefined
, resulting in no style being applied to the iframe.How to test
yarn task --task sandbox --start-from auto --template react-vite/default-ts
Checklist
MIGRATION.MD
Maintainers
make sure to add the
ci:merged
orci:daily
GH label to it.["cleanup", "BREAKING CHANGE", "feature request", "bug", "documentation", "maintenance", "dependencies", "other"]