-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.d.ts
194 lines (175 loc) · 4.03 KB
/
index.d.ts
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
export interface KeyUX {
start(): void
stop(): void
}
interface MinimalWindow {
addEventListener(
type: 'keydown' | 'keyup',
listener: (event: object) => void
): void
document: {
querySelector(selector: string): any
}
navigator: {
platform: string
userAgent: string
}
removeEventListener(
type: 'keydown' | 'keyup',
listener: (event: object) => void
): void
}
export interface KeyUXModule {
(window: MinimalWindow): () => void
}
export interface FocusGroupKeyUXOptions {
/**
* Maximum allowed pause between key presses when searching
* for a list item by name. Default is 300.
*/
searchDelayMs?: number
}
export type HotkeyOverride = Record<string, string>
type SingleDirectionTransformer = (
code: string,
window: MinimalWindow
) => false | string
export type Transformer = [
transformForward: SingleDirectionTransformer,
transformReverse: SingleDirectionTransformer
]
/**
* Press button/a according to `aria-keyshortcuts`.
*
* ```js
* import { startKeyUX, hotkeyKeyUX } from 'keyux'
*
* startKeyUX(window, [
* hotkeyKeyUX()
* ])
* ```
*/
export function hotkeyKeyUX(transformers?: Transformer[]): KeyUXModule
/**
* Add arrow-navigation on `role="menu"`.
*
* ```js
* import { startKeyUX, focusGroupKeyUX } from 'keyux'
*
* startKeyUX(window, [
* focusGroupKeyUX()
* ])
* ```
*/
export function focusGroupKeyUX(options?: FocusGroupKeyUXOptions): KeyUXModule
/**
* Add pressed style on button activation from keyboard.
*
* ```js
* import { startKeyUX, pressKeyUX } from 'keyux'
*
* startKeyUX(window, [
* pressKeyUX('is-pressed')
* ])
* ```
*/
export function pressKeyUX(className: string): KeyUXModule
/**
* Add support for focus jump into `aria-controls`.
*
* ```js
* import { startKeyUX, jumpsKeyUX } from 'keyux'
*
* startKeyUX(window, [
* jumpsKeyUX()
* ])
* ```
*/
export function jumpKeyUX(interactives?: string): KeyUXModule
/**
* Add support for `aria-hidden` block which can’t be archived by Tab,
* only by jump from another block.
*
* ```js
* import { startKeyUX, hiddenKeyUX } from 'keyux'
*
* startKeyUX(window, [
* hiddenKeyUX()
* ])
* ```
*/
export function hiddenKeyUX(): KeyUXModule
/**
* Start listeners for key events for better keyboard UX.
*
* ```js
* import {
* startKeyUX,
* hotkeyKeyUX,
* focusGroupKeyUX,
* pressKeyUX,
* jumpKeyUX,
* hiddenKeyUX
* } from 'keyux'
*
* startKeyUX(window, [
* hotkeyKeyUX(),
* focusGroupKeyUX(),
* pressKeyUX('is-pressed'),
* jumpKeyUX(),
* hiddenKeyUX()
* ])
* ```
*/
export function startKeyUX(
window: MinimalWindow,
plugins: KeyUXModule[]
): () => void
/**
* Returns `false` for tablets and phones.
*
* They still can have connected physical keyboard, it is just less likely.
* We recommend still support hot keys, but maybe do not show hints in UI.
*/
export function likelyWithKeyboard(window: MinimalWindow): boolean
/**
* Return text for `<kbd>` element with hint for hot key.
*
* It replaces `Meta` with `⌘` etc on Mac.
*
* ```js
* import { getHotKeyHint, likelyWithKeyboard } from 'keyux'
*
* export const Button = ({ hokey, children }) => {
* return <button aria-keyshortcuts={hotkey}>
* {children}
* {likelyWithKeyboard(window) && <kbd>{getHotKeyHint(window, hotkey)}</kbd>}
* </button>
* }
* ```
*/
export function getHotKeyHint(
window: MinimalWindow,
code: string,
transformers?: Transformer[]
): string
/**
* Provides a transformer for hotkey overrides that can be used
* with `hotkeyKeyUX()` and `getHotKeyHint()`.
*
* ```js
* import { getHotKeyHint, hotkeyKeyUX, hotkeyOverrides } from "keyux"
*
* let overrides = hotkeyOverrides({ 'alt+b': 'b' })
* startKeyUX(window, [
* hotkeyKeyUX([overrides])
* ])
* getHotKeyHint(window, 'b', [overrides])
* ```
*/
export function hotkeyOverrides(overrides: HotkeyOverride): Transformer
/**
* Provides a transformer for Mac compatibility mode that can be used
* with `hotkeyKeyUX()` and `getHotKeyHint()`.
*/
export function hotkeyMacCompat(): Transformer