-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12845 from Tomastomaslol/12324_zoom_buttons_in_do…
…cs_do_not_work Addon-docs: Fix Preview scaling with transform instead of zoom
- Loading branch information
Showing
7 changed files
with
242 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React, { CSSProperties, useEffect, useState } from 'react'; | ||
import { Zoom } from './Zoom'; | ||
|
||
export default { | ||
component: Zoom, | ||
title: 'Basics/Zoom', | ||
argTypes: { | ||
scale: { | ||
control: { type: 'range', min: 0.2, max: 30, step: 0.02 }, | ||
}, | ||
}, | ||
}; | ||
const EXAMPLE_ELEMENT = ( | ||
<div | ||
style={{ | ||
width: 2000, | ||
height: 2000, | ||
border: '10px solid orangered', | ||
background: `url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAM4klEQVR4Xu2d224ktxGGayTN6LTrOO//PMmlAfvOgIEkFwEcZ71a7ep8muBjd2k4re4eVpHsaS1CeKH1is1u1s86slhcrP/261r62oGInB6LLBbNb8+PRQ75x/+3bArcPYh8vu4dZjEMyELk7Hjz0JwBYdGEddMuHmnXWPixDv/NqrkAOVw0HKJtDoAcHTZcerAQOTgQ4Rv5+ysQHbKvWyReAGUt8vIi8vQi8vzS/H1fQD08iny6MnLIG0BOGgJM2SD68lAEIPhZuj09i9w/ijw+lx55fDze+8dXKyDokJP9cAjEP142QEzR4B645v5J5PGp/hsfnkQ+fTMCcrQQOZlYZAEAolENifqkefsGRNnNgwiruFZDZP153YjRThtW6suFyPFEgKAHzhGJM7LiAOb6vuGc0s0FyOpAZLXafMqHk1aBFv46RNPJcr9cMTiltcjtY6NnSrb7h4ZDetowh6wORV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fO0RWdP1q7tV5anl93GPdrCzg1o1V1C2W7Dny97QW+c/FtpXVfpsBkAwdEhMCqyScOXkH96jH3z1UlxdxZZ0LoP73a2MydypGTKPU+1ZmUPRcQbSvwx2J7KKFaYJ/1tM8qbbc7PnpqrfgTboOIZQS32WYOJ/RbsFQcNaVL/H+XWNQFpBI7lgYxJomxTsZ989cQAhne67O2zVpfk9SNMpxLgo/3MS5uZ92dAqelCcFpOOD7HYMY7N3dSByknFBcQow4VAnltgEexjd7yGsE8r/3dsqNowBwniUEiQEFTc21j5fN5knnVjcuMiKr9DrXjaZQmBvnxBtPmzCNnphmXessec0WEnEFq7wNBI6+hpcgPJG3MH9hKE0WvEKyNtt6B2ARKeRpgSkO8Fw+KcFJxTRdBgC8UqEUPyJL6H0gKEbcfGzKP9QZLNjBMQe/c2dyMWNg0PiS+0JxXdZzzOJEs+8Fs5v92505cXxMo0+h7C9vrS9vq7ENzCGxul0vHBP+ogRoM41HAIgZh0SA8KpKKu9XWricx1HM1r4Pjz4LfAHPlq3LEgbClVRt/ul65A51rKaA1Ca95x6qotvRmR96b8/axiQ4FFT9KWdNQq+9kmkORB4im9wAaJ346pcDg6cQ5lOMcH39g7VIT3f/T+lOOGi/Z/IjAAAAABJRU5ErkJggg==')`, | ||
}} | ||
/> | ||
); | ||
|
||
const TemplateElement = (args) => <Zoom.Element {...args} />; | ||
|
||
export const elementActualSize = TemplateElement.bind({}); | ||
|
||
elementActualSize.args = { | ||
scale: 1, | ||
children: EXAMPLE_ELEMENT, | ||
}; | ||
|
||
export const elementZoomedIn = TemplateElement.bind({}); | ||
|
||
elementZoomedIn.args = { | ||
scale: 0.7, | ||
children: EXAMPLE_ELEMENT, | ||
}; | ||
|
||
export const elementZoomedOut = TemplateElement.bind({}); | ||
|
||
elementZoomedOut.args = { | ||
scale: 3, | ||
children: EXAMPLE_ELEMENT, | ||
}; | ||
|
||
const style: CSSProperties = { | ||
width: '500px', | ||
height: '500px', | ||
border: '2px solid hotpink', | ||
position: 'relative', | ||
}; | ||
|
||
const TemplateIFrame = (args) => { | ||
const iFrameRef = React.useRef<HTMLIFrameElement>(null); | ||
const [scale, setScale] = useState(1); | ||
const [loaded, hasLoaded] = useState(false); | ||
|
||
useEffect(() => { | ||
if (loaded) { | ||
setScale(args.scale); | ||
} | ||
}, [args.scale, loaded]); | ||
return ( | ||
<Zoom.IFrame iFrameRef={iFrameRef} scale={scale} active={args.active}> | ||
<iframe | ||
id="iframe" | ||
title="UI Panel" | ||
onLoad={() => hasLoaded(true)} | ||
src="/iframe.html?id=ui-panel--default&viewMode=story" | ||
style={style} | ||
ref={iFrameRef} | ||
allowFullScreen | ||
/> | ||
</Zoom.IFrame> | ||
); | ||
}; | ||
export const iFrameActualSize = TemplateIFrame.bind({}); | ||
|
||
iFrameActualSize.args = { | ||
scale: 1, | ||
active: true, | ||
}; | ||
|
||
export const iFrameZoomedIn = TemplateIFrame.bind({}); | ||
|
||
iFrameZoomedIn.args = { | ||
scale: 0.7, | ||
active: true, | ||
}; | ||
|
||
export const iFrameZoomedOut = TemplateIFrame.bind({}); | ||
|
||
iFrameZoomedOut.args = { | ||
scale: 3, | ||
active: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import window from 'global'; | ||
import { ZoomElement as Element } from './ZoomElement'; | ||
import { ZoomIFrame as IFrame } from './ZoomIFrame'; | ||
|
||
export const browserSupportsCssZoom = (): boolean => | ||
window.document.implementation.createHTMLDocument().body.style.zoom !== undefined; | ||
|
||
export const Zoom = { | ||
Element, | ||
IFrame, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React, { ReactElement, useEffect, useState } from 'react'; | ||
import { styled } from '@storybook/theming'; | ||
import { browserSupportsCssZoom } from './Zoom'; | ||
|
||
const ZoomElementWrapper = styled.div<{ scale: number; height: number }>(({ scale = 1, height }) => | ||
browserSupportsCssZoom() | ||
? { | ||
'> *': { | ||
zoom: 1 / scale, | ||
}, | ||
} | ||
: { | ||
height: height + 50, | ||
transformOrigin: 'top left', | ||
transform: `scale(${1 / scale})`, | ||
} | ||
); | ||
type ZoomProps = { | ||
scale: number; | ||
children: ReactElement | ReactElement[]; | ||
}; | ||
|
||
export function ZoomElement({ scale, children }: ZoomProps) { | ||
const componentWrapperRef = React.useRef<HTMLDivElement>(null); | ||
const [height, setHeight] = useState(0); | ||
|
||
useEffect(() => { | ||
if (componentWrapperRef.current) { | ||
setHeight(componentWrapperRef.current.getBoundingClientRect().height); | ||
} | ||
}, [scale, componentWrapperRef.current]); | ||
|
||
return ( | ||
<ZoomElementWrapper scale={scale} height={height}> | ||
<div ref={componentWrapperRef} className="innerZoomElementWrapper"> | ||
{children} | ||
</div> | ||
</ZoomElementWrapper> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Component, ReactElement } from 'react'; | ||
import { browserSupportsCssZoom } from './Zoom'; | ||
|
||
export type IZoomIFrameProps = { | ||
scale: number; | ||
children: ReactElement<HTMLIFrameElement>; | ||
iFrameRef: React.MutableRefObject<HTMLIFrameElement>; | ||
active?: boolean; | ||
}; | ||
|
||
export class ZoomIFrame extends Component<IZoomIFrameProps> { | ||
iframe: HTMLIFrameElement = null; | ||
|
||
componentDidMount() { | ||
const { iFrameRef } = this.props; | ||
this.iframe = iFrameRef.current; | ||
} | ||
|
||
shouldComponentUpdate(nextProps: IZoomIFrameProps) { | ||
const { scale, active } = this.props; | ||
|
||
if (scale !== nextProps.scale) { | ||
this.setIframeInnerZoom(nextProps.scale); | ||
} | ||
|
||
if (active !== nextProps.active) { | ||
this.iframe.setAttribute('data-is-storybook', nextProps.active ? 'true' : 'false'); | ||
} | ||
|
||
// this component renders an iframe, which gets updates via post-messages | ||
// never update this component, it will cause the iframe to refresh | ||
return false; | ||
} | ||
|
||
setIframeInnerZoom(scale: number) { | ||
try { | ||
if (browserSupportsCssZoom()) { | ||
Object.assign(this.iframe.contentDocument.body.style, { | ||
zoom: 1 / scale, | ||
}); | ||
} else { | ||
Object.assign(this.iframe.contentDocument.body.style, { | ||
width: `${scale * 100}%`, | ||
height: `${scale * 100}%`, | ||
transform: `scale(${1 / scale})`, | ||
transformOrigin: 'top left', | ||
}); | ||
} | ||
} catch (e) { | ||
this.setIframeZoom(scale); | ||
} | ||
} | ||
|
||
setIframeZoom(scale: number) { | ||
Object.assign(this.iframe.style, { | ||
width: `${scale * 100}%`, | ||
height: `${scale * 100}%`, | ||
transform: `scale(${1 / scale})`, | ||
transformOrigin: 'top left', | ||
}); | ||
} | ||
|
||
render() { | ||
const { children } = this.props; | ||
return children; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters