The List Group component offers a flexible, customizable way to display items like links or text in a list format. It supports various styles and types, making it ideal for structured content.
Example
To use the List Group component, import it from the theui-svelte
library. Below is a simple example demonstrating how to use the List Group:
- First Item
- Second Item
- Third Item
<script>
import { ListGroup, ListItem } from "theui-svelte";
</script>
<ListGroup>
<ListItem>First Item</ListItem>
<ListItem>Second Item</ListItem>
<ListItem>Third Item</ListItem>
</ListGroup>
List Style Variant
The variant
prop in the List Group component allows you to control the overall style of the list. It provides two options:
- bordered: This style adds borders around each list item, giving a defined and structured look to the list. It is useful when you want to visually separate items clearly.
- flat: This style removes borders, resulting in a clean, minimalist design. It's ideal for cases where a simple and unobtrusive list presentation is preferred.
By default, the variant
is set to "bordered"
. You can easily switch between these styles to match the design needs of your application.
- First Item
- Second Item
- Third Item
<ListGroup variant="flat">
<ListItem>First Item</ListItem>
<ListItem>Second Item</ListItem>
<ListItem>Third Item</ListItem>
</ListGroup>
List Element Type
The type
prop in the List Group component determines the HTML element used to render the list. This allows you to customize the structure of the list based on your needs. The available options are:
- ul (default): Renders the list as an unordered list (
<ul>
), which is ideal for general list items or navigation. EachListItem
will be a<li>
in that case. - div: Setting
type="div"
renders eachListItem
as a<div>
by default. If theListItem
has anhref
, it becomes an<a>
. Withouthref
, it stays as a<div>
. This allows a mix of static and clickable content.
<ListGroup type="div">
<ListItem>Non-clickable item</ListItem>
<ListItem href="/link">Clickable link item</ListItem>
</ListGroup>
Accessibility
The List Group component ensures accessibility by using semantic HTML and ARIA roles:
- The parent container
<ul>
includesrole="list"
- Each
<li>
element hasrole="listitem"
to provide proper structure for assistive technologies.
To maintain accessibility:
- Ensure sufficient color contrast for readability.
- Interactive list items should be focusable and navigable using a keyboard.
- Add labels like `aria-label` if additional context is required for interactive elements.