@cas-smartdesign/virtual-list
v9.0.8
Published
A virtualized list element with SmartDesign look and feel.
Readme
@cas-smartdesign/virtual-list
A list element with ui-virtualization and infinite scrolling.
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
item-height- Defines the height in px of the list items. Defaults to 48.
item-count- Defines the virtual number of items in the list.
selection-type- Defines the mode of selection handling
- Available values: "trigger-only", "single", "multi"
focus-index- Defines the index of currently focused item
focus-target- 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
The following properties are available:
itemsany[] (default=empty)- Contains data for the currently rendered list items.
itemGeneratorItemGenerator (default is the generator inside the list-item element)- A method that renders the list items.
itemHeightnumber (default=48)- Reflects the corresponding attribute
itemCountnumber (default=0)- Reflects the corresponding attribute
selectionTypestring (default="trigger-only")- Reflects the corresponding attribute
focusIndexnumber (default=-1)- Reflects the corresponding attribute.
-1means no item is focused.
- Reflects the corresponding attribute.
selectedIndicesnumber[] (default=empty)- Contains the indices of the selected items.
focusTargetboolean (default=false)- Reflects the
focus-targetattribute. Keeps the focused item marked even while the list does not have DOM focus.
- Reflects the
finalSizeIsKnownboolean (default=undefined)- Indicates whether the total number of items is final. While false/undefined the list is in infinite-scrolling state and items report
aria-setsize="-1", and ArrowUp stops at the first item instead of wrapping around to the last. ArrowDown wraps from the last item to the first either way.
- Indicates whether the total number of items is final. While false/undefined the list is in infinite-scrolling state and items report
Custom events
sd-virtual-list-data-request- Sent after scrolling the list
- Contains the starting and ending index that is visible in the list
sd-virtual-list-selection- Dispatched when one of the items is selected/deselected: by a left or middle click, or by Enter or Space on the focused item. An Enter or Space that comes from a control inside an item (a text field, button, link and the like) is left to that control. Disabled items can be focused but not selected.
- 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
- Note that only visible items can be accessed this way
scrollToItem- Scrolls the list to the item with the given index.
- Accepts an optional alignment argument:
"auto"(default),"start","center", or"end". A small padding is kept between the item and the viewport edge so the item's focus ring stays visible.
increaseWidthOnNextRenderIfNeeded- The width of the list is increased on the next rendering phase 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
Focus ring
The virtual-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 — including while the list virtualizes and scrolls its items. 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.
The items are updated with the aria-setsize & aria-posinset attributes. The former one is -1 if the final size is not known to indicate infinite scrolling state.
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.
item-index- The index of the item.
last- Set if the item is the last in the currently rendered set of items.
