Skip to content

Commit 89ba716

Browse files
committed
rustdoc: use consistent "popover" styling for notable traits
1 parent 42325c5 commit 89ba716

File tree

7 files changed

+89
-115
lines changed

7 files changed

+89
-115
lines changed

src/librustdoc/html/render/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1312,9 +1312,7 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &mut Context<'_>) -> O
13121312
if has_notable_trait {
13131313
cx.types_with_notable_traits.insert(ty.clone());
13141314
Some(format!(
1315-
"<span class=\"notable-traits\" data-ty=\"{ty}\">\
1316-
<span class=\"notable-traits-tooltip\">ⓘ</span>\
1317-
</span>",
1315+
" <a href=\"#\" class=\"notable-traits\" data-ty=\"{ty}\">ⓘ</a>",
13181316
ty = Escape(&format!("{:#}", ty.print(cx))),
13191317
))
13201318
} else {
@@ -1343,7 +1341,7 @@ fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {
13431341
if out.is_empty() {
13441342
write!(
13451343
&mut out,
1346-
"<h3 class=\"notable\">Notable traits for <code>{}</code></h3>\
1344+
"<h3>Notable traits for <code>{}</code></h3>\
13471345
<pre class=\"content\"><code>",
13481346
impl_.for_.print(cx)
13491347
);

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

+19-54
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ h4.code-header {
183183
font-weight: 600;
184184
margin: 0;
185185
padding: 0;
186-
/* position notable traits in mobile mode within the header */
187-
position: relative;
188186
}
189187

190188
#crate-search,
@@ -930,13 +928,14 @@ so that we can apply CSS-filters to change the arrow color in themes */
930928
border-radius: 3px;
931929
border: 1px solid var(--border-color);
932930
font-size: 1rem;
931+
--popover-arrow-offset: 11px;
933932
}
934933

935934
/* This rule is to draw the little arrow connecting the settings menu to the gear icon. */
936935
.popover::before {
937936
content: '';
938937
position: absolute;
939-
right: 11px;
938+
right: var(--popover-arrow-offset);
940939
border: solid var(--border-color);
941940
border-width: 1px 1px 0 0;
942941
display: inline-block;
@@ -953,10 +952,7 @@ so that we can apply CSS-filters to change the arrow color in themes */
953952
/* use larger max-width for help popover, but not for help.html */
954953
#help.popover {
955954
max-width: 600px;
956-
}
957-
958-
#help.popover::before {
959-
right: 48px;
955+
--popover-arrow-offset: 48px;
960956
}
961957

962958
#help dt {
@@ -1275,54 +1271,34 @@ h3.variant {
12751271
border-right: 3px solid var(--target-border-color);
12761272
}
12771273

1278-
.notable-traits-tooltip {
1279-
display: inline-block;
1280-
cursor: pointer;
1281-
}
1282-
1283-
.notable-traits .notable-traits-tooltiptext {
1284-
display: inline-block;
1285-
visibility: hidden;
1274+
.notable-traits {
1275+
color: inherit;
1276+
margin-right: 15px;
1277+
position: relative;
12861278
}
12871279

1288-
.notable-traits-tooltiptext {
1289-
padding: 5px 3px 3px 3px;
1290-
border-radius: 6px;
1291-
margin-left: 5px;
1292-
z-index: 10;
1293-
font-size: 1rem;
1294-
cursor: default;
1280+
/* placeholder thunk so that the mouse can easily travel from "(i)" to popover
1281+
the resulting "hover tunnel" is a stepped triangle, approximating
1282+
https://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown */
1283+
.notable-traits:hover::after {
12951284
position: absolute;
1296-
border: 1px solid;
1297-
}
1298-
1299-
.notable-traits-tooltip::after {
1300-
/* The margin on the tooltip does not capture hover events,
1301-
this extends the area of hover enough so that mouse hover is not
1302-
lost when moving the mouse to the tooltip */
1303-
content: "\00a0\00a0\00a0";
1304-
}
1305-
1306-
.notable-traits-tooltiptext .docblock {
1307-
margin: 0;
1285+
top: calc(100% - 10px);
1286+
left: -15px;
1287+
right: -15px;
1288+
height: 20px;
1289+
content: "\00a0";
13081290
}
13091291

1310-
.notable-traits-tooltiptext .notable {
1311-
font-size: 1.1875rem;
1312-
font-weight: 600;
1313-
display: block;
1292+
.notable .docblock {
1293+
margin: 0.25em 0.5em;
13141294
}
13151295

1316-
.notable-traits-tooltiptext pre, .notable-traits-tooltiptext code {
1296+
.notable .docblock pre, .notable .docblock code {
13171297
background: transparent;
1318-
}
1319-
1320-
.notable-traits-tooltiptext .docblock pre.content {
13211298
margin: 0;
13221299
padding: 0;
13231300
font-size: 1.25rem;
13241301
white-space: pre-wrap;
1325-
overflow: hidden;
13261302
}
13271303

13281304
.search-failed {
@@ -1365,12 +1341,6 @@ h3.variant {
13651341
font-size: 1rem;
13661342
}
13671343

1368-
.notable-traits {
1369-
cursor: pointer;
1370-
z-index: 2;
1371-
margin-left: 5px;
1372-
}
1373-
13741344
#sidebar-toggle {
13751345
position: sticky;
13761346
top: 0;
@@ -1855,11 +1825,6 @@ in storage.js
18551825
border-bottom: 1px solid;
18561826
}
18571827

1858-
.notable-traits .notable-traits-tooltiptext {
1859-
left: 0;
1860-
top: 100%;
1861-
}
1862-
18631828
/* We don't display the help button on mobile devices. */
18641829
#help-button {
18651830
display: none;

src/librustdoc/html/static/css/themes/ayu.css

-4
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,6 @@ details.rustdoc-toggle > summary::before {
183183
border-color: transparent #314559 transparent transparent;
184184
}
185185

186-
.notable-traits-tooltiptext {
187-
background-color: #314559;
188-
}
189-
190186
#titles > button.selected {
191187
background-color: #141920 !important;
192188
border-bottom: 1px solid #ffb44c !important;

src/librustdoc/html/static/css/themes/dark.css

-4
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ details.rustdoc-toggle > summary::before {
106106
border-color: transparent black transparent transparent;
107107
}
108108

109-
.notable-traits-tooltiptext {
110-
background-color: #111;
111-
}
112-
113109
#titles > button:not(.selected) {
114110
background-color: #252525;
115111
border-top-color: #252525;

src/librustdoc/html/static/css/themes/light.css

-4
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ body.source .example-wrap pre.rust a {
9898
border-color: transparent black transparent transparent;
9999
}
100100

101-
.notable-traits-tooltiptext {
102-
background-color: #eee;
103-
}
104-
105101
#titles > button:not(.selected) {
106102
background-color: #e6e6e6;
107103
border-top-color: #e6e6e6;

src/librustdoc/html/static/js/main.js

+43-9
Original file line numberDiff line numberDiff line change
@@ -850,18 +850,33 @@ function loadCss(cssUrl) {
850850
}
851851
hideNotable();
852852
const ty = e.getAttribute("data-ty");
853-
const tooltip = e.getElementsByClassName("notable-traits-tooltip")[0];
854853
const wrapper = document.createElement("div");
855854
wrapper.innerHTML = "<div class=\"docblock\">" + window.NOTABLE_TRAITS[ty] + "</div>";
856-
wrapper.className = "notable-traits-tooltiptext";
857-
tooltip.appendChild(wrapper);
858-
const pos = wrapper.getBoundingClientRect();
859-
tooltip.removeChild(wrapper);
860-
wrapper.style.top = (pos.top + window.scrollY) + "px";
861-
wrapper.style.left = (pos.left + window.scrollX) + "px";
862-
wrapper.style.width = pos.width + "px";
855+
wrapper.className = "notable popover";
856+
const focusCatcher = document.createElement("div");
857+
focusCatcher.setAttribute("tabindex", "0");
858+
focusCatcher.onfocus = hideNotable;
859+
wrapper.appendChild(focusCatcher);
860+
const pos = e.getBoundingClientRect();
861+
// 5px overlap so that the mouse can easily travel from place to place
862+
wrapper.style.top = (pos.top + window.scrollY + pos.height) + "px";
863+
wrapper.style.left = 0;
864+
wrapper.style.right = "auto";
865+
wrapper.style.visibility = "hidden";
863866
const body = document.getElementsByTagName("body")[0];
864867
body.appendChild(wrapper);
868+
const wrapperPos = wrapper.getBoundingClientRect();
869+
// offset so that the arrow points at the center of the "(i)"
870+
const finalPos = pos.left + window.scrollX - wrapperPos.width + 24;
871+
if (finalPos > 0) {
872+
wrapper.style.left = finalPos + "px";
873+
} else {
874+
wrapper.style.setProperty(
875+
"--popover-arrow-offset",
876+
(wrapperPos.right - pos.right + 4) + "px"
877+
);
878+
}
879+
wrapper.style.visibility = "";
865880
window.CURRENT_NOTABLE_ELEMENT = wrapper;
866881
window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE = e;
867882
wrapper.onpointerleave = function(ev) {
@@ -875,9 +890,23 @@ function loadCss(cssUrl) {
875890
};
876891
}
877892

893+
function notableBlurHandler(event) {
894+
if (window.CURRENT_NOTABLE_ELEMENT &&
895+
!elemIsInParent(document.activeElement, window.CURRENT_NOTABLE_ELEMENT) &&
896+
!elemIsInParent(event.relatedTarget, window.CURRENT_NOTABLE_ELEMENT) &&
897+
!elemIsInParent(document.activeElement, window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE) &&
898+
!elemIsInParent(event.relatedTarget, window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE)
899+
) {
900+
hideNotable();
901+
}
902+
}
903+
878904
function hideNotable() {
879905
if (window.CURRENT_NOTABLE_ELEMENT) {
880-
window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE.NOTABLE_FORCE_VISIBLE = false;
906+
if (window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE.NOTABLE_FORCE_VISIBLE) {
907+
window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE.focus();
908+
window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE.NOTABLE_FORCE_VISIBLE = false;
909+
}
881910
const body = document.getElementsByTagName("body")[0];
882911
body.removeChild(window.CURRENT_NOTABLE_ELEMENT);
883912
window.CURRENT_NOTABLE_ELEMENT = null;
@@ -891,7 +920,11 @@ function loadCss(cssUrl) {
891920
hideNotable();
892921
} else {
893922
showNotable(this);
923+
window.CURRENT_NOTABLE_ELEMENT.setAttribute("tabindex", "0");
924+
window.CURRENT_NOTABLE_ELEMENT.focus();
925+
window.CURRENT_NOTABLE_ELEMENT.onblur = notableBlurHandler;
894926
}
927+
return false;
895928
};
896929
e.onpointerenter = function(ev) {
897930
// If this is a synthetic touch event, ignore it. A click event will be along shortly.
@@ -1018,6 +1051,7 @@ function loadCss(cssUrl) {
10181051
onEachLazy(document.querySelectorAll(".search-form .popover"), elem => {
10191052
elem.style.display = "none";
10201053
});
1054+
hideNotable();
10211055
};
10221056

10231057
/**

src/test/rustdoc-gui/notable-trait.goml

+25-36
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,26 @@ assert-position: (
2222
)
2323
assert-position: (
2424
"//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']",
25-
{"x": 951},
25+
{"x": 955},
2626
)
27-
// The tooltip should be beside the `i`
27+
// The tooltip should be below the `i`
2828
// Also, clicking the tooltip should bring its text into the DOM
29-
assert-count: ("//*[@class='notable-traits-tooltiptext']", 0)
29+
assert-count: ("//*[@class='notable popover']", 0)
3030
click: "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']"
31-
assert-count: ("//*[@class='notable-traits-tooltiptext']", 1)
31+
assert-count: ("//*[@class='notable popover']", 1)
3232
compare-elements-position-near: (
3333
"//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']",
34-
"//*[@class='notable-traits-tooltiptext']",
35-
{"y": 2}
34+
"//*[@class='notable popover']",
35+
{"y": 30}
3636
)
3737
compare-elements-position-false: (
3838
"//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']",
39-
"//*[@class='notable-traits-tooltiptext']",
39+
"//*[@class='notable popover']",
4040
("x")
4141
)
42-
// The docblock should be flush with the border.
43-
assert-css: (
44-
"//*[@class='notable-traits-tooltiptext']/*[@class='docblock']",
45-
{"margin-left": "0px"}
46-
)
4742
click: "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']"
4843
move-cursor-to: "//h1"
49-
assert-count: ("//*[@class='notable-traits-tooltiptext']", 0)
44+
assert-count: ("//*[@class='notable popover']", 0)
5045

5146
// Now only the `i` should be on the next line.
5247
size: (1055, 600)
@@ -77,7 +72,7 @@ assert-position: (
7772
)
7873
assert-position: (
7974
"//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']",
80-
{"x": 519},
75+
{"x": 523},
8176
)
8277

8378
// Checking on mobile now.
@@ -101,34 +96,28 @@ assert-position: (
10196
)
10297
assert-position: (
10398
"//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']",
104-
{"x": 289},
99+
{"x": 293},
105100
)
106-
// The tooltip should be below `i`
101+
// The tooltip should STILL be below `i`
107102
click: "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']"
108-
assert-count: ("//*[@class='notable-traits-tooltiptext']", 1)
109-
compare-elements-position-near-false: (
103+
assert-count: ("//*[@class='notable popover']", 1)
104+
compare-elements-position-near: (
110105
"//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']",
111-
"//*[@class='notable-traits-tooltiptext']",
112-
{"y": 2}
106+
"//*[@class='notable popover']",
107+
{"y": 30}
113108
)
114109
compare-elements-position-false: (
115110
"//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']",
116-
"//*[@class='notable-traits-tooltiptext']",
111+
"//*[@class='notable popover']",
117112
("x")
118113
)
119-
compare-elements-position-near: (
120-
"//*[@id='method.create_an_iterator_from_read']",
121-
"//*[@class='notable-traits-tooltiptext']",
122-
{"x": 10}
123-
)
124-
// The docblock should be flush with the border.
125-
assert-css: (
126-
"//*[@class='notable-traits-tooltiptext']/*[@class='docblock']",
127-
{"margin-left": "0px"}
114+
assert-position: (
115+
"//*[@class='notable popover']",
116+
{"x": 0}
128117
)
129118
click: "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']"
130119
move-cursor-to: "//h1"
131-
assert-count: ("//*[@class='notable-traits-tooltiptext']", 0)
120+
assert-count: ("//*[@class='notable popover']", 0)
132121

133122
// Checking on very small mobile. The `i` should be on its own line.
134123
size: (365, 600)
@@ -153,25 +142,25 @@ define-function: (
153142
("reload"),
154143

155144
("move-cursor-to", "//*[@id='method.create_an_iterator_from_read']//*[@class='notable-traits']"),
156-
("assert-count", (".notable-traits-tooltiptext", 1)),
145+
("assert-count", (".notable.popover", 1)),
157146

158147
("assert-css", (
159-
".notable-traits-tooltiptext h3.notable",
148+
".notable.popover h3",
160149
{"color": |header_color|},
161150
ALL,
162151
)),
163152
("assert-css", (
164-
".notable-traits-tooltiptext pre.content",
153+
".notable.popover pre",
165154
{"color": |content_color|},
166155
ALL,
167156
)),
168157
("assert-css", (
169-
".notable-traits-tooltiptext pre.content a.struct",
158+
".notable.popover pre a.struct",
170159
{"color": |type_color|},
171160
ALL,
172161
)),
173162
("assert-css", (
174-
".notable-traits-tooltiptext pre.content a.trait",
163+
".notable.popover pre a.trait",
175164
{"color": |trait_color|},
176165
ALL,
177166
)),

0 commit comments

Comments
 (0)