From 1822bc5eafaf35adcb350ec0bcee5b190a611852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Fri, 16 Feb 2018 06:10:06 -0800 Subject: [PATCH] Copy generated code to clipboard Summary: Generated code modal now has a copy to clipboard button Reviewed By: emilsjolander Differential Revision: D7009716 fbshipit-source-id: 7a9b083d7067aa66ffa5b9d9c23741321547c4f6 --- .../components/Playground/CodeGenerators.css | 20 ++++++ .../components/Playground/CodeGenerators.js | 66 +++++++++++++++---- 2 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 website/src/components/Playground/CodeGenerators.css diff --git a/website/src/components/Playground/CodeGenerators.css b/website/src/components/Playground/CodeGenerators.css new file mode 100644 index 00000000..483e7ab3 --- /dev/null +++ b/website/src/components/Playground/CodeGenerators.css @@ -0,0 +1,20 @@ +.CodeGeneratorsTitle { + display: flex; + justify-content: space-between; +} + + +.CodeGeneratorsTitle a { + font-size: 13px; + font-weight: normal; + margin-right: 20px; +} + +.CodeGeneratorsCopyText { + position: absolute; + top: -9999em; + left: -9999em; + pointer-events: none; + width: 0; + height: 0; +} diff --git a/website/src/components/Playground/CodeGenerators.js b/website/src/components/Playground/CodeGenerators.js index 338e5ce0..46e127cb 100644 --- a/website/src/components/Playground/CodeGenerators.js +++ b/website/src/components/Playground/CodeGenerators.js @@ -11,7 +11,7 @@ */ import React, {Component} from 'react'; -import {Menu, Button, Row, Col, Dropdown, Icon, Modal} from 'antd'; +import {Menu, Button, Row, Col, Dropdown, Icon, Modal, Tooltip} from 'antd'; import SyntaxHighlighter, { registerLanguage, } from 'react-syntax-highlighter/prism-light'; @@ -29,6 +29,8 @@ registerLanguage('jsx', jsx); registerLanguage('java', java); registerLanguage('objectivec', objectivec); +import './CodeGenerators.css'; + import type {LayoutRecordT} from './LayoutRecord'; import type {Yoga$Direction} from 'yoga-layout'; @@ -38,6 +40,7 @@ type Props = { }; type State = { showModal: ?string, + copied: boolean, }; const LANGUAGES = { @@ -62,10 +65,19 @@ const LANGUAGES = { export default class CodeGenerators extends Component { state = { showModal: null, + copied: false, }; onClick = ({key}: {key: string}) => this.setState({showModal: key}); + onCopy = () => { + if (this._ref) { + this._ref.select(); + document.execCommand('Copy'); + this.setState({copied: true}); + } + }; + render() { const {showModal} = this.state; @@ -77,27 +89,53 @@ export default class CodeGenerators extends Component { ); + const code = showModal + ? LANGUAGES[showModal].generator( + this.props.layoutDefinition, + this.props.direction, + ) + : ''; + return [ + {LANGUAGES[showModal].title} + this.setState({copied: false})}> + copy to clipboard + + + ) : ( + '' + ) + } visible={Boolean(showModal)} footer={null} bodyStyle={{padding: 0}} onCancel={() => this.setState({showModal: null})}> {showModal && ( - - {LANGUAGES[showModal].generator( - this.props.layoutDefinition, - this.props.direction, - )} - +
+