I spent some time exploring how to improve the UX of code blocks on the web
ped.ro
ped.ro
-.sx5jq50 .highlight-line[data-highlighted="false"], .sx5jq50 .highlight-line[data-highlighted="false"] * {
- color: var(---fadedLines);
-}
+.sx5jq50 .highlight-line[data-highlighted="false"] {
+ opacity: 0.5;
+ filter: grayscale(0.6);
+}
But I have always found adding a yellow background to the line to be by far the most effective (that is, the most likely to be read and understood correctly without explanation), most likely combined with opacity reduction and/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.)So here's another protip: Make the highlight pop with a brighter left-border, so your background color doesn't have to pop so much on its own and you have more wiggle room to maintain contrast. Something like what mdx-prism has in their readme image [1], though I don't like the specific blue-on-blue colors. This blog article [2] has an example in the middle with nicer colors.
Maybe adding an underlines or something to make it slightly different might help with that.
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't allow mobile device to zoom out.
I wish I could read full lines of code instead of trying to scroll back and forth.
Is it becoming a tend to have longer limits?
If 80 char is a warning it’s ok. Because obviously if every line is super long that’s not readable either. Needs to be a balance.
I have three huge monitors, but long lines just makes most of the right-hand side of an editor window wasted.
Text files (source) are documents, and documents are much taller than they are wide: portrait orientation, like letter or A4. My two side monitors and oriented in portrait mode for this reason, and their quadrants (also portrait) are where I put editors.
I made this comment not to show my age, but to overcome the loudness bias of trends. There are plenty of us out here who still do it the old way, because the new way isn't better.
Probably not the best solution.
But commenters here suggesting the 80 char limit, inherited from IBM punchcards and typewriters before that, for diplaying code on smart phones half a century later is a little hilarious to me.
Units of precisely 20 feet is also a standard, used mostly in countries that use SI. It doesn't diminish the value or utility of containerization.
Alternatively, on a mobile device, you could try to display the "desktop view" and put your phone in landscape mode.
maybe using some combination of pre tags and white-space pre-wrap
What you'd probably rather have is for the `<pre>` element's default styling of `white-space: pre` to be changed to `white-space: normal`, that would make the text wrap as it does in paragraphs and such. It's easy to tell when such wrapping is happening in the code block styling that includes line numbers.
to add to what you wrote, mobile web has this nasty bug with code sections with a horizontal scrolling bar inside a page that already has a horizontal scrolling bar. these two don't play well.
Enter a line break if you need one. No need to force. We have high-res widescreen monitors now and it results in much more productive viewing. And editors can automatically wrap text if you need a narrower window.
Ignoring that: this is pretty nice looking, both the explanation (clear and with good examples) and the component itself. Good work!
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.
I do like that you use the `line=1-3` syntax for highlighting instead of mdx-prism's `lang{1-3}`. Do you have any appetite for upstreaming that change?
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.
This always annoys me for single-line commands. I'd love a CSS solution but can only think of some ugly JS approaches.
It's one of those little UI annoyances that refuse to disappear. Just like blinking cursors in unfocused input fields & windows.
Apart from undo state, you also want cut to be equivalent to copy; backspace.
Whether this is a good idea is a whole different matter.
My team is also working on code sample editor and preview components, but as web components for this reason: https://polymerlabs.github.io/playground-elements/
Thank you!
There might be some minimalist way to get Monaco into read-only mode for it...
Other than that, a great write-up.