-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
focuscycler.ts
419 lines (366 loc) · 12.2 KB
/
focuscycler.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module ui/focuscycler
*/
import {
isVisible,
type ArrayOrItem,
type FocusTracker,
type KeystrokeHandler,
EmitterMixin
} from '@ckeditor/ckeditor5-utils';
import type View from './view.js';
import type ViewCollection from './viewcollection.js';
/**
* A utility class that helps cycling over {@link module:ui/focuscycler~FocusableView focusable views} in a
* {@link module:ui/viewcollection~ViewCollection} when the focus is tracked by the
* {@link module:utils/focustracker~FocusTracker} instance. It helps implementing keyboard
* navigation in HTML forms, toolbars, lists and the like.
*
* To work properly it requires:
* * a collection of focusable (HTML `tabindex` attribute) views that implement the `focus()` method,
* * an associated focus tracker to determine which view is focused.
*
* A simple cycler setup can look like this:
*
* ```ts
* const focusables = new ViewCollection<FocusableView>();
* const focusTracker = new FocusTracker();
*
* // Add focusable views to the focus tracker.
* focusTracker.add( ... );
* ```
*
* Then, the cycler can be used manually:
*
* ```ts
* const cycler = new FocusCycler( { focusables, focusTracker } );
*
* // Will focus the first focusable view in #focusables.
* cycler.focusFirst();
*
* // Will log the next focusable item in #focusables.
* console.log( cycler.next );
* ```
*
* Alternatively, it can work side by side with the {@link module:utils/keystrokehandler~KeystrokeHandler}:
*
* ```ts
* const keystrokeHandler = new KeystrokeHandler();
*
* // Activate the keystroke handler.
* keystrokeHandler.listenTo( sourceOfEvents );
*
* const cycler = new FocusCycler( {
* focusables, focusTracker, keystrokeHandler,
* actions: {
* // When arrowup of arrowleft is detected by the #keystrokeHandler,
* // focusPrevious() will be called on the cycler.
* focusPrevious: [ 'arrowup', 'arrowleft' ],
* }
* } );
* ```
*
* Check out the {@glink framework/deep-dive/ui/focus-tracking "Deep dive into focus tracking"} guide to learn more.
*/
export default class FocusCycler extends EmitterMixin() {
/**
* A {@link module:ui/focuscycler~FocusableView focusable views} collection that the cycler operates on.
*/
public readonly focusables: ViewCollection<FocusableView>;
/**
* A focus tracker instance that the cycler uses to determine the current focus
* state in {@link #focusables}.
*/
public readonly focusTracker: FocusTracker;
/**
* An instance of the {@link module:utils/keystrokehandler~KeystrokeHandler}
* which can respond to certain keystrokes and cycle the focus.
*/
public readonly keystrokeHandler?: KeystrokeHandler;
/**
* Actions that the cycler can take when a keystroke is pressed. Requires
* `options.keystrokeHandler` to be passed and working. When an action is
* performed, `preventDefault` and `stopPropagation` will be called on the event
* the keystroke fired in the DOM.
*
* ```ts
* actions: {
* // Will call #focusPrevious() when arrowleft or arrowup is pressed.
* focusPrevious: [ 'arrowleft', 'arrowup' ],
*
* // Will call #focusNext() when arrowdown is pressed.
* focusNext: 'arrowdown'
* }
* ```
*/
public readonly actions?: FocusCyclerActions;
/**
* Creates an instance of the focus cycler utility.
*
* @param options Configuration options.
*/
constructor( options: {
focusables: ViewCollection<FocusableView>;
focusTracker: FocusTracker;
keystrokeHandler?: KeystrokeHandler;
actions?: FocusCyclerActions;
} ) {
super();
this.focusables = options.focusables;
this.focusTracker = options.focusTracker;
this.keystrokeHandler = options.keystrokeHandler;
this.actions = options.actions;
if ( options.actions && options.keystrokeHandler ) {
for ( const methodName in options.actions ) {
let actions = options.actions[ methodName as keyof FocusCyclerActions ]!;
if ( typeof actions == 'string' ) {
actions = [ actions ];
}
for ( const keystroke of actions ) {
options.keystrokeHandler.set( keystroke, ( data, cancel ) => {
this[ methodName as keyof FocusCyclerActions ]();
cancel();
} );
}
}
}
this.on<FocusCyclerForwardCycleEvent>( 'forwardCycle', () => this.focusFirst(), { priority: 'low' } );
this.on<FocusCyclerBackwardCycleEvent>( 'backwardCycle', () => this.focusLast(), { priority: 'low' } );
}
/**
* Returns the first focusable view in {@link #focusables}.
* Returns `null` if there is none.
*
* **Note**: Hidden views (e.g. with `display: none`) are ignored.
*/
public get first(): FocusableView | null {
return ( this.focusables.find( isDomFocusable ) || null ) as FocusableView | null;
}
/**
* Returns the last focusable view in {@link #focusables}.
* Returns `null` if there is none.
*
* **Note**: Hidden views (e.g. with `display: none`) are ignored.
*/
public get last(): FocusableView | null {
return ( this.focusables.filter( isDomFocusable ).slice( -1 )[ 0 ] || null ) as FocusableView | null;
}
/**
* Returns the next focusable view in {@link #focusables} based on {@link #current}.
* Returns `null` if there is none.
*
* **Note**: Hidden views (e.g. with `display: none`) are ignored.
*/
public get next(): FocusableView | null {
return this._getDomFocusableItem( 1 );
}
/**
* Returns the previous focusable view in {@link #focusables} based on {@link #current}.
* Returns `null` if there is none.
*
* **Note**: Hidden views (e.g. with `display: none`) are ignored.
*/
public get previous(): FocusableView | null {
return this._getDomFocusableItem( -1 );
}
/**
* An index of the view in the {@link #focusables} which is focused according
* to {@link #focusTracker}. Returns `null` when there is no such view.
*/
public get current(): number | null {
let index: number | null = null;
// There's no focused view in the focusables.
if ( this.focusTracker.focusedElement === null ) {
return null;
}
this.focusables.find( ( view, viewIndex ) => {
const focused = view.element === this.focusTracker.focusedElement;
if ( focused ) {
index = viewIndex;
}
return focused;
} );
return index;
}
/**
* Focuses the {@link #first} item in {@link #focusables}.
*
* **Note**: Hidden views (e.g. with `display: none`) are ignored.
*/
public focusFirst(): void {
this._focus( this.first, 1 );
}
/**
* Focuses the {@link #last} item in {@link #focusables}.
*
* **Note**: Hidden views (e.g. with `display: none`) are ignored.
*/
public focusLast(): void {
this._focus( this.last, -1 );
}
/**
* Focuses the {@link #next} item in {@link #focusables}.
*
* **Note**: Hidden views (e.g. with `display: none`) are ignored.
*/
public focusNext(): void {
const next = this.next;
// If there's only one focusable item, we need to let the outside world know
// that the next cycle is about to happen. This may be useful
// e.g. if you want to move the focus to the parent focus cycler.
// Note that the focus is not actually moved in this case.
if ( next && this.focusables.getIndex( next ) === this.current ) {
this.fire<FocusCyclerForwardCycleEvent>( 'forwardCycle' );
return;
}
if ( next === this.first ) {
this.fire<FocusCyclerForwardCycleEvent>( 'forwardCycle' );
} else {
this._focus( next, 1 );
}
}
/**
* Focuses the {@link #previous} item in {@link #focusables}.
*
* **Note**: Hidden views (e.g. with `display: none`) are ignored.
*/
public focusPrevious(): void {
const previous = this.previous;
if ( previous && this.focusables.getIndex( previous ) === this.current ) {
this.fire<FocusCyclerBackwardCycleEvent>( 'backwardCycle' );
return;
}
if ( previous === this.last ) {
this.fire<FocusCyclerBackwardCycleEvent>( 'backwardCycle' );
} else {
this._focus( previous, -1 );
}
}
/**
* Focuses the given view if it exists.
*
* @param view The view to be focused
* @param direction The direction of the focus if the view has focusable children.
* @returns
*/
private _focus( view: FocusableView | null, direction: 1 | -1 ) {
// Don't fire focus events if the view is already focused.
// Such attempt may occur when cycling with only one focusable item:
// even though `focusNext()` method returns without changing focus,
// the `forwardCycle` event is fired, triggering the `focusFirst()` method.
if ( view && this.focusTracker.focusedElement !== view.element ) {
view.focus( direction );
}
}
/**
* Returns the next or previous focusable view in {@link #focusables} with respect
* to {@link #current}.
*
* @param step Either `1` for checking forward from {@link #current} or `-1` for checking backwards.
*/
private _getDomFocusableItem( step: 1 | -1 ): FocusableView | null {
// Cache for speed.
const collectionLength = this.focusables.length;
if ( !collectionLength ) {
return null;
}
const current = this.current;
// Start from the beginning if no view is focused.
// https://github.com/ckeditor/ckeditor5-ui/issues/206
if ( current === null ) {
return this[ step === 1 ? 'first' : 'last' ];
}
// Note: If current is the only focusable view, it will also be returned for the given step.
let focusableItem = this.focusables.get( current )!;
// Cycle in both directions.
let index = ( current + collectionLength + step ) % collectionLength;
do {
const focusableItemCandidate = this.focusables.get( index )!;
if ( isDomFocusable( focusableItemCandidate ) ) {
focusableItem = focusableItemCandidate;
break;
}
// Cycle in both directions.
index = ( index + collectionLength + step ) % collectionLength;
} while ( index !== current );
return focusableItem;
}
}
/**
* A {@link module:ui/view~View} that can be focused (e.g. has `focus()` method).
*/
export type FocusableView = View & {
/**
* Focuses the view.
*
* @param direction This optional parameter helps improve the UX by providing additional information about the direction the focus moved
* (e.g. in a complex view or a form). It is useful for views that host multiple focusable children (e.g. lists, toolbars):
* * `1` indicates that the focus moved forward and, in most cases, the first child of the focused view should get focused,
* * `-1` indicates that the focus moved backwards, and the last focusable child should get focused
*
* See {@link module:ui/focuscycler~FocusCycler#event:forwardCycle} and {@link module:ui/focuscycler~FocusCycler#event:backwardCycle}
* to learn more.
*/
focus( direction?: 1 | -1 ): void;
};
/**
* A {@link module:ui/view~View} that hosts one or more of focusable children being managed by a {@link module:ui/focuscycler~FocusCycler}
* instance exposed under `focusCycler` property.
*/
export type ViewWithFocusCycler = FocusableView & {
focusCycler: FocusCycler;
};
export interface FocusCyclerActions {
focusFirst?: ArrayOrItem<string>;
focusLast?: ArrayOrItem<string>;
focusNext?: ArrayOrItem<string>;
focusPrevious?: ArrayOrItem<string>;
}
/**
* Fired when the focus cycler is about to move the focus from the last focusable item
* to the first one.
*
* @eventName ~FocusCycler#forwardCycle
*/
export type FocusCyclerForwardCycleEvent = {
name: 'forwardCycle';
args: [];
};
/**
* Fired when the focus cycler is about to move the focus from the first focusable item
* to the last one.
*
* @eventName ~FocusCycler#backwardCycle
*/
export type FocusCyclerBackwardCycleEvent = {
name: 'backwardCycle';
args: [];
};
/**
* Checks whether a view can be focused (has `focus()` method and is visible).
*
* @param view A view to be checked.
*/
function isDomFocusable( view: View ) {
return isFocusable( view ) && isVisible( view.element );
}
/**
* Checks whether a view is {@link ~FocusableView}.
*
* @param view A view to be checked.
*/
export function isFocusable( view: View ): view is FocusableView {
return !!( 'focus' in view && typeof view.focus == 'function' );
}
/**
* Checks whether a view is an instance of {@link ~ViewWithFocusCycler}.
*
* @param view A view to be checked.
*/
export function isViewWithFocusCycler( view: View ): view is ViewWithFocusCycler {
return isFocusable( view ) && 'focusCycler' in view && view.focusCycler instanceof FocusCycler;
}