Skip to content

Chip And ChipGroup

Chip is the interactive/filter-oriented counterpart to the informational Badge. Tag remains available and compatible; use Chip when selected state, a value, or removal is part of the UI contract.

from faststrap import Chip, ChipGroup

ChipGroup(
    Chip("Python", value="python", selected=True, hx_get="/items?tag=python"),
    Chip("ML", value="ml", removable=True, on_remove="hx-delete='/filters/ml'"),
    label="Filter by topic",
)
Component Important parameters
Chip variant, size, selected, removable, icon, on_remove, value, and HTML/HTMX attributes.
ChipGroup selected index, accessible label, gap, wrap, and HTML/HTMX attributes.

Selection is server-rendered. For a filter request, add HTMX attributes to each chip and return the updated result region from the route.

Plain Chip("Label") stays a non-interactive <span>. Supplying href makes it an anchor; supplying type or an HTMX action such as hx_get/hx_post makes it a native <button type="button"> so it is keyboard-operable. A removable chip cannot also be an href, explicit button, or direct HTMX action because its remove control must not be nested inside another interactive control; put on_remove on its internal remove button instead. Selection styling/ARIA is still server-rendered and does not create a client-side toggle.

faststrap.components.display.tag.Chip(children, *, variant='secondary', size='md', selected=False, removable=False, icon=None, on_remove=None, value=None, href=None, **kwargs)

Render a selectable/removable chip for filters and compact metadata.

Source code in src/faststrap/components/display/tag.py
@register(category="display")
@beta
def Chip(
    children: str | tuple,
    *,
    variant: VariantType = "secondary",
    size: Literal["sm", "md", "lg"] = "md",
    selected: bool = False,
    removable: bool = False,
    icon: str | None = None,
    on_remove: str | None = None,
    value: str | None = None,
    href: str | None = None,
    **kwargs: Any,
) -> FT:
    """Render a selectable/removable chip for filters and compact metadata."""
    tag_size = "md" if size == "lg" else size
    chip = Tag(
        children,
        variant=variant,
        size=tag_size,
        removable=removable,
        icon=icon,
        on_remove=on_remove,
        **kwargs,
    )
    explicit_type = "type" in chip.attrs
    has_htmx_action = any(
        str(key).lower().replace("_", "-").startswith("hx-") for key in chip.attrs
    )
    if removable and (href is not None or explicit_type or has_htmx_action):
        raise ValueError("removable chips cannot also be links or action controls")
    if href is not None:
        chip.tag = "a"
        chip.attrs["href"] = href
    elif explicit_type or has_htmx_action or (selected and not removable):
        chip.tag = "button"
        chip.attrs.setdefault("type", "button")
    chip_attrs = chip.attrs
    chip_attrs["data-fs-chip"] = "true"
    current_cls = chip_attrs.get("class", chip_attrs.get("cls", ""))
    if size == "lg":
        current_cls = merge_classes(current_cls, "chip-lg px-3 py-2 fs-6")
    if selected:
        current_cls = merge_classes(current_cls, "chip-selected")
        if chip.tag == "button":
            chip_attrs["aria-pressed"] = "true"
        elif chip.tag == "a":
            chip_attrs["aria-current"] = "true"
    chip_attrs["class"] = current_cls
    if "cls" in chip_attrs:
        del chip_attrs["cls"]
    if value is not None:
        chip_attrs["data-fs-value"] = value
    return chip

faststrap.components.display.tag.ChipGroup(*chips, selected=None, label=None, gap=2, wrap=True, **kwargs)

Group chips into a wrapping filter or metadata control row.

Source code in src/faststrap/components/display/tag.py
@register(category="display")
@beta
def ChipGroup(
    *chips: Any,
    selected: int | None = None,
    label: str | None = None,
    gap: int | str = 2,
    wrap: bool = True,
    **kwargs: Any,
) -> Div:
    """Group chips into a wrapping filter or metadata control row."""
    chip_list = list(chips)
    if selected is not None and not 0 <= selected < len(chip_list):
        raise ValueError("selected must refer to an existing chip index")
    for index, chip in enumerate(chip_list):
        if selected is None or not hasattr(chip, "attrs"):
            continue
        chip_attrs = chip.attrs
        current_cls = chip_attrs.get("class", chip_attrs.get("cls", ""))
        current_cls = " ".join(
            token for token in str(current_cls).split() if token != "chip-selected"
        )
        is_selected = index == selected
        if is_selected:
            current_cls = merge_classes(current_cls, "chip-selected")
        tag = getattr(chip, "tag", "")
        removable = any(
            getattr(child, "tag", "") == "button" for child in getattr(chip, "children", ())
        )
        if tag == "a":
            chip_attrs.pop("aria-pressed", None)
            if is_selected:
                chip_attrs["aria-current"] = "true"
            else:
                chip_attrs.pop("aria-current", None)
        elif tag == "button" or (not removable and tag == "span"):
            if tag == "span" and not removable:
                chip.tag = "button"
                chip_attrs.setdefault("type", "button")
            chip_attrs.pop("aria-current", None)
            chip_attrs["aria-pressed"] = "true" if is_selected else "false"
        else:
            chip_attrs.pop("aria-pressed", None)
            chip_attrs.pop("aria-current", None)
        chip_attrs["class"] = current_cls
        if "cls" in chip_attrs:
            del chip_attrs["cls"]

    user_cls = kwargs.pop("cls", "")
    attrs: dict[str, Any] = {
        "cls": merge_classes(
            "faststrap-chip-group d-flex align-items-center",
            "flex-wrap" if wrap else "flex-nowrap",
            f"gap-{gap}",
            user_cls,
        ),
        "role": "group",
    }
    if label:
        attrs["aria_label"] = label
    attrs.update(convert_attrs(kwargs))
    return Div(*chip_list, **attrs)

For an accessible native control, ordinary non-removable selected chips render as buttons and expose aria-pressed. Link chips remain links and use aria-current for the selected destination. Removable chips keep their non-button wrapper so the remove button is not nested inside another button. ChipGroup(selected=...) synchronizes the selected visual class and ARIA state; it does not add client-side selection behavior by itself.