Skip to content

Commit 0defb7a

Browse files
authored
Merge pull request #13024 from quarto-dev/bugfix/13009
Wrap code copy button around outer scaffold
2 parents 852567d + 62c7122 commit 0defb7a

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/format/html/format-html-shared.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ export const quartoDefaults = (format: Format) => {
317317
"code-copy-selector",
318318
format.metadata[kCodeCopy] === undefined ||
319319
format.metadata[kCodeCopy] === "hover"
320-
? '"pre.sourceCode:hover > "'
320+
// ? '"div.sourceCode:hover > "'
321+
? '"div.code-copy-outer-scaffold:hover > "'
321322
: '""',
322323
),
323324
),

src/format/html/format-html.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,12 +750,25 @@ function htmlFormatPostprocessor(
750750

751751
// insert code copy button (with specfic attribute when inside a modal)
752752
if (codeCopy) {
753-
code.classList.add("code-with-copy");
753+
// the interaction of code copy button fixed position
754+
// and scrolling overflow behavior requires a scaffold div to be inserted
755+
// as a parent of the code block and the copy button both
756+
// (see #13009, #5538, and #12787)
757+
const outerScaffold = doc.createElement("div");
758+
outerScaffold.classList.add("code-copy-outer-scaffold");
759+
754760
const copyButton = createCodeCopyButton(doc, format);
755761
if (EmbedSourceModal && EmbedSourceModal.contains(code)) {
756762
copyButton.setAttribute("data-in-quarto-modal", "");
757763
}
758-
code.appendChild(copyButton);
764+
code.classList.add("code-with-copy");
765+
766+
const sourceCodeDiv = code.parentElement!;
767+
const sourceCodeDivParent = code.parentElement?.parentElement;
768+
sourceCodeDivParent!.replaceChild(outerScaffold, sourceCodeDiv);
769+
770+
outerScaffold.appendChild(sourceCodeDiv);
771+
outerScaffold.appendChild(copyButton);
759772
}
760773

761774
// insert example iframe

src/resources/formats/html/_quarto-rules.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ details > summary > p:only-child {
282282
}
283283

284284
// codeCopy
285-
div.sourceCode {
285+
div.code-copy-outer-scaffold {
286286
position: relative;
287287
}
288288

src/resources/formats/html/bootstrap/_bootstrap-rules.scss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,8 @@ pre.sourceCode {
877877
border: none;
878878
}
879879
font-size: $code-block-font-size;
880-
overflow-y: auto !important;
880+
overflow-y: visible !important;
881+
// overflow-y: auto !important;
881882
@if $code-block-bg {
882883
padding: $code-block-bg-padding;
883884
}
@@ -890,7 +891,8 @@ pre.sourceCode > code.sourceCode {
890891
}
891892

892893
div.sourceCode {
893-
position: relative;
894+
overflow-y: hidden;
895+
// position: relative;
894896
}
895897

896898
.callout div.sourceCode {

0 commit comments

Comments
 (0)