{"id":1027,"date":"2014-11-21T01:32:36","date_gmt":"2014-11-20T17:32:36","guid":{"rendered":"http:\/\/blog.turn.tw\/?page_id=1027"},"modified":"2014-11-21T01:49:27","modified_gmt":"2014-11-20T17:49:27","slug":"react-lightbox-plugin-1-0","status":"publish","type":"page","link":"https:\/\/blog.turn.tw\/?page_id=1027","title":{"rendered":"React Lightbox Plugin 1.0"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>React Lightbox is a React plugin for implementing lightbox effect easily with React.<br \/>\nIt provides some basic functions for you to extend including:<br \/>\n* Turn on\/off the lightbox (openLightbox, closeLightbox)<br \/>\n* Communicate variables between different elements in the lightbox (setLightboxState).<\/p>\n<h3>Demo<\/h3>\n<p><a target='_blank' href=\"http:\/\/howtomakeaturn.github.io\/react-lightbox\/basic.html#\">Basic usage<\/a><br \/>\n<a target='_blank' href=\"http:\/\/howtomakeaturn.github.io\/react-lightbox\/pass-variable.html#\">Passing variables<\/a><br \/>\n<a target='_blank' href=\"http:\/\/howtomakeaturn.github.io\/react-lightbox\/custom-element.html\">Custome elements<\/a><br \/>\n<a target='_blank' href=\"http:\/\/howtomakeaturn.github.io\/react-lightbox\/communication-between-elements.html#\">Communication between elements<\/a><\/p>\n<h3>Getting started<\/h3>\n<p>To use the lightbox plugin, include the react library, the JSX transformer, and the react-lightbox library inside the <head> tag of your HTML document:<\/p>\n<pre>\r\n<head>\r\n    <script src='\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/0.12.0\/JSXTransformer.js'><\/script>\r\n    <script src='\/\/cdnjs.cloudflare.com\/ajax\/libs\/react\/0.12.0\/react.js'><\/script>\r\n    <script type=\"text\/jsx\" src='react-lightbox.jsx'><\/script>    \r\n<\/head>\r\n<\/pre>\n<p>Start by telling react-lightbox to render the elements you want when the document is loaded.<br \/>\nJust put them inside the LightboxTrigger element and LightboxModal element:<\/p>\n<pre>\r\n<body>\r\n    <div id='react-canvas'><\/div>\r\n    <script type=\"text\/jsx\">\r\n        \/** @jsx React.DOM *\/\r\n        \r\n        React.renderComponent(\r\n            <Lightbox>\r\n                <LightboxTrigger>\r\n                    <a href='#'>Click me to open!<\/a>\r\n                <\/LightboxTrigger>\r\n                <LightboxModal>\r\n                    <div>\r\n                        <h1>This is the basic usage!<\/h1>\r\n                        <p>Good luck :D<\/p>\r\n                    <\/div>\r\n                <\/LightboxModal>\r\n            <\/Lightbox>,                \r\n            document.getElementById('react-canvas')\r\n        );\r\n    <\/script>\r\n<\/body>\r\n<\/pre>\n<p>We done! That&#8217;s all you need to do!<br \/>\nClick on the text and you&#8217;ll see the beautiful lightbox popup on your browser!<br \/>\nSee the <a target='_blank' href=\"http:\/\/howtomakeaturn.github.io\/react-lightbox\/basic.html#\">Basic usage<\/a>.<\/p>\n<p><i>NOTE! react-lightbox will auto-bind openLightbox function to the onClick event of the element inside the LightboxTrigger. You don&#8217;t need to set the onClick event yourself.<\/i><\/p>\n<p>If you think simply use &#8216;a&#8217; tag as trigger and &#8216;p&#8217; tag as the content of lightbox is too simple, you can create custom elements for both LightboxTrigger and LightboxModal:<\/p>\n<pre>\r\n<body>\r\n    <div id='react-canvas'><\/div>\r\n    <script type=\"text\/jsx\">\r\n        \/** @jsx React.DOM *\/\r\n        var ToggleButton = React.createClass({\r\n            render: function(){\r\n                return(<button onClick={this.props.openLightbox}>Open Lightbox<\/button>);\r\n            }\r\n        });\r\n        \r\n        var MyPanel = React.createClass({            \r\n            onClickSave: function(){\r\n                alert('saved!');\r\n                this.props.closeLightbox();\r\n            },            \r\n            render: function(){\r\n                return (\r\n                    <div>\r\n                        <h3>My Panel<\/h3>\r\n                        <hr \/>\r\n                        <textarea placeholder='Type something here...'><\/textarea>\r\n                        <hr \/>\r\n                        <button onClick={this.onClickSave}>Save<\/button>\r\n                    <\/div>                \r\n                );\r\n            }\r\n        });                \r\n        React.renderComponent(\r\n            <Lightbox>\r\n                <LightboxTrigger>\r\n                    <ToggleButton \/>\r\n                <\/LightboxTrigger>\r\n                <LightboxModal>\r\n                    <MyPanel \/>\r\n                <\/LightboxModal>\r\n            <\/Lightbox>,                \r\n            document.getElementById('react-canvas')\r\n        );\r\n    <\/script>\r\n<\/body>\r\n<\/pre>\n<p>Your custom elements inside LightboxTrigger and LightboxModal will receive &#8216;openLightbox&#8217; and &#8216;closeLightbox&#8217; functions automatically. Use them in whatever way you want.<\/p>\n<p>The live example for above code is here: <a target='_blank' href=\"http:\/\/howtomakeaturn.github.io\/react-lightbox\/custom-element.html\">Custome elements<\/a><\/p>\n<p><i>NOTE! If you use custom element as trigger, react-lightbox will NOT auto-bind openLightbox for you. Remember to bind it as the example shows.<\/i><\/p>\n<h3>Advanced<\/h3>\n<p>Sometimes you need to share variables between elements inside the lightbox. For instance, you want the change the text in the trigger according to the text user type into the textarea in modal. How can we deal with that?<\/p>\n<p>React-lightbox provide &#8216;setLightboxState&#8217; function to solve this.<br \/>\nFirst, pass the values you want to share between elements to the Lightbox element &#8216;data&#8217; props: <\/p>\n<pre>\r\n<Lightbox data={{ content: ''}}>\r\n<\/pre>\n<p>Then you can use this values from the props:<\/p>\n<pre>\r\n<textarea ref='note' defaultValue={this.props.content} placeholder='Type something here...'><\/textarea>\r\n<\/pre>\n<p>To change the shared value, use the &#8216;setLightboxState&#8217; function which all children elements will receive automatically.<\/p>\n<p>What happened behind it is easy. Lightbox set the &#8216;data&#8217; object as state. And then pass every value inside the state to all children as props! So every time you &#8216;setLightboxState&#8217;, all the elements will update automatically!<\/p>\n<p>Here&#8217;s a more concrete example:<\/p>\n<pre>\r\n<body>\r\n    <div id='react-canvas'><\/div>\r\n    <script type=\"text\/jsx\">\r\n        \/** @jsx React.DOM *\/\r\n        var ToggleText = React.createClass({                        \r\n            render: function() {\r\n                var text = this.props.content.trim() ? 'View Notes' : 'Add Notes';                \r\n                if (this.props.content.trim()){\r\n                    return ( <a style={{color: 'red'}} href='#' onClick={this.props.onClick}>{text}<\/a> );                                                            \r\n                } else {\r\n                    return ( <a href='#' onClick={this.props.openLightbox}>{text}<\/a>);                                                \r\n                }                \r\n            }\r\n        });\r\n\r\n        var NotePanel = React.createClass({\r\n            \r\n            clickSave: function(){\r\n                var content = this.refs['note'].getDOMNode().value;\r\n                this.props.setLightboxState({ content: content});\r\n                \/\/ you may want to send ajax here\r\n                \/\/$.post('\/ajax\/document\/update-note', data);\r\n                this.props.closeLightbox();\r\n            },\r\n            \r\n            render: function(){\r\n                return (\r\n                    <div>\r\n                        <h3>Note<\/h3>\r\n                        <hr \/>\r\n                        <textarea ref='note' defaultValue={this.props.content} placeholder='Type something here...'><\/textarea>\r\n                        <hr \/>\r\n                        <button onClick={this.clickSave}>Save<\/button>        \r\n                    <\/div>\r\n                );\r\n            }\r\n        });\r\n\r\n        React.renderComponent(\r\n            <Lightbox data={{ content: ''}}>\r\n                <LightboxTrigger>\r\n                    <ToggleText \/>\r\n                <\/LightboxTrigger>\r\n                <LightboxModal>\r\n                    <NotePanel \/>\r\n                <\/LightboxModal>\r\n            <\/Lightbox>,                \r\n            document.getElementById('react-canvas')\r\n        );\r\n    <\/script>\r\n<\/body>\r\n<\/pre>\n<p>A live demo for this can be found here: <a target='_blank' href=\"http:\/\/howtomakeaturn.github.io\/react-lightbox\/communication-between-elements.html#\">Communication between elements<\/a><\/p>\n<h3>Download<\/h3>\n<p>Get the plugin on <a href='https:\/\/github.com\/howtomakeaturn\/React-Lightbox'>Github<\/a><\/p>\n<h3>Requirements<\/h3>\n<p>* React<br \/>\n* JSXTransformer<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction React Lightbox is a React plugin for imple &hellip; <a href=\"https:\/\/blog.turn.tw\/?page_id=1027\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">React Lightbox Plugin 1.0<\/span> <span class=\"meta-nav\">&rarr;<\/span> <\/a><\/p>\n","protected":false},"author":1,"featured_media":1061,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_mi_skip_tracking":false},"_links":{"self":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1027"}],"collection":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1027"}],"version-history":[{"count":19,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1027\/revisions"}],"predecessor-version":[{"id":1078,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1027\/revisions\/1078"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/media\/1061"}],"wp:attachment":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}