Skip to content

Commit db04093

Browse files
dabbottbubblesunyum
authored andcommitted
Add react-native-web-player to core components docs
Summary: This PR adds the interactive [React Native Web Player](http://dabbott.github.io/react-native-web-player/) to the docs. The web player is an embeddable iframe which runs React Native code using components from [react-native-web](https://github.com/necolas/react-native-web). For now, it's primarily for educational purposes, since only the basic components are implemented. Some details: - The iframe is loaded from MaxCDN using rawgit, locked down to a git tag. - Asset paths (i.e. images) are resolved relative to `//facebook.github.io/react-native/` - When viewed on mobile, it falls back to the syntax-highlighted code blocks. The WebPlayer can be inserted into markdown by using the fences: ``` ```ReactNativeWebPlayer import ... AppRegistry.registerComponent ... `` ` ``` ![screen shot 2016-06-22 at 12 46 50 pm](https://cloud.githubusercontent.com/assets/1198882/16281068/7056804e-3877-11e6-82f7-ece245690548.png) I didn't actually add the WebPlayer to any docs pages in this PR. That we c Closes facebook#8328 Differential Revision: D3471527 Pulled By: lacker fbshipit-source-id: 704da41cd77e08c7e2bc820557a74d36e88e8eb7
1 parent bf1fc86 commit db04093

File tree

4 files changed

+114
-2
lines changed

4 files changed

+114
-2
lines changed

website/core/Marked.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
var React = require('React');
1010
var Prism = require('Prism');
11+
var WebPlayer = require('WebPlayer');
1112
var Header = require('Header');
1213

1314
/**
@@ -827,7 +828,16 @@ Parser.prototype.tok = function() {
827828
);
828829
}
829830
case 'code': {
830-
return <Prism>{this.token.text}</Prism>;
831+
var lang = this.token.lang
832+
, text = this.token.text;
833+
834+
if (lang && lang.indexOf('ReactNativeWebPlayer') === 0) {
835+
return (
836+
<WebPlayer params={lang.split('?')[1]}>{text}</WebPlayer>
837+
);
838+
}
839+
840+
return <Prism>{text}</Prism>;
831841
}
832842
case 'table': {
833843
var table = []

website/core/WebPlayer.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @providesModule WebPlayer
10+
*/
11+
12+
var React = require('React');
13+
var Prism = require('Prism');
14+
15+
/**
16+
* Use the WebPlayer by including a ```ReactNativeWebPlayer``` block in markdown.
17+
*
18+
* Optionally, include url parameters directly after the block's language. For
19+
* the complete list of url parameters, see: https://github.com/dabbott/react-native-web-player
20+
*
21+
* E.g.
22+
* ```ReactNativeWebPlayer?platform=android
23+
* import React from 'react';
24+
* import { AppRegistry, Text } from 'react-native';
25+
*
26+
* const App = () => <Text>Hello World!</Text>;
27+
*
28+
* AppRegistry.registerComponent('MyApp', () => App);
29+
* ```
30+
*/
31+
var WebPlayer = React.createClass({
32+
parseParams: function(paramString) {
33+
var params = {};
34+
35+
if (paramString) {
36+
var pairs = paramString.split('&');
37+
for (var i = 0; i < pairs.length; i++) {
38+
var pair = pairs[i].split('=');
39+
params[pair[0]] = pair[1];
40+
}
41+
}
42+
43+
return params;
44+
},
45+
46+
render: function() {
47+
var hash = `#code=${encodeURIComponent(this.props.children)}&runApp=AwesomeProject`;
48+
49+
if (this.props.params) {
50+
hash += `&${this.props.params}`;
51+
}
52+
53+
return (
54+
<div className={'web-player'}>
55+
<Prism>{this.props.children}</Prism>
56+
<iframe
57+
style={{marginTop: 4}}
58+
width='880'
59+
height={this.parseParams(this.props.params).platform === 'android' ? '425' : '420'}
60+
data-src={`//cdn.rawgit.com/dabbott/react-native-web-player/v0.1.2/index.html${hash}`}
61+
frameBorder='0'
62+
/>
63+
</div>
64+
);
65+
},
66+
});
67+
68+
module.exports = WebPlayer;

website/src/react-native/css/react-native.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,3 +1557,18 @@ input#algolia-doc-search:focus {
15571557
margin-bottom: 0;
15581558
padding-bottom: 0;
15591559
}
1560+
1561+
1562+
/** Web player **/
1563+
1564+
.web-player > iframe, .web-player > .prism {
1565+
display: none;
1566+
}
1567+
1568+
.web-player.desktop > iframe {
1569+
display: block;
1570+
}
1571+
1572+
.web-player.mobile > .prism {
1573+
display: block;
1574+
}

website/src/react-native/js/scripts.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,29 @@
77
document.addEventListener('DOMContentLoaded', init);
88

99
function init() {
10-
if (isMobile()) {
10+
var mobile = isMobile();
11+
12+
if (mobile) {
1113
document.querySelector('.nav-site-wrapper a[data-target]').addEventListener('click', toggleTarget);
1214
}
1315

16+
var webPlayerList = document.querySelectorAll('.web-player');
17+
18+
// Either show interactive or static code block, depending on desktop or mobile
19+
for (var i = 0; i < webPlayerList.length; ++i) {
20+
webPlayerList[i].classList.add(mobile ? 'mobile' : 'desktop');
21+
22+
if (!mobile) {
23+
24+
// Determine location to look up required assets
25+
var assetRoot = encodeURIComponent(document.location.origin + '/react-native');
26+
27+
// Set iframe src. Do this dynamically so the iframe never loads on mobile.
28+
var iframe = webPlayerList[i].querySelector('iframe');
29+
iframe.src = iframe.getAttribute('data-src') + '&assetRoot=' + assetRoot;
30+
}
31+
}
32+
1433
var backdrop = document.querySelector('.modal-backdrop');
1534
if (!backdrop) return;
1635

0 commit comments

Comments
 (0)