Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | // VariantSelect: allows a variant to be set up within a document hierarchy
//
// For example, given two paths `/docs/v1.0/foo` and `/docs/v2.0/foo`, the
// second folder acts as a variant. If you use <VariantSelect root="/docs">
// then you'll get a selection for the different variants (v1.0, v2.0).
import React from 'react';
import styled from 'styled-components';
import { Location } from '@reach/router';
import { Dropdown } from '@primer/components';
import navItems from '../nav.yml';
function getVariantAndPage(path, root) {
if (!path.startsWith(root + '/')) {
return null;
}
path = path.substring(root.length + 1);
const match = /^([^/]+)(?:\/(.*))?/.exec(path);
if (!match) {
return null;
}
return { variant: match[1], page: match[2] };
}
function find(fn, items) {
if (!items) {
items = navItems;
}
for (let i = 0; i < items.length; i++) {
const item = items[i];
let result = fn(item);
if (!result && item.children) {
result = find(fn, item.children);
}
if (result) {
return result;
}
}
return null;
}
function getVariantPages(root, page) {
const pages = [];
const rootItem = find((item) => item.url === root ? item : null);
if (rootItem && rootItem.children) {
rootItem.children.forEach((variant) => {
if (!variant.children) {
return;
}
const vp = getVariantAndPage(variant.url, root);
let variantPage;
if (vp.page === page) {
variantPage = variant;
} else {
variantPage = find((item) => {
const vp = getVariantAndPage(item.url, root);
return (vp && vp.page === page) ? item : null;
}, variant.children);
}
pages.push({ variant: variant, page: variantPage });
});
}
return pages;
}
VariantSelect.Menu = styled(Dropdown.Menu)`
width: ${props => props.width ? props.width : '160px'};
`
function VariantSelect(props) {
return (
<Location>
{({location}) => {
const vp = getVariantAndPage(location.pathname, props.root);
const variantPages = getVariantPages(props.root, vp.page);
const items = [];
let selectedItem = variantPages[0];
if (variantPages.length === 0) {
return;
}
variantPages.forEach((match) => {
console.log(match);
if (match && match.page && match.page.url === location.pathname) {
selectedItem = match;
}
items.push(<Dropdown.Item onClick={() => { window.location.href = match.page.url; }} key={match.variant.title}>{match.variant.title}</Dropdown.Item>);
});
return (
<Dropdown overlay={props.overlay}>
<Dropdown.Button>{selectedItem.variant.title}</Dropdown.Button>
<VariantSelect.Menu direction={props.direction} width={props.menuWidth}>
{items}
</VariantSelect.Menu>
</Dropdown>
);
}}
</Location>
);
}
export default VariantSelect
|