Skip to content

Commit c6e2e3a

Browse files
committed
Added support for focusable code blocks on overflow
1 parent 900c113 commit c6e2e3a

File tree

8 files changed

+85
-6
lines changed

8 files changed

+85
-6
lines changed

material/assets/javascripts/bundle.4e2bfc5d.min.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

material/assets/javascripts/bundle.4e2bfc5d.min.js renamed to material/assets/javascripts/bundle.ff925e8b.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/assets/javascripts/bundle.ff925e8b.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

material/assets/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"assets/javascripts/bundle.js": "assets/javascripts/bundle.4e2bfc5d.min.js",
3-
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.4e2bfc5d.min.js.map",
2+
"assets/javascripts/bundle.js": "assets/javascripts/bundle.ff925e8b.min.js",
3+
"assets/javascripts/bundle.js.map": "assets/javascripts/bundle.ff925e8b.min.js.map",
44
"assets/javascripts/vendor.js": "assets/javascripts/vendor.809e24aa.min.js",
55
"assets/javascripts/vendor.js.map": "assets/javascripts/vendor.809e24aa.min.js.map",
66
"assets/javascripts/worker/search.js": "assets/javascripts/worker/search.f6ebf1dc.min.js",

material/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ <h1>{{ page.title | default(config.site_name, true)}}</h1>
179179
</div>
180180
{% block scripts %}
181181
<script src="{{ 'assets/javascripts/vendor.809e24aa.min.js' | url }}"></script>
182-
<script src="{{ 'assets/javascripts/bundle.4e2bfc5d.min.js' | url }}"></script>
182+
<script src="{{ 'assets/javascripts/bundle.ff925e8b.min.js' | url }}"></script>
183183
{%- set translations = {} -%}
184184
{%- for key in [
185185
"clipboard.copy",

src/assets/javascripts/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import {
8686
SearchIndex
8787
} from "integrations"
8888
import {
89+
patchCodeBlocks,
8990
patchTables,
9091
patchDetails,
9192
patchScrollfix,
@@ -178,6 +179,7 @@ export function initialize(config: unknown) {
178179

179180
const keyboard$ = setupKeyboard()
180181

182+
patchCodeBlocks({ document$, viewport$ })
181183
patchDetails({ document$, hash$ })
182184
patchScripts({ document$ })
183185
patchSource({ document$ })
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2016-2020 Martin Donath <martin.donath@squidfunk.com>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
23+
import { Observable, combineLatest } from "rxjs"
24+
import { distinctUntilKeyChanged, map } from "rxjs/operators"
25+
26+
import { Viewport, getElements } from "browser"
27+
28+
/* ----------------------------------------------------------------------------
29+
* Helper types
30+
* ------------------------------------------------------------------------- */
31+
32+
/**
33+
* Mount options
34+
*/
35+
interface MountOptions {
36+
document$: Observable<Document> /* Document observable */
37+
viewport$: Observable<Viewport> /* Viewport observable */
38+
}
39+
40+
/* ----------------------------------------------------------------------------
41+
* Functions
42+
* ------------------------------------------------------------------------- */
43+
44+
/**
45+
* Patch all `code` elements
46+
*
47+
* This function will make overflowing code blocks focusable via keyboard, so
48+
* they can be scrolled without a mouse.
49+
*
50+
* @param options - Options
51+
*/
52+
export function patchCodeBlocks(
53+
{ document$, viewport$ }: MountOptions
54+
): void {
55+
const els$ = document$
56+
.pipe(
57+
map(() => getElements<HTMLTableElement>("pre > code"))
58+
)
59+
60+
/* Observe viewport size only */
61+
const size$ = viewport$
62+
.pipe(
63+
distinctUntilKeyChanged("size")
64+
)
65+
66+
/* Make overflowing elements focusable */
67+
combineLatest([els$, size$])
68+
.subscribe(([els]) => {
69+
for (const el of els) {
70+
if (el.scrollWidth > el.clientWidth)
71+
el.setAttribute("tabindex", "0")
72+
else
73+
el.removeAttribute("tabindex")
74+
}
75+
})
76+
}

src/assets/javascripts/patches/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* IN THE SOFTWARE.
2121
*/
2222

23+
export * from "./code"
2324
export * from "./details"
2425
export * from "./script"
2526
export * from "./scrollfix"

0 commit comments

Comments
 (0)