|
| 1 | +import React from 'react' |
| 2 | +import data from '../../../lib/data/docs' |
| 3 | +import Link from 'next/link' |
| 4 | +import qs from 'query-string' |
| 5 | + |
| 6 | +export class NavLink extends React.Component { |
| 7 | + getCurrentHref() { |
| 8 | + const { url } = this.props |
| 9 | + const query = qs.stringify(url.query) |
| 10 | + let href = url.pathname |
| 11 | + |
| 12 | + if (query !== '') { |
| 13 | + href = `${href}?${query}` |
| 14 | + } |
| 15 | + |
| 16 | + return href |
| 17 | + } |
| 18 | + |
| 19 | + isSelected() { |
| 20 | + const { href, aliases = [] } = this.props.info |
| 21 | + const currentHref = this.getCurrentHref() |
| 22 | + |
| 23 | + if (href === currentHref) return true |
| 24 | + if (aliases.indexOf(currentHref) >= 0) return true |
| 25 | + |
| 26 | + return false |
| 27 | + } |
| 28 | + |
| 29 | + render() { |
| 30 | + const { info } = this.props |
| 31 | + |
| 32 | + return ( |
| 33 | + <div> |
| 34 | + <Link href={info.href} as={info.as || info.href}> |
| 35 | + <a className={this.isSelected() ? 'selected' : ''}>{info.name}</a> |
| 36 | + </Link> |
| 37 | + <style jsx>{` |
| 38 | + a { |
| 39 | + text-decoration: none; |
| 40 | + font-size: 14px; |
| 41 | + color: #000; |
| 42 | + } |
| 43 | +
|
| 44 | + a.selected { |
| 45 | + font-weight: 600; |
| 46 | + color: #000; |
| 47 | + } |
| 48 | + `}</style> |
| 49 | + </div> |
| 50 | + ) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +export default class DocsNavbarDesktop extends React.Component { |
| 55 | + renderPost(info, level) { |
| 56 | + if (info.posts) { |
| 57 | + return this.renderCategory(info, level + 1) |
| 58 | + } |
| 59 | + |
| 60 | + return ( |
| 61 | + <div className="link" key={info.href}> |
| 62 | + <NavLink info={info} url={this.props.url} /> |
| 63 | + <style jsx>{` |
| 64 | + .link { |
| 65 | + margin: 10px 0; |
| 66 | + } |
| 67 | +
|
| 68 | + @media screen and (max-width: 950px) { |
| 69 | + .link { |
| 70 | + padding: 20px 0; |
| 71 | + margin: 0; |
| 72 | + border-bottom: 1px solid #EEE; |
| 73 | + } |
| 74 | + } |
| 75 | + `}</style> |
| 76 | + </div> |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + renderCategory(info, level = 1) { |
| 81 | + const levelClass = `level-${level}` |
| 82 | + const postStyle = { |
| 83 | + marginLeft: 10 * (level - 1) |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className={`category ${levelClass}`} key={info.name}> |
| 88 | + <div className="label"> |
| 89 | + {info.href ? <NavLink info={info} url={this.props.url} /> : info.name} |
| 90 | + </div> |
| 91 | + <div className="posts" style={postStyle}> |
| 92 | + {info.posts.map(postInfo => this.renderPost(postInfo, level))} |
| 93 | + </div> |
| 94 | + <style jsx>{` |
| 95 | + .label { |
| 96 | + margin: 0 0 15px 0; |
| 97 | + font-size: 13px; |
| 98 | + text-transform: uppercase; |
| 99 | + letter-spacing: 1.3px; |
| 100 | + font-weight: 400; |
| 101 | + color: #888; |
| 102 | + cursor: default; |
| 103 | + } |
| 104 | +
|
| 105 | + .level-2 .label { |
| 106 | + font-size: 14px; |
| 107 | + font-weight: 400; |
| 108 | + margin: 10px 0; |
| 109 | + text-transform: none; |
| 110 | + letter-spacing: 0; |
| 111 | + cursor: default; |
| 112 | + } |
| 113 | +
|
| 114 | + .category.level-1 { |
| 115 | + margin: 0 0 50px 0; |
| 116 | + } |
| 117 | +
|
| 118 | + @media screen and (max-width: 950px) { |
| 119 | + .label { |
| 120 | + margin: -10px 0; |
| 121 | + } |
| 122 | +
|
| 123 | + .level-2 .label { |
| 124 | + padding: 20px 0; |
| 125 | + margin: 0; |
| 126 | + border-bottom: 1px solid #EEE; |
| 127 | + } |
| 128 | + } |
| 129 | + `}</style> |
| 130 | + </div> |
| 131 | + ) |
| 132 | + } |
| 133 | + |
| 134 | + render() { |
| 135 | + return ( |
| 136 | + <div> |
| 137 | + {data.map(categoryInfo => this.renderCategory(categoryInfo))} |
| 138 | + </div> |
| 139 | + ) |
| 140 | + } |
| 141 | +} |
0 commit comments