Skip to content

Commit 33da0c2

Browse files
committed
Some updates to the help popup
This makes a few changes to the help popup: - Move css to chrome.css, since this is a UI element. - Move HTML code to index.hbs instead of generated in JavaScript. In general I prefer to keep HTML out of JavaScript when possible, and I didn't see a particular reason to avoid it. - Added a click handler to dismiss the popup. - Make sure handlers get removed when dismissed. - Use `mdbook-` prefixes for IDs to avoid collisions with headers. - Don't show search if it isn't enabled. - Add the new `/` shortcut. - Use flex layout for better positioning. - Dim out the surrounding text using an overlay. - Various other styling tweaks. - Add a GUI test.
1 parent 91e9402 commit 33da0c2

File tree

6 files changed

+93
-49
lines changed

6 files changed

+93
-49
lines changed

src/front-end/css/chrome.css

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,3 +656,46 @@ html:not(.sidebar-resizing) .sidebar {
656656
margin-inline-start: -14px;
657657
width: 14px;
658658
}
659+
660+
/* The container for the help popup that covers the whole window. */
661+
#mdbook-help-container {
662+
/* Position and size for the whole window. */
663+
position: fixed;
664+
top: 0;
665+
left: 0;
666+
right: 0;
667+
bottom: 0;
668+
/* This uses flex layout (which is set in book.js), and centers the popup
669+
in the window.*/
670+
display: none;
671+
align-items: center;
672+
justify-content: center;
673+
z-index: 1000;
674+
/* Dim out the book while the popup is visible. */
675+
background: var(--overlay-bg);
676+
}
677+
678+
/* The popup help box. */
679+
#mdbook-help-popup {
680+
box-shadow: 0 4px 24px rgba(0,0,0,0.15);
681+
min-width: 300px;
682+
max-width: 500px;
683+
width: 100%;
684+
box-sizing: border-box;
685+
display: flex;
686+
flex-direction: column;
687+
align-items: center;
688+
background-color: var(--bg);
689+
color: var(--fg);
690+
border-width: 1px;
691+
border-color: var(--theme-popup-border);
692+
border-style: solid;
693+
border-radius: 8px;
694+
padding: 10px;
695+
}
696+
697+
.mdbook-help-title {
698+
text-align: center;
699+
/* mdbook's margin for h2 is way too large. */
700+
margin: 10px;
701+
}

src/front-end/css/general.css

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -277,33 +277,3 @@ sup {
277277
.result-no-output {
278278
font-style: italic;
279279
}
280-
281-
#help-overlay {
282-
position: fixed;
283-
display: none;
284-
width: 20%;
285-
height: 60%;
286-
top: 10%;
287-
left: 40%;
288-
right: 0;
289-
bottom: 0;
290-
background-color: rgba(225,225,225,1);
291-
color: black;
292-
z-index: 2;
293-
cursor: pointer;
294-
border-width: 3px;
295-
border-color: black;
296-
border-style: solid;
297-
border-radius: 25px;
298-
299-
padding: 10px;
300-
}
301-
#help-overlay kbd {
302-
border-width: 1px;
303-
border-color: black;
304-
border-style: solid;
305-
background-color: white;
306-
}
307-
#help-title {
308-
text-align: center;
309-
}

src/front-end/css/variables.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
--copy-button-filter-hover: invert(68%) sepia(55%) saturate(531%) hue-rotate(341deg) brightness(104%) contrast(101%);
6565

6666
--footnote-highlight: #2668a6;
67+
68+
--overlay-bg: rgba(33, 40, 48, 0.4);
6769
}
6870

6971
.coal {
@@ -115,6 +117,8 @@
115117
--copy-button-filter-hover: invert(36%) sepia(70%) saturate(503%) hue-rotate(167deg) brightness(98%) contrast(89%);
116118

117119
--footnote-highlight: #4079ae;
120+
121+
--overlay-bg: rgba(33, 40, 48, 0.4);
118122
}
119123

120124
.light, html:not(.js) {
@@ -166,6 +170,8 @@
166170
--copy-button-filter-hover: invert(14%) sepia(93%) saturate(4250%) hue-rotate(243deg) brightness(99%) contrast(130%);
167171

168172
--footnote-highlight: #7e7eff;
173+
174+
--overlay-bg: rgba(200, 200, 205, 0.4);
169175
}
170176

171177
.navy {
@@ -217,6 +223,8 @@
217223
--copy-button-filter-hover: invert(46%) sepia(20%) saturate(1537%) hue-rotate(156deg) brightness(85%) contrast(90%);
218224

219225
--footnote-highlight: #4079ae;
226+
227+
--overlay-bg: rgba(33, 40, 48, 0.4);
220228
}
221229

222230
.rust {
@@ -266,6 +274,8 @@
266274
--copy-button-filter-hover: invert(77%) sepia(16%) saturate(1798%) hue-rotate(328deg) brightness(98%) contrast(83%);
267275

268276
--footnote-highlight: #d3a17a;
277+
278+
--overlay-bg: rgba(150, 150, 150, 0.25);
269279
}
270280

271281
@media (prefers-color-scheme: dark) {

src/front-end/js/book.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -644,30 +644,39 @@ aria-label="Show hidden lines"></button>';
644644
}
645645
}
646646
function showHelp() {
647-
document.getElementById('help-overlay').style.display = 'block';
648-
let help = '<h2 id="help-title">Keyboard shortcuts</h2>';
649-
help += '<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>';
650-
help += '<p>Press <kbd>s</kbd> to search in the book</p>';
651-
help += '<p>Press <kbd>?</kbd> to show this help</p>';
652-
help += '<p>Press <kbd>Esc</kbd> to hide this help</p>';
647+
const container = document.getElementById('mdbook-help-container');
648+
const overlay = document.getElementById('mdbook-help-popup');
649+
container.style.display = 'flex';
653650

654-
document.getElementById('help-overlay').innerHTML = help;
655-
656-
document.addEventListener('keydown', function(e) {
657-
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) {
651+
// Clicking outside the popup will dismiss it.
652+
const mouseHandler = event => {
653+
if (overlay.contains(event.target)) {
658654
return;
659655
}
660-
661-
switch (e.key) {
662-
case 'Escape':
663-
e.preventDefault();
656+
if (event.button !== 0) {
657+
return;
658+
}
659+
event.preventDefault();
660+
event.stopPropagation();
661+
document.removeEventListener('mousedown', mouseHandler);
662+
hideHelp();
663+
};
664+
665+
// Pressing esc will dismiss the popup.
666+
const escapeKeyHandler = event => {
667+
if (event.key === 'Escape') {
668+
event.preventDefault();
669+
event.stopPropagation();
670+
document.removeEventListener('keydown', escapeKeyHandler, true);
664671
hideHelp();
665-
break;
666672
}
667-
});
673+
};
674+
document.addEventListener('keydown', escapeKeyHandler, true);
675+
document.getElementById('mdbook-help-container')
676+
.addEventListener('mousedown', mouseHandler);
668677
}
669678
function hideHelp() {
670-
document.getElementById('help-overlay').style.display = 'none';
679+
document.getElementById('mdbook-help-container').style.display = 'none';
671680
}
672681

673682
// Usually needs the Shift key to be pressed

src/front-end/templates/index.hbs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,19 @@
6363
<script src="{{ resource "toc.js" }}"></script>
6464
</head>
6565
<body>
66-
<div id="help-overlay"></div>
66+
<div id="mdbook-help-container">
67+
<div id="mdbook-help-popup">
68+
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
69+
<div>
70+
<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>
71+
{{#if search_enabled}}
72+
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
73+
{{/if}}
74+
<p>Press <kbd>?</kbd> to show this help</p>
75+
<p>Press <kbd>Esc</kbd> to hide this help</p>
76+
</div>
77+
</div>
78+
</div>
6779
<div id="body-container">
6880
<!-- Work around some values being stored in localStorage wrapped in quotes -->
6981
<script>

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ fn build_header_links(html: &str) -> String {
667667
static BUILD_HEADER_LINKS: LazyLock<Regex> = LazyLock::new(|| {
668668
Regex::new(r#"<h(\d)(?: id="([^"]+)")?(?: class="([^"]+)")?>(.*?)</h\d>"#).unwrap()
669669
});
670-
static IGNORE_CLASS: &[&str] = &["menu-title"];
670+
static IGNORE_CLASS: &[&str] = &["menu-title", "mdbook-help-title"];
671671

672672
let mut id_counter = HashMap::new();
673673

0 commit comments

Comments
 (0)