Skip to content

Commit a7cb1a5

Browse files
Make the buttons remain when code example is clicked
1 parent 80d8270 commit a7cb1a5

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,20 @@ a.test-arrow:hover {
14741474
.example-wrap:hover > .test-arrow {
14751475
padding: 2px 7px;
14761476
}
1477-
.example-wrap:hover > .test-arrow, .example-wrap:hover > .button-holder {
1477+
/*
1478+
On iPad, the ":hover" state sticks around, making things work not greatly. Do work around
1479+
it, we move it into this media query. More information can be found at:
1480+
https://css-tricks.com/solving-sticky-hover-states-with-media-hover-hover/
1481+
1482+
However, using `@media (hover: hover)` makes this rule never to be applied in GUI tests, so
1483+
instead, we check that it's not a "finger" cursor.
1484+
*/
1485+
@media not (pointer: coarse) {
1486+
.example-wrap:hover > .test-arrow, .example-wrap:hover > .button-holder {
1487+
visibility: visible;
1488+
}
1489+
}
1490+
.example-wrap .button-holder.keep-visible {
14781491
visibility: visible;
14791492
}
14801493
.example-wrap .button-holder .copy-button {

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

+23-2
Original file line numberDiff line numberDiff line change
@@ -1829,14 +1829,22 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
18291829
copyContentToClipboard(codeElem.textContent);
18301830
}
18311831

1832-
function addCopyButton(event) {
1832+
function getExampleWrap(event) {
18331833
let elem = event.target;
18341834
while (!hasClass(elem, "example-wrap")) {
18351835
elem = elem.parentElement;
18361836
if (elem.tagName === "body" || hasClass(elem, "docblock")) {
1837-
return;
1837+
return null;
18381838
}
18391839
}
1840+
return elem;
1841+
}
1842+
1843+
function addCopyButton(event) {
1844+
const elem = getExampleWrap(event);
1845+
if (elem === null) {
1846+
return;
1847+
}
18401848
// Since the button will be added, no need to keep this listener around.
18411849
elem.removeEventListener("mouseover", addCopyButton);
18421850

@@ -1858,7 +1866,20 @@ href="https://doc.rust-lang.org/${channel}/rustdoc/read-documentation/search.htm
18581866
parent.appendChild(copyButton);
18591867
}
18601868

1869+
function showHideCodeExampleButtons(event) {
1870+
const elem = getExampleWrap(event);
1871+
if (elem === null) {
1872+
return;
1873+
}
1874+
const buttons = elem.querySelector(".button-holder");
1875+
if (buttons === null) {
1876+
return;
1877+
}
1878+
buttons.classList.toggle("keep-visible");
1879+
}
1880+
18611881
onEachLazy(document.querySelectorAll(".docblock .example-wrap"), elem => {
18621882
elem.addEventListener("mouseover", addCopyButton);
1883+
elem.addEventListener("click", showHideCodeExampleButtons);
18631884
});
18641885
}());

0 commit comments

Comments
 (0)