@cas-smartdesign/list
v9.0.8
Published
A list element with SmartDesign look and feel.
Readme
@cas-smartdesign/list
An element that renders a given array of items in a list.
This element must be used together with the design tokens of SmartDesign. For an independent application, make sure to include the @cas-smartdesign/design-tokens package. For a web app / web widget, refer to the official documentation about design token usage.
Attributes
selection-type: string- Defines the type of selection that the list uses. Defaults to
trigger-only. Allowed values are:single: the list items can be selected one at a time, theselectedattribute is set on the selected itemmulti: multiple list items can be selected, theselectedattribute is set on the selected itemstrigger-only: theselectedattribute is not set on the selected item(s)
- Defines the type of selection that the list uses. Defaults to
focus-index: number- The index of the currently focused item. Setting it moves the roving focus to that item and updates
aria-activedescendant. A value of-1means no item is focused.
- The index of the currently focused item. Setting it moves the roving focus to that item and updates
focus-target: boolean- When present, the item at
focus-indexis kept marked asfocusedeven while the list itself does not have DOM focus.
- When present, the item at
role: string- Aria attribute, default value is "listbox"
Properties
items: array- The data to be used by the list.
itemGenerator: ItemGenerator- A function that creates a HTMLElement from an element of the
itemsarray to be rendered by the list. - If it is not set then the default implementation is used.
- A function that creates a HTMLElement from an element of the
selectedIndexes: array- The indexes of the currently selected items.
selectionType: SelectionType- Property accessor for the
selection-typeattribute (trigger-only|single|multi).
- Property accessor for the
focusIndex: number- Property accessor for the
focus-indexattribute.
- Property accessor for the
focusTarget: boolean- Property accessor for the
focus-targetattribute.
- Property accessor for the
listItems: HTMLElement[] (read-only)- The list item elements currently rendered in the list (separators excluded).
Custom Events
sd-list-selection- Dispatched when one of the items is selected/deselected
- Contains the index of the item and a
selectedflag which is when false indicates that the item is deselected - The
originalEventa mouse or keyboard event that triggered the selection event.
Public methods
getListItem- Returns the DOM node that represents the item with the given index
increaseWidthIfNeeded- The width of the list is increased to an extent, that all texts in the list-items are visible without ellipsis.
- If the list has already reached it's maximum width, then line clamp may be enabled on the items to wrap the content if possible.
- Note that this functionality works only if
sd-list-itemsare rendered in the list
Selection state
The list marks the items with selected attribute in case if they are selected, but does not define any style for this state.
The items generated by the itemGenerator can use this attribute if custom logic is required.
Focus ring
The list does not draw a focus ring itself — it only marks the focused item (focused attribute) and keeps aria-activedescendant up to date. The visible ring is drawn by @cas-smartdesign/focus-ring (an optional peer dependency), which resolves the focused item from exactly these signals. Install it once per application, not per component:
import { FocusRing } from "@cas-smartdesign/focus-ring";
FocusRing.install();The docs examples of this package are driven by this setup (see docs/doc.ts); no per-list wiring or reserved padding around the list is needed.
A11Y
The default role is listbox, with the corresponding option role on the items, since most of the time this element is used with a selection state.
It ensures the items have an associated id which is used to update aria-activedescendant.
The selection is also marked with aria-selected.
Please note that the selection type trigger-only does not provide any accessibility event in case of a selection,
but it can be added externally for example with an aria-live area where the content can notify about the selection.
Although normally the page would change by such a selection, so it may not be necessary to do this notification.
Attributes managed on items
In addition to above mentioned aria attributes, the items are marked with certain custom attributes:
focused- Set if the item is currently focused.
selected- Set if the item is selected.
