From 462f6126b422eda9ccc3c612739454aa78b87a9c Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Wed, 18 Dec 2019 16:26:39 +1100 Subject: [PATCH] feat: Only render visible frames, avoid extra history entries (#106) --- .babelrc | 6 +- lib/makeWebpackConfig.js | 6 +- package.json | 3 + src/Playroom/Preview/Iframe.tsx | 98 ++++++++++----- src/Playroom/Preview/Preview.tsx | 89 +++++++------- yarn.lock | 205 ++++++++++++++++++++++++++++++- 6 files changed, 324 insertions(+), 83 deletions(-) diff --git a/.babelrc b/.babelrc index 2518f76a..990249e7 100644 --- a/.babelrc +++ b/.babelrc @@ -4,5 +4,9 @@ "@babel/preset-env", "@babel/preset-react" ], - "plugins": ["@babel/plugin-proposal-class-properties"] + "plugins": [ + "@babel/plugin-proposal-class-properties", + "@babel/plugin-proposal-optional-chaining", + "@babel/plugin-proposal-nullish-coalescing-operator" + ] } diff --git a/lib/makeWebpackConfig.js b/lib/makeWebpackConfig.js index 652fee77..758c2032 100644 --- a/lib/makeWebpackConfig.js +++ b/lib/makeWebpackConfig.js @@ -67,7 +67,11 @@ module.exports = async (playroomConfig, options) => { require.resolve('@babel/preset-typescript') ], plugins: [ - require.resolve('@babel/plugin-proposal-class-properties') + require.resolve('@babel/plugin-proposal-class-properties'), + require.resolve('@babel/plugin-proposal-optional-chaining'), + require.resolve( + '@babel/plugin-proposal-nullish-coalescing-operator' + ) ] } } diff --git a/package.json b/package.json index 18b070a5..97975a50 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,8 @@ "@babel/cli": "^7.7.5", "@babel/core": "^7.7.5", "@babel/plugin-proposal-class-properties": "^7.7.4", + "@babel/plugin-proposal-nullish-coalescing-operator": "^7.7.4", + "@babel/plugin-proposal-optional-chaining": "^7.7.5", "@babel/preset-env": "^7.7.6", "@babel/preset-react": "^7.7.4", "@babel/preset-typescript": "^7.7.4", @@ -97,6 +99,7 @@ "re-resizable": "^6.1.1", "react-codemirror2": "^6.0.0", "react-docgen-typescript": "^1.16.1", + "react-use": "^13.12.2", "read-pkg-up": "^7.0.1", "scope-eval": "^1.0.0", "style-loader": "^1.0.1", diff --git a/src/Playroom/Preview/Iframe.tsx b/src/Playroom/Preview/Iframe.tsx index 13137dff..ac229c57 100644 --- a/src/Playroom/Preview/Iframe.tsx +++ b/src/Playroom/Preview/Iframe.tsx @@ -1,35 +1,69 @@ -import React, { Component, AllHTMLAttributes } from 'react'; +import React, { + useState, + useEffect, + useRef, + AllHTMLAttributes, + MutableRefObject +} from 'react'; +import { useIntersection } from 'react-use'; -interface State { - loaded: boolean; +interface IframeProps extends AllHTMLAttributes { + src: string; + intersectionRootRef: MutableRefObject; } -interface Props extends AllHTMLAttributes {} - -export default class Iframe extends Component { - constructor(props: Props) { - super(props); - - this.state = { - loaded: false - }; - } - - handleLoad = () => { - this.setState({ loaded: true }); - }; - - render() { - const { style, ...props } = this.props; - const { loaded } = this.state; - - const combinedStyles = { - ...style, - transition: 'opacity .3s ease', - opacity: loaded ? 1 : 0 - }; - - return ( -