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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
30d1ea1
use transform instead of zoom to scale component in Preview
Tomastomaslol Oct 21, 2020
9c681dc
Add new zoom component and use it in Preview blocks
Tomastomaslol Oct 24, 2020
704d91e
removed unused imports
Tomastomaslol Oct 25, 2020
7a9575a
add support to be able to zoom on a iframe
Tomastomaslol Oct 25, 2020
6e969b5
add Element to children props for Zoom
Tomastomaslol Oct 25, 2020
f31bf0d
fix Zoom Props
Oct 25, 2020
16e86e7
split zoom element and iframe in seperate Zoom components
Tomastomaslol Oct 27, 2020
69e4e78
do not pass id to Zoom iframe. make initial zommed stories out less z…
Tomastomaslol Oct 27, 2020
d60370b
Merge branch 'next' of https://github.com/storybookjs/storybook into …
Tomastomaslol Oct 27, 2020
c875d45
only wrap element in ZoomElement if css Zoom is not supported. Use sp…
Tomastomaslol Oct 28, 2020
09e5fdd
re-add css zoom to target all child elements for ZoomElement
Tomastomaslol Oct 28, 2020
50e7385
access iframe as a ref in Zoom.Iframe. add innerWrapper for ZoomEleme…
Tomastomaslol Oct 29, 2020
79d755e
Merge branch 'next' of https://github.com/storybookjs/storybook into …
Tomastomaslol Oct 29, 2020
bb9f95d
Merge branch 'next' into pr/12845
shilman Nov 1, 2020
4e65bd6
do not use default exports for Zoom component
Tomastomaslol Nov 9, 2020
57c83c9
Merge branch 'next' of https://github.com/storybookjs/storybook into …
Tomastomaslol Nov 9, 2020
7f79f75
Merge branch '12324_zoom_buttons_in_docs_do_not_work' of github.com:T…
Tomastomaslol Nov 9, 2020
de43e6e
fix zoom iframe story. make udateable using controls
Tomastomaslol Nov 12, 2020
790b371
Merge branch 'next' of https://github.com/storybookjs/storybook into …
Tomastomaslol Nov 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions lib/components/src/Zoom/Zoom.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@ elementZoomedIn.args = {
export const elementZoomedOut = TemplateElement.bind({});

elementZoomedOut.args = {
scale: 30,
scale: 3,
children: EXAMPLE_ELEMENT,
};
const TemplateIFrame = (args) => <Zoom.IFrame {...args} />;
export const iFrameActualSize = TemplateIFrame.bind({});

iFrameActualSize.args = {
scale: 1,
id: 'iframe',
active: true,
children: EXAMPLE_IFRAME,
};
Expand All @@ -74,16 +73,14 @@ export const iFrameZoomedIn = TemplateIFrame.bind({});

iFrameZoomedIn.args = {
scale: 0.7,
id: 'iframe',
active: true,
children: EXAMPLE_IFRAME,
};

export const iFrameZoomedOut = TemplateIFrame.bind({});

iFrameZoomedOut.args = {
scale: 30,
id: 'iframe',
scale: 3,
active: true,
children: EXAMPLE_IFRAME,
};
31 changes: 14 additions & 17 deletions lib/components/src/Zoom/ZoomIFrame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ import { Component, ReactElement } from 'react';
import { browserSupportsCssZoom } from './Zoom';

export type IZoomIFrameProps = {
id: string;
scale: number;
active: boolean;
children: ReactElement<HTMLIFrameElement>;
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! 🙂

iframe: HTMLIFrameElement = null;

componentDidMount() {
const { id } = this.props;
this.iframe = window.document.getElementById(id);
this.iframe.addEventListener('load', this.setAttributeDataIsLoaded);
this.iframe = this.validateAndGetIFrame();
}

shouldComponentUpdate(nextProps: IZoomIFrameProps) {
Expand All @@ -34,27 +31,19 @@ export default class ZoomIFrame extends Component<IZoomIFrameProps> {
return false;
}

componentWillUnmount() {
this.iframe.removeEventListener('load', this.setAttributeDataIsLoaded);
}

setAttributeDataIsLoaded() {
this.iframe.setAttribute('data-is-loaded', 'true');
}

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?

zoom: 1 / scale,
});
} else {
Object.assign(this.iframe.contentDocument.body.style, {
width: `${scale * 100}%`,
height: `${scale * 100}%`,
transform: `scale(${1 / scale})`,
transformOrigin: 'top left',
});
} else {
Object.assign(this.iframe.contentDocument.body.style, {
zoom: 1 / scale,
});
}
} catch (e) {
this.setIframeZoom(scale);
Expand All @@ -70,6 +59,14 @@ export default class ZoomIFrame extends Component<IZoomIFrameProps> {
});
}

validateAndGetIFrame(): HTMLIFrameElement {
const { children } = this.props;
if (!children.props.id) {
Tomastomaslol marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`missing id on Iframe given to ZoomIFrame`);
}
return window.document.getElementById(children.props.id);
}

render() {
const { children } = this.props;
return children;
Expand Down
3 changes: 2 additions & 1 deletion lib/ui/src/components/preview/iframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export interface IFrameProps {
export function IFrame(props: IFrameProps & IframeHTMLAttributes<HTMLIFrameElement>) {
const { active, id, title, src, allowFullScreen, scale, ...rest } = props;
return (
<Zoom.IFrame id={id} scale={scale} active={active}>
<Zoom.IFrame scale={scale} active={active}>
<StyledIframe
data-is-storybook={active ? 'true' : 'false'}
onLoad={(e) => e.currentTarget.setAttribute('data-is-loaded', 'true')}
id={id}
title={title}
src={src}
Expand Down