Skip to content

Commit a8c5a23

Browse files
committed
Revert "Revert "Fix Snack links""
This reverts commit cf53475.
1 parent cf53475 commit a8c5a23

File tree

5 files changed

+55
-37
lines changed

5 files changed

+55
-37
lines changed

docusaurus.config.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
'https://buttons.github.io/buttons.js',
1414
'/js/snack.js',
1515
'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js',
16-
'/js/code-block-buttons.js'
16+
'/js/code-block-buttons.js',
1717
],
1818
themeConfig: {
1919
prism: {
@@ -31,17 +31,17 @@ module.exports = {
3131
src: 'img/spiro.svg',
3232
},
3333
links: [
34-
{
35-
to: 'versions',
36-
label: 'Versions',
37-
},
38-
{to: 'docs/getting-started', label: 'Docs', position: 'left'},
39-
{to: 'blog', label: 'Blog', position: 'left'},
34+
{ to: 'docs/getting-started', label: 'Docs', position: 'left' },
35+
{ to: 'blog', label: 'Blog', position: 'left' },
4036
{
4137
href: 'https://github.com/react-navigation',
4238
label: 'GitHub',
4339
position: 'right',
4440
},
41+
{
42+
to: 'versions',
43+
label: 'Versions',
44+
},
4545
],
4646
},
4747
footer: {
@@ -73,15 +73,17 @@ module.exports = {
7373
},
7474
{
7575
label: 'Get help on Stack Overflow',
76-
href: 'https://stackoverflow.com/questions/tagged/react-navigation',
76+
href:
77+
'https://stackoverflow.com/questions/tagged/react-navigation',
7778
},
7879
{
7980
label: 'Request a feature on Canny',
8081
href: 'https://react-navigation.canny.io/feature-requests',
8182
},
8283
{
8384
label: 'Report a bug on Github',
84-
href: 'https://github.com/react-navigation/react-navigation/issues/new/choose',
85+
href:
86+
'https://github.com/react-navigation/react-navigation/issues/new/choose',
8587
},
8688
],
8789
},
@@ -106,14 +108,16 @@ module.exports = {
106108
},
107109
},
108110
plugins: [
109-
path.resolve(__dirname, './src/plugins/docusaurus-plugin-redirect-html')
111+
path.resolve(__dirname, './src/plugins/docusaurus-plugin-redirect-html'),
110112
],
111113
presets: [
112114
[
113115
'@docusaurus/preset-classic',
114116
{
115117
docs: {
116118
sidebarPath: require.resolve('./sidebars.js'),
119+
docLayoutComponent: require.resolve('./src/layouts/DocPage.js'),
120+
docItemComponent: require.resolve('./src/layouts/DocItem.js'),
117121
editUrl:
118122
'https://github.com/react-navigation/react-navigation.github.io/edit/source/',
119123
remarkPlugins: [require('./src/plugins/remark-npm2yarn')],

src/css/custom.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ samp {
5252

5353
.snack-sample-link {
5454
display: block;
55-
margin-bottom: 20px;
56-
margin-top: -5px;
55+
margin-bottom: 25px;
56+
margin-top: -8px;
5757
}

src/layouts/DocItem.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import DefaultDocItem from '@theme/DocItem';
3+
4+
function DocItem(props) {
5+
return <DefaultDocItem {...props} />;
6+
}
7+
8+
export default DocItem;

src/layouts/DocPage.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import DefaultDocPage from '@theme/DocPage';
3+
4+
function DocPage(props) {
5+
React.useEffect(() => {
6+
// Tags in Head aren't immediately available when the snack script loads, so
7+
// instead let's just store this in a global...
8+
window.__reactNavigationVersion = props.docsMetadata.version;
9+
});
10+
11+
return <DefaultDocPage {...props} />;
12+
}
13+
14+
export default DocPage;

static/js/snack.js

+17-25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// NOTE(brentvatne): the package:version meta tag isn't available when this code
2+
// first executes....
13
const DEFAULT_PLATFORM = 'android';
24
const DEPS_VERSIONS = {
35
'4': [
@@ -23,16 +25,12 @@ const DEPS_VERSIONS = {
2325
next: [],
2426
};
2527

26-
function getVersion(){
27-
const navBarItems = document.getElementsByClassName('navbar__items');
28-
if(navBarItems[0] && navBarItems[0].children[2] && navBarItems[0].children[2].textContent){
29-
return navBarItems[0].children[2].textContent;
28+
function getVersion() {
29+
if (window.__reactNavigationVersion) {
30+
return window.__reactNavigationVersion;
3031
}
31-
return 'next';
3232
}
33-
// todo: should get the version somewhere, maybe within the page html,
34-
// and match the appropriate version of react-navigation/stack/tabs/drawer
35-
// based on that
33+
3634
function getSnackUrl(options) {
3735
let currentVersion = getVersion();
3836
let label = options.label || document.title;
@@ -59,24 +57,20 @@ function getSnackUrl(options) {
5957
}
6058
}
6159

62-
function findNearestPreElement(node) {
60+
function findNearestCodeBlock(node) {
6361
let nextElement = node.nextElementSibling;
6462
if (!nextElement && node.parentElement.tagName === 'P') {
6563
nextElement = node.parentElement.nextElementSibling;
6664
}
6765

6866
while (nextElement) {
69-
if(nextElement.tagName === 'DIV' && nextElement.className.includes('mdxCodeBlock_node_modules-@docusaurus-theme-classic-src-theme-MDXComponents-')){
70-
nextElement = nextElement.firstChild;
71-
}
7267
if (
73-
nextElement.tagName === 'PRE' &&
74-
nextElement.children[1] &&
75-
nextElement.children[1].tagName === 'CODE'
68+
nextElement.tagName === 'DIV' &&
69+
nextElement.className.includes(
70+
'mdxCodeBlock_node_modules-@docusaurus-theme-classic-src-theme-MDXComponents-'
71+
)
7672
) {
77-
return nextElement.children[1];
78-
} else {
79-
nextElement = nextElement.nextElementSibling;
73+
return nextElement;
8074
}
8175
}
8276
}
@@ -92,20 +86,19 @@ function appendSnackLink() {
9286
}
9387

9488
samples.forEach(samp => {
95-
let pre = findNearestPreElement(samp);
89+
let codeBlock = findNearestCodeBlock(samp);
9690

97-
if (!pre) {
91+
if (!codeBlock) {
9892
console.log(
99-
`<pre> tag with <code> child not found for <samp> element ${samp.innerText}`
93+
`Code block not found for <samp> element ${samp.innerText}`
10094
);
10195
return;
10296
}
10397

104-
let code = pre.innerText;
98+
let code = codeBlock.innerText;
10599
let label = samp.innerText;
106100
let templateId = samp.getAttribute('id');
107101

108-
109102
let link = document.createElement('a');
110103
link.className = 'snack-sample-link';
111104
link.dataset.snack = true;
@@ -117,7 +110,6 @@ function appendSnackLink() {
117110
link.innerHTML = `Try this example on Snack ${openIcon}`;
118111
}
119112

120-
121113
// Add the href and append the link element if we have some code
122114
let href = getSnackUrl({ code, templateId, label });
123115

@@ -127,7 +119,7 @@ function appendSnackLink() {
127119

128120
link.href = href;
129121

130-
pre.insertAdjacentElement('afterend', link);
122+
codeBlock.insertAdjacentElement('afterend', link);
131123

132124
// Don't try to add the link more than once!
133125
samp.remove();

0 commit comments

Comments
 (0)