Skip to content

Commit bea40db

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

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,8 @@ 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+
.example-wrap:hover > .test-arrow, .example-wrap:hover > .button-holder,
1478+
.example-wrap .button-holder.keep-visible {
14781479
visibility: visible;
14791480
}
14801481
.example-wrap .button-holder .copy-button {

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

Lines changed: 23 additions & 2 deletions
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)