FocusCycler (ui)
@ckeditor/ckeditor5-ui/src/focuscycler
A utility class that helps cycling over focusable views in a
ViewCollection
when the focus is tracked by the
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 thefocus()
method, - an associated focus tracker to determine which view is focused.
A simple cycler setup can look like this:
const focusables = new ViewCollection();
const focusTracker = new FocusTracker();
// Add focusable views to the focus tracker.
focusTracker.add( ... );
Then, the cycler can be used manually:
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 KeystrokeHandler
:
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 "Deep dive into focus tracking" guide to learn more.
Filtering
Properties
-
actions : Object
module:ui/focuscycler~FocusCycler#actions
readonly
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
andstopPropagation
will be called on the event the keystroke fired in the DOM.actions: { // Will call #focusPrevious() when arrowleft or arrowup is pressed. focusPrevious: [ 'arrowleft', 'arrowup' ], // Will call #focusNext() when arrowdown is pressed. focusNext: 'arrowdown' }
-
current : Number | null
module:ui/focuscycler~FocusCycler#current
readonly
An index of the view in the
focusables
which is focused according tofocusTracker
. Returnsnull
when there is no such view. -
Returns the first focusable view in
focusables
. Returnsnull
if there is none.Note: Hidden views (e.g. with
display: none
) are ignored. -
focusTracker : FocusTracker
module:ui/focuscycler~FocusCycler#focusTracker
readonly
A focus tracker instance that the cycler uses to determine the current focus state in
focusables
. -
focusables : Collection
module:ui/focuscycler~FocusCycler#focusables
readonly
A view collection that the cycler operates on.
-
keystrokeHandler : KeystrokeHandler
module:ui/focuscycler~FocusCycler#keystrokeHandler
readonly
An instance of the
KeystrokeHandler
which can respond to certain keystrokes and cycle the focus. -
Returns the last focusable view in
focusables
. Returnsnull
if there is none.Note: Hidden views (e.g. with
display: none
) are ignored. -
Returns the next focusable view in
focusables
based oncurrent
. Returnsnull
if there is none.Note: Hidden views (e.g. with
display: none
) are ignored. -
Returns the previous focusable view in
focusables
based oncurrent
. Returnsnull
if there is none.Note: Hidden views (e.g. with
display: none
) are ignored.
Methods
-
constructor( options = { options.focusables, options.focusTracker, [options.keystrokeHandler], [options.actions] } )
module:ui/focuscycler~FocusCycler#constructor
Creates an instance of the focus cycler utility.
Parameters
options : Object
Configuration options.
Propertiesoptions.focusables : Collection | Object
options.focusTracker : FocusTracker
[ options.keystrokeHandler ] : KeystrokeHandler
[ options.actions ] : Object
-
focusFirst()
module:ui/focuscycler~FocusCycler#focusFirst
Focuses the
first
item infocusables
.Note: Hidden views (e.g. with
display: none
) are ignored. -
focusLast()
module:ui/focuscycler~FocusCycler#focusLast
Focuses the
last
item infocusables
.Note: Hidden views (e.g. with
display: none
) are ignored. -
focusNext()
module:ui/focuscycler~FocusCycler#focusNext
Focuses the
next
item infocusables
.Note: Hidden views (e.g. with
display: none
) are ignored. -
focusPrevious()
module:ui/focuscycler~FocusCycler#focusPrevious
Focuses the
previous
item infocusables
.Note: Hidden views (e.g. with
display: none
) are ignored. -
_focus( view )
module:ui/focuscycler~FocusCycler#_focus
protected
Focuses the given view if it exists.
Parameters
view : View
-
_getFocusableItem( step ) → View | null
module:ui/focuscycler~FocusCycler#_getFocusableItem
protected
Returns the next or previous focusable view in
focusables
with respect tocurrent
.
Every day, we work hard to keep our documentation complete. Have you spotted an outdated information? Is something missing? Please report it via our issue tracker.