Skip to content

Fix copy code button size when over oneline code blocks#2007

Open
Oaphi wants to merge 2 commits intodevelopfrom
0valt/2002/code-btn-fix
Open

Fix copy code button size when over oneline code blocks#2007
Oaphi wants to merge 2 commits intodevelopfrom
0valt/2002/code-btn-fix

Conversation

@Oaphi
Copy link
Member

@Oaphi Oaphi commented Mar 6, 2026

With this PR, multiline blocks will still show the big button as before:

Screenshot from 2026-03-07 00-51-06

Single-line blocks, however, will show a smaller better-fitting version of the button:

Screenshot from 2026-03-07 00-51-18

closes #2002

@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.72%. Comparing base (f8f56f5) to head (a261a62).
⚠️ Report is 2 commits behind head on develop.

Additional details and impacted files
Components Coverage Δ
controllers 75.82% <ø> (ø)
helpers 85.06% <ø> (-0.02%) ⬇️
jobs 66.88% <ø> (ø)
models 93.01% <ø> (ø)
tasks 61.11% <ø> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Oaphi Oaphi requested review from a team, cellio and trichoplax March 6, 2026 22:19
Copy link
Contributor

@trichoplax trichoplax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm away from my development machine today so I haven't tested either the code or my suggestion, so my comments here are just thoughts.

const $content = $(this).text();
const numLines = $content.trim().split(/\r?\n/).length

if (numLines <= 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using <= 1 suggests we are considering not only the case where numLines is 1, but also the case where numLines is 0. Should that case avoid having the Copy button (of either size)?

This seems a very rare case. The only example I can think of where avoiding having the Copy button might be a problem is Code Golf. In the esolang (esoteric programming language) Whitespace it's possible to have a valid program composed entirely of newlines, tabs, and spaces, which would all be removed by the .trim() leaving zero length, but still needing a Copy button.

If we wanted to cover both of these rare cases we could also test the length of the content:

  • For zero length content, show no button.
  • For zero lines after trimming, but non-zero length content, show the small or large Copy button depending on number of newlines.

Is there a simpler approach where we find the height of the code block without trimming? Since both \r\n and \n contain one \n, could we just count how many times \n occurs in the content to determine the number of lines, and choose a button accordingly:

  • 0 lines: No button
  • 1 line: Small button
  • Anything else: Large button

Maybe something like (untested):

numLines = $content.length == 0 ? 0 : $content.match(/\n/g).length + 1

$(".post--content pre > code")
const buttonTemplate = `<button class="copy-button button is-muted is-outlined has-margin-2">Copy</button>`;

$('.post--content pre > code')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: would this be a good opportunity to phase out jQuery for this file?

Copy link
Member

@cellio cellio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and looks fine to me. Deferring to others for code review. During testing I wondered if the smaller button would actually be fine everywhere, but I didn't test on a phone and maybe the smaller button is a more difficult target there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy code button doesn't align well with single-line code blocks

3 participants