-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.tsx
148 lines (138 loc) · 4.03 KB
/
index.tsx
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
/**
* WordPress dependencies
*/
import { useState, useRef } from '@wordpress/element';
import {
createHigherOrderComponent,
useRefEffect,
useMergeRefs,
} from '@wordpress/compose';
import { isKeyboardEvent } from '@wordpress/keycodes';
import type { WPKeycodeModifier } from '@wordpress/keycodes';
const defaultShortcuts = {
previous: [
{
modifier: 'ctrlShift',
character: '`',
},
{
modifier: 'ctrlShift',
character: '~',
},
{
modifier: 'access',
character: 'p',
},
] as const,
next: [
{
modifier: 'ctrl',
character: '`',
},
{
modifier: 'access',
character: 'n',
},
] as const,
};
type Shortcuts = {
previous: readonly { modifier: WPKeycodeModifier; character: string }[];
next: readonly { modifier: WPKeycodeModifier; character: string }[];
};
export function useNavigateRegions( shortcuts: Shortcuts = defaultShortcuts ) {
const ref = useRef< HTMLDivElement >( null );
const [ isFocusingRegions, setIsFocusingRegions ] = useState( false );
function focusRegion( offset: number ) {
const regions = Array.from(
ref.current?.querySelectorAll< HTMLElement >(
'[role="region"][tabindex="-1"]'
) ?? []
);
if ( ! regions.length ) {
return;
}
let nextRegion = regions[ 0 ];
// Based off the current element, use closest to determine the wrapping region since this operates up the DOM. Also, match tabindex to avoid edge cases with regions we do not want.
const wrappingRegion =
ref.current?.ownerDocument?.activeElement?.closest< HTMLElement >(
'[role="region"][tabindex="-1"]'
);
const selectedIndex = wrappingRegion
? regions.indexOf( wrappingRegion )
: -1;
if ( selectedIndex !== -1 ) {
let nextIndex = selectedIndex + offset;
nextIndex = nextIndex === -1 ? regions.length - 1 : nextIndex;
nextIndex = nextIndex === regions.length ? 0 : nextIndex;
nextRegion = regions[ nextIndex ];
}
nextRegion.focus();
setIsFocusingRegions( true );
}
const clickRef = useRefEffect(
( element ) => {
function onClick() {
setIsFocusingRegions( false );
}
element.addEventListener( 'click', onClick );
return () => {
element.removeEventListener( 'click', onClick );
};
},
[ setIsFocusingRegions ]
);
return {
ref: useMergeRefs( [ ref, clickRef ] ),
className: isFocusingRegions ? 'is-focusing-regions' : '',
onKeyDown( event: React.KeyboardEvent< HTMLDivElement > ) {
if (
shortcuts.previous.some( ( { modifier, character } ) => {
return isKeyboardEvent[ modifier ]( event, character );
} )
) {
focusRegion( -1 );
} else if (
shortcuts.next.some( ( { modifier, character } ) => {
return isKeyboardEvent[ modifier ]( event, character );
} )
) {
focusRegion( 1 );
}
},
};
}
/**
* `navigateRegions` is a React [higher-order component](https://facebook.github.io/react/docs/higher-order-components.html)
* adding keyboard navigation to switch between the different DOM elements marked as "regions" (role="region").
* These regions should be focusable (By adding a tabIndex attribute for example). For better accessibility,
* these elements must be properly labelled to briefly describe the purpose of the content in the region.
* For more details, see "Landmark Roles" in the [WAI-ARIA specification](https://www.w3.org/TR/wai-aria/)
* and "Landmark Regions" in the [ARIA Authoring Practices Guide](https://www.w3.org/WAI/ARIA/apg/practices/landmark-regions/).
*
* ```jsx
* import { navigateRegions } from '@wordpress/components';
*
* const MyComponentWithNavigateRegions = navigateRegions( () => (
* <div>
* <div role="region" tabIndex="-1" aria-label="Header">
* Header
* </div>
* <div role="region" tabIndex="-1" aria-label="Content">
* Content
* </div>
* <div role="region" tabIndex="-1" aria-label="Sidebar">
* Sidebar
* </div>
* </div>
* ) );
* ```
*/
export default createHigherOrderComponent(
( Component ) =>
( { shortcuts, ...props } ) => (
<div { ...useNavigateRegions( shortcuts ) }>
<Component { ...props } />
</div>
),
'navigateRegions'
);