Skip to content

Commit 47456ad

Browse files
authored
Rollup merge of #98774 - notriddle:notriddle/source-code-sidebar-button, r=GuillaumeGomez
rustdoc: make source sidebar toggle a real button This fixes tab focus, so that you can open and close the sidebar from keyboard. This should cause no visible change in appearance at all. The only way you'd know anything different is if you tried to use keyboard controls to open the source code file navigation sidebar. Separated out from #98772
2 parents 7352c7b + 9938d56 commit 47456ad

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/librustdoc/html/static/css/rustdoc.css

+19-3
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ nav.sub {
418418
background-color: var(--sidebar-background-color);
419419
}
420420

421-
#sidebar-toggle:hover {
421+
#sidebar-toggle > button:hover, #sidebar-toggle > button:focus {
422422
background-color: var(--sidebar-background-color-hover);
423423
}
424424

@@ -1401,7 +1401,6 @@ pre.rust {
14011401
position: sticky;
14021402
top: 0;
14031403
left: 0;
1404-
cursor: pointer;
14051404
font-weight: bold;
14061405
font-size: 1.25rem;
14071406
border-bottom: 1px solid;
@@ -1422,7 +1421,24 @@ pre.rust {
14221421
border-bottom: 1px solid;
14231422
margin-bottom: 6px;
14241423
}
1425-
1424+
#sidebar-toggle > button {
1425+
background: none;
1426+
color: inherit;
1427+
cursor: pointer;
1428+
text-align: center;
1429+
border: none;
1430+
outline: none;
1431+
position: absolute;
1432+
top: 0;
1433+
bottom: 0;
1434+
left: 0;
1435+
right: 0;
1436+
/* work around button layout strangeness: https://stackoverflow.com/q/7271561 */
1437+
width: 100%;
1438+
/* iOS button gradient: https://stackoverflow.com/q/5438567 */
1439+
-webkit-appearance: none;
1440+
opacity: 1;
1441+
}
14261442
#settings-menu, #help-button {
14271443
margin-left: 4px;
14281444
outline: none;

src/librustdoc/html/static/js/source-script.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
5757
}
5858

5959
function toggleSidebar() {
60-
const child = this.children[0];
60+
const child = this.parentNode.children[0];
6161
if (child.innerText === ">") {
6262
if (window.innerWidth < 701) {
6363
// This is to keep the scroll position on mobile.
@@ -86,15 +86,15 @@ function toggleSidebar() {
8686
function createSidebarToggle() {
8787
const sidebarToggle = document.createElement("div");
8888
sidebarToggle.id = "sidebar-toggle";
89-
sidebarToggle.onclick = toggleSidebar;
9089

91-
const inner = document.createElement("div");
90+
const inner = document.createElement("button");
9291

9392
if (getCurrentValue("source-sidebar-show") === "true") {
9493
inner.innerText = "<";
9594
} else {
9695
inner.innerText = ">";
9796
}
97+
inner.onclick = toggleSidebar;
9898

9999
sidebarToggle.appendChild(inner);
100100
return sidebarToggle;

src/test/rustdoc-gui/sidebar-source-code-display.goml

+27
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ assert-css: (
3030
"#source-sidebar details[open] > .files a.selected",
3131
{"color": "rgb(0, 0, 0)", "background-color": "rgb(255, 255, 255)"},
3232
)
33+
// Without hover or focus.
34+
assert-css: ("#sidebar-toggle > button", {"background-color": "rgba(0, 0, 0, 0)"})
35+
// With focus.
36+
focus: "#sidebar-toggle > button"
37+
assert-css: ("#sidebar-toggle > button", {"background-color": "rgb(224, 224, 224)"})
38+
focus: ".search-input"
39+
// With hover.
40+
move-cursor-to: "#sidebar-toggle > button"
41+
assert-css: ("#sidebar-toggle > button", {"background-color": "rgb(224, 224, 224)"})
3342
// Without hover.
3443
assert-css: (
3544
"#source-sidebar details[open] > .files a:not(.selected)",
@@ -76,6 +85,15 @@ assert-css: (
7685
"#source-sidebar details[open] > .files > a.selected",
7786
{"color": "rgb(221, 221, 221)", "background-color": "rgb(51, 51, 51)"},
7887
)
88+
// Without hover or focus.
89+
assert-css: ("#sidebar-toggle > button", {"background-color": "rgba(0, 0, 0, 0)"})
90+
// With focus.
91+
focus: "#sidebar-toggle > button"
92+
assert-css: ("#sidebar-toggle > button", {"background-color": "rgb(103, 103, 103)"})
93+
focus: ".search-input"
94+
// With hover.
95+
move-cursor-to: "#sidebar-toggle > button"
96+
assert-css: ("#sidebar-toggle > button", {"background-color": "rgb(103, 103, 103)"})
7997
// Without hover.
8098
assert-css: (
8199
"#source-sidebar details[open] > .files > a:not(.selected)",
@@ -122,6 +140,15 @@ assert-css: (
122140
"#source-sidebar details[open] > .files a.selected",
123141
{"color": "rgb(255, 180, 76)", "background-color": "rgb(20, 25, 31)"},
124142
)
143+
// Without hover or focus.
144+
assert-css: ("#sidebar-toggle > button", {"background-color": "rgba(0, 0, 0, 0)"})
145+
// With focus.
146+
focus: "#sidebar-toggle > button"
147+
assert-css: ("#sidebar-toggle > button", {"background-color": "rgba(70, 70, 70, 0.33)"})
148+
focus: ".search-input"
149+
// With hover.
150+
move-cursor-to: "#sidebar-toggle > button"
151+
assert-css: ("#sidebar-toggle > button", {"background-color": "rgba(70, 70, 70, 0.33)"})
125152
// Without hover.
126153
assert-css: (
127154
"#source-sidebar details[open] > .files a:not(.selected)",

0 commit comments

Comments
 (0)