diff --git a/src/front-end/css/chrome.css b/src/front-end/css/chrome.css index 507a6056..360a6537 100644 --- a/src/front-end/css/chrome.css +++ b/src/front-end/css/chrome.css @@ -656,3 +656,46 @@ html:not(.sidebar-resizing) .sidebar { margin-inline-start: -14px; width: 14px; } + +/* The container for the help popup that covers the whole window. */ +#mdbook-help-container { + /* Position and size for the whole window. */ + position: fixed; + top: 0; + left: 0; + right: 0; + bottom: 0; + /* This uses flex layout (which is set in book.js), and centers the popup + in the window.*/ + display: none; + align-items: center; + justify-content: center; + z-index: 1000; + /* Dim out the book while the popup is visible. */ + background: var(--overlay-bg); +} + +/* The popup help box. */ +#mdbook-help-popup { + box-shadow: 0 4px 24px rgba(0,0,0,0.15); + min-width: 300px; + max-width: 500px; + width: 100%; + box-sizing: border-box; + display: flex; + flex-direction: column; + align-items: center; + background-color: var(--bg); + color: var(--fg); + border-width: 1px; + border-color: var(--theme-popup-border); + border-style: solid; + border-radius: 8px; + padding: 10px; +} + +.mdbook-help-title { + text-align: center; + /* mdbook's margin for h2 is way too large. */ + margin: 10px; +} diff --git a/src/front-end/css/general.css b/src/front-end/css/general.css index 7b917bb2..9946cfc0 100644 --- a/src/front-end/css/general.css +++ b/src/front-end/css/general.css @@ -277,33 +277,3 @@ sup { .result-no-output { font-style: italic; } - -#help-overlay { - position: fixed; - display: none; - width: 20%; - height: 60%; - top: 10%; - left: 40%; - right: 0; - bottom: 0; - background-color: rgba(225,225,225,1); - color: black; - z-index: 2; - cursor: pointer; - border-width: 3px; - border-color: black; - border-style: solid; - border-radius: 25px; - - padding: 10px; -} -#help-overlay kbd { - border-width: 1px; - border-color: black; - border-style: solid; - background-color: white; -} -#help-title { - text-align: center; -} \ No newline at end of file diff --git a/src/front-end/css/variables.css b/src/front-end/css/variables.css index 43eb9e0c..5742d241 100644 --- a/src/front-end/css/variables.css +++ b/src/front-end/css/variables.css @@ -64,6 +64,8 @@ --copy-button-filter-hover: invert(68%) sepia(55%) saturate(531%) hue-rotate(341deg) brightness(104%) contrast(101%); --footnote-highlight: #2668a6; + + --overlay-bg: rgba(33, 40, 48, 0.4); } .coal { @@ -115,6 +117,8 @@ --copy-button-filter-hover: invert(36%) sepia(70%) saturate(503%) hue-rotate(167deg) brightness(98%) contrast(89%); --footnote-highlight: #4079ae; + + --overlay-bg: rgba(33, 40, 48, 0.4); } .light, html:not(.js) { @@ -166,6 +170,8 @@ --copy-button-filter-hover: invert(14%) sepia(93%) saturate(4250%) hue-rotate(243deg) brightness(99%) contrast(130%); --footnote-highlight: #7e7eff; + + --overlay-bg: rgba(200, 200, 205, 0.4); } .navy { @@ -217,6 +223,8 @@ --copy-button-filter-hover: invert(46%) sepia(20%) saturate(1537%) hue-rotate(156deg) brightness(85%) contrast(90%); --footnote-highlight: #4079ae; + + --overlay-bg: rgba(33, 40, 48, 0.4); } .rust { @@ -266,6 +274,8 @@ --copy-button-filter-hover: invert(77%) sepia(16%) saturate(1798%) hue-rotate(328deg) brightness(98%) contrast(83%); --footnote-highlight: #d3a17a; + + --overlay-bg: rgba(150, 150, 150, 0.25); } @media (prefers-color-scheme: dark) { diff --git a/src/front-end/js/book.js b/src/front-end/js/book.js index 64438e57..5df2096f 100644 --- a/src/front-end/js/book.js +++ b/src/front-end/js/book.js @@ -644,30 +644,39 @@ aria-label="Show hidden lines">'; } } function showHelp() { - document.getElementById('help-overlay').style.display = 'block'; - let help = '

Keyboard shortcuts

'; - help += '

Press or to navigate between chapters

'; - help += '

Press s to search in the book

'; - help += '

Press ? to show this help

'; - help += '

Press Esc to hide this help

'; + const container = document.getElementById('mdbook-help-container'); + const overlay = document.getElementById('mdbook-help-popup'); + container.style.display = 'flex'; - document.getElementById('help-overlay').innerHTML = help; - - document.addEventListener('keydown', function(e) { - if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { + // Clicking outside the popup will dismiss it. + const mouseHandler = event => { + if (overlay.contains(event.target)) { return; } - - switch (e.key) { - case 'Escape': - e.preventDefault(); - hideHelp(); - break; + if (event.button !== 0) { + return; } - }); + event.preventDefault(); + event.stopPropagation(); + document.removeEventListener('mousedown', mouseHandler); + hideHelp(); + }; + + // Pressing esc will dismiss the popup. + const escapeKeyHandler = event => { + if (event.key === 'Escape') { + event.preventDefault(); + event.stopPropagation(); + document.removeEventListener('keydown', escapeKeyHandler, true); + hideHelp(); + } + }; + document.addEventListener('keydown', escapeKeyHandler, true); + document.getElementById('mdbook-help-container') + .addEventListener('mousedown', mouseHandler); } function hideHelp() { - document.getElementById('help-overlay').style.display = 'none'; + document.getElementById('mdbook-help-container').style.display = 'none'; } // Usually needs the Shift key to be pressed diff --git a/src/front-end/templates/index.hbs b/src/front-end/templates/index.hbs index 0e17bbb7..aeae9a84 100644 --- a/src/front-end/templates/index.hbs +++ b/src/front-end/templates/index.hbs @@ -63,7 +63,19 @@ -
+
+
+

Keyboard shortcuts

+
+

Press or to navigate between chapters

+ {{#if search_enabled}} +

Press S or / to search in the book

+ {{/if}} +

Press ? to show this help

+

Press Esc to hide this help

+
+
+