Skip to content

Commit cf6cee6

Browse files
authored
Merge pull request #727 from SableRaf/accessibility-fixes
Homepage keyboard accessibility fixes
2 parents c57ae10 + 57ce3aa commit cf6cee6

9 files changed

Lines changed: 119 additions & 22 deletions

File tree

‎i18n/react-intl/en.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"privacy": "Privacy",
2222
"donate": "Donate",
2323
"search": "Search",
24+
"skipToContent": "Skip to main content",
2425
"searchWithGoogle": "Search with Google",
2526
"introTitle": "Welcome to Processing!",
2627
"introText": "Processing is a flexible software sketchbook and a language for learning how to code. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology. There are tens of thousands of students, artists, designers, researchers, and hobbyists who use Processing for learning and prototyping.",

‎i18n/react-intl/es.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"people": "Personas",
2121
"donate": "Donar",
2222
"search": "Buscar",
23+
"skipToContent": "Saltar al contenido principal",
2324
"searchWithGoogle": "Buscar con Google",
2425
"introTitle": "¡Bienvenido a Processing!",
2526
"introText": "Processing es un software flexible para bosquejar y un lenguaje para aprender cómo programar dentro del contexto de las artes visuales. Desde el 2001 Processing ha promovido el conocimiento de software dentro de las artes visuales y el conocimiento de las artes visuales dentro de la tecnología.Hay decenas de miles de estudiantes, artistas, diseñadores investigadores y entusiastas que usan Processing para aprender y prototipar.",

‎src/components/Button.module.css‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
color: white;
3030
}
3131

32+
.root:focus-visible {
33+
outline: 2px solid var(--processing-blue-mid);
34+
outline-offset: 8px;
35+
}
36+
37+
.dark:focus-visible,
38+
.outline:focus-visible {
39+
outline-color: var(--white);
40+
}
41+
3242
/* Variant: Animate1 & Animate2 */
3343
.dark {
3444
background-color: var(--white);

‎src/components/Layout.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33
import { Helmet } from 'react-helmet';
44
import { MDXProvider } from '@mdx-js/react';
55
import classnames from 'classnames';
6+
import { useIntl } from 'react-intl';
67

78
import Header from './Header';
89
import Footer from './Footer';
@@ -22,6 +23,7 @@ import * as css from './Layout.module.css';
2223
export const LayoutContext = React.createContext();
2324

2425
const Layout = ({ children, withSidebar, withBreadcrumbs, mainClassName }) => {
26+
const intl = useIntl();
2527
const [headerScrolled, setHeaderScrolled] = useState(false);
2628
const [currentHeading, setCurrentHeading] = useState('');
2729

@@ -69,13 +71,18 @@ const Layout = ({ children, withSidebar, withBreadcrumbs, mainClassName }) => {
6971

7072
return (
7173
<div className={css.root}>
74+
<a href="#main-content" className={css.skipLink}>
75+
{intl.formatMessage({ id: 'skipToContent' })}
76+
</a>
7277
<div id="processing-banner" />
7378
<link rel="stylesheet" href="https://foundation-donate-banner.netlify.app/static/css/main.css" />
7479
<script src="https://foundation-donate-banner.netlify.app/static/js/main.js" ></script>
7580
<LayoutContext.Provider value={{ headerScrolled, currentHeading }}>
7681
<Helmet titleTemplate="%s / Processing.org" />
7782
<Header siteTitle="Processing" scrolled={headerScrolled} />
7883
<main
84+
id="main-content"
85+
tabIndex={-1}
7986
className={classnames(css.main, mainClassName, {
8087
[css.withBreadcrumbs]: withBreadcrumbs
8188
})}>

‎src/components/Layout.module.css‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,34 @@
1313
}
1414
}
1515

16+
.skipLink {
17+
position: absolute;
18+
top: var(--gutter-half);
19+
left: var(--gutter-half);
20+
z-index: 1000;
21+
padding: var(--gutter-half) var(--gutter);
22+
background-color: var(--processing-blue-dark);
23+
color: var(--white);
24+
font-weight: 600;
25+
transform: translateY(calc(-100% - var(--gutter)));
26+
27+
&:focus {
28+
transform: none;
29+
color: var(--white);
30+
outline: 2px solid var(--processing-blue-mid);
31+
outline-offset: 2px;
32+
}
33+
}
34+
1635
.main {
1736
position: relative;
1837
flex: 1;
1938
padding-top: var(--vertical-margin-large);
2039
padding-bottom: var(--vertical-margin-large);
40+
41+
&:focus {
42+
outline: none;
43+
}
2144
}
2245

2346
.withBreadcrumbs {

‎src/components/Navbar.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useRef, useState } from 'react';
22
import classnames from 'classnames';
33
import { LocalizedLink as Link } from 'gatsby-theme-i18n';
44

@@ -14,6 +14,19 @@ import MenuIcon from '../images/menu-icon.svg';
1414

1515
const Navbar = ({ siteTitle, scrolled }) => {
1616
const [expanded, setExpanded] = useState(false);
17+
const toggleRef = useRef(null);
18+
19+
useEffect(() => {
20+
if (!expanded) return;
21+
const onKeyDown = (e) => {
22+
if (e.key !== 'Escape') return;
23+
setExpanded(false);
24+
toggleRef.current?.focus();
25+
};
26+
document.addEventListener('keydown', onKeyDown);
27+
return () => document.removeEventListener('keydown', onKeyDown);
28+
}, [expanded]);
29+
1730
return (
1831
<div
1932
className={classnames(
@@ -32,6 +45,7 @@ const Navbar = ({ siteTitle, scrolled }) => {
3245
</h1>
3346
<div className={classnames(grid.col, css.menuWrapper)}>
3447
<button
48+
ref={toggleRef}
3549
className={css.menuToggle}
3650
aria-label="Main menu"
3751
aria-expanded={expanded}

‎src/components/NavbarItems.module.css‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828

2929
.active,
3030
.subitem.active,
31-
.item.hasSubmenu:hover {
31+
.item.hasSubmenu:hover,
32+
.item.hasSubmenu:focus-within {
3233
border-left-color: var(--processing-blue-dark);
3334
}
3435

@@ -50,7 +51,8 @@
5051
transition: max-width 0.15s ease-in-out, opacity 0.15s ease-in-out;
5152
}
5253

53-
.item:hover .submenu {
54+
.item:hover .submenu,
55+
.item:focus-within .submenu {
5456
max-width: none;
5557
opacity: 1;
5658
}
@@ -77,14 +79,6 @@
7779
}
7880

7981

80-
.item:hover .submenu,
81-
.item:focus-visible .submenu,
82-
.submenu:focus-within {
83-
opacity: 1;
84-
max-height: 400px;
85-
}
86-
87-
8882
.donate {
8983
background-color: var(--donate-bg-start);
9084
background-image: linear-gradient(0deg, var(--donate-bg-start), var(--donate-bg-end));

‎src/components/sketch/Shape.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ const Shape = (props) => {
7979
return (
8080
/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */
8181
<span
82-
tabIndex={tabIndex}
8382
className={css.root}
8483
onMouseEnter={handleMouseEnterLine}
8584
onMouseLeave={handleMouseLeaveLine}>

‎src/components/sketch/SketchCode.js‎

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import Button from '../Button';
88

99
import * as css from './SketchCode.module.css';
1010

11+
const CONTROLS = 'button, summary, input, [role="button"]';
12+
13+
// Controls inside a folded <details> have no layout and can't take focus
14+
const isShown = (el) => el.getClientRects().length > 0;
15+
1116
const SketchCode = ({
1217
width,
1318
height,
@@ -23,6 +28,43 @@ const SketchCode = ({
2328
}) => {
2429
const [hasInteracted, setHasInteracted] = useState(false);
2530
const showGridButton = useRef(null);
31+
const rootRef = useRef(null);
32+
const activeRef = useRef(null);
33+
34+
// Roving tabindex: the editor is a single tab stop and arrow keys move
35+
// between its controls (Left/Right stay free for changing values)
36+
useEffect(() => {
37+
const controls = [...rootRef.current.querySelectorAll(CONTROLS)];
38+
if (!controls.includes(activeRef.current)) {
39+
activeRef.current = controls.find(isShown) ?? null;
40+
}
41+
controls.forEach((el) => {
42+
el.tabIndex = isVisible && el === activeRef.current ? 0 : -1;
43+
});
44+
});
45+
46+
const handleFocus = (e) => {
47+
if (!e.target.matches(CONTROLS)) return;
48+
if (activeRef.current) activeRef.current.tabIndex = -1;
49+
activeRef.current = e.target;
50+
e.target.tabIndex = 0;
51+
};
52+
53+
const handleKeyDown = (e) => {
54+
const controls = [...rootRef.current.querySelectorAll(CONTROLS)].filter(
55+
isShown
56+
);
57+
const current = controls.indexOf(e.target);
58+
const next = {
59+
ArrowDown: Math.min(current + 1, controls.length - 1),
60+
ArrowUp: Math.max(current - 1, 0),
61+
Home: 0,
62+
End: controls.length - 1
63+
}[e.key];
64+
if (current === -1 || next === undefined) return;
65+
e.preventDefault();
66+
controls[next].focus();
67+
};
2668

2769
useEffect(() => {
2870
setTimeout(() => {
@@ -34,8 +76,14 @@ const SketchCode = ({
3476
return (
3577
/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */
3678
<div
79+
ref={rootRef}
80+
role="toolbar"
81+
aria-label="Sketch code"
82+
aria-orientation="vertical"
3783
className={classnames(css.root, { [css.blink]: !hasInteracted })}
38-
onMouseEnter={() => setHasInteracted(true)}>
84+
onMouseEnter={() => setHasInteracted(true)}
85+
onFocus={handleFocus}
86+
onKeyDown={handleKeyDown}>
3987
<div className={css.numBar}></div>
4088
<Line num={1}>
4189
<span className="hljs-datatype">int</span> u = 60;
@@ -45,15 +93,15 @@ const SketchCode = ({
4593
<button
4694
className={classnames(css.interactive, 'hljs-keyword')}
4795
onClick={(e) => onChange('showGrid', !showGrid)}
48-
tabIndex={isVisible ? 0 : -1}
96+
tabIndex={-1}
4997
ref={showGridButton}>
5098
{showGrid ? 'true' : 'false'}
5199
</button>
52100
;
53101
</Line>
54102
<Line num={3} />
55103
<details open>
56-
<summary tabIndex={isVisible ? 0 : -1}>
104+
<summary tabIndex={-1}>
57105
<Line num={4}>
58106
<span className="hljs-keyword">void</span>{' '}
59107
<span className="hljs-title">setup</span>
@@ -70,7 +118,7 @@ const SketchCode = ({
70118
</details>
71119
<Line num={7} />
72120
<details open>
73-
<summary tabIndex={isVisible ? 0 : -1}>
121+
<summary tabIndex={-1}>
74122
<Line num={8}>
75123
<span className="hljs-keyword">void</span>{' '}
76124
<span className="hljs-title">draw</span>
@@ -90,7 +138,7 @@ const SketchCode = ({
90138
<button
91139
className={classnames(css.interactive, 'hljs-constant')}
92140
onClick={(e) => onChange('strokeCap', !strokeCap)}
93-
tabIndex={isVisible ? 0 : -1}>
141+
tabIndex={-1}>
94142
{strokeCap ? 'SQUARE' : 'ROUND'}
95143
</button>
96144
);
@@ -105,7 +153,7 @@ const SketchCode = ({
105153
max={2}
106154
isInteger={false}
107155
onChange={(val) => onChange('strokeWeight', val)}
108-
tabIndex={isVisible ? 0 : -1}
156+
tabIndex={-1}
109157
/>
110158
{' * u);'}
111159
</Line>
@@ -122,7 +170,7 @@ const SketchCode = ({
122170
onChangeShape={onChangeShape}
123171
color={shapes[i].color}
124172
shapeIndex={i}
125-
tabIndex={isVisible ? 0 : -1}
173+
tabIndex={-1}
126174
/>
127175
);
128176
</Line>
@@ -134,7 +182,7 @@ const SketchCode = ({
134182
draggableClassName={css.interactive}
135183
min={0}
136184
max={8}
137-
tabIndex={isVisible ? 0 : -1}
185+
tabIndex={-1}
138186
/>
139187
;
140188
</Line>
@@ -146,7 +194,7 @@ const SketchCode = ({
146194
</details>
147195
<Line num={23} />
148196
<details>
149-
<summary tabIndex={isVisible ? 0 : -1}>
197+
<summary tabIndex={-1}>
150198
<Line num={24}>
151199
<span className="hljs-keyword">void</span>
152200
{` drawGrid() {`}
@@ -195,7 +243,7 @@ const SketchCode = ({
195243
className={css.reset}
196244
variant="gray"
197245
onClick={onResetState}
198-
tabIndex={isVisible ? 0 : -1}>
246+
tabIndex={-1}>
199247
Reset
200248
</Button>
201249
</div>

0 commit comments

Comments
 (0)