Switch Installation to a tab when hash is present (#9422)
Dan Abramov committed
Apr 13, 2017 at 21:51 UTC
4c8c5debac5074427d5e6ce19896d4062e078a83
1 file changed
+39
docs/docs/installation.md
+39
@@ -195,6 +195,10 @@ To load a specific version of `react` and `react-dom`, replace `15` with the ver
195
If you use Bower, React is available via the `react` package.
196
197
<script>
198
+/**
199
+ * The code below is based on a snippet from React Native Getting Started page.
200
+ */
201
+
202
// Convert <div>...<span><block /></span>...</div>
203
// Into <div>...<block />...</div>
204
var blocks = document.getElementsByTagName('block');
@@ -219,4 +223,39 @@ function display(type, value) {
223
container.className = 'display-' + type + '-' + value + ' ' +
224
container.className.replace(RegExp('display-' + type + '-[a-z]+ ?'), '');
225
}
226
+
227
+// If we are coming to the page with a hash in it (i.e. from a search, for example), try to get
228
+// us as close as possible to the correct platform and dev os using the hashtag and block walk up.
229
+var foundHash = false;
230
+if (window.location.hash !== '' && window.location.hash !== 'content') { // content is default
231
+ // Hash links are added a bit later so we wait for them.
232
+ window.addEventListener('DOMContentLoaded', selectTabForHashLink);
233
+}
234
+
235
+function selectTabForHashLink() {
236
+ var hashLinks = document.querySelectorAll('a.hash-link');
237
+ for (var i = 0; i < hashLinks.length && !foundHash; ++i) {
238
+ if (hashLinks[i].hash === window.location.hash) {
239
+ var parent = hashLinks[i].parentElement;
240
+ while (parent) {
241
+ if (parent.tagName === 'BLOCK') {
242
+ var target = null;
243
+ if (parent.className.indexOf('fiddle') > -1) {
244
+ target = 'fiddle';
245
+ } else if (parent.className.indexOf('newapp') > -1) {
246
+ target = 'newapp';
247
+ } else if (parent.className.indexOf('existingapp') > -1) {
248
+ target = 'existingapp';
249
+ } else {
250
+ break; // assume we don't have anything.
251
+ }
252
+ display('target', target);
253
+ foundHash = true;
254
+ break;
255
+ }
256
+ parent = parent.parentElement;
257
+ }
258
+ }
259
+ }
260
+}
261
</script>
\ No newline at end of file