Carousel

Carousel with a sliding effect and navigation controls
Mountain lake Terraced farm Winter trees Forest river Red panda
Demo: A typical Carousel

Overview

Carousel presents a typical carousel with a sliding effect, left/right arrow buttons, and page dots. The user can move between items with touch, the mouse, the keyboard, or a trackpad.

By default, Carousel uses a SlidingStage as the "stage" for rendering the featured or selected item. The default left/right arrow buttons are provided by ArrowDirectionMixin.

Each item in the list is represented by default with a simple PageDot as a proxy element. The related CarouselWithThumbnails represents items with a Thumbnail proxy instead of a dot. You can also supply custom proxy elements used to represent the items.

Mountain lake Terraced farm Winter trees Forest river Red panda Snowy peaks Stone arch Snowy river Palm trees Sunset through palm trees River canyon Lake with milky water Dirt road lined with fallen leaves Iceberg River at sunrise Coastal mountains Hoodoo formations Sandy beach Autumn foliage on mountainside Sunset over rocky ocean pool
Demo: A CarouselWithThumbnails represents each item with a Thumbnail instead of a PageDot

For a carousel that supports the same types of navigation, but without arrows, dots, or other adornments, see SlidingPages.

Mountain lake Terraced farm Winter trees Forest river Red panda
Demo: SlidingPages provides no visible adornments

The CarouselSlideshow variation adds a timer for slideshow functionality.

Mountain lake Terraced farm Winter trees Forest river Red panda
Demo: CarouselSlideshow adds a timer that automatically advances the carousel

Carousel uses ArrowDirectionMixin to create the left and right arrow buttons. See that mixin for customization options.

A Carousel typically exhibits a horizontal orientation, although you can set the orientation property to "vertical". (On iOS, Mobile Safari may try to handle vertical touch gestures itself; the demo works more cleanly on Chrome for Android, which does not have that problem.)

Mountain lake Terraced farm Winter trees Forest river Red panda
Demo: A Carousel with vertical orientation

Carousel incorporates LanguageDirectionMixin so that it can flip the order of items in right-to-left languages. See that page for a demonstration.

Usage

import Carousel from 'elix/src/Carousel.js';
const carousel = new Carousel(); // or
const carousel = document.createElement('elix-carousel');

In HTML:

<script type="module" src="node_modules/elix/src/Carousel.js"></script>
<elix-carousel>
  <!-- Carousel items such as img elements go here. -->
  <img src="image1.jpg">
  <img src="image2.jpg">
  <img src="image3.jpg">
  <img src="image4.jpg">
  <img src="image5.jpg">
</elix-carousel>

Carousel is appropriate when:

  • You want to present to the user a collection of images, or a mixture of graphical and textual content.
  • You want to let the user focus on a single item at a time.
  • The number of items is small (2–10). By default, Carousel will create a PageDot for each item, which would be unweildy for larger collections. If you have a large collection of images to show, CarouselWithThumbnails or SlidingPages may be more suitable.

See also

API

Ancestry: Carousel → ExplorerReactiveElement → HTMLElement

Built with mixins AriaListMixin, ArrowDirectionMixin, AttributeMarshallingMixin, DarkModeMixin, DirectionSelectionMixin, FocusVisibleMixin, KeyboardDirectionMixin, KeyboardMixin, LanguageDirectionMixin, ReactiveMixin, ShadowTemplateMixin, SingleSelectionMixin, SlotItemsMixin, SwipeDirectionMixin, TouchSwipeMixin, and TrackpadSwipeMixin.

By default, this uses these elements in the following roles:

Extended by classes CarouselSlideshow and CarouselWithThumbnails.

arrowButtonOverlap property

True if the arrow buttons should overlap the component contents; false if they should appear to the side of the contents.

Type: boolean

Default: true

Defined by ArrowDirectionMixin

arrowButtonRole property

The class, tag, or template used to create the previous/next arrow buttons.

Type: (component class constructor)|HTMLTemplateElement|string

Default: ArrowDirectionButton

Defined by ArrowDirectionMixin

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

darkMode property

True if the component should configure itself for display on a dark background; false if the component should assume a light background.

The default value of this property is inferred when the component is initially added to the page. The component will look up its hierarchy for an ancestor that has an explicit background color. If the color's lightness value in the HSL cylindrical-coordinate system is below 50%, the background is assumed to be dark and darkMode will default to true. If the color is lighter than that, or no explicit background color can be found, the default value of darkMode will be false.

Type: boolean

Defined by DarkModeMixin

[internal.contentSlot] property

Defined by SlotContentMixin

[internal.defaultState] property

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

Type: State

Defined by ReactiveMixin

[internal.goDown]() method

Invokes selectNext to select the next item.

Defined by DirectionSelectionMixin

[internal.goEnd]() method

Invokes selectLast to select the next item.

Defined by DirectionSelectionMixin

[internal.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

[internal.goNext]() method

Invokes selectNext to select the next item.

Defined by DirectionSelectionMixin

[internal.goPrevious]() method

Invokes selectPrevious to select the previous item.

Defined by DirectionSelectionMixin

[internal.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

[internal.goStart]() method

Invokes selectFirst to select the first item.

Defined by DirectionSelectionMixin

[internal.goUp]() method

Invokes selectPrevious to select the previous item.

Defined by DirectionSelectionMixin

[internal.ids] property

A convenient shortcut for looking up an element by ID 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[internal.ids].foo to obtain the corresponding button in the component instance's shadow tree. The ids property is simply a shorthand for getElementById, so this[internal.ids].foo is the same as this.shadowRoot.getElementById('foo').

Type: object

Defined by ShadowTemplateMixin

[internal.itemMatchesState](item, state) method

Returns true if the given item should be shown in the indicated state.

Parameters:

  • item: HTMLElement|SVGElement
  • state: PlainObject

Returns: boolean

Defined by ContentItemsMixin

[internal.keydown]() method

See the symbols documentation for details.

Defined by KeyboardMixin

[internal.render](changed) method

Render the indicated changes in state to the DOM.

The default implementation of this method does nothing. Override this method in your component to update your component's host element and any shadow elements to reflect the component's new state. See the rendering example.

Be sure to call super in your method implementation so that your component's base classes and mixins have a chance to perform their own render work.

Parameters:

  • changed: objectdictionary of flags indicating which state members have changed since the last render

Defined by ReactiveMixin

[internal.renderChanges]() method

Render any pending component changes to the DOM.

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

ReactiveMixin will invoke this method following a setState call; you should not need to invoke this method yourself.

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

Defined by ReactiveMixin

[internal.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

[internal.state] property

The component's current state.

The returned state object is immutable. To update it, invoke internal.setState.

It's extremely useful to be able to inspect component state while debugging. If you append ?elixdebug=true to a page's URL, then ReactiveMixin will conditionally expose a public state property that returns the component's state. You can then access the state in your browser's debug console.

Type: State

Defined by ReactiveMixin

[internal.swipeDown]() method

Invokes the internal.goUp method.

Defined by SwipeDirectionMixin

[internal.swipeLeft]() method

Invokes the internal.goRight method.

Defined by SwipeDirectionMixin

[internal.swipeRight]() method

Invokes the internal.goLeft method.

Defined by SwipeDirectionMixin

[internal.swipeTarget] property

Type: HTMLElement

Defined by TouchSwipeMixin

[internal.swipeUp]() method

Invokes the internal.goDown method.

Defined by SwipeDirectionMixin

items property

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

Type: Array.|null

Defined by ContentItemsMixin

items-changed event

Raised when the items property changes.

Defined by ContentItemsMixin

proxies property

The current set of proxy elements that correspond to the component's main items. If you have assigned elements to the proxy slot, this returns the collection of those elements. Otherwise, this will return a collection of default proxies generated by the component, one for each item.

Type: Array.

Defined by Explorer

proxyListOverlap property

True if the list of proxies should overlap the stage, false if not.

Type: boolean

Default: false

Defined by Explorer

proxyListPosition property

The position of the proxy list relative to the stage.

The start and end values refer to text direction: in left-to-right languages such as English, these are equivalent to left and right, respectively.

Type: 'bottom'|'end'|'left'|'right'|'start'|'top'

Default: 'start'

Defined by Explorer

proxyListRole property

The class, tag, or template used to create the Explorer's list of proxies.

Type: (component class constructor)|HTMLTemplateElement|string

Default: ListBox

Defined by Explorer

proxyRole property

The class, tag, or template used to create default proxies for the list items.

Type: (component class constructor)|HTMLTemplateElement|string

Default: 'div'

Defined by Explorer

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

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

stageRole property

The class, tag, or template used for the main "stage" element that shows a single item at a time.

Type: (component class constructor)|HTMLTemplateElement|string

Default: Modes

Defined by Explorer

suppressFocusVisibility() method

Temporarily suppress visibility of the keyboard focus until the next keydown event.

This can be useful in components like Menu that actively manage where the focus is in response to mouse hover activity. If the user uses the keyboard to invoke a menu, then changes to using the mouse, it can be distracting to see the focus indicator moving as well. In such situations, the component can invoke this method (e.g., in response to mousemove) to temporarily suppress focus visibility.

Defined by FocusVisibleMixin

wrap(original) method

Destructively wrap a node with elements to show arrow buttons.

Parameters:

  • original: Nodethe node that should be wrapped by buttons

Defined by ArrowDirectionMixin