Class

FocusCycler (ui)

@ckeditor/ckeditor5-ui/src/focuscycler

class

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 the focus() method,
  • an associated focus tracker to determine which view is focused.

A simple cycler setup can look like this:

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:

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

  • readonly

    actions : FocusCyclerActions | undefined

    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.

    actions: {
    	// Will call #focusPrevious() when arrowleft or arrowup is pressed.
    	focusPrevious: [ 'arrowleft', 'arrowup' ],
    
    	// Will call #focusNext() when arrowdown is pressed.
    	focusNext: 'arrowdown'
    }
    
  • readonly

    current : null | number

    An index of the view in the focusables which is focused according to focusTracker. Returns null when there is no such view.

  • readonly

    first : null | FocusableView

    Returns the first focusable view in focusables. Returns null if there is none.

    Note: Hidden views (e.g. with display: none) are ignored.

  • readonly

    focusTracker : FocusTracker

    A focus tracker instance that the cycler uses to determine the current focus state in focusables.

  • readonly

    focusables : ViewCollection<FocusableView>

    A focusable views collection that the cycler operates on.

  • readonly

    keystrokeHandler : KeystrokeHandler | undefined

    An instance of the KeystrokeHandler which can respond to certain keystrokes and cycle the focus.

  • readonly

    last : null | FocusableView

    Returns the last focusable view in focusables. Returns null if there is none.

    Note: Hidden views (e.g. with display: none) are ignored.

  • readonly

    next : null | FocusableView

    Returns the next focusable view in focusables based on current. Returns null if there is none.

    Note: Hidden views (e.g. with display: none) are ignored.

  • readonly

    previous : null | FocusableView

    Returns the previous focusable view in focusables based on current. Returns null if there is none.

    Note: Hidden views (e.g. with display: none) are ignored.

Methods

  • constructor( options = { [options.actions], options.focusTracker, options.focusables, [options.keystrokeHandler] } )

    Creates an instance of the focus cycler utility.

    Parameters

    options : object

    Configuration options.

    Properties
    [ options.actions ] : FocusCyclerActions
    options.focusTracker : FocusTracker
    options.focusables : ViewCollection<FocusableView>
    [ options.keystrokeHandler ] : KeystrokeHandler
  • inherited

    delegate( events ) → EmitterMixinDelegateChain

    Delegates selected events to another Emitter. For instance:

    emitterA.delegate( 'eventX' ).to( emitterB );
    emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );
    

    then eventX is delegated (fired by) emitterB and emitterC along with data:

    emitterA.fire( 'eventX', data );
    

    and eventY is delegated (fired by) emitterC along with data:

    emitterA.fire( 'eventY', data );
    

    Parameters

    events : Array<string>

    Event names that will be delegated to another emitter.

    Returns

    EmitterMixinDelegateChain
  • inherited

    fire( eventOrInfo, args ) → GetEventInfo<TEvent>[ 'return' ]

    Fires an event, executing all callbacks registered for it.

    The first parameter passed to callbacks is an EventInfo object, followed by the optional args provided in the fire() method call.

    Type parameters

    TEvent : extends BaseEvent

    The type describing the event. See BaseEvent.

    Parameters

    eventOrInfo : GetNameOrEventInfo<TEvent>

    The name of the event or EventInfo object if event is delegated.

    args : TEvent[ 'args' ]

    Additional arguments to be passed to the callbacks.

    Returns

    GetEventInfo<TEvent>[ 'return' ]

    By default the method returns undefined. However, the return value can be changed by listeners through modification of the evt.return's property (the event info is the first param of every callback).

  • focusFirst() → void

    Focuses the first item in focusables.

    Note: Hidden views (e.g. with display: none) are ignored.

    Returns

    void
  • focusLast() → void

    Focuses the last item in focusables.

    Note: Hidden views (e.g. with display: none) are ignored.

    Returns

    void
  • focusNext() → void

    Focuses the next item in focusables.

    Note: Hidden views (e.g. with display: none) are ignored.

    Returns

    void
  • focusPrevious() → void

    Focuses the previous item in focusables.

    Note: Hidden views (e.g. with display: none) are ignored.

    Returns

    void
  • inherited

    listenTo( emitter, event, callback, [ options ] ) → void

    Registers a callback function to be executed when an event is fired in a specific (emitter) object.

    Events can be grouped in namespaces using :. When namespaced event is fired, it additionally fires all callbacks for that namespace.

    // myEmitter.on( ... ) is a shorthand for myEmitter.listenTo( myEmitter, ... ).
    myEmitter.on( 'myGroup', genericCallback );
    myEmitter.on( 'myGroup:myEvent', specificCallback );
    
    // genericCallback is fired.
    myEmitter.fire( 'myGroup' );
    // both genericCallback and specificCallback are fired.
    myEmitter.fire( 'myGroup:myEvent' );
    // genericCallback is fired even though there are no callbacks for "foo".
    myEmitter.fire( 'myGroup:foo' );
    

    An event callback can stop the event and set the return value of the fire method.

    Type parameters

    TEvent : extends BaseEvent

    The type describing the event. See BaseEvent.

    Parameters

    emitter : Emitter

    The object that fires the event.

    event : TEvent[ 'name' ]

    The name of the event.

    callback : GetCallback<TEvent>

    The function to be called on event.

    [ options ] : GetCallbackOptions<TEvent>

    Additional options.

    Returns

    void
  • inherited

    off( event, callback ) → void

    Stops executing the callback on the given event. Shorthand for this.stopListening( this, event, callback ).

    Parameters

    event : string

    The name of the event.

    callback : Function

    The function to stop being called.

    Returns

    void
  • inherited

    on( event, callback, [ options ] ) → void

    Registers a callback function to be executed when an event is fired.

    Shorthand for this.listenTo( this, event, callback, options ) (it makes the emitter listen on itself).

    Type parameters

    TEvent : extends BaseEvent

    The type descibing the event. See BaseEvent.

    Parameters

    event : TEvent[ 'name' ]

    The name of the event.

    callback : GetCallback<TEvent>

    The function to be called on event.

    [ options ] : GetCallbackOptions<TEvent>

    Additional options.

    Returns

    void
  • inherited

    once( event, callback, [ options ] ) → void

    Registers a callback function to be executed on the next time the event is fired only. This is similar to calling on followed by off in the callback.

    Type parameters

    TEvent : extends BaseEvent

    The type descibing the event. See BaseEvent.

    Parameters

    event : TEvent[ 'name' ]

    The name of the event.

    callback : GetCallback<TEvent>

    The function to be called on event.

    [ options ] : GetCallbackOptions<TEvent>

    Additional options.

    Returns

    void
  • inherited

    stopDelegating( [ event ], [ emitter ] ) → void

    Stops delegating events. It can be used at different levels:

    • To stop delegating all events.
    • To stop delegating a specific event to all emitters.
    • To stop delegating a specific event to a specific emitter.

    Parameters

    [ event ] : string

    The name of the event to stop delegating. If omitted, stops it all delegations.

    [ emitter ] : Emitter

    (requires event) The object to stop delegating a particular event to. If omitted, stops delegation of event to all emitters.

    Returns

    void
  • inherited

    stopListening( [ emitter ], [ event ], [ callback ] ) → void

    Stops listening for events. It can be used at different levels:

    • To stop listening to a specific callback.
    • To stop listening to a specific event.
    • To stop listening to all events fired by a specific object.
    • To stop listening to all events fired by all objects.

    Parameters

    [ emitter ] : Emitter

    The object to stop listening to. If omitted, stops it for all objects.

    [ event ] : string

    (Requires the emitter) The name of the event to stop listening to. If omitted, stops it for all events from emitter.

    [ callback ] : Function

    (Requires the event) The function to be removed from the call list for the given event.

    Returns

    void
  • private

    _focus( view, direction ) → void

    Focuses the given view if it exists.

    Parameters

    view : null | FocusableView

    The view to be focused

    direction : 1 | 1

    The direction of the focus if the view has focusable children.

    Returns

    void
  • private

    _getDomFocusableItem( step ) → null | FocusableView

    Returns the next or previous focusable view in focusables with respect to current.

    Parameters

    step : 1 | 1

    Either 1 for checking forward from current or -1 for checking backwards.

    Returns

    null | FocusableView

Events

  • backwardCycle( eventInfo )

    Fired when the focus cycler is about to move the focus from the first focusable item to the last one.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

  • forwardCycle( eventInfo )

    Fired when the focus cycler is about to move the focus from the last focusable item to the first one.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.