-
Notifications
You must be signed in to change notification settings - Fork 884
Expand file tree
/
Copy patha11y.tsx
More file actions
150 lines (131 loc) · 4.25 KB
/
Copy patha11y.tsx
File metadata and controls
150 lines (131 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Copyright (c) Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
/* global document cancelIdleCallback requestIdleCallback */
import * as React from 'react';
import axe from 'axe-core';
import { Layer, TetherBehavior, TETHER_PLACEMENT } from '../layer';
import { ParagraphSmall, ParagraphXSmall } from '../typography';
import { styled } from '../styles';
import { ThemeContext } from '../styles/theme-provider';
import type { ViolationProps } from './types';
function validateNode(node: HTMLElement) {
return new Promise<axe.Result[]>((resolve, reject) => {
axe.run(node, { reporter: 'v2' }, (error, results) => {
if (error) reject(error);
resolve(results.violations);
});
});
}
function segmentViolationsByNode(violations: axe.Result[]): Array<[string, axe.Result[]]> {
const nodes = violations.reduce((map, violation) => {
violation.nodes.forEach((node) => {
// @ts-expect-error todo(flow-ts) node.target is an Array
if (!map[node.target]) {
// @ts-expect-error todo(flow-ts) node.target is an Array
map[node.target] = [violation];
} else {
// @ts-expect-error todo(flow-ts) node.target is an Array
map[node.target].push(violation);
}
});
return map;
}, {});
return Object.entries(nodes);
}
const ViolationContainer = styled<
'div',
{
$top: string;
$left: string;
}
>('div', ({ $theme, $top, $left }) => {
return {
backgroundColor: $theme.colors.backgroundPrimary,
boxShadow: $theme.lighting.shadow600,
position: 'absolute',
padding: $theme.sizing.scale400,
top: $top,
left: $left,
};
});
function Violation(props: ViolationProps) {
const [offset, setOffset] = React.useState({ top: 0, left: 0 });
const [anchor, setAnchor] = React.useState(null);
const [popper, setPopper] = React.useState(null);
const [isHovered, setIsHovered] = React.useState(false);
const theme = React.useContext(ThemeContext);
const handleMouseEnter = () => setIsHovered(true);
const handleMouseLeave = () => setIsHovered(false);
React.useEffect(() => {
const node = document.querySelector(props.target);
if (node) {
// @ts-ignore
setAnchor(node);
node.setAttribute('style', `border: solid 1px ${theme.colors.borderNegative};`);
node.addEventListener('mouseenter', handleMouseEnter);
node.addEventListener('mouseleave', handleMouseLeave);
}
return () => {
if (node) {
node.removeEventListener('mouseenter', handleMouseEnter);
node.removeEventListener('mouseleave', handleMouseLeave);
}
};
}, [props.target]);
if (!isHovered) return null;
return (
<Layer>
<TetherBehavior
anchorRef={anchor}
popperRef={popper}
onPopperUpdate={(update) => setOffset(update.popper)}
placement={TETHER_PLACEMENT.bottom}
>
<ViolationContainer
// @ts-ignore
ref={setPopper}
$top={`${offset.top}px` || '0px'}
$left={`${offset.left}px` || '0px'}
>
<ParagraphXSmall>{props.target}</ParagraphXSmall>
{props.violations.map((violation, index) => (
<ParagraphSmall key={index}>{violation.description}</ParagraphSmall>
))}
</ViolationContainer>
</TetherBehavior>
</Layer>
);
}
export default function A11y(props: { children: React.ReactNode }) {
const [violations, setViolations] = React.useState<axe.Result[]>([]);
const [idleID, setIdleID] = React.useState(null);
const child = React.useRef<HTMLSpanElement>(null);
React.useEffect(() => {
if (child.current) {
if (idleID) {
cancelIdleCallback(idleID);
setIdleID(null);
}
const id = requestIdleCallback(() => {
// @ts-ignore
validateNode(child.current).then(setViolations);
});
// @ts-ignore
setIdleID(id);
}
}, [props.children]);
const violationsByNode = segmentViolationsByNode(violations);
return (
<>
<span ref={child}>{props.children}</span>
<div>
{violationsByNode.map(([node, violations], index) => (
<Violation target={node} violations={violations} key={index} />
))}
</div>
</>
);
}