-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglobal.js
More file actions
74 lines (60 loc) · 2.37 KB
/
global.js
File metadata and controls
74 lines (60 loc) · 2.37 KB
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
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
// Hack v1: Does not always work
// Issue: CSS :target selector does not work with client-side routing
// Hack: Update the URL hash on each route update
// export function onRouteUpdate({location, previousLocation}) {
// if (location.pathname !== previousLocation?.pathname) {
// if (ExecutionEnvironment.canUseDOM && window.location.hash) {
// const {hash} = window.location
// window.location.hash = ""
// window.setTimeout(() => {
// window.location.hash = hash
// }, 1)
// }
// }
// return undefined;
// }
// Hack v2
// Issue: CSS :target selector does not work with client-side routing
// Hack: Add a class to the element with the hash ID until Docusaurus fixes the issue
function handleHashChange() {
if (ExecutionEnvironment.canUseDOM && window.location.hash) {
const hash = window.location.hash.substring(1)
const targetElement = document.getElementById(hash);
const targetClassName = "url-target"
document.querySelectorAll(`.${targetClassName}`).forEach((el) => el.classList.remove(targetClassName))
if (targetElement) {
targetElement.classList.add(targetClassName);
}
}
}
export function onRouteDidUpdate({location, previousLocation}) {
if (location.pathname !== previousLocation?.pathname) {
if (ExecutionEnvironment.canUseDOM && window.location.hash) {
handleHashChange()
}
}
return undefined;
}
if (ExecutionEnvironment.canUseDOM) {
window.addEventListener("hashchange", () => {
handleHashChange()
});
}
// A cross-browser check for high contrast mode detection
// https://stackoverflow.com/questions/55241841/high-contrast-mode-on-mozilla-browser
const detectHighContrastMode = () => {
if (ExecutionEnvironment.canUseDOM) {
const testColor = 'rgb(200, 100, 50)'
const testDiv = document.createElement('div')
testDiv.style.color = testColor
document.body.appendChild(testDiv)
// If the element's color after appending is different than it was set, the high contrast mode is active
const currentColor = document.defaultView.getComputedStyle(testDiv, null).color
document.body.removeChild(testDiv)
if (currentColor !== testColor) {
document.body.dataset.highContrast = ""
}
}
}
detectHighContrastMode()