TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

I spent some time exploring how to improve the UX of code blocks on the web

191 pointsby lovelydropabout 4 years ago

18 comments

chrismorganabout 4 years ago
For the highlighted code, you’ve made unhighlighted lines look more like <i>comments</i> than merely deemphasised lines. One alternative style that I’ve found effective in such situations is to reduce the opacity and partially desaturate:<p><pre><code> -.sx5jq50 .highlight-line[data-highlighted=&quot;false&quot;], .sx5jq50 .highlight-line[data-highlighted=&quot;false&quot;] * { - color: var(---fadedLines); -} +.sx5jq50 .highlight-line[data-highlighted=&quot;false&quot;] { + opacity: 0.5; + filter: grayscale(0.6); +} </code></pre> But I have always found adding a yellow background to the line to be by <i>far</i> the most effective (that is, the most likely to be read and understood correctly without explanation), most likely combined with opacity reduction and&#x2F;or desaturation. And yes, specifically yellow will normally yield the best results. (There may be cultural factors to weigh against this recommendation, but there’s also some actual colour science supporting it. I’d definitely avoid red and green for normal highlighting in western culture, though it’s great for removed and added lines in diffs—but do remember the colourblind and not make colour the only indication.)
评论 #27083389 未加载
carlosperateabout 4 years ago
This is pretty good! One thing I wasn&#x27;t able to trigger (or understand) was the &quot;hover me&quot; part from the &quot;Interact with the content&quot; section.
评论 #27081515 未加载
milliamsabout 4 years ago
I wish the &quot;Click a highlighted word to navigate to a different page&quot; was just an &lt;a&gt; tag rather than a JavaScript onclick handler.
评论 #27077934 未加载
metalrainabout 4 years ago
It&#x27;s looks great.<p>I think for me biggest problem with code snippets is that I encounter them with mobile device. Lines are quite long, like who uses 80 character line limits these days, more like 160. This combined with reality that most websites don&#x27;t allow mobile device to zoom out.<p>I wish I could read full lines of code instead of trying to scroll back and forth.
评论 #27080360 未加载
评论 #27080395 未加载
评论 #27080594 未加载
评论 #27081532 未加载
评论 #27080591 未加载
评论 #27083935 未加载
评论 #27080459 未加载
评论 #27084528 未加载
评论 #27083658 未加载
评论 #27079555 未加载
评论 #27081886 未加载
评论 #27081572 未加载
评论 #27080231 未加载
Groxxabout 4 years ago
Possible bug? The `hover me` seems to be implying that hovering <i>that text</i> should highlight something in the code sample, but nothing is happening. I assume that&#x27;s the goal anyway - mouse over sections of explaining-text to see the highlighted-code that&#x27;s relevant. (both &quot;highlight word&quot; and &quot;highlight lines&quot; could be handy here. tbh I&#x27;d probably use &quot;highlight lines&quot; more often)<p>Ignoring that: this is pretty nice looking, both the explanation (clear and with good examples) and the component itself. Good work!
jraphabout 4 years ago
The best feature for me is that it works well without JavaScript. The document is still a document.<p>Except for collapsed code blocks. I would suggest collapsing them using JavaScript, so they are fully readable without. They are currently unavailable to someone who does not run JavaScript.
评论 #27081946 未加载
marcusbuffettabout 4 years ago
Looks great! I&#x27;d add a Copy to Clipboard button somewhere, since that&#x27;s such a common use case when showing code
ahurleabout 4 years ago
I&#x27;m curious why you avoided using mdx-prism in favor of re-implementing much of it yourself. If only because I just used it in my own project and spent time patching a minor Firefox-specific bug, and now I have FOMO because I like your implementation here :) Were your features impossible without huge changes?<p>I do like that you use the `line=1-3` syntax for highlighting instead of mdx-prism&#x27;s `lang{1-3}`. Do you have any appetite for upstreaming that change?
blunteabout 4 years ago
Viewer attention is increasingly limited these days, so avoiding small mistakes which limit your audience is recommended.<p>Dark mode content such as TFA is practically unreadable outside in bright light. Your reader will need to remember to return to your page some other time when they can see it, but chances are they will not return.
randomfoolabout 4 years ago
Does anyone know if there&#x27;s a trick to avoid the trailing newline when double-clicking a line to copy&#x2F;paste it elsewhere?<p>This always annoys me for single-line commands. I&#x27;d love a CSS solution but can only think of some ugly JS approaches.
评论 #27078661 未加载
评论 #27082155 未加载
yuchiabout 4 years ago
And this is the power of MDX (and some great focus from the author of course). I’m having a hard time trying to understand why some people seem to dislike MDX, it is the holy grail of web authoring imho.
评论 #27082347 未加载
sergkopabout 4 years ago
Nice! Here is another collection of ideas on improving code blocks <a href="https:&#x2F;&#x2F;co-pilot.dev&#x2F;docs-code-block" rel="nofollow">https:&#x2F;&#x2F;co-pilot.dev&#x2F;docs-code-block</a>
AlexeyMKabout 4 years ago
This is super well done &amp; polished. Will try out for future blog posts.<p>Thank you!
评论 #27078366 未加载
zaloabout 4 years ago
Would be nice to see Intellisense-style hover tooltips on code-blocks someday (via referencing a `.d.ts` or `.ts` file).<p>There might be some minimalist way to get Monaco into read-only mode for it...
评论 #27081239 未加载
macandoabout 4 years ago
The date on this article is <i>June 02, 2021</i><p>Other than that, a great write-up.
评论 #27078388 未加载
l0b0about 4 years ago
Excellent work, although the &quot;hover me&quot; doesn&#x27;t do anything in Firefox 88 on Linux.
评论 #27082462 未加载
o_____________oabout 4 years ago
Great job! Would be nice to see a docusaurus plugin for this.
cryptonectorabout 4 years ago
TIL I like turquoise themes.