Skip to content
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

Addon-docs: Fix Preview scaling with transform instead of zoom #12845

Merged

Conversation

Tomastomaslol
Copy link
Member

Issue: #12324

What I did

  • Replace zoom with transform to be able to support component scaling in browsers that don't support CSS zoom
  • Add ref around the previewed component to be able to calculate its height
  • Set the height of the ChildrenContainer based on the height of the previewed component

https://caniuse.com/?search=zoom

How to test

  • Go to a component
  • Go to the docs tab
  • Use the zoom in and zoom out buttons in the component preview

firefoxpreview

@ndelangen
Copy link
Member

There seems to be some typescript issue preventing a successful build. If those were fixed, I can take a look at the stories in chromatic. Do you need help?

@Tomastomaslol
Copy link
Member Author

I think I got it under control! I'm going to do some refactoring to simplify the Zoom component and that should solve all the typescript issues. Thanks for the offer! 🙂

@ndelangen
Copy link
Member

@Tomastomaslol it seems there's some bug in the zoom component when it comes to snapshotting it in chromatic, could you take a look?
https://www.chromatic.com/snapshot?appId=5a375b97f4b14f0020b0cda3&id=5f97c01b2896dc00223749eb

@Tomastomaslol
Copy link
Member Author

@ndelangen It looks like the zoomed out example was too "zoomed out" and caused the UI test to not run. I'm currently getting some unintentional diffs in UI tests. Hopefully, I will figure out a good solution for it soon 🙂

setIframeInnerZoom(scale: number) {
try {
if (browserSupportsCssZoom()) {
Object.assign(this.iframe.contentDocument.body.style, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iframe.contentDocument can't work cross origin.. but I'm guessing addressing that is out of scope.

can we use useRef() to get the current element instead though?

@Tomastomaslol Tomastomaslol marked this pull request as ready for review October 29, 2020 11:49
active?: boolean;
};

export default class ZoomIFrame extends Component<IZoomIFrameProps> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit -- we've stopped using default exports in new code. Can you make these named exports instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great otherwise! 💯

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes names exports are easier to trace and refactor 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should now be resolved! 🙂

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

@shilman
Copy link
Member

shilman commented Nov 9, 2020

Hi @Tomastomaslol ! Can you check the Chromatic snapshots and see if they match your expectations? There are a bunch that look the same ...

@Tomastomaslol
Copy link
Member Author

To me, it looks like the chromatic snapshots are picking up some changes to props. This PR shouldn't introduce any visual diff for chrome and to the best of my knowledge, there is nothing in the Chromatic test that would indicate that a visual diff has been introduced.

Please let me know if there are any other scenarios you would like me to verify before we merge this code! 🙂

@shilman
Copy link
Member

shilman commented Nov 10, 2020

@Tomastomaslol I'm not talking about the args differences, I'm talking about the various zoom mode stories that you added. Do they look good to you?

@Tomastomaslol
Copy link
Member Author

Hi @shilman! Sorry, I misunderstood what you were asking. Looking at the Iframe examples it doesn't look like the scale parameter has any effect on the scale of the content within the <Zoom.Iframe /> when used in a story. I spent some time trying to fix the examples and understand why this is happening. But so far I have been unable to explain the behaviour.

It possible it might be quite difficult to achieve a good example of the scaling levels due to the meta nature of this story. (Loading an example of itself into itself) and so far I have been unable to create any good examples demonstrating the scaling of the component. I will give it another go tomorrow with fresh eyes and see what I can come up with.

Any suggestions or ideas on how to resolve this would be welcomed! 🙂

@Tomastomaslol
Copy link
Member Author

I have been spending some more time trying to figuring out why the iframe zoom examples didn't work.

To me, it looks like the component gets recreated each time the controls are updated. because of that, the Zoom Iframe component will never set the scale: https://github.com/storybookjs/storybook/pull/12845/files#diff-b03e456200be1b58f1ba7f6b26405348ad11c73e5e7281b8601ff50c7b7dcb76R22

Example not using controls to set the scale:

const TemplateIFrame = (args) => {
  const iFrameRef = React.useRef<HTMLIFrameElement>(null);
  const [scale, setScale] = useState(1);
  return (
    <div>
      <input type="number" onChange={(e) => setScale(parseInt(e.target.value))} value={scale} />
      <Zoom.IFrame iFrameRef={iFrameRef} scale={scale} active={true}>
        <iframe
          id="iframe"
          title="UI Panel"
          src="/iframe.html?id=ui-panel--default&viewMode=story"
          style={style}
          ref={iFrameRef}
          allowFullScreen
        />
      </Zoom.IFrame>
    </div>
  );
};

Results in:

Kapture 2020-11-11 at 19 45 45

My current suggested solution applies the scale after the iframe content has loaded which make it possible to demonstrate the functionality within a story.

The Zoom Iframe examples in chromatic look good now!

Please let me know if this makes sense or if there is a better way to solve this issue!

@shilman shilman changed the title Fix: use transform instead of zoom to scale component in Preview Addon-docs: Fix Preview scaling with transform instead of zoom Nov 19, 2020
@shilman
Copy link
Member

shilman commented Nov 19, 2020

Tested and it's looking great. Thanks for your patience on this @Tomastomaslol 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants