ListBox

This component presents its assigned children as items in a single-selection list box. This is modeled after the list box controls in macOS and Microsoft Windows, and the standard select element in HTML in its list box mode.

Acai
Acerola
Apple
Apricot
Banana
Blackberry
Blueberry
Cantaloupe
Cherry
Cranberry
Currant
Date
Durian
Fig
Goji berry
Gooseberry
Grape
Grapefruit
Honeydew
Jackfruit
Kiwi
Kumquat
Lemon
Lime
Lychee
Mango
Mangosteen
Mulberry
Nectarine
Orange
Papaya
Passion Fruit
Peach
Pear
Pineapple
Plum
Pomegranate
Pomelo
Prickly Pear
Raspberry
Strawberry
Tangerine
Watermelon
Demo: Typical single-selection list box

ListBox uses SingleSelectionMixin to expose a single selection. The user can click an item to select it, or navigate the selection with the keyboard (per KeyboardDirectionMixin, KeyboardPagedSelectionMixin, and KeyboardPrefixSelectionMixin).

By default, a ListBox shows a generic visual style. The selected item is shown with standard highlight colors (CSS highlight color for the background, highlighttext for the text). You can create custom subclasses of ListBox that override the updates property to provide custom styling.

The ListBox exposes an orientation property that can either by "vertical" (the default), or "horizontal". Internally, the orientation is tracked as state.orientation.

😀
😁
😂
😍
😊
🤔
😆
😮
🙄
😘
Demo: A horizontal list

The ListBox applies SelectedItemTextValueMixin (below) to expose a value property.

Lists typically obtain their items from their child elements using SlotContentMixin. However, it is easy to create variations of ListBox for lists with hard-coded content. The custom list box below uses the same mixins as ListBox, but omits SlotContentMixin. Instead, it draws its content from a hard-coded set of items in its template.

Demo: A list with hard-coded items

You could create such components in cases where you want to ensure a particular set of items is always provided.

Usage

import ListBox from 'elix/src/ListBox.js';
const listBox = new ListBox(); // or
const listBox = document.createElement('elix-list-box');

In HTML:

<script type="module" src="node_modules/elix/src/ListBox.js"></script>
<elix-list-box>
  <!-- List items go here -->
</elix-list-box>

Single-selection list boxes are common in user interfaces. As written, the ListBox component supports the same use cases as a standard select element in list box mode (with the size attribute set to a value greater than 1). The standard select, however, has many constraints on what can be done with it, forcing developers to recreate much of its functionality. The advantage of providing ListBox as a web component is that developers are free to extend it to meet the needs of their interface. For example, one use case for ListBox is a horizontal list (instead of the fixed vertical orientation provided by select).

API

Ancestry: ListBox → ReactiveElement → HTMLElement

Built with mixins AriaListMixin, AttributeMarshallingMixin, ClickSelectionMixin, DirectionSelectionMixin, KeyboardDirectionMixin, KeyboardMixin, KeyboardPagedSelectionMixin, KeyboardPrefixSelectionMixin, LanguageDirectionMixin, ReactiveMixin, RenderUpdatesMixin, SelectedItemTextValueMixin, SelectionInViewMixin, ShadowTemplateMixin, SingleSelectionMixin, and SlotItemsMixin.

Included as a subelement in Explorer and ListExplorer.

$ property

The collection of references to the elements with IDs in the component's Shadow DOM subtree.

Example: if component's template contains a shadow element <button id="foo">, you can use the reference this.$.foo to obtain the corresponding button in the component instance's shadow tree.

Such references simplify a component's access to its own elements. In exchange, this mixin trades off a one-time cost of querying all elements in the shadow tree instead of paying an ongoing cost to query for an element each time the component wants to inspect or manipulate it.

These shadow element references are established the first time you read the $ property. They are not updated if you subsequently modify the shadow tree yourself (to replace one item with another, to add new items with id attributes, etc.).

Type: object

Defined by ShadowTemplateMixin inherited from ReactiveElement

canSelectNext property

True if the selection can be moved to the next item, false if not (the selected item is the last item in the list).

Type: boolean

Defined by SingleSelectionMixin

canSelectPrevious property

True if the selection can be moved to the previous item, false if not (the selected item is the first one in the list).

Type: boolean

Defined by SingleSelectionMixin

defaultState property

The default state for the component. This can be extended by mixins and classes to provide additional default state.

Type: object

Defined by ReactiveMixin inherited from ReactiveElement

getIndexOfItemAtY() method

Return the item whose content spans the given y position (relative to the top of the list's scrolling client area), or null if not found.

If downward is true, move down the list of items to find the first item found at the given y position; if downward is false, move up the list of

items to find the last item at that position.

Defined by KeyboardPagedSelectionMixin

itemCalcs(item, index) method

Returns a set of calculations about the given item that can be derived from the component's current state.

The goal of the itemCalcs step is to ensure that all mixins/classes use a consistent definition for facts about an item that can be derived from component state. By default, itemCalcs includes a member index containing the index of the indicated item. Other mixins/classes can extend the result of itemCalcs to include additional facts.

For example, the SingleSelectionMixin tracks selection at the component level through a state member state.selectedIndex. When rendering a specific item, a component generally wants to know, "Is this specific item the one which is selected?". SingleSelectionMixin does this with a defintion for itemCalcs that looks like this:

itemCalcs(item, index) {
  const base = super.itemCalcs ? super.itemCalcs(item, index) : null;
  return Object.assign({}, base, {
    selected: index === this.selectedIndex
  });
}


This ensures that any other aspect of the component that wants to inspect the selected state of a given item uses a consistent definition for selection.

Parameters:
  • item: Elementthe item being considered
  • index: numberthe item's position in the list

Defined by ContentItemsMixin inherited from SlotItemsMixin

items property

The current set of items drawn from the element's current state.

Defined by ContentItemsMixin inherited from SlotItemsMixin

itemUpdates(item, calcs, original) method

Determine what updates should be applied to an item to reflect the current state, using the format defined by the updates helpers.

By default, this returns an empty object. You should override this method (or use mixins that override this method) to indicate what updates should be applied to the given item during rendering.

Example: AriaListMixin uses code similar to the following to have an item's aria-selected attribute reflect its selection state:

itemUpdates(item, calcs, original) {
  const base = super.itemUpdates ? super.itemUpdates(item, calcs, original) : {};
  return merge(base, {
    attributes: {
      'aria-selected': calcs.selected
    },
  });
}


This code fragment is intended for use with SingleSelectionMixin, which provides the calcs.selected member.

Parameters:
  • item: Elementthe item to be updated
  • calcs: objectper-item calculations derived from element state
  • original: objectthe item's original HTML attributes, classes, and style

Returns: object the DOM updates that should be applied to the item

Defined by ContentItemsMixin inherited from SlotItemsMixin

pageDown() method

Scroll down one page.

Defined by KeyboardPagedSelectionMixin

pageUp() method

Scroll up one page.

Defined by KeyboardPagedSelectionMixin

refineState(state) method

Apply changes to a proposed new state for the component to enforce necessary consistency between state members. See Refining state for details.

Parameters:
  • state: objecta proposed new state for the component

Returns: boolean - true if the state is already acceptable as it is

Defined by ReactiveMixin inherited from ReactiveElement

render() method

Render the component to the DOM.

This method does nothing if the state has not changed since the last render call.

This method invokes all internal render methods. It then invoked componentDidMount (for first render) or componentDidUpdate (for subsequent renders).

Defined by ReactiveMixin inherited from ReactiveElement

scrollOnePage() method

Move by one page downward (if downward is true), or upward (if false). Return true if we ended up changing the selection, false if not.

Defined by KeyboardPagedSelectionMixin

scrollSelectionIntoView() method

Scroll the selected item element completely into view, minimizing the degree of scrolling performed.

Blink has a scrollIntoViewIfNeeded() function that does something similar, but unfortunately it's non-standard, and in any event often ends up scrolling more than is absolutely necessary.

This scrolls the containing element defined by the scrollTarget property. By default, it will scroll the element itself.

Defined by SelectionInViewMixin

selected-index-changed event

Raised when the selectedIndex property changes.

Defined by SingleSelectionMixin

selectedIndex property

The index of the currently-selected item, or -1 if no item is selected.

Type: number

Defined by SingleSelectionMixin

selectedItem property

The currently-selected item, or null if no item is selected.

Type: Element

Defined by SingleSelectionMixin

selectFirst() method

Select the first item in the list.

Returns: Boolean True if the selection changed, false if not.

Defined by SingleSelectionMixin

selectionRequired property

True if the list should always have a selection (if it has items).

Type: boolean

Default: false

Defined by SingleSelectionMixin

selectionWraps property

True if selection navigations wrap from last to first, and vice versa.

Type: boolean

Default: false

Defined by SingleSelectionMixin

selectItemWithTextPrefix(prefix) method

Select the first item whose text content begins with the given prefix.

Parameters:
  • prefix: stringThe prefix string to search for

Returns: boolean

Defined by KeyboardPrefixSelectionMixin

selectLast() method

Select the last item in the list.

Returns: Boolean True if the selection changed, false if not.

Defined by SingleSelectionMixin

selectNext() method

Select the next item in the list.

If the list has no selection, the first item will be selected.

Returns: Boolean True if the selection changed, false if not.

Defined by SingleSelectionMixin

selectPrevious() method

Select the previous item in the list.

If the list has no selection, the last item will be selected.

Returns: Boolean True if the selection changed, false if not.

Defined by SingleSelectionMixin

setState(changes) method

Update the component's state by merging the specified changes on top of the existing state. If the component is connected to the document, and the new state has changed, this returns a promise to asynchronously render the component. Otherwise, this returns a resolved promise.

Parameters:
  • changes: objectthe changes to apply to the element's state

Returns: Promise - resolves when the new state has been rendered

Defined by ReactiveMixin inherited from ReactiveElement

shouldComponentUpdate(nextState) method

Return true if the component should update.

The default implementation does a shallow check of property values like React's PureComponent. This seems adequate for most web components. You can override this to always return true (like React's base Component class), or to perform more specific, deeper checks for changes in state.

Parameters:
  • nextState: objectthe proposed new state for the element

Returns: boolean - true if the component should update (rerender)

Defined by ReactiveMixin inherited from ReactiveElement

state property

The component's current state. The returned state object is immutable. To update it, invoke setState.

Type: object

Defined by ReactiveMixin inherited from ReactiveElement

symbols.canGoLeft property

Returns true if it's currently possible to go left.

Type: boolean

Defined by DirectionSelectionMixin

symbols.canGoRight property

Returns true if it's currently possible to go right.

Type: boolean

Defined by DirectionSelectionMixin

symbols.goDown() method

Invokes selectNext to select the next item.

Defined by DirectionSelectionMixin

symbols.goDown() method

Invoked when the user wants to go/navigate down. The default implementation of this method does nothing.

Defined by KeyboardDirectionMixin

symbols.goEnd() method

Invoked when the user wants to go/navigate to the end (e.g., of a list). The default implementation of this method does nothing.

Defined by KeyboardDirectionMixin

symbols.goEnd() method

Invokes selectLast to select the next item.

Defined by DirectionSelectionMixin

symbols.goLeft() method

Invoked when the user wants to go/navigate left. The default implementation of this method does nothing.

Defined by KeyboardDirectionMixin

symbols.goLeft() method

Invokes selectPrevious to select the previous item.

If the element has a rightToLeft property and it is true, then this selects the next item.

Defined by DirectionSelectionMixin

symbols.goRight() method

Invoked when the user wants to go/navigate right. The default implementation of this method does nothing.

Defined by KeyboardDirectionMixin

symbols.goRight() method

Invokes selectNext to select the next item.

If the element has a rightToLeft property and it is true, then this selects the previous item.

Defined by DirectionSelectionMixin

symbols.goStart() method

Invokes selectFirst to select the first item.

Defined by DirectionSelectionMixin

symbols.goStart() method

Invoked when the user wants to go/navigate to the start (e.g., of a list). The default implementation of this method does nothing.

Defined by KeyboardDirectionMixin

symbols.goUp() method

Invokes selectPrevious to select the previous item.

Defined by DirectionSelectionMixin

symbols.goUp() method

Invoked when the user wants to go/navigate up. The default implementation of this method does nothing.

Defined by KeyboardDirectionMixin

symbols.keydown() method

See the symbols documentation for details.

Defined by KeyboardMixin

symbols.scrollTarget property

The element that should be scrolled to get the selected item into view.

By default, this uses the defaultScrollTarget helper to find the most likely candidate for scrolling. You can override this property to directly identify which element should be scrolled.

See also symbols.scrollTarget.

Defined by SelectionInViewMixin

updates property

The attributes and properies that should be applied to the component on render. By default, this is an empty plain object. Your mixin or component can extend this to identify the properties to set on the host element or elements in the shadow subtree.

Type: object

Defined by RenderUpdatesMixin inherited from ReactiveElement

value property

The text content of the selected item.

Setting this value to a string will attempt to select the first list item whose text content match that string. Setting this to a string not matching any list item will result in no selection.

Type: string

Defined by SelectedItemTextValueMixin