Skip to content

Responsive Collections

Faststrap's collection components compose lists, grids, and record cards from ordinary FastHTML elements. They work for files, users, products, lessons, datasets, experiment runs, and other browsable records. The components are data-agnostic: your application owns fetching, authorization, and persistence.

Use a collection when people browse items as individual records. Use DataTable when comparing columns, sorting, filtering, pagination, or export is central to the task.

Choose A Composition

Need Use What it provides
A single flexible record row ListItem Leading/content/trailing slots and interaction states.
A vertical sequence of rows ListView Shared density, dividers, empty, and loading presentation.
An auto-fitting CSS grid GridList Responsive card placement without JavaScript.
One server-selected list or grid CollectionView Composes rendered items or records through renderers.
A bookmarkable list/grid selector ViewToggle Query-parameter links, optionally upgraded with HTMX.
A table that changes presentation on small screens DataTable(mobile_mode=...) Retains table behavior and offers mobile cards or stacked rows.

These are presentation primitives, not a client-side collection framework. They do not fetch records, manage selection state, or implement client-side sorting. Let FastHTML routes and HTMX own those behaviors.

Quick Start

from faststrap import Avatar, CollectionView, ListItem

people = CollectionView(
    ListItem(
        title="Ada Lovelace",
        subtitle="Mathematics department",
        meta="Active now",
        leading=Avatar(name="Ada Lovelace"),
        trailing="Administrator",
        href="/people/ada",
    ),
    ListItem(
        title="Grace Hopper",
        subtitle="Computer science department",
        meta="Last active yesterday",
        leading=Avatar(name="Grace Hopper"),
        trailing="Instructor",
        href="/people/grace",
    ),
    mode="list",
    label="People",
)

ListItem

ListItem lays out a record summary with optional leading, central, and trailing content. Its slots accept FastHTML elements, so compose Avatar, Icon, Badge, Button, checkboxes, and other components rather than embedding framework-specific behavior in the row.

from faststrap import Badge, Button, Icon, ListItem

ListItem(
    leading=Icon("file-earmark-pdf", cls="fs-4 text-danger"),
    title="Q3 report.pdf",
    subtitle="Finance team",
    meta="2.4 MB - Updated 10 minutes ago",
    trailing=Badge("PDF", variant="danger"),
    href="/files/q3-report",
    density="comfortable",
)

For an action on the row itself, use href or pass supported hx_* attributes to the component. Keep nested actions such as the example button intentional: interactive controls nested inside a link are invalid HTML; prefer a non-link ListItem with a trailing action button when the row also has an action.

ListItem(
    title="Deployment #1842",
    subtitle="Production / us-east",
    meta=Badge("Healthy", variant="success"),
    leading=Icon("rocket-takeoff"),
    trailing=Button("Open", href="/deployments/1842", size="sm"),
)
Parameter Type Default Description
*children Any () Content; the first child becomes the title when title is omitted. Remaining children are appended to the text area.
title Any \| None None Primary line.
subtitle Any \| None None Secondary line.
meta Any \| None None Tertiary or supporting line; may be a badge or other element.
leading / trailing Any \| None None Start/end slots for avatars, icons, selection controls, badges, or actions.
href str \| None None Render the row as a link. A disabled or loading row suppresses navigation.
selected bool False Adds selected styling and aria-current="true". This is presentation only; your route owns the state.
active bool False Adds active styling and aria-current="page" for link rows.
disabled bool False Adds disabled styling and aria-disabled; prevents row navigation.
loading bool False Shows a Bootstrap spinner, announces loading, and prevents link navigation.
density "compact" \| "comfortable" \| "spacious" "comfortable" Controls row padding.
lines 1 \| 2 \| 3 \| None None Hides lower text lines when set to one or two; three preserves all lines.
cls, style, css_vars Standard Faststrap styling — Add custom classes, inline styles, or CSS variables. Other HTML, ARIA, data_*, and hx_* attributes are accepted.

selected, active, disabled, and loading describe different states. selected and active do not create a selection model; set them from your server-rendered application state. Disabled/loading link rows render as non-navigable content.

ListView

ListView provides a vertical list surface. Children already carrying role="listitem" (including ListItem) remain direct children; other content is wrapped in a semantic list-item container. These are presentation semantics, not stateful selection controls. Use it for settings, files, activity feeds, or compact mobile summaries.

from faststrap import ListItem, ListView

ListView(
    ListItem(title="Notifications", meta="On"),
    ListItem(title="Privacy", meta="Standard"),
    ListItem(title="Appearance", meta="System default"),
    density="compact",
    divided=True,
    label="Account settings",
    empty="No settings are available.",
)
Parameter Type Default Description
*items FastHTML content () Child rows or other content.
density "compact" \| "comfortable" \| "spacious" "comfortable" Default row-density class for the list surface. Set a row's own density when it needs to differ.
divided bool True Show separators between direct child rows.
flush bool False Remove outer radius and side borders for edge-to-edge use inside another surface.
label str \| None None Accessible name emitted as aria-label; provide one for a meaningful list.
empty Any \| None None Content shown when there are no child items. No empty message is rendered unless supplied.
loading bool False Replace children with loading_content and expose it as a status.
loading_content Any "Loading..." Text or FastHTML content shown in the loading state.
cls, style, css_vars Standard Faststrap styling — Customize the wrapper; standard HTML, ARIA, data_*, and hx_* attributes are also accepted.

Loading takes precedence over the items and empty state. The component does not manage focus or selection: links and buttons in its children retain their native keyboard behavior. Use explicit ListItem link/action semantics rather than making an entire static list behave like an undocumented widget.

GridList

GridList is a CSS grid for cards, files, products, datasets, or custom FastHTML content. Children already carrying role="listitem" remain direct; other content is wrapped in a semantic list-item container. With columns=None, CSS uses an auto-fitting responsive layout based on min_width. Setting columns fixes the requested track count; choose a suitable value for the narrowest viewport where that grid is shown.

from faststrap import Card, GridList

GridList(
    Card("Dataset A", header="Sales"),
    Card("Dataset B", header="Retention"),
    Card("Dataset C", header="Forecast"),
    min_width="15rem",
    gap=3,
    label="Available datasets",
)
Parameter Type Default Description
*items FastHTML content () Grid children; each is wrapped in a list-item container.
min_width str "16rem" Minimum track width in auto-fit mode; accepts a CSS length.
columns int \| None None Optional explicit number of tracks; must be at least one.
gap int \| str 3 Integer uses Bootstrap spacing scale (3 becomes 0.75rem); a string is emitted as a CSS gap value.
label str \| None None Accessible list name.
empty, loading, loading_content Any, bool, Any None, False, "Loading..." Optional empty and loading content; loading takes precedence.
cls, style, css_vars Standard Faststrap styling — Customize the grid wrapper; standard HTML, ARIA, data_*, and hx_* attributes are accepted.

CollectionView

CollectionView chooses list, compact-list, grid, or card-grid presentation. It supports both already-rendered children and a record-oriented mode. In record mode, the selected renderer receives one record and returns a FastHTML element. This keeps persistence and record shape in your application while making the presentation mapping explicit.

from faststrap import Badge, Card, CollectionView, ListItem

files = [
    {"name": "lesson-plan.pdf", "kind": "PDF", "size": "1.2 MB"},
    {"name": "results.csv", "kind": "CSV", "size": "84 KB"},
]

def file_row(file):
    return ListItem(
        title=file["name"],
        subtitle=file["kind"],
        meta=file["size"],
        href=f"/files/{file['name']}",
    )

def file_card(file):
    return Card(
        file["size"],
        header=file["name"],
        footer=Badge(file["kind"], variant="secondary"),
    )

CollectionView(
    records=files,
    mode="grid",
    list_renderer=file_row,
    card_renderer=file_card,
    grid_min_width="14rem",
    label="Files",
)
Parameter Type Default Description
*items FastHTML content () Already-rendered children. Use these when your route has already selected the representation.
mode "list" \| "compact" \| "grid" \| "cards" "list" list and compact use ListView; grid and cards use GridList.
records Iterable[Any] \| None None Optional raw records. Cannot be combined with positional rendered items.
list_renderer Callable[[Any], Any] \| None None Required with records in list or compact mode. Called once per record.
card_renderer Callable[[Any], Any] \| None None Required with records in grid or cards mode. Called once per record.
list_density ListDensity "comfortable" Density forwarded to list modes. compact mode always uses compact density.
grid_min_width str "16rem" Minimum auto-fit grid track width.
grid_columns int \| None None Optional fixed grid track count.
gap int \| str 3 Grid spacing, following GridList rules.
label str \| None None Accessible collection name.
empty, loading, loading_content Any, bool, Any None, False, "Loading..." Forwarded to the selected list/grid wrapper. Loading content takes precedence and record renderers are not invoked while loading.
cls, style, css_vars Standard Faststrap styling — Customize the selected wrapper; standard HTML, ARIA, data_*, and hx_* attributes are accepted.

Only the renderer for the current mode is required. A list view does not need a card renderer until the user selects a grid mode, and vice versa. Do not put authorization or data fetching inside a renderer; perform those in the route before rendering.

Server-Selected View

Keep the selected mode in a query parameter so links are bookmarkable and work without client-side state:

from fasthtml.common import Div, FastHTML
from faststrap import Button, CollectionView, ListItem, ViewToggle

app = FastHTML()
files = [{"name": "lesson-plan.pdf", "size": "1.2 MB"}]

def render_file_row(file):
    return ListItem(title=file["name"], meta=file["size"])

def render_file_card(file):
    return Button(file["name"], variant="light", cls="w-100")

@app.get("/files")
def files_page(request):
    view = request.query_params.get("view", "list")
    if view not in {"list", "grid"}:
        view = "list"
    return Div(
        ViewToggle(value=view, endpoint="/files", aria_label="Choose file layout"),
        CollectionView(
            records=files,
            mode=view,
            list_renderer=render_file_row,
            card_renderer=render_file_card,
            label="Files",
        ),
    )

For a partial update, target the collection wrapper or a parent that includes both the toggle and results. With hx_target, ViewToggle emits hx-get links; hx_swap defaults to outerHTML. Set push_url=True if the HTMX navigation should update the browser URL. Your route must return the fragment expected by that target.

ViewToggle

ViewToggle renders accessible list/grid/cards controls. With an endpoint, each option is an ordinary link with the selected param query value. It preserves other query parameters and URL fragments, replacing any previous value for that parameter.

ViewToggle(
    value="grid",
    endpoint="/files?folder=reports&view=list#results",
    param="view",
    options=("list", "grid"),
    labels={"list": "Rows", "grid": "Tiles"},
    hx_target="#file-results",
    hx_swap="outerHTML",
    push_url=True,
    aria_label="Choose file layout",
)
Parameter Type Default Description
value "list" \| "grid" \| "cards" "list" Currently selected option; it must occur in options.
endpoint str \| None None When set, render ordinary links to this URL with the selected query parameter. Without it, render buttons with data-fs-view for application-owned enhancement.
param str "view" Query parameter to replace/add in endpoint mode. Must not be blank.
options tuple of allowed modes ("list", "grid") Options to render; must be non-empty and use supported values.
labels dict[str, str] \| None None Accessible/display names; defaults to List, Grid, and Cards.
aria_label str "Choose view" Accessible name for the control group.
hx_target str \| None None Add hx-get and target attributes to endpoint links.
hx_swap str "outerHTML" HTMX swap strategy used with hx_target.
push_url bool False Add hx-push-url="true" in HTMX endpoint mode.
cls and standard attrs — — Customize the group; standard HTML, ARIA, and data_* attributes are accepted.

Without an endpoint, data-fs-view is only a hook. Faststrap does not ship a global script that changes CollectionView in place; use endpoint mode for server-rendered interaction or implement your own JavaScript enhancement.

File Explorer Pattern

The collection primitives intentionally do not impose a file schema or a FileCollection component. Pair them with FilePreview, icons, badges, and actions to build the file-specific surface your application needs. This route uses the record renderer API and returns the selected view on each request. The example assumes records contain id, name, folder, size, and kind; add a preview_url when a card should include an inline preview.

from fasthtml.common import Div
from faststrap import (
    Badge,
    Button,
    Card,
    CollectionView,
    FilePreview,
    Icon,
    ListItem,
    ViewToggle,
)

def render_file_row(file):
    return ListItem(
        leading=Icon("file-earmark"),
        title=file["name"],
        subtitle=file["folder"],
        meta=file["size"],
        trailing=Badge(file["kind"], variant="secondary"),
        href=f"/files/{file['id']}",
    )

def render_file_card(file):
    return Card(
        FilePreview(file["preview_url"], title=file["name"], height=150)
        if file.get("preview_url")
        else Icon("file-earmark", cls="display-4 text-secondary"),
        file["size"],
        header=file["name"],
        footer=Div(
            Badge(file["kind"], variant="secondary"),
            Button("Open", href=f"/files/{file['id']}", size="sm", cls="ms-auto"),
            cls="d-flex align-items-center justify-content-between",
        ),
    )

def file_browser(files, view):
    if view not in {"list", "grid"}:
        view = "list"
    return Div(
        ViewToggle(value=view, endpoint="/files", options=("list", "grid")),
        CollectionView(
            records=files,
            mode=view,
            list_renderer=render_file_row,
            card_renderer=render_file_card,
            grid_min_width="13rem",
            label="Files",
            empty=Div("No files found.", cls="text-muted py-4"),
        ),
    )

Responsive DataTable Presentation

Use DataTable when the same records need semantic columns and table operations, but a phone-sized screen benefits from a more vertical presentation. Its default remains the existing responsive table. Set mobile_mode="cards" for separate record cards or mobile_mode="stack" for flatter stacked record rows below the breakpoint.

For a custom action area, pass mobile_card_renderer; the callable receives the normalized row dictionary and returns FastHTML content. Use mobile_field_renderers when only selected field values need rich rendering (for example a StatusBadge or formatted metric) while the generated layout should remain in control.

The custom card callable can include buttons and hx_* attributes. Those actions exist only in the mobile representation; render equivalent actions in the desktop table's cells if users need them at every viewport size.

See DataTable: Mobile Card Presentation for field selection, server-side state, accessibility, and a full example.

Accessibility And Styling

  • Give meaningful lists and grids a descriptive label.
  • Use href for navigation rows and native buttons/links for actions.
  • Do not place interactive controls inside a link row; use a non-link ListItem with a trailing action instead.
  • Provide text alternatives for icon-only leading/trailing content.
  • Keep important actions visible rather than relying on hover-only controls.
  • Selected, active, loading, and disabled styles are states, not selection or focus-management systems.
  • Use cls, style, and css_vars for application styling. Core CSS uses Bootstrap utilities and Faststrap surface tokens.
  • The components do not implement roving focus or arrow-key navigation. Their children retain native browser keyboard behavior.

API Reference

faststrap.components.display.collections.ListItem(*children, title=None, subtitle=None, meta=None, leading=None, trailing=None, href=None, selected=False, active=False, disabled=False, loading=False, density='comfortable', lines=None, **kwargs)

Render a flexible one-, two-, or three-line collection row.

ListItem is designed for files, users, mobile settings, datasets, and other record summaries. Use the leading and trailing slots for avatars, icons, checkboxes, status badges, or actions.

Source code in src/faststrap/components/display/collections.py
@register(category="display")
@beta
def ListItem(
    *children: Any,
    title: Any | None = None,
    subtitle: Any | None = None,
    meta: Any | None = None,
    leading: Any | None = None,
    trailing: Any | None = None,
    href: str | None = None,
    selected: bool = False,
    active: bool = False,
    disabled: bool = False,
    loading: bool = False,
    density: ListDensity = "comfortable",
    lines: Literal[1, 2, 3] | None = None,
    **kwargs: Any,
) -> Div | A:
    """Render a flexible one-, two-, or three-line collection row.

    ``ListItem`` is designed for files, users, mobile settings, datasets, and
    other record summaries. Use the leading and trailing slots for avatars,
    icons, checkboxes, status badges, or actions.
    """
    if density not in {"compact", "comfortable", "spacious"}:
        raise ValueError("density must be 'compact', 'comfortable', or 'spacious'")
    if lines not in {None, 1, 2, 3}:
        raise ValueError("lines must be 1, 2, or 3")

    user_cls = kwargs.pop("cls", "")
    title_content = title if title is not None else (children[0] if children else None)
    extra_content = list(children[1:] if title is None and children else children)

    content_parts: list[Any] = []
    if title_content is not None:
        content_parts.append(Div(title_content, cls="faststrap-list-item-title"))
    if subtitle is not None:
        content_parts.append(Div(subtitle, cls="faststrap-list-item-subtitle"))
    if meta is not None:
        content_parts.append(Div(meta, cls="faststrap-list-item-meta"))
    content_parts.extend(extra_content)

    row_parts: list[Any] = []
    if leading is not None:
        row_parts.append(Div(leading, cls="faststrap-list-item-leading flex-shrink-0"))
    row_parts.append(Div(*content_parts, cls="faststrap-list-item-content flex-grow-1 min-w-0"))
    if trailing is not None:
        row_parts.append(Div(trailing, cls="faststrap-list-item-trailing flex-shrink-0"))
    if loading:
        row_parts.append(
            Span(
                Span(cls="spinner-border spinner-border-sm", aria_hidden="true"),
                Span("Loading", cls="visually-hidden"),
                cls="faststrap-list-item-loading-indicator",
                role="status",
            )
        )

    attrs: dict[str, Any] = {
        "cls": merge_classes(
            "faststrap-list-item d-flex align-items-center gap-3",
            f"faststrap-list-item-{density}",
            f"faststrap-list-item-lines-{lines}" if lines else "",
            "faststrap-list-item-selected" if selected else "",
            "faststrap-list-item-disabled" if disabled else "",
            "faststrap-list-item-action" if href else "",
            user_cls,
        )
    }
    attrs["role"] = "listitem"
    if selected:
        attrs["aria-current"] = "true"
    if disabled:
        attrs["aria-disabled"] = "true"
    if active:
        attrs["aria-current"] = "page" if href else "true"
        attrs["cls"] = merge_classes(attrs["cls"], "faststrap-list-item-active")
    if loading:
        attrs["aria-busy"] = "true"
        attrs["aria-disabled"] = "true"
        attrs["cls"] = merge_classes(attrs["cls"], "faststrap-list-item-loading")
    attrs.update(convert_attrs(kwargs))

    if href and not (disabled or loading):
        return A(*row_parts, href=href, **attrs)
    if href:
        if disabled or loading:
            attrs["tabindex"] = "-1"
    return Div(*row_parts, **attrs)

faststrap.components.display.collections.ListView(*items, density='comfortable', divided=True, flush=False, label=None, empty=None, loading=False, loading_content='Loading...', **kwargs)

Render a vertical collection of ListItem surfaces.

Source code in src/faststrap/components/display/collections.py
@register(category="display")
@beta
def ListView(
    *items: Any,
    density: ListDensity = "comfortable",
    divided: bool = True,
    flush: bool = False,
    label: str | None = None,
    empty: Any | None = None,
    loading: bool = False,
    loading_content: Any = "Loading...",
    **kwargs: Any,
) -> Div:
    """Render a vertical collection of ``ListItem`` surfaces."""
    if density not in {"compact", "comfortable", "spacious"}:
        raise ValueError("density must be 'compact', 'comfortable', or 'spacious'")

    user_cls = kwargs.pop("cls", "")
    content = [_ensure_list_item(item, "faststrap-list-view-item") for item in items]
    if loading:
        content = [
            Div(
                Div(loading_content, cls="faststrap-collection-loading", role="status"),
                cls="faststrap-collection-state-item",
                role="listitem",
            )
        ]
    elif not content and empty is not None:
        content = [
            Div(
                Div(empty, cls="faststrap-collection-empty", role="status"),
                cls="faststrap-collection-state-item",
                role="listitem",
            )
        ]

    attrs: dict[str, Any] = {
        "cls": merge_classes(
            "faststrap-list-view",
            f"faststrap-list-view-{density}",
            "faststrap-list-view-divided" if divided else "",
            "faststrap-list-view-flush" if flush else "",
            user_cls,
        ),
        "role": "list",
    }
    if label:
        attrs["aria-label"] = label
    attrs.update(convert_attrs(kwargs))
    return Div(*content, **attrs)

faststrap.components.display.collections.GridList(*items, min_width='16rem', columns=None, gap=3, label=None, empty=None, loading=False, loading_content='Loading...', **kwargs)

Render a responsive CSS grid for cards, files, products, or records.

Source code in src/faststrap/components/display/collections.py
@register(category="display")
@beta
def GridList(
    *items: Any,
    min_width: str = "16rem",
    columns: int | None = None,
    gap: int | str = 3,
    label: str | None = None,
    empty: Any | None = None,
    loading: bool = False,
    loading_content: Any = "Loading...",
    **kwargs: Any,
) -> Div:
    """Render a responsive CSS grid for cards, files, products, or records."""
    if columns is not None and columns < 1:
        raise ValueError("columns must be at least 1")

    user_cls = kwargs.pop("cls", "")
    user_style = kwargs.pop("style", None)
    gap_value = f"{gap * 0.25}rem" if isinstance(gap, int) else str(gap)
    styles = [f"--fs-grid-min-width: {min_width}", f"--fs-grid-gap: {gap_value}"]
    if columns is not None:
        styles.append(f"--fs-grid-columns: {columns}")

    content = [_ensure_list_item(item, "faststrap-grid-list-item") for item in items]
    if loading:
        content = [
            Div(
                Div(loading_content, cls="faststrap-collection-loading", role="status"),
                cls="faststrap-grid-list-item faststrap-collection-state-item",
                role="listitem",
            )
        ]
    elif not content and empty is not None:
        content = [
            Div(
                Div(empty, cls="faststrap-collection-empty", role="status"),
                cls="faststrap-grid-list-item faststrap-collection-state-item",
                role="listitem",
            )
        ]

    attrs: dict[str, Any] = {
        "cls": merge_classes("faststrap-grid-list", user_cls),
        "role": "list",
        "style": _merge_style("; ".join(part for part in styles if part), user_style),
    }
    if label:
        attrs["aria-label"] = label
    attrs.update(convert_attrs(kwargs))
    return Div(*content, **attrs)

faststrap.components.display.collections.CollectionView(*items, mode='list', list_density='comfortable', grid_min_width='16rem', grid_columns=None, gap=3, label=None, records=None, list_renderer=None, card_renderer=None, empty=None, loading=False, loading_content='Loading...', **kwargs)

Render items as a vertical list, compact list, grid, or card grid.

The application owns the selected mode. Pair this with ViewToggle and a query parameter or HTMX endpoint when users should choose the view.

Source code in src/faststrap/components/display/collections.py
@register(category="display")
@beta
def CollectionView(
    *items: Any,
    mode: CollectionMode = "list",
    list_density: ListDensity = "comfortable",
    grid_min_width: str = "16rem",
    grid_columns: int | None = None,
    gap: int | str = 3,
    label: str | None = None,
    records: Iterable[Any] | None = None,
    list_renderer: Callable[[Any], Any] | None = None,
    card_renderer: Callable[[Any], Any] | None = None,
    empty: Any | None = None,
    loading: bool = False,
    loading_content: Any = "Loading...",
    **kwargs: Any,
) -> Div:
    """Render items as a vertical list, compact list, grid, or card grid.

    The application owns the selected mode. Pair this with ``ViewToggle`` and
    a query parameter or HTMX endpoint when users should choose the view.
    """
    if mode not in {"list", "grid", "cards", "compact"}:
        raise ValueError("mode must be 'list', 'grid', 'cards', or 'compact'")

    if records is not None and items:
        raise ValueError("Pass either items or records, not both")

    rendered_items = list(items)
    if records is not None and not loading:
        record_items = list(records)
        if record_items:
            renderer = list_renderer if mode in {"list", "compact"} else card_renderer
            renderer_name = "list_renderer" if mode in {"list", "compact"} else "card_renderer"
            if renderer is None:
                raise ValueError(f"{renderer_name} is required when records are provided")
            rendered_items = [renderer(record) for record in record_items]

    user_cls = kwargs.pop("cls", "")
    if mode in {"grid", "cards"}:
        return GridList(
            *rendered_items,
            min_width=grid_min_width,
            columns=grid_columns,
            gap=gap,
            label=label,
            empty=empty,
            loading=loading,
            loading_content=loading_content,
            cls=merge_classes(
                "faststrap-collection-view", f"faststrap-collection-view-{mode}", user_cls
            ),
            **kwargs,
        )

    density = "compact" if mode == "compact" else list_density
    return ListView(
        *rendered_items,
        density=density,
        label=label,
        empty=empty,
        loading=loading,
        loading_content=loading_content,
        cls=merge_classes(
            "faststrap-collection-view", f"faststrap-collection-view-{mode}", user_cls
        ),
        **kwargs,
    )

faststrap.components.display.collections.ViewToggle(*, value='list', endpoint=None, param='view', options=('list', 'grid'), labels=None, aria_label='Choose view', hx_target=None, hx_swap='outerHTML', push_url=False, **kwargs)

Render an accessible list/grid view selector.

When endpoint is supplied, options render as ordinary links with the selected value in a query parameter. hx_target optionally upgrades those links to HTMX requests. Without an endpoint, buttons expose data attributes for application-owned progressive enhancement.

Source code in src/faststrap/components/display/collections.py
@register(category="display")
@beta
def ViewToggle(
    *,
    value: Literal["list", "grid", "cards"] = "list",
    endpoint: str | None = None,
    param: str = "view",
    options: tuple[Literal["list", "grid", "cards"], ...] = ("list", "grid"),
    labels: dict[str, str] | None = None,
    aria_label: str = "Choose view",
    hx_target: str | None = None,
    hx_swap: str = "outerHTML",
    push_url: bool = False,
    **kwargs: Any,
) -> Div:
    """Render an accessible list/grid view selector.

    When ``endpoint`` is supplied, options render as ordinary links with the
    selected value in a query parameter. ``hx_target`` optionally upgrades
    those links to HTMX requests. Without an endpoint, buttons expose data
    attributes for application-owned progressive enhancement.
    """
    allowed = {"list", "grid", "cards"}
    if value not in allowed or not options or any(option not in allowed for option in options):
        raise ValueError("value and options must use 'list', 'grid', or 'cards'")
    if value not in options:
        raise ValueError("value must be included in options")
    if not param.strip():
        raise ValueError("param must not be empty")

    icon_names = {"list": "list-ul", "grid": "grid-3x3-gap", "cards": "grid"}
    default_labels = {"list": "List", "grid": "Grid", "cards": "Cards"}
    label_map = {**default_labels, **(labels or {})}
    controls: list[Any] = []
    for option in options:
        active = option == value
        content = (
            Icon(icon_names[option], aria_hidden="true"),
            Span(label_map[option], cls="visually-hidden"),
        )
        control_cls = merge_classes(
            "btn btn-sm", "btn-primary" if active else "btn-outline-secondary"
        )
        if endpoint:
            parts = urlsplit(endpoint)
            query = [
                (key, item)
                for key, item in parse_qsl(parts.query, keep_blank_values=True)
                if key != param
            ]
            query.append((param, option))
            href = urlunsplit(
                (parts.scheme, parts.netloc, parts.path, urlencode(query), parts.fragment)
            )
            link_attrs: dict[str, Any] = {
                "href": href,
                "cls": control_cls,
                "aria_current": "true" if active else None,
                "aria_label": label_map[option],
            }
            if hx_target:
                link_attrs.update(
                    {
                        "hx_get": href,
                        "hx_target": hx_target,
                        "hx_swap": hx_swap,
                    }
                )
                if push_url:
                    link_attrs["hx_push_url"] = "true"
            controls.append(A(*content, **link_attrs))
        else:
            controls.append(
                Button(
                    *content,
                    type="button",
                    cls=control_cls,
                    aria_pressed="true" if active else "false",
                    aria_label=label_map[option],
                    **{"data-fs-view": option},
                )
            )

    user_cls = kwargs.pop("cls", "")
    attrs: dict[str, Any] = {
        "cls": merge_classes("faststrap-view-toggle btn-group", user_cls),
        "role": "group",
        "aria-label": aria_label,
    }
    attrs.update(convert_attrs(kwargs))
    return Div(*controls, **attrs)