Skip to content

Commit ae08bf0

Browse files
authored
feat(#62): Add support for code block line highlighting comments (#111)
* feat(#62): adjust code block styles to accomodate line highlighting * chore(#62): bump versions @testing-library/* versions - Needed to be able to access <code/> elements by role (see: testing-library/dom-testing-library#1100) * chore(#62): major version update for prism-react-renderer * refactor(#62): improve code readability * feat(#62): add support for highlight-line comments * fix(#62): lint * feat(#62): add support for highlight-start / highlight-end comments * feat(#62): add support for highlight-next-line comments * refactor(#62): group initial shouldHighlightLine values * fix(#62): move codeClassNames into getLineProps, to avoid overwriting the token-line class from prism-react-renderer * fix(#62): update line highlight color to match Night Owl theme * refactor(#62): clean up semantic HTML structure - use figure/figcaption instead of div - use only one code element (vs one per line) * fix(#62): fix padding on pre element when no language tag or file tag * feat(#62): update semantics for highlighted lines to use <mark> * content(#62): refactor code blocks to use line highlights instead of comments * fix(#62): make font size consistent on iOS
1 parent 3652805 commit ae08bf0

File tree

6 files changed

+468
-384
lines changed

6 files changed

+468
-384
lines changed

blog/managing-focus-with-react-and-jest/index.mdx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ Then, find the place where that element is rendered. Add a `ref` attribute to th
207207

208208
```javascript
209209
return (
210-
<button ref={myRef}> // add the ref attribute
210+
<button ref={myRef}> // highlight-line
211211
Click me!
212212
</button>
213213
)
@@ -268,7 +268,7 @@ With that big picture in mind, let's get into the code to implement this:
268268
const [showSidebar, setShowSidebar] = useState(false)
269269
const [activeCell, setActiveCell] = useState(null)
270270

271-
const sidebarRef = useRef(null) // add this
271+
const sidebarRef = useRef(null) // highlight-line
272272
// ...
273273
}
274274
```
@@ -283,7 +283,7 @@ With that big picture in mind, let's get into the code to implement this:
283283
colors={activeCell}
284284
hideSidebar={hideSidebar}
285285
isHidden={!showSidebar}
286-
sidebarRef={sidebarRef} // add this
286+
sidebarRef={sidebarRef} // highlight-line
287287
/>
288288
// ...
289289
)
@@ -295,14 +295,14 @@ With that big picture in mind, let's get into the code to implement this:
295295
colors,
296296
hideSidebar,
297297
isHidden,
298-
sidebarRef, // add this
298+
sidebarRef, // highlight-line
299299
}) => {
300300
// ...
301301
return (
302302
// ...
303303
<h1
304-
ref={sidebarRef} // add this
305-
tabIndex={-1} // add this
304+
ref={sidebarRef} // highlight-line
305+
tabIndex={-1} // highlight-line
306306
>
307307
{colors.output}
308308
</h1>
@@ -324,7 +324,7 @@ With that big picture in mind, let's get into the code to implement this:
324324
const updateSidebar = (colors) => {
325325
setActiveCell(colors)
326326
setShowSidebar(true)
327-
sidebarRef.current.focus() // add this
327+
sidebarRef.current.focus() // highlight-line
328328
}
329329
// ...
330330
}
@@ -384,7 +384,7 @@ Now let's implement this in code:
384384
1. Create `buttonRef`. Which component should we create it in? Since we want to have a separate `ref` object for each TableCell, let's define `buttonRef` in the TableCell component. That way, each TableCell that mounts will have its own unique `ref` that can be focused independently.
385385
```javascript
386386
const TableCell = ({ colors, updateSidebar }) => {
387-
const buttonRef = useRef(null) // add this
387+
const buttonRef = useRef(null) // highlight-line
388388
// ...
389389
}
390390
```
@@ -396,7 +396,7 @@ Now let's implement this in code:
396396
<td>
397397
<button
398398
onClick={() => updateSidebar(colors)}
399-
ref={buttonRef} // add this
399+
ref={buttonRef} // highlight-line
400400
>
401401
{colors.output}
402402
</button>
@@ -411,7 +411,7 @@ Now let's implement this in code:
411411
return (
412412
// ...
413413
<button
414-
onClick={() => updateSidebar(colors, buttonRef)} // add buttonRef
414+
onClick={() => updateSidebar(colors, buttonRef)} // highlight-line
415415
ref={buttonRef}
416416
>
417417
// ...
@@ -423,7 +423,7 @@ Now let's implement this in code:
423423
const App = () => {
424424
const [showSidebar, setShowSidebar] = useState(false)
425425
const [activeCell, setActiveCell] = useState(null)
426-
const [lastCellClicked, setLastCellClicked] = useState(null) // add this
426+
const [lastCellClicked, setLastCellClicked] = useState(null) // highlight-line
427427
// ...
428428
}
429429
```
@@ -438,9 +438,8 @@ Now let's implement this in code:
438438
```javascript
439439
const App = () => {
440440
// ...
441-
const updateSidebar = (colors, buttonRef) => {
442-
// add buttonRef parameter
443-
setLastCellClicked(buttonRef) // add this
441+
const updateSidebar = (colors, buttonRef) => { // highlight-line
442+
setLastCellClicked(buttonRef) // highlight-line
444443
setActiveCell(colors)
445444
setShowSidebar(true)
446445
sidebarRef.current.focus()
@@ -455,7 +454,7 @@ Now let's implement this in code:
455454
// ...
456455
const hideSidebar = () => {
457456
setShowSidebar(false)
458-
lastCellClicked.current.focus() // add this
457+
lastCellClicked.current.focus() // highlight-line
459458
}
460459
// ...
461460
}
@@ -585,7 +584,7 @@ In this post, you learned about how to programmatically move a user's focus when
585584
586585
The next improvement I'm hoping to make is trapping focus inside the sidebar when it's open. That is, when users have the sidebar open and they repeatedly hit the Tab key, their focus should stay inside of the sidebar and not end up back in the rest of the body of the page. I'm planning on using something like the inert polyfill described in this [A11ycasts YouTube Video: Inert Polyfill](https://www.youtube.com/watch?v=fGLp_gfMMGU).
587586
588-
Until then, [reach out to me on Twitter](https://twitter.com/meganesulli) and let me know what you think about this post! I'm by no means an accessibility expert, and I'm always looking for new things to learn. What other opportunities do you see for accessibility improvements, in this project or in general?
587+
Until then, [reach out to me on Twitter](https://twitter.com/meganesulli) and let me know what you think about this post! I'm always looking for new things to learn. What other opportunities do you see for accessibility improvements, in this project or in general?
589588
590589
## Resources
591590

0 commit comments

Comments
 (0)