|
| 1 | +/* eslint-disable strict, no-var, vars-on-top, prefer-template */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const url = window.location.pathname; |
| 6 | + |
| 7 | +function createUrlForVersion(version) { |
| 8 | + return url.replace(/react-native-paper\//, `react-native-paper/${version}/`); |
| 9 | +} |
| 10 | + |
| 11 | +function appendVersionBanner() { |
| 12 | + const html = ` |
| 13 | + <style> |
| 14 | + .banner { |
| 15 | + margin: 0; |
| 16 | + padding: 12px; |
| 17 | + margin: 24px; |
| 18 | + border-radius: 3px; |
| 19 | + text-align: center; |
| 20 | + background-color: #f8f9fa; |
| 21 | + margin-right: 70px; |
| 22 | + |
| 23 | + @media (max-width: 640px) { |
| 24 | + margin-top: 72px; |
| 25 | + } |
| 26 | + } |
| 27 | +
|
| 28 | + .menu-button { |
| 29 | + position: relative; |
| 30 | + font-size: 14px; |
| 31 | + padding: 3px 4px 4px 4px; |
| 32 | + margin-left: 2px; |
| 33 | + border-radius: 4px; |
| 34 | + color: #6200ee; |
| 35 | + transition: background-color 200ms; |
| 36 | + border: 1px solid #6200ee; |
| 37 | + display: inline-block; |
| 38 | + cursor: default; |
| 39 | + } |
| 40 | +
|
| 41 | + .menu-button:hover { |
| 42 | + background-color: #efefef; |
| 43 | + } |
| 44 | +
|
| 45 | + .menu-button:focus { |
| 46 | + outline-offset: -3px; |
| 47 | + } |
| 48 | +
|
| 49 | + .menu-button:focus > ul { |
| 50 | + opacity: 1; |
| 51 | + pointer-events: auto; |
| 52 | + } |
| 53 | +
|
| 54 | + .menu-wrapper { |
| 55 | + position: relative; |
| 56 | + display: inline-block; |
| 57 | + width: 0; |
| 58 | + height: 0; |
| 59 | + } |
| 60 | +
|
| 61 | + .list { |
| 62 | + min-width: 110px; |
| 63 | + display: flex; |
| 64 | + position: absolute; |
| 65 | + z-index: 2; |
| 66 | + margin: 0; |
| 67 | + padding: 0; |
| 68 | + left: 0; |
| 69 | + top: 30px; |
| 70 | + list-style: none; |
| 71 | + flex-direction: column; |
| 72 | + background-color: #f8f9fa; |
| 73 | + box-shadow: 0px 2px 5px -1px #dedede; |
| 74 | + border-radius: 5px; |
| 75 | + overflow: hidden; |
| 76 | + opacity: 0; |
| 77 | + transition: opacity 150ms; |
| 78 | + pointer-events: none; |
| 79 | + } |
| 80 | +
|
| 81 | + .list:focus-within { |
| 82 | + opacity: 1; |
| 83 | + pointer-events: auto; |
| 84 | + } |
| 85 | +
|
| 86 | + .list:hover { |
| 87 | + opacity: 1; |
| 88 | + } |
| 89 | +
|
| 90 | + .list-item { |
| 91 | + flex: 1; |
| 92 | + margin: 0; |
| 93 | + box-shadow: inset 0 1px 0 0 #efefef; |
| 94 | + text-align: left; |
| 95 | + display: flex; |
| 96 | + } |
| 97 | +
|
| 98 | + .link { |
| 99 | + flex: 1; |
| 100 | + padding: 10px 5px; |
| 101 | + transition: background-color 250ms; |
| 102 | + } |
| 103 | +
|
| 104 | + .link:hover { |
| 105 | + background-color: #efefef; |
| 106 | + } |
| 107 | +
|
| 108 | + .link:focus { |
| 109 | + background-color: #efefef; |
| 110 | + outline-offset: -3px; |
| 111 | + outline-width: 3px; |
| 112 | + } |
| 113 | + </style> |
| 114 | + <div class="banner"> |
| 115 | + Looking for the documentation for previous version? You can find it |
| 116 | + <div tabindex="0" class="menu-button" role="button" aria-haspopup="true"> |
| 117 | + here |
| 118 | + <ul class="list" role="menu"> |
| 119 | + <li class="list-item"> |
| 120 | + <a class="link" href="${createUrlForVersion( |
| 121 | + '1.0' |
| 122 | + )}" role="menuitem">v1.x</a> |
| 123 | + </li> |
| 124 | + <li class="list-item"> |
| 125 | + <a class="link" href="${createUrlForVersion( |
| 126 | + '2.0' |
| 127 | + )}" role="menuitem">v2.x</a> |
| 128 | + </li> |
| 129 | + </ul> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + `; |
| 133 | + |
| 134 | + if (document.getElementById('version-banner')) return; |
| 135 | + |
| 136 | + const div = document.createElement('div'); |
| 137 | + div.innerHTML = html; |
| 138 | + div.id = 'version-banner'; |
| 139 | + |
| 140 | + const root = document.getElementById('root'); |
| 141 | + |
| 142 | + if (root) { |
| 143 | + if (root.childNodes.length === 1) { |
| 144 | + const wrappingDiv = root.childNodes[0]; |
| 145 | + if (wrappingDiv.childNodes.length === 2) { |
| 146 | + const targetDiv = wrappingDiv.childNodes[1]; |
| 147 | + targetDiv.insertBefore(div, targetDiv.childNodes[0]); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +appendVersionBanner(); |
| 154 | + |
| 155 | +var mutationObserver = new MutationObserver(mutations => { |
| 156 | + mutations.forEach(appendVersionBanner); |
| 157 | +}); |
| 158 | + |
| 159 | +mutationObserver.observe(document.documentElement, { |
| 160 | + childList: true, |
| 161 | + subtree: true, |
| 162 | +}); |
0 commit comments