1
- import window from 'global' ;
2
- import React , { Component , IframeHTMLAttributes } from 'react' ;
3
-
1
+ import React , { IframeHTMLAttributes } from 'react' ;
4
2
import { styled } from '@storybook/theming' ;
5
-
6
- const FIREFOX_BROWSER = 'Firefox' ;
3
+ import { Zoom } from '@storybook/components' ;
7
4
8
5
const StyledIframe = styled . iframe ( {
9
6
position : 'absolute' ,
@@ -25,70 +22,21 @@ export interface IFrameProps {
25
22
active : boolean ;
26
23
}
27
24
28
- export class IFrame extends Component < IFrameProps & IframeHTMLAttributes < HTMLIFrameElement > > {
29
- iframe : HTMLIFrameElement = null ;
30
-
31
- componentDidMount ( ) {
32
- const { id } = this . props ;
33
- this . iframe = window . document . getElementById ( id ) ;
34
- }
35
-
36
- shouldComponentUpdate ( nextProps : IFrameProps ) {
37
- const { scale, active } = this . props ;
38
-
39
- if ( scale !== nextProps . scale ) {
40
- this . setIframeInnerZoom ( nextProps . scale ) ;
41
- }
42
-
43
- if ( active !== nextProps . active ) {
44
- this . iframe . setAttribute ( 'data-is-storybook' , nextProps . active ? 'true' : 'false' ) ;
45
- }
46
-
47
- // this component renders an iframe, which gets updates via post-messages
48
- // never update this component, it will cause the iframe to refresh
49
- return false ;
50
- }
51
-
52
- setIframeInnerZoom ( scale : number ) {
53
- try {
54
- if ( window . navigator . userAgent . indexOf ( FIREFOX_BROWSER ) !== - 1 ) {
55
- Object . assign ( this . iframe . contentDocument . body . style , {
56
- width : `${ scale * 100 } %` ,
57
- height : `${ scale * 100 } %` ,
58
- transform : `scale(${ 1 / scale } )` ,
59
- transformOrigin : 'top left' ,
60
- } ) ;
61
- } else {
62
- Object . assign ( this . iframe . contentDocument . body . style , {
63
- zoom : 1 / scale ,
64
- } ) ;
65
- }
66
- } catch ( e ) {
67
- this . setIframeZoom ( scale ) ;
68
- }
69
- }
70
-
71
- setIframeZoom ( scale : number ) {
72
- Object . assign ( this . iframe . style , {
73
- width : `${ scale * 100 } %` ,
74
- height : `${ scale * 100 } %` ,
75
- transform : `scale(${ 1 / scale } )` ,
76
- transformOrigin : 'top left' ,
77
- } ) ;
78
- }
79
-
80
- render ( ) {
81
- const { id, title, src, allowFullScreen, scale, active, ...rest } = this . props ;
82
- return (
25
+ export function IFrame ( props : IFrameProps & IframeHTMLAttributes < HTMLIFrameElement > ) {
26
+ const { active, id, title, src, allowFullScreen, scale, ...rest } = props ;
27
+ const iFrameRef = React . useRef < HTMLIFrameElement > ( null ) ;
28
+ return (
29
+ < Zoom . IFrame scale = { scale } active = { active } iFrameRef = { iFrameRef } >
83
30
< StyledIframe
84
- onLoad = { ( ) => this . iframe . setAttribute ( 'data-is-loaded' , 'true' ) }
85
31
data-is-storybook = { active ? 'true' : 'false' }
32
+ onLoad = { ( e ) => e . currentTarget . setAttribute ( 'data-is-loaded' , 'true' ) }
86
33
id = { id }
87
34
title = { title }
88
35
src = { src }
89
36
allowFullScreen = { allowFullScreen }
37
+ ref = { iFrameRef }
90
38
{ ...rest }
91
39
/>
92
- ) ;
93
- }
40
+ </ Zoom . IFrame >
41
+ ) ;
94
42
}
0 commit comments