-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable focus + keyboard scroll on Markdown code blocks #2566
Conversation
✅ You can preview this change here:
To edit notification comments on pull requests, go to your Netlify site settings. |
@@ -37,7 +37,7 @@ nodeListForEach($tabs, function ($tabs) { | |||
OptionsTable.init() | |||
|
|||
// Add copy to clipboard to code blocks inside tab containers | |||
var $codeBlocks = document.querySelectorAll('[data-module="app-copy"]') | |||
var $codeBlocks = document.querySelectorAll('[data-module="app-copy"] pre') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now render code through Markdown so I've moved data-module
to the wrapper, otherwise every code block on every page will get an unintentional Copy code button
margin-bottom: 0; | ||
padding: 0; | ||
border: $govuk-focus-width solid transparent; | ||
.app-tabs__container pre code { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Newer highlight.js
updates revealed we should scroll <code>
not <pre>
font-size: 16px; | ||
@include govuk-responsive-margin(4, "bottom"); | ||
|
||
&:focus { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b9857de
to
d3bcbcf
Compare
<code>
not <pre>
d3bcbcf
to
193f37f
Compare
<code>
not <pre>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One comment that could do with being updated, but otherwise looks good to me. Good catch updating the code blocks in 'Updating your code' too 👍🏻
lib/marked-renderer.js
Outdated
* Custom markdown renderer that avoids wrapping `<img>` image | ||
* tags (or code with syntax highlighting) in `<p>` paragraphs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite right any more – as far as I know the code
method has nothing to do with avoiding the code blocks being wrapped in paragraphs?
Suggest either adding comment blocks to the individual methods or changing to something like:
* Custom markdown renderer that avoids wrapping `<img>` image | |
* tags (or code with syntax highlighting) in `<p>` paragraphs | |
* Custom markdown renderer that: | |
* - avoids wrapping `<img>` image tags in `<p>` paragraphs | |
* - adds tabindex="0" to code blocks so they can be focused and scrolled with | |
* the keyboard |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh let me dig into why I adjusted the comment
I found that "new lines" in code blocks were triggering another .code()
call as "code within code"
The first .code()
call gets <pre><code>
and has a syntax language set
Subsequent .code()
calls skip straight to .html()
and don't have a syntax language
Hopefully it's something we fixed (or will fix) in:
We might both be right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it was related new lines so the original comment has been moved to:
This PR is now stacked on top and adds the comment:
// Ensure code blocks can be focused and scrolled
// with the keyboard via `tabindex="0"`
a6431f8
to
9fa400b
Compare
9fa400b
to
957a022
Compare
This change stops every Markdown-rendered code block (on every page) getting an unintentional “Copy code” button
e70e622
to
cbb6f8b
Compare
957a022
to
24b5e99
Compare
24b5e99
to
ca027d6
Compare
@36degrees I've adjusted the PR title to show that scrolling only works on Markdown-rendered code blocks Code blocks rendered by Nunjucks won't scroll until this next one is merged: Hope that's alright |
*/ | ||
code (code, language, isEscaped) { | ||
return super.code(code, language, isEscaped) | ||
.replace('<code', '<code tabindex="0"') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure we add tabindex="0"
to Markdown code blocks
This maintains keyboard focus + arrow key scrolling as added in:
Fixes issue where “inline code” styles (see `updating-your-code` how to guide) was conflicting Also consistent with `highlight.js` which now scrolls code not the preformatted text
ca027d6
to
b860e15
Compare
This PR consolidates styling between
<pre>
and<code>
All code blocks can now receive focus and scroll with keyboard left/right arrows